0

I have the following configuration lines in my init.el file regarding a specific mode:

(add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd "SPC h") #'evil-window-left)))
(add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd "SPC l") #'evil-window-right)))
(add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd "SPC k") #'evil-window-up)))
(add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd "SPC j") #'evil-window-down)))
(add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd "SPC") #'nil)))
(add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd "j") 'pdf-view-next-line-or-next-page)))
(add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd "k") 'pdf-view-previous-line-or-previous-page)))
(add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd "4") 'pdf-annot-add-highlight-markup-annotation)))

I thought to myself: "Hey this is a good place to use a macro!". What I want to do is to be able to type

(pdf-binding "something" function)

And that it expands to

(add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd "something") #'function)))

Very similar to C's macros, the simple "text replacement" stuff.

So I tried the following:

(defmacro pdf-binding (key function)
  (add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd key) function))))

But when I tried using it like in the example above, I get the following error:

Symbol's value as variable is void: pdf-tools-enable-minor-modes

I tried using " ", doing(list add-hook ...)`. But nothing works.

How could I achieve it? What is the problem?

2
  • 2
    Does this answer your question? How does a Lisp macro behave?
    – shynur
    Commented Jan 19 at 15:33
  • 3
    Don't use lambdas as hook functions - makes it hard to remove them.
    – Drew
    Commented Jan 19 at 15:36

1 Answer 1

1

Adding eight anonymous functions to the hook makes no sense to me, vs this:

(add-hook 'pdf-view-mode-hook #'my-pdf-view-mode-config)

(defun my-pdf-view-mode-config ()
  "Called via `pdf-view-mode-hook'."
  (local-set-key (kbd "SPC") nil)
  (local-set-key (kbd "SPC h") #'evil-window-left)
  (local-set-key (kbd "SPC l") #'evil-window-right)
  (local-set-key (kbd "SPC k") #'evil-window-up)
  (local-set-key (kbd "SPC j") #'evil-window-down)
  (local-set-key (kbd "j") #'pdf-view-next-line-or-next-page)
  (local-set-key (kbd "k") #'pdf-view-previous-line-or-previous-page)
  (local-set-key (kbd "4") #'pdf-annot-add-highlight-markup-annotation))

or a one-time evaluation:

(with-eval-after-load "pdf-view"
  (my-pdf-view-config))

(defun my-pdf-view-config ()
  "Called after loading pdf-view.el."
  (define-key pdf-view-mode-map (kbd "SPC") nil)
  (define-key pdf-view-mode-map (kbd "SPC h") #'evil-window-left)
  (define-key pdf-view-mode-map (kbd "SPC l") #'evil-window-right)
  (define-key pdf-view-mode-map (kbd "SPC k") #'evil-window-up)
  (define-key pdf-view-mode-map (kbd "SPC j") #'evil-window-down)
  (define-key pdf-view-mode-map (kbd "j") #'pdf-view-next-line-or-next-page)
  (define-key pdf-view-mode-map (kbd "k") #'pdf-view-previous-line-or-previous-page)
  (define-key pdf-view-mode-map (kbd "4") #'pdf-annot-add-highlight-markup-annotation))

You could iterate over a list of (KEY . COMMAND) pairs if you wanted to reduce the amount of text.

(with-eval-after-load "pdf-view"
  (my-pdf-view-config))

(defun my-pdf-view-config ()
  "Called after loading pdf-view.el."
  (dolist (keybind '(("SPC" . nil)
                     ("SPC h" . evil-window-left)
                     ("SPC l" . evil-window-right)
                     ("SPC k" . evil-window-up)
                     ("SPC j" . evil-window-down)
                     ("j" . pdf-view-next-line-or-next-page)
                     ("k" . pdf-view-previous-line-or-previous-page)
                     ("4" . pdf-annot-add-highlight-markup-annotation)))
    (define-key pdf-view-mode-map (kbd (car keybind)) (cdr keybind))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.