rhashimoto
rhashimoto

Reputation: 15841

How do I configure Emacs to dedicate the Calculator window?

I'm using emacs 24.3 from emacsformacosx.com on OS X 10.9 (Mavericks). The behavior is the same on emacs 23.4.1 on Debian Wheezy.

I want to automate applying set-window-dedicated-p so switching/opening a buffer won't use certain windows. For example, if I'm in the Calculator and manually use Meta-: and enter (set-window-dedicated-p (get-buffer-window) t) then it works great - my Calculator window doesn't get hijacked by other buffers. I want it to work like that automatically.

I put this in my .emacs file:

(add-hook 'calc-mode-hook
  (lambda ()
    (message "Dedicating %s" (buffer-name))
    (set-window-dedicated-p (get-buffer-window) t)
    (message "Dedication %s" (window-dedicated-p (get-buffer-window "*Calculator*")))))

Then I start up emacs, switch to the *Messages* window, and Meta-x calc. The *Messages* buffer shows

Dedicating *Calculator*
Dedication t

so I know my hook was called and what buffer it operated on. But the *Calculator* buffer is not dedicated - it doesn't behave properly and Meta-: (window-dedicated-p) returns nil. The *Messages* buffer is dedicated instead.

Why is the *Calculator* window shown as dedicated in the hook but not afterwards? What am I doing wrong here?

Upvotes: 3

Views: 229

Answers (1)

phils
phils

Reputation: 73246

Unfortunately the *Calculator* buffer is not displayed in any window at the point your code runs.

Your 'validation' messages were misleading you. (buffer-name) is certainly the buffer you want, but it's not in any window, and so you're actually passing a nil argument for the window in all situations. i.e. You're setting the current window dedicated, and then confirming that it's dedicated (which it should indeed be).

I think after advice to calc is what you need here. e.g.:

(defadvice calc (after my-dedicated-calc-window)
  "Make the *Calculator* window dedicated."
  (let ((win (get-buffer-window "*Calculator*")))
    (when win
      (set-window-dedicated-p win t))))
(ad-activate 'calc)

n.b. I'm not sure exactly how the arguments to calc affect the window display, but I think with the test for the window wrapping the call to set-window-dedicated-p this code is probably fine in all cases.

Upvotes: 4

Related Questions