Emacs: use add-hook function inside function (defun)

If i do

(add-hook 'haskell-mode-hook
    (lambda ()
        (setq indent-tabs-mode t)
        (setq tab-width 4)
        (message "OK")))

in mine ~/.emacs.d/init.el, then (lambda ...)executed when I enter haskell-mode.

However, if I use a function like this:

(defun my-add-hook (hook tmode twidth)
    (add-hook hook
        (lambda ()
            (setq indent-tabs-mode tmode)
            (setq tab-width twidth)
            (message "OK"))))

and then name it later in the ~/.emacs.d/init.elfollowing way:

(my-add-hook 'haskell-mode-hook t 4)

Then nothing happens (even the "OK" message is not displayed). Is add-hooka special function that cannot be used from defun? I have project parameters defined in a separate initialization file that defines the buffer name and adds (lambda ()...)calls to the corresponding main mode (in the example above, haskell-mode); I want to reduce the frequency of the code using a thin wrapper similar to the my-add-hookabove, but I cannot say why add-hook it is difficult.

EDIT1: .

EDIT2: my-add-hook, " : (void-variable tmode)".

+4
2

:

(defun my-add-hook (hook tmode twidth)
  (add-hook hook
            `(lambda ()
               (setq indent-tabs-mode ,tmode)
               (setq tab-width ,twidth)
               (message "OK"))))
+6

lexical-let , lambda :

(defun my-add-hook (hook tmode twidth)
  (lexical-let ((tmode tmode)
                (twidth twidth))
    (add-hook hook
        (lambda ()
            (setq indent-tabs-mode tmode)
            (setq tab-width twidth)
            (message "OK")))))

, Emacs Lisp, , Emacs Wiki DynamicBindingVsLexicalBinding, compose :

(defun compose (f g)
  (lexical-let ((f f)
                (g g))
    (lambda (x)
      (funcall f (funcall g x)))))
+2

All Articles