Skip to content

Instantly share code, notes, and snippets.

@tkosaka
Last active August 29, 2015 14:01
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 tkosaka/3eebce285b6fee51de7d to your computer and use it in GitHub Desktop.
Save tkosaka/3eebce285b6fee51de7d to your computer and use it in GitHub Desktop.
(defvar my-isearch-use-region-p nil
"Variable that indicates that a region exists when the user starts isearch."
)
(make-variable-buffer-local 'my-isearch-use-region-p)
(add-hook 'isearch-mode-hook
'(lambda ()
(setq my-isearch-use-region-p nil)
(when (or (use-region-p)
(and (marker-position (mark-marker))
(= (point) (marker-position (mark-marker)))))
(setq my-isearch-use-region-p t))))
(defadvice isearch-done (around preserve-region activate)
"Preserve region after isearch if exists.
This advice overrides the original definition."
(let ((no-push (ad-get-arg 0))
(edit (ad-get-arg 1)))
(if isearch-resume-in-command-history
(let ((command `(isearch-resume ,isearch-string ,isearch-regexp
,isearch-word ,isearch-forward
,isearch-message
',isearch-case-fold-search)))
(unless (equal (car command-history) command)
(setq command-history (cons command command-history)))))
(remove-hook 'mouse-leave-buffer-hook 'isearch-done)
(remove-hook 'kbd-macro-termination-hook 'isearch-done)
(setq isearch-lazy-highlight-start nil)
;; Called by all commands that terminate isearch-mode.
;; If NOPUSH is non-nil, we don't push the string on the search ring.
(setq overriding-terminal-local-map nil)
;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
(setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
(isearch-dehighlight)
(lazy-highlight-cleanup lazy-highlight-cleanup)
(let ((found-start (window-start (selected-window)))
(found-point (point)))
(when isearch-window-configuration
(set-window-configuration isearch-window-configuration)
(if isearch-small-window
(goto-char found-point)
;; set-window-configuration clobbers window-start; restore it.
;; This has an annoying side effect of clearing the last_modiff
;; field of the window, which can cause unwanted scrolling,
;; so don't do it unless truly necessary.
(set-window-start (selected-window) found-start t))))
(setq isearch-mode nil)
(if isearch-input-method-local-p
(setq input-method-function isearch-input-method-function)
(kill-local-variable 'input-method-function))
(force-mode-line-update)
;; If we ended in the middle of some intangible text,
;; move to the further end of that intangible text.
(let ((after (if (eobp) nil
(get-text-property (point) 'intangible)))
(before (if (bobp) nil
(get-text-property (1- (point)) 'intangible))))
(when (and before after (eq before after))
(goto-char
(if isearch-forward
(next-single-property-change (point) 'intangible)
(previous-single-property-change (point) 'intangible)))))
(if (and (> (length isearch-string) 0) (not nopush))
;; Update the ring data.
(isearch-update-ring isearch-string isearch-regexp))
(let ((isearch-mode-end-hook-quit (and nopush (not edit))))
(run-hooks 'isearch-mode-end-hook))
;; If there was movement, mark the starting position.
;; Maybe should test difference between and set mark only if > threshold.
(if (/= (point) isearch-opoint)
(or (and transient-mark-mode mark-active)
(progn
;;; pushing the start point should be omitted.
;
;(push-mark isearch-opoint t)
;(or executing-kbd-macro (> (minibuffer-depth) 0) edit
; (message "Mark saved where search started")))
;
;;; hack to preserve region
(when my-isearch-use-region-p
(exchange-point-and-mark)
(exchange-point-and-mark))
)))
(and (not edit) isearch-recursive-edit (exit-recursive-edit))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment