boon/boon-search.el

107 lines
3.6 KiB
EmacsLisp
Raw Normal View History

2014-10-19 14:17:46 +02:00
;;; boon --- An Ergonomic Command Mode -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:
(require 'boon-main)
2015-10-21 22:26:15 +02:00
(defvar-local boon-regexp nil "Use boon-set-search-regexp to set this variable.")
2014-10-19 22:29:29 +02:00
2015-10-21 22:26:15 +02:00
(defun boon-set-search-regexp (regexp)
"Set boon-regexp to REGEXP and manage highlighting."
(when boon-regexp (hi-lock-unface-buffer boon-regexp))
(setq boon-regexp regexp)
(boon-highlight-regexp))
(defun boon-qsearch (forward)
"Re-search the current regexp, in the direction specified (as FORWARD).
Point is set at the beginning of the match. Moreover, highlight the regexp."
(boon-highlight-regexp)
2014-10-19 13:59:12 +02:00
(when (not boon-regexp)
(error "Search string not set"))
(when (not isearch-success)
(message "Wrapping around")
2014-10-19 22:29:29 +02:00
(goto-char (if forward (point-min) (point-max))))
2014-10-19 13:59:12 +02:00
(setq isearch-success nil)
2014-10-19 22:29:29 +02:00
(if forward
(progn
2015-10-21 22:26:15 +02:00
(save-excursion ;; so that we don't move the point if an exception is thrown
(goto-char (+ 1 (point))) ;; so that we find another occurence
(re-search-forward boon-regexp))
(goto-char (match-beginning 0)))
2014-10-19 13:59:12 +02:00
(re-search-backward boon-regexp))
2014-10-19 22:29:29 +02:00
(setq isearch-success t) ;; If search fails an exception is thrown and this won't be set.
2014-10-19 13:59:12 +02:00
)
(defun boon-qsearch-next ()
2014-10-19 22:29:29 +02:00
"Search the next occurence of the current search regexp."
2014-10-19 13:59:12 +02:00
(interactive)
(boon-qsearch t))
(defun boon-qsearch-previous ()
2014-10-19 22:29:29 +02:00
"Search the previous occurence of the current search regexp."
2014-10-19 13:59:12 +02:00
(interactive)
(boon-qsearch nil))
(defun boon-qsearch-next-at-point ()
2015-10-21 22:26:15 +02:00
"Search the next occurence of the current string at point and select the match."
2014-10-19 13:59:12 +02:00
(interactive)
(boon-set-search-string (boon-stuff-at-point))
2015-10-21 22:26:15 +02:00
(boon-qsearch t)
(activate-mark)
2015-10-21 22:31:20 +02:00
(set-marker (mark-marker) (match-end 0))
(goto-char (match-beginning 0))
2015-10-21 22:26:15 +02:00
)
2014-10-19 13:59:12 +02:00
(defun boon-qsearch-previous-at-point ()
2015-10-21 22:26:15 +02:00
"Search the previous occurence of the current string at point and select the match."
2014-10-19 13:59:12 +02:00
(interactive)
(boon-set-search-string (boon-stuff-at-point))
2015-10-21 22:26:15 +02:00
(save-excursion
(when (use-region-p)
;; make sure that we don't find the stuff that we've just
;; selected, by moving the point at the beginning of the match.
(goto-char (region-beginning)))
(boon-qsearch nil))
(activate-mark)
2015-10-21 22:31:20 +02:00
(set-marker (mark-marker) (match-end 0))
(goto-char (match-beginning 0))
2015-10-21 22:26:15 +02:00
)
2014-10-19 13:59:12 +02:00
(defun boon-set-search-string (string)
2014-10-19 22:29:29 +02:00
"Set the search regexp by providing a string so match (as STRING)."
2014-10-19 13:59:12 +02:00
(interactive "M")
2015-10-21 22:26:15 +02:00
(boon-set-search-regexp (cond ((if (and (eq isearch-case-fold-search t)
search-upper-case)
(isearch-no-upper-case-p
string isearch-regexp)
isearch-case-fold-search)
;; Turn isearch-string into a case-insensitive
;; regexp.
(mapconcat
(lambda (c)
(let ((s (string c)))
(if (string-match "[[:alpha:]]" s)
(format "[%s%s]" (upcase s) (downcase s))
(regexp-quote s))))
string ""))
(t (regexp-quote string)))))
2014-10-19 13:59:12 +02:00
(defun boon-highlight-regexp ()
2014-10-19 22:29:29 +02:00
"Make sure the current regexp is highlighted."
2014-10-19 13:59:12 +02:00
(interactive)
;; (global-hi-lock-mode 1)
2015-10-21 22:26:15 +02:00
2014-10-19 13:59:12 +02:00
(hi-lock-face-buffer boon-regexp 'hi-yellow))
(defadvice isearch-exit (after ysph-hl-search activate compile)
2015-10-21 22:26:15 +02:00
"After isearch, highlight the search term and set it as boon current regexp."
(boon-set-search-regexp isearch-string)
2014-10-19 13:59:12 +02:00
(boon-highlight-regexp))
2014-10-19 14:17:46 +02:00
(provide 'boon-search)
;;; boon-search.el ends here