# HG changeset patch # User Kim F. Storm # Date 1074804139 0 # Node ID 347987843452406ef8f3c45c2334321a2b34bea3 # Parent 526d0dbfdbf5302b724ee05dcbbed015e004565b (line-at-pos): New defun. (what-line): Use it. Optimize by only counting lines in narrowed region once. diff -r 526d0dbfdbf5 -r 347987843452 lisp/simple.el --- a/lisp/simple.el Thu Jan 22 15:28:25 2004 +0000 +++ b/lisp/simple.el Thu Jan 22 20:42:19 2004 +0000 @@ -498,20 +498,15 @@ (defun what-line () "Print the current buffer line number and narrowed line number of point." (interactive) - (let ((opoint (point)) start) - (save-excursion - (save-restriction - (goto-char (point-min)) - (widen) - (forward-line 0) - (setq start (point)) - (goto-char opoint) - (forward-line 0) - (if (/= start (point-min)) - (message "line %d (narrowed line %d)" - (1+ (count-lines (point-min) (point))) - (1+ (count-lines start (point)))) - (message "Line %d" (1+ (count-lines (point-min) (point))))))))) + (let ((opoint (point)) (start (point-min)) + (n (line-at-pos))) + (if (= start 1) + (message "Line %d" n) + (save-excursion + (save-restriction + (widen) + (message "line %d (narrowed line %d)" + (+ n (line-at-pos start) -1) n)))))) (defun count-lines (start end) "Return number of lines between START and END. @@ -536,6 +531,17 @@ done))) (- (buffer-size) (forward-line (buffer-size))))))) +(defun line-at-pos (&optional pos) + "Return (narrowed) buffer line number at position POS. +If POS is nil, use current buffer location." + (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). Also describe the character after point, and give its character code