3

Whenever I open a pdf in emacs, I'd like to increase the font size. I thought I would just place (doc-view-enlarge) in a 'doc-view-mode-hook. However, I can't find such a hook.

Yet this source claims the hook exists:

http://www.r-bloggers.com/using-doc-view-with-auto-revert-to-view-latex-pdf-output-in-emacs/

What is the hook I am looking for in this case?

erjoalgo
  • 853
  • 1
  • 5
  • 18
  • Unfortunately, that source is 5+ years old, and it looks like that hook no longer exists. – Dan Jan 19 '15 at 15:44
  • 1
    @Dan I see that hook reference in `lisp/doc-view.el` in emacs 24.4 source. – Kaushal Modi Jan 19 '15 at 16:08
  • @kaushalmodi: weird: the only hook I have is `doc-view-clone-buffer-hook`, although I'm on 24.3. – Dan Jan 19 '15 at 16:09
  • @Dan That's strange.. I have been using that hook forever now for a different purpose though. `(add-hook 'doc-view-mode-hook 'auto-revert-mode)` – Kaushal Modi Jan 19 '15 at 16:11
  • @user84207 What happens when you use the hook? Does emacs give an error? – Kaushal Modi Jan 19 '15 at 16:19
  • the variable does not exist – erjoalgo Jan 19 '15 at 16:23
  • @user84207 What emacs version are you on? Did you try without loading your init file, `\emacs -Q&`? I am on emacs 24.4 stable and after launching `\emacs -Q&` I can successfully evaluate (`C-x C-e`) `(add-hook 'doc-view-mode-hook 'auto-revert-mode)` in the \*scratch\* buffer. – Kaushal Modi Jan 19 '15 at 16:27
  • I believe the confusion is because `doc-view.el` is missing `(defvar doc-view-mode-hook nil)`. – Kaushal Modi Jan 19 '15 at 16:30
  • @kaushalmodi GNU Emacs 24.3.1. I have no problems with my init-file. You can successfully evaluate (add-hook 'any-random-unbound-symbol 'non-existent-command), but normally the hook is `defvar`-ed by the mode that uses it – erjoalgo Jan 19 '15 at 16:48
  • @user84207 I think that the `defvar` is not mandatory. I can check the value of the `doc-view-mode-hook` by doing `C-h v` after doing `add-hook` and it also works as intended. – Kaushal Modi Jan 19 '15 at 16:50

1 Answers1

7

Mode hooks exist, whether or not they are predefined variables.

If doc-view-mode is a major mode then doc-view-mode-hook is alive and well.

That is to say that the coding conventions call for a major mode to run its mode hook when the mode is turned on. But typically no hook variable is predefined.

The doc for define-derived-mode tells you that the newly defined mode will run a hook that is constructed by derived-mode-hook-name, which interns the hook name.

In other words, no such variable is defined ahead of time, but when you enter the mode its symbol is interned and bound to a local variable, which is run as a hook (the functions in its value are invoked).

If there happens to be a global variable of the same name then that variable is bound (dynamically). But there need not be such a global variable.

From Elisp, Major Mode Conventions:

 Each major mode should have a normal "mode hook" named
 `MODENAME-mode-hook'.  The very last thing the major mode command
 should do is to call `run-mode-hooks'.  This runs the normal hook
 `change-major-mode-after-body-hook', the mode hook, and then the
 normal hook `after-change-major-mode-hook'.
Drew
  • 75,699
  • 9
  • 109
  • 225
  • 1
    Awesome. I did not know this. I thought I was at the mercy of the mode's writer's choice to define a hook, including his choice of a naming convention. – erjoalgo Jan 20 '15 at 04:07
  • After reading the doc a bit, if you think it could be improved to make this clearer, consider filing an enhancement request: `M-x report-emacs-bug`. – Drew Jan 20 '15 at 05:33
  • Actually, yeah, that is what I forgot to add. That I'm pretty sure I had read that part of the docs before, but my impression was still what I expressed above. – erjoalgo Jan 20 '15 at 06:37