1

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
                ns-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?
Drew
  • 75,699
  • 9
  • 109
  • 225
David Moles
  • 111
  • 4
  • 6
    You are missing the `interactive` declaration. Check [here](https://www.gnu.org/software/emacs/manual/html_node/eintr/Interactive.html#Interactive). – Juancho Oct 15 '18 at 19:27
  • 2
    @Juancho Can you make that an answer? Possibly with a link to the [Using interactive](https://www.gnu.org/software/emacs/manual/html_node/elisp/Using-Interactive.html) 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](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_08_01.html#sect_08_01_01)") 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
  • Please don't bother to add another answer. The question is a duplicate - it's been asked many times. – Drew Oct 15 '18 at 22:58
  • @Drew: but the duplicate you suggest has a misleading title that seems to imply that it's specific to anonymous functions. – Stefan Oct 16 '18 at 01:32
  • @stefan: Yes, and each of the other possible duplicates has similar drawbacks. Please feel free to post a Community question that asks directly about that error message and an answer to use `interactive` (explaining `commandp`). That will cover many, perhaps most, of the duplicates. (Others are problems of a command being removed from a library etc.) And you can mention the most common case where users run into this: trying to bind a key to a non-command. But the question and answer are not necessarily about key bindings. If you do that then we can point to that Q&A instead, and close others. – Drew Oct 16 '18 at 01:47
  • 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 03:41
  • Yes, we should have a Community question that has that error msg in the Subject. It's not necessarily about key bindings (though that's the most common way of hitting the gotcha). And it's not necessarily about anonymous functions (lambdas). And sometimes that error msg doesn't mean you lack an `interactive` spec - it could be that you lack the function altogether. But the error msg always means that something expected a command (`commandp`) and didn't get one. – Drew Oct 16 '18 at 04:19
  • 1
    @drew I had a go at a generic commandp question: https://emacs.stackexchange.com/questions/45401/cant-bind-my-function-to-a-key-or-call-it-with-m-x-wrong-type-argument-comma – Tyler Oct 16 '18 at 13:49
  • @Drew which error message? `[No match]`? – David Moles Oct 16 '18 at 20:46
  • 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
  • @Drew You'll note I never got that error message, which is another good argument this isn't a duplicate of that question. (The new Community Wiki question is a good generic version of my question, although why this question couldn't just be marked Community Wiki isn't obvious to me.) – David Moles Oct 16 '18 at 22:51

0 Answers0