Function: speedbar-center-buffer-smartly

speedbar-center-buffer-smartly is a byte-compiled function defined in speedbar.el.gz.

Signature

(speedbar-center-buffer-smartly)

Documentation

Recenter a speedbar buffer so the current indentation level is all visible.

This assumes that the cursor is on a file, or tag of a file which the user is interested in.

Source Code

;; Defined in /usr/src/emacs/lisp/speedbar.el.gz
;;; Centering Utility
;;
(defun speedbar-center-buffer-smartly ()
  "Recenter a speedbar buffer so the current indentation level is all visible.
This assumes that the cursor is on a file, or tag of a file which the user is
interested in."
  (save-selected-window
    (select-window (get-buffer-window speedbar-buffer t))
    (set-buffer speedbar-buffer)
    (if (<= (count-lines (point-min) (point-max))
	    (1- (window-height)))
	;; whole buffer fits
	(let ((cp (point)))
	  (goto-char (point-min))
	  (recenter 0)
	  (goto-char cp))
      ;; too big
      (let (depth start end exp p)
	(save-excursion
	  (beginning-of-line)
	  (setq depth (if (looking-at "[0-9]+")
			  (string-to-number (buffer-substring-no-properties
					  (match-beginning 0) (match-end 0)))
			0))
	  (setq exp (format "^%d:" depth)))
	(save-excursion
	  (end-of-line)
	  (if (re-search-backward exp nil t)
	      (setq start (point))
	    (setq start (point-min)))
	  (save-excursion		;Not sure about this part.
	    (end-of-line)
	    (setq p (point))
	    (while (and (not (re-search-forward exp nil t))
			(>= depth 0))
	      (setq depth (1- depth))
	      (setq exp (format "^%d:" depth)))
	    (if (/= (point) p)
		(setq end (point))
	      (setq end (point-max)))))
	;; Now work out the details of centering
	(let ((nl (count-lines start end))
              (wl (1- (window-height)))
	      (cp (point)))
	  (if (> nl wl)
	      ;; We can't fit it all, so just center on cursor
	      (progn (goto-char start)
		     (recenter 1))
	    ;; we can fit everything on the screen, but...
	    (if (and (pos-visible-in-window-p start (selected-window))
		     (pos-visible-in-window-p end (selected-window)))
		;; we are all set!
		nil
	      ;; we need to do something...
	      (goto-char start)
	      (let ((newcent (/ (- (window-height) nl) 2))
		    (lte (count-lines start (point-max))))
		(if (and (< (+ newcent lte) (window-height))
			 (> (- (window-height) lte 1)
			    newcent))
		    (setq newcent (- (window-height)
				     lte 1)))
		(recenter newcent))))
          (goto-char cp))))))