unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* custom function getting initialized randomly
@ 2023-03-27  8:22 Michael Maurer
  2023-03-27 13:56 ` Richard Copley
  0 siblings, 1 reply; 8+ messages in thread
From: Michael Maurer @ 2023-03-27  8:22 UTC (permalink / raw)
  To: help-gnu-emacs

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)



^ permalink raw reply	[flat|nested] 8+ messages in thread

* custom function getting initialized randomly
@ 2023-03-27  8:29 Michael Maurer
  0 siblings, 0 replies; 8+ messages in thread
From: Michael Maurer @ 2023-03-27  8:29 UTC (permalink / raw)
  To: help-gnu-emacs

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)



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: custom function getting initialized randomly
  2023-03-27  8:22 custom function getting initialized randomly Michael Maurer
@ 2023-03-27 13:56 ` Richard Copley
  2023-03-27 14:56   ` Michael Maurer
  2023-03-27 15:20   ` tomas
  0 siblings, 2 replies; 8+ messages in thread
From: Richard Copley @ 2023-03-27 13:56 UTC (permalink / raw)
  To: Michael Maurer; +Cc: help-gnu-emacs

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`.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: custom function getting initialized randomly
  2023-03-27 13:56 ` Richard Copley
@ 2023-03-27 14:56   ` Michael Maurer
  2023-03-27 15:06     ` Richard Copley
  2023-03-27 15:20   ` tomas
  1 sibling, 1 reply; 8+ messages in thread
From: Michael Maurer @ 2023-03-27 14:56 UTC (permalink / raw)
  To: Richard Copley; +Cc: help-gnu-emacs

On Mon, 27 Mar 2023 at 15:56, Richard Copley <rcopley@gmail.com> wrote:
>
> 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`.

Would wrapping the body of the function in (ignore errors ..) be an ok
solution as far as cheap hacks go, or is it too sketchy?



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: custom function getting initialized randomly
  2023-03-27 14:56   ` Michael Maurer
@ 2023-03-27 15:06     ` Richard Copley
  0 siblings, 0 replies; 8+ messages in thread
From: Richard Copley @ 2023-03-27 15:06 UTC (permalink / raw)
  To: Michael Maurer; +Cc: help-gnu-emacs

On Mon, 27 Mar 2023 at 15:56, Michael Maurer <maurer.michael@gmail.com> wrote:
> Would wrapping the body of the function in (ignore errors ..) be an ok
> solution as far as cheap hacks go, or is it too sketchy?

Your call, but I would try wrapping it in (when kill-ring ...) before
going that far.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: custom function getting initialized randomly
  2023-03-27 13:56 ` Richard Copley
  2023-03-27 14:56   ` Michael Maurer
@ 2023-03-27 15:20   ` tomas
  2023-03-27 15:30     ` Michael Maurer
  1 sibling, 1 reply; 8+ messages in thread
From: tomas @ 2023-03-27 15:20 UTC (permalink / raw)
  To: help-gnu-emacs

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

On Mon, Mar 27, 2023 at 02:56:01PM +0100, Richard Copley wrote:
> 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`.

There's more: where does the variable `prev-kill-string' come from?

 - if nobody has set it previously, this will be the error
   killing your hook (this is my hunch)
 - if somebody has, you are lucky if that somebody was you
   (otherwise you are into an entertaining debugging session

If you are using a global variable like that, it makes sense
to declare, document and perhaps initialize it with `defvar'

Another possibility would be to hide it away in a closure [1]
(you are using lexical binding, aren't you?).  Just ask around
here if the concept is unfamiliar to you.

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: custom function getting initialized randomly
  2023-03-27 15:20   ` tomas
@ 2023-03-27 15:30     ` Michael Maurer
  2023-03-27 15:52       ` tomas
  0 siblings, 1 reply; 8+ messages in thread
From: Michael Maurer @ 2023-03-27 15:30 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs

On Mon, 27 Mar 2023 at 17:21, <tomas@tuxteam.de> wrote:
>
> On Mon, Mar 27, 2023 at 02:56:01PM +0100, Richard Copley wrote:
> > 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`.
>
> There's more: where does the variable `prev-kill-string' come from?
>
>  - if nobody has set it previously, this will be the error
>    killing your hook (this is my hunch)
>  - if somebody has, you are lucky if that somebody was you
>    (otherwise you are into an entertaining debugging session
>
> If you are using a global variable like that, it makes sense
> to declare, document and perhaps initialize it with `defvar'
>
> Another possibility would be to hide it away in a closure [1]
> (you are using lexical binding, aren't you?).  Just ask around
> here if the concept is unfamiliar to you.
>

You're right, that's something I forgot to paste.
The function is preceded by (defvar prev-kill-string "")
I'll take a look at closures and see how far I get on my own, ty.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: custom function getting initialized randomly
  2023-03-27 15:30     ` Michael Maurer
@ 2023-03-27 15:52       ` tomas
  0 siblings, 0 replies; 8+ messages in thread
From: tomas @ 2023-03-27 15:52 UTC (permalink / raw)
  To: Michael Maurer; +Cc: help-gnu-emacs

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

On Mon, Mar 27, 2023 at 05:30:50PM +0200, Michael Maurer wrote:
> On Mon, 27 Mar 2023 at 17:21, <tomas@tuxteam.de> wrote:

[...]

> > There's more: where does the variable `prev-kill-string' come from?

[...]

> You're right, that's something I forgot to paste.
> The function is preceded by (defvar prev-kill-string "")
> I'll take a look at closures and see how far I get on my own, ty.

Ah, OK. I'll shut up, then :-D

Cheers
-- 
t

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-03-27 15:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-27  8:22 custom function getting initialized randomly Michael Maurer
2023-03-27 13:56 ` Richard Copley
2023-03-27 14:56   ` Michael Maurer
2023-03-27 15:06     ` Richard Copley
2023-03-27 15:20   ` tomas
2023-03-27 15:30     ` Michael Maurer
2023-03-27 15:52       ` tomas
  -- strict thread matches above, loose matches on Subject: below --
2023-03-27  8:29 Michael Maurer

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).