I have the following function, which does what it says on the tin -- swaps the values of the ns-alternate-modifier and ns-right-alternate-modifier variables.

(defun swap-left-and-right-alt ()
  "Swap left and right alt keys"
  (setq ns-alternate-modifier
        (prog1 ns-right-alternate-modifier
          (setq ns-right-alternate-modifier

When I put this in my init.el, I don't get any errors, but I also don't get a M-x autocomplete for swap-left-and-right-alt, and if I type it out I get [No match].

I've read the manual section on defun and the section following it on installing function definitions, and it seems reasonable to think my problem is that the function isn't installed. However, when I try to install it by hand (following the manual) by opening the init.el, placing the cursor at the end of the function definition, and hitting C-x C-e, though this does do something (I see swap-left-and-right-alt in the minibuffer), it still doesn't do what I want, i.e. M-x swap-left-and-right-alt still gets me [No match].

(There is one difference: after the C-x C-e experiment, if I try to evaluate swap-left-and-right-alt, I get Symbol's value as variable is void, whereas just having it in the init file I get the debugger, with Lisp error: (void-variable swap-left-and-right-alt). Also, evaluating (swap-left-and-right-alt), with parentheses, does appear to run the function, i.e., the keys get swapped.)

Clearly there's a conceptual piece that I'm missing, but I haven't been able to hit on the right combination of Emacs-speak search terms to find the answer.

What do I need to put in my init file to

  1. install the function, and
  2. be able to autocomplete and run the function as a command?
  • 6
    You are missing the interactive declaration. Check here. – Juancho Oct 15 '18 at 19:27
  • 2
    @Juancho Can you make that an answer? Possibly with a link to the Using interactive manual section? Coming from a different environment, the Emacs meaning of "interactive" is not at all intuitive (e.g., in shell programming, "interactive" means something like "requires input or provides output") and it might be helpful to have an answer here explaining what it means in an Emacs context. – David Moles Oct 15 '18 at 20:12
  • 1
    @Drew even if the answer turns out to be the same, I don’t think it’s the same question. I did see that question before I posted this one, and it was not at all obvious that it was related (my question having nothing to do with key bindings or lambda expressions), which is why I didn’t read as far as the answer that somewhat explains “interactive”. Possibly to an experienced elisp programmer they’re the same question, but an experienced elisp programmer wouldn’t need to ask. – David Moles Oct 16 '18 at 3:41
  • 1
    @drew I had a go at a generic commandp question: emacs.stackexchange.com/questions/45401/… – Tyler Oct 16 '18 at 13:49
  • 1
    Wrong type argument: commandp, XXXXX, where XXXXX is a lambda form or the name of a function. – Drew Oct 16 '18 at 22:39

Browse other questions tagged or ask your own question.