unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Jeremy Bryant <jb@jeremybryant.net>, 65511@debbugs.gnu.org
Subject: bug#65511: [PATCH] copy-next-command-output suggestion
Date: Mon, 04 Sep 2023 18:17:44 -0400	[thread overview]
Message-ID: <jwvjzt5mxg5.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <83v8d3odvt.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 25 Aug 2023 09:05:42 +0300")

> Reading the code, I'm worried by adding/removing hooks without
> suitable unwind-protect protection: what if some code signals an error
> or the user hits C-g before this code runs to completion?  We need to
> make sure these hooks are cleaned up properly in those cases.

This is a prefix command, like `C-u`, `C-x 8 u`, and a few more: it runs
to completion before it does anything useful, since its mode of
operation is to arrange for the next command to be run
slightly differently.

The way it tries to make sure to disable itself "soon" is to use
`pre/post-command-hooks` in a way that should hopefully ensure it won't
linger longer than desired.

It's not as definite as I'd like either, but it's the best way I could
come up with so far.  I think that if we want to make it better, then we
should seriously look at improving our command-loop to provide better
built-in support for prefix commands.

The patch below includes a longer comment explaining the various
possible states,, which should hopefully help convince oneself that it
should work acceptably.

> I also wonder whether we should bind interprogram-cut-function to nil
> around the call to kill-new, since this stuff probably should be put
> in the clipboard, right?

I don't understand: binding it to nil would *prevent* it from making it
to the clipboard, whereas I don't see why we shouldn't obey
`select-enable-clipboard` and `select-enable-primary` here.

> Also, what happens if some process-filter or process-sentinel or timer
> fire during the time these hooks are in effect: will the stuff added
> to the kill-ring include their output as well?

Yes.
Not sure we should try to do something about it.

> And finally, this feature only works with commands whose output goes
> to *Messages*, right?

Yup.

> If so, there are commands which show messages in other ways, and at
> the very least the doc string should mention that caveat.
> Bonus points for adding ways of capturing those other kinds of output
> as well.

Agreed.

BTW, the reason why I haven't pushed to include it in Emacs is that I'm
not really satisfied with the UI: in most cases I don't know beforehand
that I want to capture a command's output, so a "postfix command"
(i.e. one we can run after the fact) would be much preferable.

It might not be that hard to do: tho: just push markers in *Messages* at
the beginning/end of every command (unless there have been no messages
since the last push), make sure we throw away those markers that reach
`point-min`, and then add a `copy-last-command-output` command that uses
those markers to extract the last message using those markers.
The tricky part will be to find the right message when messages are
emitted (e.g. by the completion UI) while the user types
`M-x copy-last-command-output RET`.


        Stefan


diff --git a/doc/emacs/screen.texi b/doc/emacs/screen.texi
index 5e9e89e6b11..4e65bc1105f 100644
--- a/doc/emacs/screen.texi
+++ b/doc/emacs/screen.texi
@@ -146,6 +146,11 @@ Echo Area
 this limit, one line is deleted from the beginning whenever a new
 message line is added at the end.
 
+@cindex{copy-next-command-output}
+  You can also capture the messages of a command by running the
+command @code{copy-next-command-output} beforehand, which will put them
+in the kill ring.
+
   @xref{Display Custom}, for options that control how Emacs uses the
 echo area.
 
diff --git a/etc/NEWS b/etc/NEWS
index fbb13254e64..d5bd372ecfb 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -162,6 +162,8 @@ displayed on the mode line when 'appt-display-mode-line' is non-nil.
 \f
 * Editing Changes in Emacs 30.1
 
+** New command 'copy-next-command-output' to capture echo area messages
+
 ---
 ** New global minor mode 'kill-ring-deindent-mode'.
 When enabled, text being saved to the kill ring will be de-indented by
diff --git a/lisp/simple.el b/lisp/simple.el
index 05a3c4b93d6..2fc1b2dad96 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -5877,6 +5877,67 @@ copy-region-as-kill
   (setq deactivate-mark t)
   nil)
 
+(defun copy-next-command-output ()
+  "Prefix command to add the output of the next command to the `kill-ring`.
+\"Output\" here refers to text emitted in the echo area, and currently this
+is limited to those messages which also appear in the *Messages* buffer."
+  (interactive)
+  (let ((md (minibuffer-depth))
+        (marker (with-current-buffer "*Messages*"
+                  (point-max-marker))))
+    ;; We consider the following states:
+    ;; - The command (and potentially a few other prefix commands) has just
+    ;;   been run, `post-command-hook' is unaffected yet and `pre-command-hook'
+    ;;   and `prefix-command-*' hooks are set.
+    ;; - The next command is about to be executed: we already run
+    ;;   `pre-command-hook'.  All the hooks are set.
+    ;; - We just finished running the next command: `post-command-hook'
+    ;;   should then hopefully remove all the hooks.
+    ;; - Within a minibuffer: commands run in the minibuffer should not affects
+    ;;   hooks since they are either "within" another prefix command
+    ;;   (such as `C-x 8 u') or within the command we want to affect.
+    ;;   We do (re)set the `marker', OTOH so as to try and skip the messages
+    ;;   that occur while we're inside the minibuffer.
+    (cl-labels ((pre ()
+                     (unless (> (minibuffer-depth) md)
+                       (add-hook 'post-command-hook #'post)
+                       (prepare)))
+                (prepare ()
+                         (with-current-buffer "*Messages*"
+                           (move-marker marker (point-max))))
+                (preserve ()
+                          (unless (> (minibuffer-depth) md)
+                            (remove-hook 'post-command-hook #'post)
+                            (add-hook 'pre-command-hook #'pre)))
+                (echo ()
+                      (unless (> (minibuffer-depth) md)
+                        "[copy-output]"))
+                (post ()
+                      (if (> (minibuffer-depth) md)
+                          ;; Prepare, in case there's no pre-command-hook before
+                          ;; the next post-command-hook.  E.g. in the case of
+                          ;; execute-extended-command.
+                          (prepare)
+                        (remove-hook 'pre-command-hook #'pre)
+                        (remove-hook 'post-command-hook #'post)
+                        (remove-hook 'prefix-command-preserve-state-hook
+                                     #'preserve)
+                        (remove-hook 'prefix-command-echo-keystrokes-functions
+                                     #'echo)
+                        (prefix-command-update)
+                        (with-current-buffer (marker-buffer marker)
+                          (when (< marker (point-max))
+                            (kill-new (buffer-substring marker (point-max)))))
+                          (set-marker marker nil))))
+      (add-hook 'prefix-command-preserve-state-hook #'preserve)
+      (add-hook 'prefix-command-echo-keystrokes-functions #'echo)
+      ;; (message "BEFORE: prefix-arg=%S current-prefix-arg=%S"
+      ;;          prefix-arg current-prefix-arg)
+      (prefix-command-preserve-state)
+      ;; (message "AFTER: prefix-arg=%S current-prefix-arg=%S"
+      ;;          prefix-arg current-prefix-arg)
+      )))
+
 (defun kill-ring-save (beg end &optional region)
   "Save the region as if killed, but don't kill it.
 In Transient Mark mode, deactivate the mark.






  reply	other threads:[~2023-09-04 22:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-24 21:46 bug#65511: [PATCH] copy-next-command-output suggestion Jeremy Bryant
2023-08-25  6:05 ` Eli Zaretskii
2023-09-04 22:17   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-09-05 11:14     ` Eli Zaretskii
2023-09-05 13:10       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-05 18:33         ` Jeremy Bryant

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=jwvjzt5mxg5.fsf-monnier+emacs@gnu.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=65511@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=jb@jeremybryant.net \
    --cc=monnier@iro.umontreal.ca \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).