joostkremers / writeroom-mode

Writeroom-mode: distraction-free writing for Emacs.
BSD 3-Clause "New" or "Revised" License
611 stars 28 forks source link

add a "buffer effects" var #62

Closed snan closed 4 years ago

snan commented 4 years ago

In writeroom--enable there is

(when (not writeroom--buffers)
  (setq writeroom--frame (selected-frame))
  (writeroom--set-global-effects 1)
  (if writeroom-restore-window-config
  (setq writeroom--saved-window-config (current-window-configuration))))

Which is great but I hooked on some other custom stuff into writeroom-global-effects and they aren't getting run in writeroom-global-mode, only when activating a single writeroom buffer.. Which, ok, they aren't what writeroom calls "global effects", so that tracks. I first tried adding hooks:

(defun to-june-b ()
   (interactive)
   (setq buffer-face-mode-face '(:family "Junicode" :height 140))
   (buffer-face-mode))

(defun unbufface ()
  (interactive)
  (buffer-face-mode -1))

(add-hook 'writeroom-mode-hook 'to-june-b)
(add-hook ' writeroom-mode-disable-hook 'unbufface)

But for some reason I'm still left in buffer-face-mode when I disable writeroom-mode. The hook is getting run (I tried putting some message stuff in there, just to check and that worked), so I don't know why. Everything looks cool in writeroom.el.

snan commented 4 years ago

Uh, I think I might be XY probleming it up a bit in here. What I want is a specific font only for Writeroom buffers.

joostkremers commented 4 years ago

I'd have to look into this a bit more than I have time for right now. Does unbufface work if you call it with M-x?

snan commented 4 years ago

Yes, it does. I just checked.

I haven't been using unbufface too much since I can also call buffer-face-mode directly from M-x.

joostkremers commented 4 years ago

It's been a while, sorry for that, but I finally got round to solving this issue. The reason buffer-face-mode wasn't disabled turned out to be something I'd never realised before: minor-mode hooks are run when the minor mode is enabled, but (surprisingly, to me) also when the mode is disabled. So in your case, disabling writeroom-mode would call unbufface, but then to-june-b would be called again immediately after.

I added a hook writeroom-mode-enable-hook, which is run only when writeroom-mode is enabled, so you can use that instead. I also added an option writeroom-local-effects, which can be used to add buffer-local effects to your writeroom-mode setup. You can add minor mode functions to it, e.g.,:

(add-hook 'writeroom-local-effects 'variable-pitch-mode)

though in your case, that may not be sufficient, because you want to use a particular face. You'd have to write a function (or define a minor mode) that activates buffer-face-mode with the correct face yourself. Or you could keep the functions you have now and do:

(add-hook 'writeroom-mode-enable-hook 'to-june-b)
(add-hook 'writeroom-mode-disable-hook 'unbufface)

Thanks for reporting this bug and please let me know if you still run into problems.

snan commented 4 years ago

minor-mode hooks are run when the minor mode is enabled, but (surprisingly, to me) also when the mode is disabled.

I would've never guessed that! Are they also run with the same args?

You'd have to write a function (or define a minor mode) that activates buffer-face-mode with the correct face yourself.

Thank you, I will♥

joostkremers commented 4 years ago

I would've never guessed that!

I've been using Emacs for over 20 years (huh? Has it really been that long? :open_mouth:) and I never would have guessed either...

Are they also run with the same args?

The functions in a mode hook are run without any arguments, so technically, yeah, they are. :smiley: