local keymap for emacs outline-minor-mode

2019-09-19 06:05发布

问题:

I want to set the outline-minor-mode for init.el file and when TAB key is pressed on the lines starting with ; the function outline-toggle-children should be called in order to fold and expand the sub headings.

Below is the code for hook. But it does not work for the "TAB" key binding as expected.

(add-hook 'emacs-lisp-mode-hook
      (lambda ()           
        (if (equal (buffer-name) "init.el")
        (progn
          (outline-regexp "^;+")
          (outline-minor-mode 1)
          (local-set-key (kbd "TAB") ; this does not work
                 (lambda ()
                   (if (string-match outline-regexp (thing-at-point 'line))
                       (outline-toggle-children))))))))

回答1:

I presume that the error you get is wrong-type-argument commandp. This happens because functions bound to keys must be "interactive" functions. You need to add an (interactive) declaration to the function, so that Emacs knows how to invoke the function in response to an event:

 (lambda ()
   (interactive)
   (if (string-match outline-regexp (thing-at-point 'line))
       (outline-toggle-children)))


标签: emacs elisp