[Top][All Lists]

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

Re: A patch for `pwd' - copying the current directory to the kill ring

From: Richard Copley
Subject: Re: A patch for `pwd' - copying the current directory to the kill ring
Date: Thu, 25 Jan 2018 23:05:54 +0000

On 25 January 2018 at 20:13, Stefan Monnier <address@hidden> wrote:
>> Except making it work with M-x (the above will grab the "output" of the
>> M-x itself (i.e. no output), rather than the output of the command Emacs
>> runs after running the commands bound to M-x, p, w, d, and RET).
> The version below seems to work (tested with M-: and M-x)
>         Stefan
> (defvar copy-next-command-output--marker nil)
> (defun copy-next-command-output ()
>   "Prefix command to add the output of the next command to the `kill-ring`."
>   (interactive)
>   (let ((ml (minibuffer-depth)))
>     (cl-labels ((pre ()
>                      (add-hook 'post-command-hook #'post)
>                      (setq copy-next-command-output--marker
>                            (with-current-buffer "*Messages*"
>                              (point-max-marker))))
>                 (post ()
>                       (unless (> (minibuffer-depth) ml)
>                         (remove-hook 'pre-command-hook #'pre)
>                         (remove-hook 'post-command-hook #'post)
>                         (when copy-next-command-output--marker
>                           (with-current-buffer
>                               (marker-buffer copy-next-command-output--marker)
>                             (when (< copy-next-command-output--marker
>                                      (point-max))
>                               (kill-new (buffer-substring
>                                          copy-next-command-output--marker
>                                          (point-max)))))
>                           (setq copy-next-command-output--marker nil)))))
>       (add-hook 'pre-command-hook #'pre))))

I evaluated those two forms, and this one:

(global-set-key [?\C-c ?p] #'copy-next-command-output)

Then in an empty buffer,

;; Put "x" on the kill ring
<C-backspace>    ;; backward-kill-word
;; Test the new prefix command on "M-x pwd"
C-c p
M-x pwd RET
C-y        ;; yank

The result is "x" (in emacs-26 -Q). Where did I go wrong?

The interactive message output of a command (in this case, e.g.,
"Directory C:/Users/Buster") is not usually going to be useful as a
building block.

I think the generalisation is unwarranted in this case anyway. I bind
C-c f to a command that puts the pwd on the kill ring, with a few
tweaks to make it more useful to me (it optionally replaces "/" with
"\", and if called in a buffer visiting a file, returns the file's
name). It's more specialised, not more general. I use it a lot.

reply via email to

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