Function: goto-line
goto-line is an interactive and byte-compiled function defined in
simple.el.gz.
Signature
(goto-line LINE &optional BUFFER RELATIVE)
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 optional argument BUFFER is non-nil, switch to that buffer and
move to line LINE there. If called interactively with C-u (universal-argument)
as argument, BUFFER is the most recently selected other buffer.
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.
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)
"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 optional argument BUFFER is non-nil, switch to that buffer and
move to line LINE there. If called interactively with \\[universal-argument]
as argument, BUFFER is the most recently selected other buffer.
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.
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."
(declare (interactive-only forward-line))
(interactive (goto-line-read-args))
;; Switch to the desired buffer, one way or another.
(if 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)))