From a7754a250b74c17e1f63194e601f20fdb911dd7c Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Sun, 9 Apr 2017 12:46:57 +0200 Subject: Add absolute optional parameter to line-number-at-pos (Bug#26417) * lisp/simple.el (line-number-at-pos): Add a second optional argument 'absolute'. * test/list/simple-tests.el: Add tests for 'line-number-at-pos'. --- lisp/simple.el | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'lisp') diff --git a/lisp/simple.el b/lisp/simple.el index a5565ab6e73..1db14a859d6 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -1270,18 +1270,25 @@ and the greater of them is not at the start of a line." done))) (- (buffer-size) (forward-line (buffer-size))))))) -(defun line-number-at-pos (&optional pos) - "Return (narrowed) buffer line number at position POS. +(defun line-number-at-pos (&optional pos absolute) + "Return buffer line number at position POS. If POS is nil, use current buffer location. -Counting starts at (point-min), so the value refers -to the contents of the accessible portion of the buffer." - (let ((opoint (or pos (point))) start) - (save-excursion - (goto-char (point-min)) - (setq start (point)) - (goto-char opoint) - (forward-line 0) - (1+ (count-lines start (point)))))) + +If ABSOLUTE is nil, the default, counting starts +at (point-min), so the value refers to the contents of the +accessible portion of the (potentially narrowed) buffer. If +ABSOLUTE is non-nil, ignore any narrowing and return the +absolute line number." + (save-restriction + (when absolute + (widen)) + (let ((opoint (or pos (point))) start) + (save-excursion + (goto-char (point-min)) + (setq start (point)) + (goto-char opoint) + (forward-line 0) + (1+ (count-lines start (point))))))) (defun what-cursor-position (&optional detail) "Print info on cursor position (on screen and within buffer). -- cgit v1.2.3