From 00a369aca266f18f561df5848c64b26538519e97 Mon Sep 17 00:00:00 2001 From: "Richard M. Stallman" Date: Sat, 5 Mar 2005 18:02:40 +0000 Subject: (goto-line): Use a number at point as the default. With C-u as arg, switch buffers. --- lisp/simple.el | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) (limited to 'lisp/simple.el') diff --git a/lisp/simple.el b/lisp/simple.el index 3d3178888b..67d3bc6b8e 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -733,16 +733,54 @@ that uses or sets the mark." ;; Counting lines, one way or another. -(defun goto-line (arg) - "Goto line ARG, counting from line 1 at beginning of buffer." - (interactive "NGoto line: ") - (setq arg (prefix-numeric-value arg)) +(defun goto-line (arg &optional buffer) + "Goto line ARG, counting from line 1 at beginning of buffer. +Normally, move point in the curren buffer. +With just C-u as argument, move point in the most recently displayed +other buffer, and switch to it. + +If there's a number in the buffer at point, it is the default for ARG." + (interactive + (if (and current-prefix-arg (not (consp current-prefix-arg))) + (list (prefix-numeric-value current-prefix-arg)) + ;; Look for a default, a number in the buffer at point. + (let* ((default + (save-excursion + (skip-chars-backward "0-9") + (if (looking-at "[0-9]") + (buffer-substring-no-properties + (point) + (progn (skip-chars-forward "0-9") + (point)))))) + ;; Decide if we're switching buffers. + (buffer + (if (consp current-prefix-arg) + (other-buffer (current-buffer) t))) + (buffer-prompt + (if buffer + (concat " in " (buffer-name buffer)) + ""))) + ;; Read the argument, offering that number (if any) as default. + (list (read-from-minibuffer (format (if default "Goto line%s (%s): " + "Goto line%s: ") + buffer-prompt + default) + nil nil t + 'minibuffer-history + default) + buffer)))) + ;; 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)))) + ;; Move to the specified line number in that buffer. (save-restriction (widen) (goto-char 1) (if (eq selective-display t) (re-search-forward "[\n\C-m]" nil 'end (1- arg)) - (forward-line (1- arg))))) + (forward-line (1- arg)))))) (defun count-lines-region (start end) "Print number of lines and characters in the region." -- cgit v1.2.3