emacs-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] Run hook when variable is set


From: Kelly Dean
Subject: Re: [PATCH] Run hook when variable is set
Date: Sat, 31 Jan 2015 09:18:03 +0000

Stefan Monnier wrote:
> you can drop the let-with-hook

Without it, code which would be simply:
(let-with-hook ((hooked-var foo))
               (body))

would have to become:
(setq tmp hooked-var)
(setq-with-hook hooked-var foo)
(body)
(setq-with-hook hooked-var tmp)

That's cumbersome.

> since it would be a misfeature anyway,

If it's missing (and you don't manually do it as above), then the hooked 
variable and the dependent variable become unsynchronized when the former is 
let-bound. That's bound to introduce bugs.

> and then
> consolidate the remaining 4 into a single function.

How do you combine setq-with-hook, set-with-hook, setq-default-with-hook, 
set-default-with-hook, and setq-local-with-hook into a single function?

However you do it, if it's really a reasonable thing to do, then why aren't 
setq, set, setq-default, set-default, and setq-local also combined into a 
single function in the same way?

Anyway, since you said varhook, or something like it, would be useful for 
debugging even though you don't want to use it to implement regular features, 
is my implementation reasonably close to what you had in mind?


WRT DCM, in deactivate-mark, you put:
(when dynamic-cursor-mode
         (if (and dynamic-cursor-mode--set (eq cursor-type 'bar))
             (setq cursor-type t))
         (setq dynamic-cursor-mode--set nil))

and in activate-mark:
(if (and dynamic-cursor-mode (eq cursor-type t))
           (setq cursor-type 'bar dynamic-cursor-mode--set t))

That would fail in this case:
(setq-default cursor-type 'bar)
Later in the same Emacs session...
(dynamic-cursor-mode)

The user would reasonably think that DCM's failure to work in that case is a 
bug.

To solve that, you wrote, ⌜dynamic-cursor-mode can do (setq-default cursor-type 
t).⌝ IIUC, you meant:
(define-minor-mode dynamic-cursor-mode
  "docs..." :global t :init-value t
  (if dynamic-cursor-mode
      (setq-default cursor-type t)))

In that case, how do you turn it on buffer-locally? If cursor-type isn't set to 
t, and you do:
(setq-local dynamic-cursor-mode t)

then it won't work. If you instead do:
(make-local-variable 'dynamic-cursor-mode)
(dynamic-cursor-mode)

then DCM is enabled only locally, which is the correct thing to do, but 
cursor-type is globally set to t, which is a bug. Turning on DCM locally must 
not affect CT globally.

I guess you could fix that bug with:
(define-minor-mode dynamic-cursor-mode
  "docs..." :global t :init-value t
  (if dynamic-cursor-mode
      (if (assq 'dynamic-cursor-mode (buffer-local-variables))
          (setq cursor-type t)
        (setq-default cursor-type t))))

> The code I provided disables the effect of dynamic-cursor-mode as soon
> as the value of cursor-type is not the expected one (without
> needing to disable dynamic-cursor-mode).

This has the edge case that if the mark happens to be active when you set 
cursor-type to 'bar, then the setting doesn't stick; DCM doesn't realize that 
CT was set outside of DCM's control, so DCM overrides the setting. Similarly, 
if you previously always used a non-standard cursor type, and were therefore 
mislead into thinking DCM was disabled, then if you set cursor-type to t, DCM 
unexpectedly starts operating. That isn't user-friendly.

Another poor result of your design is that if you set a non-standard cursor 
type in your init file, then start Emacs and do C-h m, DCM is listed as 
enabled, even though it's having no effect, which is a great way to confuse the 
user, or convince him that there's a bug.

Using varhook for DCM avoids those problems.



reply via email to

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