Skip to content

Commit

Permalink
Merge pull request #13 from yang-guangliang/master
Browse files Browse the repository at this point in the history
fix a small bug : differentiate isearch-regexp when highlighting symbol
  • Loading branch information
juanjux committed Dec 15, 2015
2 parents c563a05 + 7a463da commit 635718b
Showing 1 changed file with 103 additions and 13 deletions.
116 changes: 103 additions & 13 deletions evil-search-highlight-persist.el
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,91 @@
(require 'advice)
(require 'highlight)

(setq evil-search-highlight-regex-flag t)
(defun hlt-+/--highlight-regexp-region (unhighlightp start end regexp face msgp mousep nth &optional buffers)
"Helper for `hlt-(un)highlight-regexp-region'.
Non-nil UNHIGHLIGHTP means unhighlight. Otherwise, highlight.
The other arguments are as for `hlt-highlight-regexp-region'.
If UNHIGHLIGHTP:
Do not advance to the next face, even if `hlt-auto-faces-flag'.
If FACE is nil then unhighlight all faces."
(unless regexp (setq regexp hlt-last-regexp))
(unless (stringp regexp) ; Else re-search-forward gets an error
(error "HLT-%sHIGHLIGHT-REGEXP-REGION: REGEXP arg is not a string: `%S'"
(if unhighlightp "UN" "")
regexp))
(let ((mbufs buffers))
(unless buffers (setq buffers (list (current-buffer))))
;; Advance the face if highlighting (but not unhighlighting) with auto faces.
(when (and hlt-auto-faces-flag (not unhighlightp)) (hlt-next-face))
(if face (setq hlt-last-face face) (unless unhighlightp (setq face hlt-last-face)))
(dolist (buf buffers)
(with-current-buffer buf
;; Use START and END if provided non-interactively, but not otherwise.
(unless (and start end (or (not (cadr buffers)) (not (interactive-p))))
(let ((start-end (hlt-region-or-buffer-limits buf)))
(setq start (car start-end)
end (cadr start-end))))
;; Do nothing if START or END is a marker for a different buffer.
(when (and (or (not (markerp start)) (eq buf (marker-buffer start)))
(or (not (markerp end)) (eq buf (marker-buffer end))))
(when (and msgp (not unhighlightp))
(let ((reg-size (abs (- end start))))
(when (and (> reg-size hlt-max-region-no-warning)
(not (progn (and (fboundp 'flash-ding) ; In `frame-fns.el'
(flash-ding 'no-terminate-macros (selected-frame)))
(y-or-n-p (substitute-command-keys
(format "Lots of highlighting slows things down. Do you \
really want to highlight up to %d chars? "
reg-size))))))
(error "OK, highlighting cancelled"))))
(when (eq t msgp)
(message "%sighlighting occurrences of `%s'%s..."
(if unhighlightp "UNh" "H")
regexp
(if mbufs (format " in `%s'" buf) "")))
(let ((hits-p nil)
(hlt-auto-faces-flag nil)) ; Prevent advancing - we already advanced.
(save-excursion
(goto-char start)
(if evil-search-highlight-regex-flag
(while (and (< start end) (not (eobp)) (re-search-forward regexp end t) (setq hits-p t))
(condition-case nil
(progn (forward-char 1) (setq start (1+ (point))))
(end-of-buffer (setq start end)))
(funcall (if unhighlightp #'hlt-unhighlight-region #'hlt-highlight-region)
(match-beginning (or nth 0))
(match-end (or nth 0))
face
nil
mousep))
(while (and (< start end) (not (eobp)) (search-forward regexp end t) (setq hits-p t))
(condition-case nil
(progn (forward-char 1) (setq start (1+ (point))))
(end-of-buffer (setq start end)))
(funcall (if unhighlightp #'hlt-unhighlight-region #'hlt-highlight-region)
(match-beginning (or nth 0))
(match-end (or nth 0))
face
nil
mousep)))
)
(when (eq t msgp)
(if hits-p
(message "%sighlighting occurrences of `%s'%s done %s"
(if unhighlightp "UNh" "H")
regexp
(if mbufs (format " in `%s'" buf) "")
(if unhighlightp
""
(let ((remove-msg "\\[hlt-unhighlight-regexp-region]' to remove highlighting"))
(when mousep (setq remove-msg (concat "\\[universal-argument] " remove-msg)))
(setq remove-msg (substitute-command-keys (concat "`" remove-msg)))
remove-msg)))
(message "No occurrences of `%s' in `%s'" regexp buf))))
(setq hlt-last-regexp regexp))))))


(defgroup evil-search-highlight-persist nil
"evil-search-highlight-persist -- Search Highlight Remain, Vim's style"
:tag "SearchHighlightPersist"
Expand All @@ -72,12 +157,17 @@
(hlt-last-face 'evil-search-highlight-persist-highlight-face))
(setq tmp nil)
(if isearch-regexp
(setq tmp (car-safe regexp-search-ring))
(setq tmp (car-safe search-ring)))
(progn
(setq tmp (car-safe regexp-search-ring))
(setq evil-search-highlight-regex-flag t))
(progn
(setq tmp (car-safe search-ring))
(setq evil-search-highlight-regex-flag nil)))
(if (>= (length tmp) evil-search-highlight-string-min-len)
(hlt-highlight-regexp-region-in-buffers
tmp
(list (current-buffer))))))
(hlt-highlight-regexp-region-in-buffers tmp (list (current-buffer)))
))
(setq evil-search-highlight-regex-flag t)
)

(defadvice isearch-exit (after isearch--highlight-persist)
(evil-search-highlight-persist-remove-all)
Expand All @@ -89,15 +179,15 @@

;;;###autoload
(define-minor-mode evil-search-highlight-persist
"Keep the highlights persist after a search"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-x SPC") 'evil-search-highlight-persist-remove-all)
"Keep the highlights persist after a search"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-x SPC") 'evil-search-highlight-persist-remove-all)
map)
(if evil-search-highlight-persist
(progn
(ad-activate 'isearch-exit)
(ad-activate 'evil-flash-search-pattern))
(progn
(if evil-search-highlight-persist
(progn
(ad-activate 'isearch-exit)
(ad-activate 'evil-flash-search-pattern))
(progn
(evil-search-highlight-persist-remove-all)
(ad-deactivate 'isearch-exit)
(ad-deactivate 'evil-flash-search-pattern))))
Expand Down

0 comments on commit 635718b

Please sign in to comment.