I want to set a key binding to both open and close nodes in origami-mode.

This is my attempt

(require 'origami)
(defun open-close-node ()

(global-set-key (kbd "C-c C-f") 'open-close-node)

When pressing C-cC-f the "Messages" buffer return this:

open-close-node: Wrong number of arguments: (2 . 2), 0

  • 2
    When creating your own functions, it is a good idea to always read the doc-string for the functions you want to use and find out what the arguments are -- including, but not limited to, which ones are optional and which ones are required. M-x describe-function or C-h f. I usually like to visit the source code and see what makes it tick with M-x find-function and I also grep the source code to see how it is used throughout Emacs. Someone will be along shortly to write-up an answer ... I upvoted because the question is clear and you have taken the first step towards troubleshooting ...
    – lawlist
    Dec 7 '17 at 5:11

There are a few things to consider here. You may need to dig in to the Elisp Manual (C-h i to get to the available manuals...) because there are some key concepts about functions, arguments, and commands that you may not be familiar with.

First of all, as @lawlist noted in the comments you want to to use the help system and/or source code to understand what arguments are required for the functions you are calling. C-h f origami-open-node will give you the function's doc string, which shows that it expects BUFFER and POINT to be passed as arguments.

Looking at the source (follow the link from the Help buffer) and you'll see that origami-open-node has an (interactive ...) form. This specifies how the arguments are obtained when the command is called interactively (when you call it with a key binding or via M-x). But when calling the same function from lisp you need to pass in the arguments yourself, or indicate that you want the interactive behavior by using e.g. (call-interactively 'origami-open-node)

The next question is how to get the command to do two different things. As you've defined it so far (ignoring the missing arguments), your command would open and then close the node -- I suspect that is not what you want. It sounds like to want to toggle the node open or closed depending on its current state. That would require something like an (if ...) form and a way to check whether the current node is open or closed, which might require more digging in to how origami mode works.

All that said, I think what you want is already provided by origami: have you tried the command origami-toggle-node? Looks like origami does not bind any keys by default but provides a mode map, so you can use:

(define-key origami-mode-map (kbd "C-c C-f") 'origami-toggle-node)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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