boon/boon-search.el

98 lines
3.5 KiB
EmacsLisp
Raw Normal View History

;;; boon-search.el --- An Ergonomic Command Mode -*- lexical-binding: t -*-
2014-10-19 14:17:46 +02:00
;;; Commentary:
;;; Code:
2016-08-31 21:58:55 +02:00
(require 'boon-utils)
2014-10-19 14:17:46 +02:00
(defvar-local boon-regexp nil "Current regexp search. Use boon-set-search-regexp to set this variable.")
(defvar-local boon-search-success t "Last search was successful or non-existent.")
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-search-success t)
2015-10-21 22:26:15 +02:00
(setq boon-regexp regexp)
(boon-highlight-regexp))
2015-10-21 22:26:15 +02:00
(defun boon-qsearch (forward)
"Search the current boon-regexp, in the direction specified (as FORWARD).
Point is set at the beginning of the match. Moreover, highlight
the regexp."
2014-10-19 13:59:12 +02:00
(when (not boon-regexp)
(error "Search string not set"))
(boon-highlight-regexp)
(save-excursion ;; so that we don't move the point if an exception is thrown
(goto-char (if boon-search-success
2019-08-25 09:32:26 +02:00
(if forward (1+ (point)) (1- (point)))
(message "Wrapping around")
(if forward (point-min) (point-max))))
(setq boon-search-success nil)
(if forward (re-search-forward boon-regexp) (re-search-backward boon-regexp))
;; If search fails an exception is thrown and this won't be set.
(setq boon-search-success t))
(goto-char (match-beginning 0)))
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)
2016-08-24 21:51:14 +02:00
(deactivate-mark))
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))
(boon-qsearch nil)
2016-08-24 21:51:14 +02:00
(deactivate-mark))
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 ()
"Make sure boon-regexp is highlighted."
2014-10-19 13:59:12 +02:00
(interactive)
(hi-lock-face-buffer boon-regexp 'hi-yellow))
2019-08-24 14:55:40 +02:00
(defadvice isearch-exit (after boon-isearch-set-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-string isearch-string))
2014-10-19 14:17:46 +02:00
2019-08-24 14:55:40 +02:00
(defadvice swiper--action (after boon-swiper-set-search activate compile)
"After isearch, highlight the search term and set it as boon current regexp."
(boon-set-search-regexp (car regexp-search-ring)))
2014-10-19 14:17:46 +02:00
(provide 'boon-search)
;;; boon-search.el ends here