[comp.emacs] Recenter-definition

peck@eng.sun.com (Jeff Peck) (01/19/91)

Here's the one we use. post to sources if you want.

;; thanks to Cris Perdue:
(defun recenter-defun (arg)
  "Attempts to position the top of the \"defun\" at the top of the 
window. Leaves \"point\" unchanged, unless supplied with prefix ARG.
   Any comment lines just above the defun line are shown also.
If you are in a (;;;) comment beginning in column 0, just moves the top 
of the comment to the top of the window, thus positioning one nicely 
when inside a major comment."
  (interactive "P")
  (let (beg)
    (save-excursion
      ;; find top of defun or comment
      (setq beg (point))
      (or (looking-at "^\\s(\\|^;;;\\|^\014$\\|\\`")
	  (re-search-backward "^\\s(\\|^;;;\\|^\014$\\|\\`" (point-min) t))
      (skip-comment-lines)
      (if (>= (count-lines (point) beg) (window-height))
	  (or arg (error "Can't reposition!")))
      (line-to-top-of-window)))
  )

(defun skip-comment-lines (&optional arg)
  "If the current line is immediately preceded by any lines of
comments, this moves to the first line of the block of comment
lines.  We define a comment line as a line beginning with \";\"."
  (if arg 
      (progn (re-search-forward "^\\($\\|[^;]\\)\\|\\'" (point-max) t)
	     (if (eobp) (newline) (beginning-of-line)))
    (progn (re-search-backward "^\\($\\|[^;]\\)\\|\\`" (point-min) t)
	   (if (bobp) nil (forward-line 1)))))