2

In the description of the function goto-line in the built-in emacs documentation, I found the following at the end:

This function is usually the wrong thing to use in a Lisp program.
What you probably want instead is something like:
  (goto-char (point-min))
  (forward-line (1- N))
If at all possible, an even better solution is to use char counts
rather than line counts.

Similarly, the switch-to-buffer function's documentation says:

WARNING: This is NOT the way to work on another buffer temporarily
within a Lisp program!  Use `set-buffer' instead.  That avoids
messing with the window-buffer correspondences.

Is it possible to know the non-interactive versions of frequent interactive functions like save-buffer, kill-line etc. or that these are efficient and do not have a non-interactive equivalent? I did not find any relevant information about the two specific commands save-buffer and kill-line in the built-in documentation, so I would also like to know if that implies that these are okay for non-interactive use.

  • 3
    If it does not explicitly say that it is not okay then it should be okay – Jules Jun 13 '16 at 21:58
  • 1
    C-h ffor the command will give you a link to its source code. If there is a more-or-less corresponding non-interactive function then the command will typically invoke that. So the answer is to look at the source code to see what it does and whether there is a non-interactive function that does the bulk of the job. But the larger point is made by @JulesTamagnan in his comment and phils in his answer: assume that you can use a command in your code. – Drew Jun 13 '16 at 22:42
  • When I said built-in documentation, I was precisely referring to the output shown by invoking C-h f. But, I don't see a link to the source code there. – Sagar Jha Jun 13 '16 at 23:06
  • The link to the source code is in the first line of the documentation; for example for C-h f kill-line I get "kill-line is an interactive compiled Lisp function in `simple.el'", and "simple.el" is a link to the actual file on disk. Clicking on it or tabbing to it and hitting enter will activate the link and load the source file. – db48x Jun 13 '16 at 23:51
  • 1
    Sagar Jha: If you installed Emacs via a package manager for your OS, it might have excluded the source files. On Debian-based systems there's a package emacs24-el for example. That covers the elisp sources. For linking to C-code sources (if you want that) you would need to download them and tell Emacs where to find them (unless you compile Emacs yourself, in which case it already knows where to find them). – phils Jun 14 '16 at 0:35
2

Most commands are absolutely fine for both interactive and non-interactive use, so a default position of "if it doesn't say otherwise, it's ok for non-interactive use" is going to be fine in the majority of cases (and if you find undocumented cases where this is not so, you could submit a documentation bug report).

Documentation aside, I don't believe there are any explicit pointers to other functions built into the code. You can always examine the source code for the command to see whether it looks like a wrapper around a simpler piece of code which you could be using instead, however.

  • Absolutely. Most (nearly all) of the time you'll just want to use the function that seems most obvious. If you're starting out writing functions in Emacs Lisp, try recording a Macro and then peeking inside it. I believe that these comments about using other functions are just tips/advice: it's not wrong to use them, even if the comment says it is, there's just probably a more efficient way. – mike Jun 14 '16 at 3:51
  • 2
    Avoiding unwanted side-effects is a common reason. e.g. a command which automatically pushes to the mark ring as an interactive convenience before moving point would generally be inappropriate to call in non-interactive save-excursion-wrapped code. – phils Jun 14 '16 at 5:05

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.