Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "goto-line" in Emacs for interactive use only?

Tags:

emacs

elisp

What problem can happen if the goto-line function is used in a non-interactive elisp program? Its docstring gives a warning saying that:

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))

Moreover, when I try to byte-compile-file my init file including goto-line, I get a unpleasant warning like this once again:

.emacs:170:19:Warning: `goto-line' used from Lisp code
That command is designed for interactive use only

Is using goto-line in a non-interactive program really so dangerous? Relatedly, why is the suggested forward-line solution preferable?

like image 538
dkim Avatar asked Aug 04 '12 03:08

dkim


2 Answers

Firstly, this prevents Elisp programmers from fall into bad habits -- writing inefficient code in a line-number centric way. i.e. instead of using (forward-line 1) calculating the current line number, incrementing, and using goto-line.

From this mailing list article:

In a nutshell, the reason why goto-line should not be a frequently used command is that normally there's no reason to want to get to line number N unless you have a program that told you there's something interesting on that line.

Secondly, goto-line manipulates the user's environment in addition to moving the point (i.e. push-mark). For non-interactive use, this may not be what you want. On the other hand if having considered all this, you believe goto-line is exactly what you need, then just call it like this:

(defun foo ()
  (interactive)
  (with-no-warnings
    (goto-line N)))

And you won't get any compiler warnings.

like image 84
event_jr Avatar answered Oct 19 '22 20:10

event_jr


in addition to what was said:

"goto-line" finally recurs onto "(forward-line (1- line)", which in effect does the work. All other of the 43 lines of "goto-line" command body deal with interactive use. For example considering a possibly universal argument.

When writing a program resp. when running it, your computer is in another state than following an interactive call. Thus you should address this state by using "forward-line" straight on.

like image 6
Andreas Röhler Avatar answered Oct 19 '22 20:10

Andreas Röhler