123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- (defun reposition-window (&optional arg)
- "Make the current definition and/or comment visible.
- Further invocations move it to the top of the window or toggle the
- visibility of comments that precede it.
- Point is left unchanged unless prefix ARG is supplied.
- If the definition is fully onscreen, it is moved to the top of the
- window. If it is partly offscreen, the window is scrolled to get the
- definition (or as much as will fit) onscreen, unless point is in a comment
- which is also partly offscreen, in which case the scrolling attempts to get
- as much of the comment onscreen as possible.
- Initially `reposition-window' attempts to make both the definition and
- preceding comments visible. Further invocations toggle the visibility of
- the comment lines.
- If ARG is non-nil, point may move in order to make the whole defun
- visible (if only part could otherwise be made so), to make the defun line
- visible (if point is in code and it could not be made so, or if only
- comments, including the first comment line, are visible), or to make the
- first comment line visible (if point is in a comment)."
- (interactive "P")
- (let* (
- (here (point))
-
-
- (ht (- (window-height (selected-window)) 2))
- (line (repos-count-screen-lines (window-start) (point)))
- (-height
-
- (max 0
- (repos-count-screen-lines-signed
-
- (save-excursion
- (if (not (eobp)) (forward-char 1))
- (end-of-defun -1)
-
- (if (re-search-forward "[^ \t\n\f]" nil t)
- (backward-char 1))
- (point))
- here)))
- (defun-height
- (repos-count-screen-lines-signed
- (save-excursion
- (end-of-defun 1)
- (beginning-of-defun 1)
- (point))
- here))
-
- (defun-depth (repos-count-screen-lines here
- (save-excursion
- (end-of-defun 1)
- (point))))
- (defun-line-onscreen-p
- (and (<= defun-height line)
- (<= (- line defun-height) ht))))
- (cond ((or (= comment-height line)
- (and (= line ht)
- (> comment-height line)
-
- defun-line-onscreen-p))
-
-
-
-
-
-
- (if (and arg (> defun-depth (1+ ht)))
-
- (progn (end-of-defun) (beginning-of-defun) (recenter 0))
- (recenter (max defun-height 0)))
-
- )
- ((or (= defun-height line)
- (= line 0)
- (and (< line comment-height)
- (< defun-height 0)))
-
-
-
-
- (cond ((= line ht)
-
- (if arg (progn (end-of-defun) (beginning-of-defun)))
- (recenter 0)
-
- )
-
- ((and arg (< ht comment-height))
-
-
- (forward-line (- comment-height))
- (beginning-of-line)
-
- (recenter 0)
-
- )
- (t
- (recenter (min ht comment-height))
-
- ))
-
- )
- ((and (> (+ line defun-depth -1) ht)
- defun-line-onscreen-p)
-
-
-
- (recenter (max 0 (1+ (- ht defun-depth)) defun-height))
-
- )
- (t
-
-
-
-
- (if (and arg (< ht comment-height))
-
- (progn (forward-line (- defun-height))
- (beginning-of-line)
- (reposition-window))
- (recenter (min ht comment-height)))
-
- ))))
- (defun repos-count-screen-lines (start end)
- (save-excursion
- (save-restriction
- (narrow-to-region start end)
- (goto-char (point-min))
- (vertical-motion (- (point-max) (point-min))))))
- (defun repos-count-screen-lines-signed (start end)
- (let ((lines (repos-count-screen-lines start end)))
- (if (< start end)
- lines
- (- lines))))
- (provide 'reposition)
|