[net.emacs] whichline for Emacs-264

dmj@uw-june (David Jacobson) (03/11/86)

Ever want to know what line you were on in Gosling's (Unipress) Emacs?
In Gosling's Emacs it is easy to know where you are in characters or
percent, but not in terms of lines.  The obvious mlisp program of
just searching linearly is too slow.  Here is a program that does
it quite fast.  It uses incremental binary search.  
=========================
(defun 
    (whichline  
	(save-excursion pdot a b c adot bdot cdot
	    (beginning-of-line)
	    (setq pdot (dot))
	    (beginning-of-file)
	    (setq a 0)
	    (setq adot (dot))
	    (provide-prefix-argument 32 (next-line))
	    (setq c 32)
	    (setq cdot (dot))
	    (while (> pdot (dot))
		   (setq adot cdot)
		   (setq a c)
		   (provide-prefix-argument c (next-line))
		   (beginning-of-line)
		   (setq cdot (dot))
		   (setq c (* c 2))
	    )
	    (goto-character adot)
	    (while (< a c)	; assertion: dot is on line a; 
		   (setq b (/ (+ a c) 2)); c=a+1 ==> b=a
		   (provide-prefix-argument (- b a) (next-line))
		   (beginning-of-line); now at b
		   (setq bdot (dot))
		   (if 
		       (<= pdot bdot);  in lower half
		       (progn 
			      (setq cdot bdot)
			      (setq c b)
			      (goto-character adot)
		       )
		       (progn 	; in upper half ; bdot is really on line b
			   	; dot at bdot
				; at least one more line exists since pdot >
				; bdot = (dot)
			   (next-line)
			   (setq adot (dot)); dot now at adot
			   (setq a (+ b 1))
		       )
		   )
	    )
	    (if (interactive) (message "line " (+ a 1)))
	    (+ a 1)
	)
    )
)