help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: custom function getting initialized randomly


From: Richard Copley
Subject: Re: custom function getting initialized randomly
Date: Mon, 27 Mar 2023 14:56:01 +0100

On Mon, 27 Mar 2023 at 10:21, Michael Maurer <maurer.michael@gmail.com> wrote:
>
> Hello,
>
> I wrote the following function to copy the kill-ring to the scratch
> buffer, but it only gets initialized sometimes at the start of emacs,
> sometimes not. Why?
>
> (defun copy-to-scratch-on-kill
> ()
> "Copy every kill (delete or yank) to *scratch* buffer."
> (let
>     ((cur-kill-string
>       (current-kill 0 t)))
>   (when
>       (and
>        (not
> (equal cur-kill-string ""))
>        (not
> (equal cur-kill-string prev-kill-string))
>        (get-buffer "*scratch*"))
>     (with-current-buffer "*scratch*"
>       (goto-char
>        (point-max))
>       (insert cur-kill-string "\n")
>       (goto-char
>        (point-max))))
>   (setq prev-kill-string cur-kill-string)))
>
> (add-hook 'post-command-hook #'copy-to-scratch-on-kill)
>

If an error propagates from the hook function (most likely "Kill ring
is empty", signalled by `current-kill`) then Emacs removes the
function from the hook. See the docstring for variable
`post-command-hook`.



reply via email to

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