summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKim F. Storm <storm@cua.dk>2004-01-22 20:42:52 +0000
committerKim F. Storm <storm@cua.dk>2004-01-22 20:42:52 +0000
commitf076870ac39d43bd070cb3c8666a6837dc7ae4be (patch)
treef1080dec1e4e893ffca6ce136914b76dacf10805
parent455316e213b0cadbba0545a095243f90e242d16a (diff)
downloademacs-f076870ac39d43bd070cb3c8666a6837dc7ae4be.tar.gz
emacs-f076870ac39d43bd070cb3c8666a6837dc7ae4be.tar.bz2
emacs-f076870ac39d43bd070cb3c8666a6837dc7ae4be.zip
(line-at-pos): New defun.
(what-line): Use it. Optimize by only counting lines in narrowed region once.
-rw-r--r--lisp/ChangeLog6
-rw-r--r--lisp/simple.el34
2 files changed, 26 insertions, 14 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 0073e5143ab..9373f8aa984 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,9 @@
+2004-01-22 Kim F. Storm <storm@cua.dk>
+
+ * simple.el (line-at-pos): New defun.
+ (what-line): Use it. Optimize by only counting lines in narrowed
+ region once.
+
2004-01-22 Kenichi Handa <handa@m17n.org>
* language/cyrillic.el (ccl-encode-windows-1251-font): Rearrange
diff --git a/lisp/simple.el b/lisp/simple.el
index d23ed11c6c3..3d2be573012 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -498,20 +498,15 @@ that uses or sets the mark."
(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 @@ and the greater of them is not at the start of a line."
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