Skip to content

Instantly share code, notes, and snippets.

@vbuaraujo
Created December 11, 2017 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vbuaraujo/cb40d38e1f1eb5ae06faad5e4808504a to your computer and use it in GitHub Desktop.
Save vbuaraujo/cb40d38e1f1eb5ae06faad5e4808504a to your computer and use it in GitHub Desktop.
Chanege behavior od Emacs search
;;;; Search.
;; Automatically wrapping I-search.
;; https://stackoverflow.com/questions/285660/automatically-wrapping-i-search
;; TODO: Still not perfect: does not distinguish overwrapped I-search anymore.
(defadvice isearch-search (after isearch-no-fail activate)
(unless isearch-success
;; Avoid recursive loop
(ad-disable-advice 'isearch-search 'after 'isearch-no-fail)
(ad-activate 'isearch-search) ;; ad-activate to reflect the above change
;; Repeat the search (in the appropriate direction)
(isearch-repeat (if isearch-forward 'forward))
;; Restore advice
(ad-enable-advice 'isearch-search 'after 'isearch-no-fail)
(ad-activate 'isearch-search)))
;; The above solution has a serious problem: it creates a spurious "input event"
;; when it implicitly repeats the search. This means you have to type DEL
;; *twice* to undo the last command. I should find a proper solution to this,
;; but the fact is that I don't generally want DEL to undo last event anyway, I
;; want it to remove the last characher from the search (what C-M-w does (yeah,
;; really). So instead I'll redefine DEL (which apparently has to be called
;; <backspace> for the graphical Emacs) to remove last character from search
;; query, and the undo key to undo the last search event. This leaves the undo
;; key with the aforementioned bug, but whatever.
(define-key isearch-mode-map (kbd "<backspace>") 'isearch-del-char)
(define-key isearch-mode-map (kbd "DEL") 'isearch-del-char)
(define-key isearch-mode-map (kbd "C-/") 'isearch-delete-char)
(define-key isearch-mode-map (kbd "C-_") 'isearch-delete-char)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment