[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Let's make C-M-w in isearch yank symbol, not delete character

From: Juri Linkov
Subject: Re: Let's make C-M-w in isearch yank symbol, not delete character
Date: Tue, 27 Feb 2018 23:28:41 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (x86_64-pc-linux-gnu)

>> Fortunately, it's easy to add support for yanking an expression:
>> in addition to the existing option 'edit' of 'search-exit-option'
>> we can also provide a new option 'move' that will sync the current
>> search string with the new position in the buffer after moving point
>> using all standard motion commands, e.g. `C-f' will add the next char
>> to the end of the search string, `C-M-f' will add the next expression,
>> `M-f' will add the next word, `C-b' will delete text from the end of
>> the search string, etc.  Here is the implementation:
> Thanks.  I think this needs a documentation update as well.

I updated the documentation in the docstring of search-exit-option below.

> Also, this comment:
>>       ;; Other characters terminate the search and are then executed 
>> normally.
>> -     (search-exit-option
>> +     ((not (eq search-exit-option 'move))
> is now not entirely accurate, right?

Actually it was better to add a new condition, so this comment
remains accurate with this patch:

diff --git a/lisp/isearch.el b/lisp/isearch.el
index a41adf0..6b6570d 100644
--- a/lisp/isearch.el
+++ b/lisp/isearch.el
@@ -66,9 +66,17 @@ isearch
   :group 'matching)
-(defcustom search-exit-option t
-  "Non-nil means random control characters terminate incremental search."
-  :type 'boolean)
+(defcustom search-exit-option 'move
+  "Defines what control characters do in incremental search.
+If t, random control and meta characters terminate the search
+and are then executed normally.
+If `edit', edit the search string instead of exiting.
+If `move', use motion commands to extend the search string.
+If nil, run the command without exiting Isearch."
+  :type '(choice (const :tag "Terminate incremental search" t)
+                (const :tag "Edit the search string" edit)
+                (const :tag "Extend the search string by motion commands" move)
+                (const :tag "Don't terminate incremental search" nil)))
 (defcustom search-slow-window-lines 1
   "Number of lines in slow search display windows.
@@ -2391,6 +2414,7 @@ isearch-back-into-window
   (goto-char isearch-point))
 (defvar isearch-pre-scroll-point nil)
+(defvar isearch-pre-move-point nil)
 (defun isearch-pre-command-hook ()
   "Decide whether to exit Isearch mode before executing the command.
@@ -2427,6 +2451,11 @@ isearch-pre-command-hook
       ;; Swallow the up-event.
       (setq this-command 'isearch-edit-string))
+     ;; Don't terminate the search for some motion commands.
+     ((and (eq search-exit-option 'move)
+           (symbolp this-command)
+           (eq (get this-command 'isearch-move) t))
+      (setq isearch-pre-move-point (point)))
      ;; Other characters terminate the search and are then executed normally.
@@ -2436,13 +2465,23 @@ isearch-pre-command-hook
       (isearch-process-search-string key key)))))
 (defun isearch-post-command-hook ()
-  (when isearch-pre-scroll-point
+  (cond
+   (isearch-pre-scroll-point
     (let ((ab-bel (isearch-string-out-of-window isearch-pre-scroll-point)))
       (if ab-bel
          (isearch-back-into-window (eq ab-bel 'above) isearch-pre-scroll-point)
        (goto-char isearch-pre-scroll-point)))
     (setq isearch-pre-scroll-point nil)
-    (isearch-update)))
+    (isearch-update))
+   ((eq search-exit-option 'move)
+    (when (and isearch-pre-move-point
+               (not (eq isearch-pre-move-point (point))))
+      (let ((string (buffer-substring-no-properties
+                     (or isearch-other-end isearch-opoint) (point))))
+        (setq isearch-string string
+              isearch-message string)
+        (isearch-update)))
+    (setq isearch-pre-move-point nil))))
 (defun isearch-quote-char (&optional count)
   "Quote special characters for incremental search.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]