Function: goto-line
goto-line is an interactive and byte-compiled function defined in
simple.el.gz.
Signature
(goto-line LINE &optional BUFFER RELATIVE INTERACTIVE)
Documentation
Go to LINE, counting from line 1 at beginning of buffer.
If called interactively, a numeric prefix argument specifies LINE; without a numeric prefix argument, read LINE from the minibuffer.
If called interactively with C-u (universal-argument), switch to the
most recently selected other buffer and move to line LINE there.
If optional argument RELATIVE is non-nil, counting starts at the beginning of the accessible portion of the (potentially narrowed) buffer.
If the variable widen-automatically is non-nil, cancel narrowing and
leave all lines accessible. If widen-automatically is nil, just move
point to the edge of visible portion and don't change the buffer bounds.
Prior to moving point, this function sets the mark (without activating it), unless Transient Mark mode is enabled and the mark is already active.
A non-nil INTERACTIVE argument pushes the mark and switches the buffer if optional argument BUFFER is non-nil.
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.
Probably introduced at or before Emacs version 18.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun goto-line (line &optional buffer relative interactive)
"Go to LINE, counting from line 1 at beginning of buffer.
If called interactively, a numeric prefix argument specifies
LINE; without a numeric prefix argument, read LINE from the
minibuffer.
If called interactively with \\[universal-argument], switch to the
most recently selected other buffer and move to line LINE there.
If optional argument RELATIVE is non-nil, counting starts at the beginning
of the accessible portion of the (potentially narrowed) buffer.
If the variable `widen-automatically' is non-nil, cancel narrowing and
leave all lines accessible. If `widen-automatically' is nil, just move
point to the edge of visible portion and don't change the buffer bounds.
Prior to moving point, this function sets the mark (without
activating it), unless Transient Mark mode is enabled and the
mark is already active.
A non-nil INTERACTIVE argument pushes the mark and switches the buffer
if optional argument BUFFER is non-nil.
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."
(interactive (append (goto-line-read-args) '(nil t)))
;; Switch to the desired buffer, one way or another.
(when interactive
(when buffer
(let ((window (get-buffer-window buffer)))
(if window (select-window window)
(switch-to-buffer-other-window buffer))))
;; Leave mark at previous position
(or (region-active-p) (push-mark)))
;; Move to the specified line number in that buffer.
(let ((pos (save-restriction
(unless relative (widen))
(goto-char (point-min))
(if (eq selective-display t)
(re-search-forward "[\n\C-m]" nil 'end (1- line))
(forward-line (1- line)))
(point))))
(when (and (not relative)
(buffer-narrowed-p)
widen-automatically
;; Position is outside narrowed part of buffer
(or (> (point-min) pos) (> pos (point-max))))
(widen))
(goto-char pos)))