boon/boon-search.el

104 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)
2014-10-19 22:29:29 +02:00
(defvar-local boon-regexp nil)
2014-10-19 13:59:12 +02:00
(defun boon-search-regexp (forward)
"Re-serach the current regexp, in the direction specified (as FORWARD).
Point is set at the beginning of the match."
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
(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 (forward)
2014-10-19 22:29:29 +02:00
"Re-search the current regexp, in the direction specified (as FORWARD).
Moreover, highlight the regexp."
2014-10-19 13:59:12 +02:00
(boon-highlight-regexp)
(boon-search-regexp forward)
(deactivate-mark))
(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 ()
"Search the next occurence of the current string at point."
2014-10-19 13:59:12 +02:00
(interactive)
(boon-set-search-string (boon-stuff-at-point))
(boon-qsearch t))
(defun boon-qsearch-previous-at-point ()
"Search the previous occurence of the current string at point."
2014-10-19 13:59:12 +02:00
(interactive)
(boon-set-search-string (boon-stuff-at-point))
(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)))
2014-10-19 13:59:12 +02:00
(boon-qsearch nil))
(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")
(setq boon-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)))))
(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)
(hi-lock-face-buffer boon-regexp 'hi-yellow))
(defun boon-isearch-region (forward beg end)
2014-10-19 22:29:29 +02:00
"Search the current selection in the direction specified (as FORWARD).
The selection is between (as BEG END)."
2014-10-19 13:59:12 +02:00
(let ((selection (buffer-substring-no-properties beg end)))
(deactivate-mark)
(if forward (goto-char end) (goto-char (- beg 1))) ; ensuring that we find the next match
(isearch-mode forward nil nil nil)
(isearch-yank-string selection)))
(defadvice isearch-exit (after ysph-hl-search activate compile)
2014-10-19 22:29:29 +02:00
"After isearch, highlight the search term."
2014-10-19 13:59:12 +02:00
(setq boon-regexp isearch-string)
(boon-highlight-regexp))
2014-10-19 14:17:46 +02:00
(provide 'boon-search)
;;; boon-search.el ends here