unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Should write-contents-functions be permanent-local?
@ 2023-08-15  6:35 Joseph Turner
  2023-08-15 11:52 ` Eli Zaretskii
  0 siblings, 1 reply; 3+ messages in thread
From: Joseph Turner @ 2023-08-15  6:35 UTC (permalink / raw)
  To: emacs-devel

In hyperdrive.el, we set write-contents-functions in a local minor mode
so that buffer contents can be written to a hyperdrive "file." In our
use-case, we want write-contents-functions to stay the same even when
the user manually changes the buffer's major mode. AFAIK, there's no way
to make a variable permanent-local on a per-buffer basis, and we don't
dare to make write-contents-functions permanent-local globally.

The solution we came up with in hyperdrive.el is to hook into
after-change-major-mode-hook with a function (with permanent-local-hook)
that sets write-contents-functions after kill-all-local-variables:

https://git.sr.ht/~ushin/hyperdrive.el/tree/master/item/hyperdrive.el#L320

Does anyone have a better solution?

WDYT about making write-contents-functions permanent-local?

Joseph



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

* Re: Should write-contents-functions be permanent-local?
  2023-08-15  6:35 Should write-contents-functions be permanent-local? Joseph Turner
@ 2023-08-15 11:52 ` Eli Zaretskii
  2023-08-16  1:48   ` Joseph Turner
  0 siblings, 1 reply; 3+ messages in thread
From: Eli Zaretskii @ 2023-08-15 11:52 UTC (permalink / raw)
  To: Joseph Turner; +Cc: emacs-devel

> From: Joseph Turner <joseph@breatheoutbreathe.in>
> Date: Mon, 14 Aug 2023 23:35:44 -0700
> 
> WDYT about making write-contents-functions permanent-local?

That cannot be done, sorry.  Some modes set this to mode-specific
values, you can find this even in the Emacs source tree.  But even
without those examples, a variable that were not permanent-local for
so long cannot be suddenly made permanent-local, because it will
almost certainly break something somewhere.

In any case, if you are okay with making the variable permanent-local,
then why not just

  (put 'write-contents-functions 'permanent-local t)

in your package, and be done with it?  The effect of the above is the
same global effect as if we would do that by default in Emacs, right?



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

* Re: Should write-contents-functions be permanent-local?
  2023-08-15 11:52 ` Eli Zaretskii
@ 2023-08-16  1:48   ` Joseph Turner
  0 siblings, 0 replies; 3+ messages in thread
From: Joseph Turner @ 2023-08-16  1:48 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel


Eli Zaretskii <eliz@gnu.org> writes:

>> From: Joseph Turner <joseph@breatheoutbreathe.in>
>> Date: Mon, 14 Aug 2023 23:35:44 -0700
>>
>> WDYT about making write-contents-functions permanent-local?
>
> That cannot be done, sorry.  Some modes set this to mode-specific
> values, you can find this even in the Emacs source tree.  But even
> without those examples, a variable that were not permanent-local for
> so long cannot be suddenly made permanent-local, because it will
> almost certainly break something somewhere.

Thank you for the explanation!

> In any case, if you are okay with making the variable permanent-local,
> then why not just
>
>   (put 'write-contents-functions 'permanent-local t)
>
> in your package, and be done with it?  The effect of the above is the
> same global effect as if we would do that by default in Emacs, right?

I was concerned that would break things. (whereas I thought if we made
the change in Emacs, we'd have a chance to ensure that it did not.)

Probably best not to change things - our current solution seems to work:

(defun hyperdrive--hack-write-contents-functions ()
  (cl-pushnew #'hyperdrive--write-contents write-contents-functions))
(put 'hyperdrive--hack-write-contents-functions 'permanent-local-hook t)

(define-minor-mode hyperdrive-mode
  "Minor mode for buffers opened from hyperdrives."
  (if hyperdrive-mode
      (progn
        (cl-pushnew #'hyperdrive--write-contents write-contents-functions)
        (add-hook 'after-change-major-mode-hook
                  #'hyperdrive--hack-write-contents-functions nil 'local))
    (setq-local write-contents-functions
                (remove #'hyperdrive--write-contents write-contents-functions))
    (remove-hook 'after-change-major-mode-hook
                 #'hyperdrive--hack-write-contents-functions 'local)))



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

end of thread, other threads:[~2023-08-16  1:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-15  6:35 Should write-contents-functions be permanent-local? Joseph Turner
2023-08-15 11:52 ` Eli Zaretskii
2023-08-16  1:48   ` Joseph Turner

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