0

I've written my first custom elisp function for dealing with the output from grep.

(defun open-at-grep ()
  (interactive)
  (let ((thisline (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
    (find-file (nth 0 (split-string thisline ":")))
    (goto-line (string-to-number (nth 1 (split-string thisline ":"))))
    ))

This works, and I can hotkey it with a global key-binding. What I would like to do is to create a minor mode to autoload the keybinding (eventually just getting hooked for files with a custom extension). What I've tried is

(define-minor-mode grepout-mode
  "tool for making use of output of grep -n"
  nil
  "grepout-mode"
  (("C->" .  open-at-grep)))

which throws an error when emacs launches

Invalid function: (C-> . open-at-grep)

The stack-trace from launching emacs --debug-init is not particularly more helpful. I've tried (([C->] . 'open-at-grep)), and other permutations of quotes and not quotes. What am I missing?

3

1 Answer 1

2

Check the documentation for define-minor-mode. It is fairly long, so I will only quote the most relevant section:

:keymap MAP Keymap bound to the mode keymap.  Defaults to MODE-map.
        If non-nil, it should be an unquoted variable name (whose value
        is a keymap), or an expression that returns either a keymap or
        a list of (KEY . BINDING) pairs where KEY and BINDING are
        suitable for define-key.  If you supply a KEYMAP argument
        that is not a symbol, this macro defines the variable MODE-map
        and gives it the value that KEYMAP specifies.

Note well what it says. It expects either a variable name, or an expression that evaluates to a keymap, or a list of pairs. You are trying to use a list of pairs, but lists need to be quoted or else they will be evaluated as code. The quote goes in front of the form to be quoted:

(define-minor-mode grepout-mode
  "tool for making use of output of grep -n"
  nil
  "grepout-mode"
  '(("C->" .  open-at-grep)))

Of course, none of this is specific to defining a minor mode. It is a basic feature of the Emacs Lisp language. Perhaps you should review An Introduction to Programming in Emacs Lisp. To open it inside of Emacs, type C-h i to open the Info viewer. This should start you at the directory of available manuals, otherwise hit d to jump to it. m will let you type in the name of a menu item, or you can search with C-s, etc.

The Emacs Lisp Intro may not go into enough depth to answer all of your questions about quoting and evaluation, so I also recommend reading chapter 10.3 Quoting of the Emacs Lisp manual.

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.