unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Reacting to local var changes in mode-hooks and file-local vars
@ 2010-12-10 20:31 Stefan Monnier
  2010-12-16  1:24 ` Chong Yidong
  2010-12-16  1:39 ` Lennart Borgman
  0 siblings, 2 replies; 4+ messages in thread
From: Stefan Monnier @ 2010-12-10 20:31 UTC (permalink / raw)
  To: emacs-devel

Some major modes currently do things like

   (defun my-mode ()
     ...
     (kill-all-local-variables)
     ...
     (run-mode-hooks 'my-mode-hook)
     ...set up some more vars...)

The "set up some more vars" code is there because those vars may depend
on some other vars that are likely to be changed by the user in the
my-mode-hook.
This is actually a hack.  It works in some cases but has several
downsides, such as:
- can't use define-derived-mode to define such a mode.
- can't derive from this mode since the "set up some more vars" would
  then be run before the child mode hook.
- does not react properly to file-local variable settings.

Currently, the best fix I know is to do:

  (define-derived-mode my-mode nil "My"
    ...
    (add-hook 'hack-local-variables-hook
              (lambda () ...set up some more vars...)
              nil t))

but this also has some downsides:
- hack-local-variables-hook is not run if enable-local-variables is nil.
- hack-local-variables-hook is not run for non-file-modes
  (e.g. comint modes) or when the user does M-x my-mode RET

Another option is to use something like after-change-major-mode-hook.
But that one tends to be called before file-local vars.

I'd like to find a good solution to this problem.  I think part of the
solution is to make it so that M-x my-mode RET will also run
hack-local-variables.

My current idea is to try and make it so that, just before running
after-change-major-mode-hook, we either call hack-local-variables or
hack-dir-local-variables-non-file-buffer.


        Stefan



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

* Re: Reacting to local var changes in mode-hooks and file-local vars
  2010-12-10 20:31 Reacting to local var changes in mode-hooks and file-local vars Stefan Monnier
@ 2010-12-16  1:24 ` Chong Yidong
  2010-12-17  4:34   ` Stefan Monnier
  2010-12-16  1:39 ` Lennart Borgman
  1 sibling, 1 reply; 4+ messages in thread
From: Chong Yidong @ 2010-12-16  1:24 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> The "set up some more vars" code is there because those vars may depend
> on some other vars that are likely to be changed by the user in the
> my-mode-hook.

Do you have an example?  I think it's generally bad policy for a major
mode to second-guess what the user might do in the mode hook.



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

* Re: Reacting to local var changes in mode-hooks and file-local vars
  2010-12-10 20:31 Reacting to local var changes in mode-hooks and file-local vars Stefan Monnier
  2010-12-16  1:24 ` Chong Yidong
@ 2010-12-16  1:39 ` Lennart Borgman
  1 sibling, 0 replies; 4+ messages in thread
From: Lennart Borgman @ 2010-12-16  1:39 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

On Fri, Dec 10, 2010 at 9:31 PM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
> Some major modes currently do things like
>
>   (defun my-mode ()
>     ...
>     (kill-all-local-variables)
>     ...
>     (run-mode-hooks 'my-mode-hook)
>     ...set up some more vars...)
>
> The "set up some more vars" code is there because those vars may depend
> on some other vars that are likely to be changed by the user in the
> my-mode-hook.
> This is actually a hack.  It works in some cases but has several
> downsides, such as:
> - can't use define-derived-mode to define such a mode.
> - can't derive from this mode since the "set up some more vars" would
>  then be run before the child mode hook.
> - does not react properly to file-local variable settings.
>
> Currently, the best fix I know is to do:
>
>  (define-derived-mode my-mode nil "My"
>    ...
>    (add-hook 'hack-local-variables-hook
>              (lambda () ...set up some more vars...)
>              nil t))
>
> but this also has some downsides:
> - hack-local-variables-hook is not run if enable-local-variables is nil.
> - hack-local-variables-hook is not run for non-file-modes
>  (e.g. comint modes) or when the user does M-x my-mode RET

Maybe something like

(define-derived-mode my-mode nil "Your"
  ...
  :after-local-variables-setup (lambda () ...)
  )



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

* Re: Reacting to local var changes in mode-hooks and file-local vars
  2010-12-16  1:24 ` Chong Yidong
@ 2010-12-17  4:34   ` Stefan Monnier
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2010-12-17  4:34 UTC (permalink / raw)
  To: Chong Yidong; +Cc: emacs-devel

>> The "set up some more vars" code is there because those vars may depend
>> on some other vars that are likely to be changed by the user in the
>> my-mode-hook.
> Do you have an example?  I think it's generally bad policy for a major
> mode to second-guess what the user might do in the mode hook.

Examples are along the lines of "adjust font-lock keywords according to
user settings like foo-mode-extra-keywords or foo-mode-sublanguage".
If you grep for "add-hook 'hack-local-variables-hook" in lisp/progmodes,
you'll see a few example cases.


        Stefan



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

end of thread, other threads:[~2010-12-17  4:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-10 20:31 Reacting to local var changes in mode-hooks and file-local vars Stefan Monnier
2010-12-16  1:24 ` Chong Yidong
2010-12-17  4:34   ` Stefan Monnier
2010-12-16  1:39 ` Lennart Borgman

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