all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Sean Whitton <spwhitton@spwhitton.name>
To: Eli Zaretskii <eliz@gnu.org>
Cc: philipk@posteo.net, rms@gnu.org, 69097@debbugs.gnu.org,
	juri@linkov.net, stefankangas@gmail.com, acorallo@gnu.org
Subject: bug#69097: [PATCH] Add 'kill-region-or-word' command
Date: Fri, 06 Sep 2024 14:54:58 +0100	[thread overview]
Message-ID: <87r09wvqdp.fsf@zephyr.silentflame.com> (raw)
In-Reply-To: <86frqd10k1.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 06 Sep 2024 14:30:54 +0300")

[-- Attachment #1: Type: text/plain, Size: 2497 bytes --]

Hello,

On Fri 06 Sep 2024 at 02:30pm +03, Eli Zaretskii wrote:

> Thanks, but I see no reason to document this command in the manual,
> certainly not in the ELisp reference (it's a command, not a function).
> IMO it's obscure enough to be documented only in NEWS.

That's quite fine with me.

> Should this be "[:space:]\\n\\r" instead? if not, why not?
>
> Also note that [:space:] depends on mode-specific syntax table, so I
> question the wisdom of using it in a command that's supposed to be
> mode-agnostic.

Both excellent points, thank you.  I looked at the POSIX standard and
came up with a new default value.

>> +This command is like `forward-word' except that words are always
>> +delimited by whitespace, regardless of the buffer's syntax table.
>> +Like `forward-word', this command respects fields.
>> +
>> +This emulates how C-w at the Unix terminal or shell identifies words.
>> +See the `unix-word-rubout' command in Info node `(readline)Commands For
>> +Killing'."
>
> This should try to be more explicit about what "word" means for this
> command.  Since it's so different from any notion of "word" in Emacs,
> I would even seriously consider not to use that word, or maybe quote
> it (in addition to explaining what it means).

This is very helpful.  You're right.  In the attached, I've tried using
"unix-word".  Let me know what you think of that.

> Also, since this is a command, its doc string should clearly separate
> what happens in interactive invocation from what happens when called
> from Lisp.  DELIM belongs to the latter.  (Is it even useful to
> provide that option for Lisp-only calls? what's the use case for
> that?)
>
> And finally, I wonder why we need this command?  AFAIU, the original
> intent was to implement something similar to unix-word-rubout, not a
> new movement command.  If the plan has changed, I think we need to
> discuss the need once again.

I think it was unhelpful of me to send this in without callers.
I'm sorry about that.

The reason for adding this is that it factors out what is in common
between what Philip is doing, and unix-word-rubout.
So in the attached revised patch, I've included unix-word-rubout.

I've also included a second readline command which I have bound in my
own init.  It demonstrates why there is a need for DELIM.

By the way, forward-unix-word could also be a plain function.
I made it a command because, well, why not.  Let me know if you think it
should be switched back to a function.

-- 
Sean Whitton

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-New-commands-for-moving-and-killing-unix-words.patch --]
[-- Type: text/x-patch, Size: 3976 bytes --]

From c047e640399bb923728ed9967e8545d53b22ea5c Mon Sep 17 00:00:00 2001
From: Sean Whitton <spwhitton@spwhitton.name>
Date: Fri, 6 Sep 2024 11:35:46 +0100
Subject: [PATCH] New commands for moving and killing unix-words

* lisp/simple.el (forward-unix-word, unix-word-rubout)
(unix-filename-rubout): New commands.
* etc/NEWS: Document them.
---
 etc/NEWS       |  7 +++++++
 lisp/simple.el | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index f3e719a34d3..54cf0a3df52 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -123,6 +123,13 @@ 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 for moving and killing unix-words.
+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 'forward-unix-word', '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
 
diff --git a/lisp/simple.el b/lisp/simple.el
index 2453a129d0a..0322ac0cd8c 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -8892,6 +8892,63 @@ current-word
       ;; If we found something nonempty, return it as a string.
       (unless (= start end)
 	(buffer-substring-no-properties start end)))))
+
+(defun forward-unix-word (arg &optional delim)
+  "Move forward ARG unix-words.
+A unix-word is whitespace-delimited.
+Interactively, ARG is the numeric prefix argument, defaulting to 1.
+A negative ARG 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 command
+emulates how C-w at the Unix terminal or shell identifies words.
+
+When called from Lisp, DELIM specifies what characters are considered
+whitespace.  It is a string as might be passed to `skip-chars-forward'.
+The default is \" \\f\\n\\r\\t\\v\".  Do not prefix a `^' character."
+  (interactive "^p")
+  (unless (zerop arg)
+    ;; We do skip over newlines by default because `backward-word' does.
+    (let* ((delim (or delim " \f\n\r\t\v"))
+           (ndelim (format "^%s" delim))
+           (start (point))
+           (fun (if (> arg 0)
+                    #'skip-chars-forward
+                  #'skip-chars-backward)))
+      (dotimes (_ (abs arg))
+        (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 identifies words.
+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 `/' as whitespace.
+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 `/' is also considered
+whitespace.  See this command's namesake in Info node
+`(readline)Commands For Killing'."
+  (interactive "^p")
+  (let ((start (point)))
+    (forward-unix-word (- arg) "/ \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."
-- 
2.39.2


  reply	other threads:[~2024-09-06 13:54 UTC|newest]

Thread overview: 90+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-13  9:55 bug#69097: [PATCH] Add 'kill-region-or-word' command Philip Kaludercic
2024-02-17  3:53 ` Richard Stallman
     [not found]   ` <87ttm7gi9i.fsf@posteo.net>
2024-02-19  3:44     ` Richard Stallman
     [not found]       ` <87sf1obkw9.fsf@posteo.net>
2024-02-23  3:04         ` Richard Stallman
     [not found]           ` <871q93rzv8.fsf@posteo.net>
2024-02-25  3:16             ` Richard Stallman
     [not found]               ` <87frxgn73g.fsf@posteo.net>
2024-02-27  3:12                 ` Richard Stallman
2024-05-03  7:37 ` Philip Kaludercic
2024-05-03 10:40   ` Eli Zaretskii
2024-05-03 10:48     ` Philip Kaludercic
2024-05-03 10:59       ` Eli Zaretskii
2024-05-03 11:04         ` Eli Zaretskii
2024-05-03 17:32           ` Philip Kaludercic
2024-05-03 18:01             ` Eli Zaretskii
2024-05-03 19:41               ` Philip Kaludercic
2024-05-04  6:20                 ` Eli Zaretskii
2024-05-05  6:53                 ` Juri Linkov
2024-05-05  9:04                   ` Eli Zaretskii
2024-05-05 16:29                     ` Juri Linkov
2024-05-05 16:54                       ` Philip Kaludercic
2024-05-05 16:59                         ` Juri Linkov
2024-05-05 17:08                           ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-05 18:27                           ` Philip Kaludercic
2024-05-05 17:05                         ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-05 17:13                           ` Eli Zaretskii
2024-05-05 17:53                             ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-06  0:21                       ` Sean Whitton via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-06 16:46                     ` Sean Whitton via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-06 16:51                       ` Sean Whitton via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-06 17:55                         ` Eli Zaretskii
2024-05-07  8:47                           ` Sean Whitton via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-07  8:47                             ` Sean Whitton via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-01 20:06                           ` Philip Kaludercic
2024-09-02  6:36                             ` Sean Whitton
2024-09-02 11:18                             ` Eli Zaretskii
2024-09-02 18:30                               ` Stefan Kangas
2024-09-02 18:45                                 ` Sean Whitton
2024-09-02 20:39                               ` Philip Kaludercic
2024-09-02 20:42                                 ` Sean Whitton
2024-09-02 20:45                                   ` Philip Kaludercic
2024-09-02 20:46                                   ` Sean Whitton
2024-09-02 21:12                                     ` Philip Kaludercic
2024-09-03 12:21                                       ` Eli Zaretskii
2024-09-03 13:53                                         ` Robert Pluim
2024-09-03 14:27                                           ` Eli Zaretskii
2024-09-03 14:55                                             ` Robert Pluim
2024-09-03 15:32                                               ` Eli Zaretskii
2024-09-03 16:32                                         ` Philip Kaludercic
2024-09-03 16:47                                           ` Robert Pluim
2024-09-04 11:08                                           ` Eli Zaretskii
2024-09-04 14:07                                           ` Sean Whitton
2024-09-04 14:21                                             ` Eli Zaretskii
2024-09-05  9:39                                             ` Philip Kaludercic
2024-09-05  9:52                                               ` Eli Zaretskii
2024-09-05 10:02                                                 ` Philip Kaludercic
2024-09-05 10:19                                                   ` Eli Zaretskii
2024-09-05 10:23                                                     ` Philip Kaludercic
2024-09-05 13:27                                               ` Sean Whitton
2024-09-05 14:38                                                 ` Philip Kaludercic
2024-09-06 10:36                                                   ` Sean Whitton
2024-09-06 11:06                                                     ` Sean Whitton
2024-09-06 11:30                                                     ` Eli Zaretskii
2024-09-06 13:54                                                       ` Sean Whitton [this message]
2024-09-06 16:32                                                         ` Philip Kaludercic
2024-09-07  9:52                                                         ` Eli Zaretskii
2024-09-07 10:10                                                           ` Philip Kaludercic
2024-09-07 21:08                                                           ` Sean Whitton
2024-09-07 21:17                                                             ` Sean Whitton
2024-09-09 17:54                                                               ` Sean Whitton
2024-09-09 19:03                                                                 ` Eli Zaretskii
2024-09-09 19:48                                                                   ` Sean Whitton
2024-09-09 19:23                                                                 ` Philip Kaludercic
2024-09-14  9:12                                                                   ` Eli Zaretskii
2024-09-14 11:27                                                                     ` Sean Whitton
2024-09-14 13:39                                                                       ` Philip Kaludercic
2024-09-14 14:05                                                                         ` Eli Zaretskii
2024-09-14 14:13                                                                           ` Philip Kaludercic
2024-09-14 14:24                                                                             ` Eli Zaretskii
2024-09-14 16:10                                                                             ` Sean Whitton
2024-09-14 16:26                                                                               ` German Pacenza
2024-09-14 16:36                                                                                 ` Sean Whitton
2024-09-14 21:19                                                                               ` Philip Kaludercic
2024-09-14 21:22                                                                                 ` Sean Whitton
2024-09-17 17:58                                                                             ` Juri Linkov
2024-09-18 16:08                                                                               ` Juri Linkov
2024-09-19  7:19                                                                               ` Philip Kaludercic
2024-09-20  6:50                                                                                 ` Juri Linkov
2024-09-20 16:57                                                                                   ` Philip Kaludercic
2024-09-08 10:38                                                             ` Philip Kaludercic
2024-05-05 16:47                   ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-03 16:20         ` Philip Kaludercic

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r09wvqdp.fsf@zephyr.silentflame.com \
    --to=spwhitton@spwhitton.name \
    --cc=69097@debbugs.gnu.org \
    --cc=acorallo@gnu.org \
    --cc=eliz@gnu.org \
    --cc=juri@linkov.net \
    --cc=philipk@posteo.net \
    --cc=rms@gnu.org \
    --cc=stefankangas@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.