]> git.eshelyaron.com Git - emacs.git/commitdiff
New commands unix-word-rubout, unix-filename-rubout
authorSean Whitton <spwhitton@spwhitton.name>
Fri, 6 Sep 2024 10:35:46 +0000 (11:35 +0100)
committerEshel Yaron <me@eshelyaron.com>
Sat, 14 Sep 2024 20:34:55 +0000 (22:34 +0200)
* lisp/simple.el (forward-unix-word): New function.
(unix-word-rubout, unix-filename-rubout): New commands.
* etc/NEWS: Announce the new commands.

(cherry picked from commit f6417ba91b3fdffc5af43bc4a7ad0b7ed007f442)

etc/NEWS
lisp/simple.el

index 6cbd3c712e1a759b3a4f758368d7e6ef419058fa..d679b16cfb07d59ebd6c267e1158d1644d40c1bc 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -105,6 +105,12 @@ When using 'visual-wrap-prefix-mode' in buffers with variable-pitch
 fonts, the wrapped text will now be lined up correctly so that it's
 exactly below the text after the prefix on the first line.
 
+---
+** New commands 'unix-word-rubout' and 'unix-filename-rubout'.
+Unix-words are words separated by whitespace regardless of the buffer's
+syntax table.  In a Unix terminal or shell, C-w kills by Unix-word.
+The new commands 'unix-word-rubout' and 'unix-filename-rubout' allow
+you to bind keys to operate more similarly to the terminal.
 \f
 * Changes in Specialized Modes and Packages in Emacs 31.1
 
index a375e9deb1ac636273171ad77741665309d41e81..a2d6ec21250bc43c878364160835a03fb545a569 100644 (file)
@@ -8763,6 +8763,63 @@ constitute a word."
       ;; If we found something nonempty, return it as a string.
       (unless (= start end)
        (buffer-substring-no-properties start end)))))
+
+(defun forward-unix-word (n &optional delim)
+  "Move forward N Unix-words.
+A Unix-word is whitespace-delimited.
+A negative N means go backwards to the beginning of Unix-words.
+
+Unix-words differ from Emacs words in that they are always delimited by
+whitespace, regardless of the buffer's syntax table.  This function
+emulates how C-w at the Unix terminal or shell identifies words.
+
+Optional argument DELIM specifies what characters are considered
+whitespace.  It is a string as might be passed to `skip-chars-forward'.
+The default is \"\\s\\f\\n\\r\\t\\v\".  Do not prefix a `^' character."
+  (when (string-prefix-p "^" delim)
+    (error "DELIM argument must not begin with `^'"))
+  (unless (zerop n)
+    ;; We do skip over newlines by default because `backward-word' does.
+    (let* ((delim (or delim "\s\f\n\r\t\v"))
+           (ndelim (format "^%s" delim))
+           (start (point))
+           (fun (if (> n 0)
+                    #'skip-chars-forward
+                  #'skip-chars-backward)))
+      (dotimes (_ (abs n))
+        (funcall fun delim)
+        (funcall fun ndelim))
+      (constrain-to-field nil start))))
+
+(defun unix-word-rubout (arg)
+  "Kill ARG Unix-words backwards.
+A Unix-word is whitespace-delimited.
+Interactively, ARG is the numeric prefix argument, defaulting to 1.
+A negative ARG means to kill forwards.
+
+Unix-words differ from Emacs words in that they are always delimited by
+whitespace, regardless of the buffer's syntax table.
+Thus, this command emulates C-w at the Unix terminal or shell.
+See also this command's nakesake in Info node
+`(readline)Commands For Killing'."
+  (interactive "^p")
+  (let ((start (point)))
+    (forward-unix-word (- arg))
+    (kill-region start (point))))
+
+(defun unix-filename-rubout (arg)
+  "Kill ARG Unix-words backwards, also treating slashes as word delimiters.
+A Unix-word is whitespace-delimited.
+Interactively, ARG is the numeric prefix argument, defaulting to 1.
+A negative ARG means to kill forwards.
+
+This is like `unix-word-rubout' (which see), but `/' and `\\' are also
+treated as delimiting words.  See this command's namesake in Info node
+`(readline)Commands For Killing'."
+  (interactive "^p")
+  (let ((start (point)))
+    (forward-unix-word (- arg) "\\\\/\s\f\n\r\t\v")
+    (kill-region start (point))))
 \f
 (defcustom fill-prefix nil
   "String for filling to insert at front of new line, or nil for none."