diff options
| author | Juri Linkov | 2020-09-23 22:39:32 +0300 |
|---|---|---|
| committer | Juri Linkov | 2020-09-23 22:39:32 +0300 |
| commit | dc86c4cc0ba230c632b10cefb22b9fec1cd1d046 (patch) | |
| tree | 92a8db4b07c29afbd77848760685008a3f82ff1e | |
| parent | ad285e0eb8ae18f41a4d94b1a57a5ee118f6814f (diff) | |
| download | emacs-dc86c4cc0ba230c632b10cefb22b9fec1cd1d046.tar.gz emacs-dc86c4cc0ba230c632b10cefb22b9fec1cd1d046.zip | |
New command goto-line-relative (bug#5042, bug#9917)
* lisp/simple.el (goto-line-read-args): New function with code from goto-line.
(goto-line): New arg RELATIVE. Also use 'widen-automatically' to
leave all lines accessible in the narrowed buffer.
(goto-line-relative): New command.
* lisp/info.el (Info-mode-map): Remap 'goto-line' to 'goto-line-relative'.
* doc/emacs/basic.texi (Moving Point):
* doc/emacs/display.texi (Optional Mode Line): Mention goto-line-relative.
| -rw-r--r-- | doc/emacs/basic.texi | 5 | ||||
| -rw-r--r-- | doc/emacs/display.texi | 7 | ||||
| -rw-r--r-- | etc/NEWS | 7 | ||||
| -rw-r--r-- | lisp/info.el | 1 | ||||
| -rw-r--r-- | lisp/simple.el | 86 |
5 files changed, 73 insertions, 33 deletions
diff --git a/doc/emacs/basic.texi b/doc/emacs/basic.texi index 444b2469cf1..cde7b475d95 100644 --- a/doc/emacs/basic.texi +++ b/doc/emacs/basic.texi | |||
| @@ -311,13 +311,16 @@ Position 1 is the beginning of the buffer. | |||
| 311 | @kindex M-g M-g | 311 | @kindex M-g M-g |
| 312 | @kindex M-g g | 312 | @kindex M-g g |
| 313 | @findex goto-line | 313 | @findex goto-line |
| 314 | @findex goto-line-relative | ||
| 314 | Read a number @var{n} and move point to the beginning of line number | 315 | Read a number @var{n} and move point to the beginning of line number |
| 315 | @var{n} (@code{goto-line}). Line 1 is the beginning of the buffer. If | 316 | @var{n} (@code{goto-line}). Line 1 is the beginning of the buffer. If |
| 316 | point is on or just after a number in the buffer, that is the default | 317 | point is on or just after a number in the buffer, that is the default |
| 317 | for @var{n}. Just type @key{RET} in the minibuffer to use it. You can | 318 | for @var{n}. Just type @key{RET} in the minibuffer to use it. You can |
| 318 | also specify @var{n} by giving @kbd{M-g M-g} a numeric prefix argument. | 319 | also specify @var{n} by giving @kbd{M-g M-g} a numeric prefix argument. |
| 319 | @xref{Select Buffer}, for the behavior of @kbd{M-g M-g} when you give it | 320 | @xref{Select Buffer}, for the behavior of @kbd{M-g M-g} when you give it |
| 320 | a plain prefix argument. | 321 | a plain prefix argument. Alternatively, you can use the command |
| 322 | @code{goto-line-relative} to move point to the line relative to the | ||
| 323 | accessible portion of the narrowed buffer. | ||
| 321 | 324 | ||
| 322 | @item M-g @key{TAB} | 325 | @item M-g @key{TAB} |
| 323 | @kindex M-g TAB | 326 | @kindex M-g TAB |
diff --git a/doc/emacs/display.texi b/doc/emacs/display.texi index e7b8745a044..6f1bc802b85 100644 --- a/doc/emacs/display.texi +++ b/doc/emacs/display.texi | |||
| @@ -1452,9 +1452,10 @@ the displayed column number to count from one, you may set | |||
| 1452 | @cindex narrowing, and line number display | 1452 | @cindex narrowing, and line number display |
| 1453 | If you have narrowed the buffer (@pxref{Narrowing}), the displayed | 1453 | If you have narrowed the buffer (@pxref{Narrowing}), the displayed |
| 1454 | line number is relative to the accessible portion of the buffer. | 1454 | line number is relative to the accessible portion of the buffer. |
| 1455 | Thus, it isn't suitable as an argument to @code{goto-line}. (Use | 1455 | Thus, it isn't suitable as an argument to @code{goto-line}. (The |
| 1456 | @code{what-line} command to see the line number relative to the whole | 1456 | command @code{what-line} shows the line number relative to the whole |
| 1457 | file.) | 1457 | file.) You can use @code{goto-line-relative} command to move point to |
| 1458 | the line relative to the accessible portion of the narrowed buffer. | ||
| 1458 | 1459 | ||
| 1459 | @vindex line-number-display-limit | 1460 | @vindex line-number-display-limit |
| 1460 | If the buffer is very large (larger than the value of | 1461 | If the buffer is very large (larger than the value of |
| @@ -172,6 +172,13 @@ Each buffer will keep a separate history of line numbers used with | |||
| 172 | line numbers that were previously jumped to. | 172 | line numbers that were previously jumped to. |
| 173 | 173 | ||
| 174 | +++ | 174 | +++ |
| 175 | ** New command 'goto-line-relative' to use in a narrowed buffer. | ||
| 176 | It moves point to the line relative to the accessible portion of the | ||
| 177 | narrowed buffer. 'M-g M-g' in Info is rebound to this command. | ||
| 178 | When 'widen-automatically' is non-nil, 'goto-line' widens the narrowed | ||
| 179 | buffer to be able to move point to the inaccessible portion. | ||
| 180 | |||
| 181 | +++ | ||
| 175 | ** When 'suggest-key-bindings' is non-nil, the completion list of 'M-x' | 182 | ** When 'suggest-key-bindings' is non-nil, the completion list of 'M-x' |
| 176 | shows equivalent key bindings for all commands that have them. | 183 | shows equivalent key bindings for all commands that have them. |
| 177 | 184 | ||
diff --git a/lisp/info.el b/lisp/info.el index e4f75b481fe..20633fd0598 100644 --- a/lisp/info.el +++ b/lisp/info.el | |||
| @@ -4053,6 +4053,7 @@ If FORK is non-nil, it is passed to `Info-goto-node'." | |||
| 4053 | (define-key map "^" 'Info-up) | 4053 | (define-key map "^" 'Info-up) |
| 4054 | (define-key map "," 'Info-index-next) | 4054 | (define-key map "," 'Info-index-next) |
| 4055 | (define-key map "\177" 'Info-scroll-down) | 4055 | (define-key map "\177" 'Info-scroll-down) |
| 4056 | (define-key map [remap goto-line] 'goto-line-relative) | ||
| 4056 | (define-key map [mouse-2] 'Info-mouse-follow-nearest-node) | 4057 | (define-key map [mouse-2] 'Info-mouse-follow-nearest-node) |
| 4057 | (define-key map [follow-link] 'mouse-face) | 4058 | (define-key map [follow-link] 'mouse-face) |
| 4058 | (define-key map [XF86Back] 'Info-history-back) | 4059 | (define-key map [XF86Back] 'Info-history-back) |
diff --git a/lisp/simple.el b/lisp/simple.el index d7486e51ddc..825fec380cb 100644 --- a/lisp/simple.el +++ b/lisp/simple.el | |||
| @@ -1231,7 +1231,39 @@ that uses or sets the mark." | |||
| 1231 | "History of values entered with `goto-line'.") | 1231 | "History of values entered with `goto-line'.") |
| 1232 | (make-variable-buffer-local 'goto-line-history) | 1232 | (make-variable-buffer-local 'goto-line-history) |
| 1233 | 1233 | ||
| 1234 | (defun goto-line (line &optional buffer) | 1234 | (defun goto-line-read-args (&optional relative) |
| 1235 | "Read arguments for `goto-line' related commands." | ||
| 1236 | (if (and current-prefix-arg (not (consp current-prefix-arg))) | ||
| 1237 | (list (prefix-numeric-value current-prefix-arg)) | ||
| 1238 | ;; Look for a default, a number in the buffer at point. | ||
| 1239 | (let* ((default | ||
| 1240 | (save-excursion | ||
| 1241 | (skip-chars-backward "0-9") | ||
| 1242 | (if (looking-at "[0-9]") | ||
| 1243 | (string-to-number | ||
| 1244 | (buffer-substring-no-properties | ||
| 1245 | (point) | ||
| 1246 | (progn (skip-chars-forward "0-9") | ||
| 1247 | (point))))))) | ||
| 1248 | ;; Decide if we're switching buffers. | ||
| 1249 | (buffer | ||
| 1250 | (if (consp current-prefix-arg) | ||
| 1251 | (other-buffer (current-buffer) t))) | ||
| 1252 | (buffer-prompt | ||
| 1253 | (if buffer | ||
| 1254 | (concat " in " (buffer-name buffer)) | ||
| 1255 | ""))) | ||
| 1256 | ;; Read the argument, offering that number (if any) as default. | ||
| 1257 | (list (read-number (format "Goto%s line%s: " | ||
| 1258 | (if (= (point-min) 1) "" | ||
| 1259 | ;; In a narrowed buffer. | ||
| 1260 | (if relative " relative" " absolute")) | ||
| 1261 | buffer-prompt) | ||
| 1262 | (list default (line-number-at-pos)) | ||
| 1263 | 'goto-line-history) | ||
| 1264 | buffer)))) | ||
| 1265 | |||
| 1266 | (defun goto-line (line &optional buffer relative) | ||
| 1235 | "Go to LINE, counting from line 1 at beginning of buffer. | 1267 | "Go to LINE, counting from line 1 at beginning of buffer. |
| 1236 | If called interactively, a numeric prefix argument specifies | 1268 | If called interactively, a numeric prefix argument specifies |
| 1237 | LINE; without a numeric prefix argument, read LINE from the | 1269 | LINE; without a numeric prefix argument, read LINE from the |
| @@ -1241,6 +1273,13 @@ If optional argument BUFFER is non-nil, switch to that buffer and | |||
| 1241 | move to line LINE there. If called interactively with \\[universal-argument] | 1273 | move to line LINE there. If called interactively with \\[universal-argument] |
| 1242 | as argument, BUFFER is the most recently selected other buffer. | 1274 | as argument, BUFFER is the most recently selected other buffer. |
| 1243 | 1275 | ||
| 1276 | If optional argument RELATIVE is non-nil, counting starts at the beginning | ||
| 1277 | of the accessible portion of the (potentially narrowed) buffer. | ||
| 1278 | |||
| 1279 | If the variable `widen-automatically' is non-nil, cancel narrowing and | ||
| 1280 | leave all lines accessible. If `widen-automatically' is nil, just move | ||
| 1281 | point to the edge of visible portion and don't change the buffer bounds. | ||
| 1282 | |||
| 1244 | Prior to moving point, this function sets the mark (without | 1283 | Prior to moving point, this function sets the mark (without |
| 1245 | activating it), unless Transient Mark mode is enabled and the | 1284 | activating it), unless Transient Mark mode is enabled and the |
| 1246 | mark is already active. | 1285 | mark is already active. |
| @@ -1252,32 +1291,7 @@ What you probably want instead is something like: | |||
| 1252 | If at all possible, an even better solution is to use char counts | 1291 | If at all possible, an even better solution is to use char counts |
| 1253 | rather than line counts." | 1292 | rather than line counts." |
| 1254 | (declare (interactive-only forward-line)) | 1293 | (declare (interactive-only forward-line)) |
| 1255 | (interactive | 1294 | (interactive (goto-line-read-args)) |
| 1256 | (if (and current-prefix-arg (not (consp current-prefix-arg))) | ||
| 1257 | (list (prefix-numeric-value current-prefix-arg)) | ||
| 1258 | ;; Look for a default, a number in the buffer at point. | ||
| 1259 | (let* ((default | ||
| 1260 | (save-excursion | ||
| 1261 | (skip-chars-backward "0-9") | ||
| 1262 | (if (looking-at "[0-9]") | ||
| 1263 | (string-to-number | ||
| 1264 | (buffer-substring-no-properties | ||
| 1265 | (point) | ||
| 1266 | (progn (skip-chars-forward "0-9") | ||
| 1267 | (point))))))) | ||
| 1268 | ;; Decide if we're switching buffers. | ||
| 1269 | (buffer | ||
| 1270 | (if (consp current-prefix-arg) | ||
| 1271 | (other-buffer (current-buffer) t))) | ||
| 1272 | (buffer-prompt | ||
| 1273 | (if buffer | ||
| 1274 | (concat " in " (buffer-name buffer)) | ||
| 1275 | ""))) | ||
| 1276 | ;; Read the argument, offering that number (if any) as default. | ||
| 1277 | (list (read-number (format "Goto line%s: " buffer-prompt) | ||
| 1278 | (list default (line-number-at-pos)) | ||
| 1279 | 'goto-line-history) | ||
| 1280 | buffer)))) | ||
| 1281 | ;; Switch to the desired buffer, one way or another. | 1295 | ;; Switch to the desired buffer, one way or another. |
| 1282 | (if buffer | 1296 | (if buffer |
| 1283 | (let ((window (get-buffer-window buffer))) | 1297 | (let ((window (get-buffer-window buffer))) |
| @@ -1286,13 +1300,27 @@ rather than line counts." | |||
| 1286 | ;; Leave mark at previous position | 1300 | ;; Leave mark at previous position |
| 1287 | (or (region-active-p) (push-mark)) | 1301 | (or (region-active-p) (push-mark)) |
| 1288 | ;; Move to the specified line number in that buffer. | 1302 | ;; Move to the specified line number in that buffer. |
| 1289 | (save-restriction | 1303 | (if (and (not relative) (not widen-automatically)) |
| 1290 | (widen) | 1304 | (save-restriction |
| 1305 | (widen) | ||
| 1306 | (goto-char (point-min)) | ||
| 1307 | (if (eq selective-display t) | ||
| 1308 | (re-search-forward "[\n\C-m]" nil 'end (1- line)) | ||
| 1309 | (forward-line (1- line)))) | ||
| 1310 | (unless relative (widen)) | ||
| 1291 | (goto-char (point-min)) | 1311 | (goto-char (point-min)) |
| 1292 | (if (eq selective-display t) | 1312 | (if (eq selective-display t) |
| 1293 | (re-search-forward "[\n\C-m]" nil 'end (1- line)) | 1313 | (re-search-forward "[\n\C-m]" nil 'end (1- line)) |
| 1294 | (forward-line (1- line))))) | 1314 | (forward-line (1- line))))) |
| 1295 | 1315 | ||
| 1316 | (defun goto-line-relative (line &optional buffer) | ||
| 1317 | "Go to LINE, counting from line at (point-min). | ||
| 1318 | The line number is relative to the accessible portion of the narrowed | ||
| 1319 | buffer. The argument BUFFER is the same as in the function `goto-line'." | ||
| 1320 | (declare (interactive-only forward-line)) | ||
| 1321 | (interactive (goto-line-read-args t)) | ||
| 1322 | (goto-line line buffer t)) | ||
| 1323 | |||
| 1296 | (defun count-words-region (start end &optional arg) | 1324 | (defun count-words-region (start end &optional arg) |
| 1297 | "Count the number of words in the region. | 1325 | "Count the number of words in the region. |
| 1298 | If called interactively, print a message reporting the number of | 1326 | If called interactively, print a message reporting the number of |