home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / prim / reposition.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  7.2 KB  |  192 lines

  1. ;;; reposition.el --- center a Lisp function or comment on the screen
  2.  
  3. ;; Copyright (C) 1991 Free Software Foundation, Inc.
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 1, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  19. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. ;;; Written by Michael D. Ernst, mernst@theory.lcs.mit.edu, Jan 1991.
  22.  
  23. ;;; Reposition-window makes an entire function definition or comment visible,
  24. ;;; or, if it is already visible, places it at the top of the window;
  25. ;;; additional invocations toggle the visibility of comments preceding the
  26. ;;; code.  For the gory details, see the documentation for reposition-window;
  27. ;;; rather than reading that, you may just want to play with it.
  28.  
  29. ;;; This tries pretty hard to do the recentering correctly; the precise
  30. ;;; action depends on what the buffer looks like.  If you find a situation
  31. ;;; where it doesn't behave well, let me know.  This function is modeled
  32. ;;; after one of the same name in ZMACS, but the code is all-new and the
  33. ;;; behavior in some situations differs.
  34.  
  35. ;;;###autoload
  36. (defun reposition-window (&optional arg)
  37.   "Make the current definition and/or comment visible.
  38. Further invocations move it to the top of the window or toggle the
  39. visibility of comments that precede it.
  40.   Point is left unchanged unless prefix ARG is supplied.
  41.   If the definition is fully onscreen, it is moved to the top of the
  42. window.  If it is partly offscreen, the window is scrolled to get the
  43. definition (or as much as will fit) onscreen, unless point is in a comment
  44. which is also partly offscreen, in which case the scrolling attempts to get
  45. as much of the comment onscreen as possible.
  46.   Initially `reposition-window' attempts to make both the definition and
  47. preceding comments visible.  Further invocations toggle the visibility of
  48. the comment lines.
  49.   If ARG is non-nil, point may move in order to make the whole defun
  50. visible (if only part could otherwise be made so), to make the defun line
  51. visible (if point is in code and it could not be made so, or if only
  52. comments, including the first comment line, are visible), or to make the
  53. first comment line visible (if point is in a comment)."
  54.   (interactive "P")
  55.   (let* (;; (here (save-excursion (beginning-of-line) (point)))
  56.      (here (point))
  57.      ;; change this name once I've gotten rid of references to ht.
  58.      ;; this is actually the number of the last screen line
  59.      (ht (- (window-height (selected-window)) 2))
  60.      (line (repos-count-screen-lines (window-start) (point)))
  61.      (comment-height
  62.       ;; The call to max deals with the case of cursor between defuns.
  63.       (max 0
  64.            (repos-count-screen-lines-signed
  65.         ;; the beginning of the preceding comment
  66.         (save-excursion
  67.           (forward-char 1) (end-of-defun -1) 
  68.           ;; Skip whitespace, newlines, and form feeds.
  69.           (re-search-forward "[^\\s \n\014]")
  70.           (backward-char 1)
  71.           (point))
  72.         here)))
  73.      (defun-height 
  74.        (repos-count-screen-lines-signed
  75.         (save-excursion
  76.           (end-of-defun 1) ; so comments associate with following defuns
  77.           (beginning-of-defun 1)
  78.           (point))
  79.         here))
  80.      ;; This must be positive, so don't use the signed version.
  81.      (defun-depth (repos-count-screen-lines here
  82.                         (save-excursion
  83.                           (end-of-defun 1)
  84.                           (point))))
  85.      (defun-line-onscreen-p
  86.        (and (<= defun-height line)
  87.         (<= (- line defun-height) ht))))
  88.     (cond ((or (= comment-height line)
  89.            (and (= line ht)
  90.             (> comment-height line)
  91.             ;; if defun line offscreen, we should be in case 4
  92.             defun-line-onscreen-p))
  93.        ;; Either first comment line is at top of screen or (point at
  94.        ;; bottom of screen, defun line onscreen, and first comment line
  95.        ;; off top of screen).  That is, it looks like we just did
  96.        ;; recenter-definition, trying to fit as much of the comment
  97.        ;; onscreen as possible.  Put defun line at top of screen; that
  98.        ;; is, show as much code, and as few comments, as possible.
  99.  
  100.        (if (and arg (> defun-depth (1+ ht)))
  101.            ;; Can't fit whole defun onscreen without moving point.
  102.            (progn (end-of-defun) (beginning-of-defun) (recenter 0))
  103.          (recenter (max defun-height 0)))
  104.        ;;(repos-debug-macro "1")
  105.        )
  106.  
  107.       ((or (= defun-height line)
  108.            (= line 0)
  109.            (and (< line comment-height)
  110.             (< defun-height 0)))
  111.        ;; Defun line or cursor at top of screen, OR cursor in comment
  112.        ;; whose first line is offscreen.
  113.        ;; Avoid moving definition up even if defun runs offscreen;
  114.        ;; we care more about getting the comment onscreen.
  115.        
  116.        (cond ((= line ht)
  117.           ;; cursor on last screen line (and so in a comment)
  118.           (if arg (progn (end-of-defun) (beginning-of-defun)))
  119.           (recenter 0)
  120.           ;;(repos-debug-macro "2a")
  121.           )
  122.          
  123.          ;; This condition, copied from case 4, may not be quite right
  124.          
  125.          ((and arg (< ht comment-height))
  126.           ;; Can't get first comment line onscreen.
  127.           ;; Go there and try again.
  128.           (forward-line (- comment-height))
  129.           (beginning-of-line)
  130.           ;; was (reposition-window)
  131.           (recenter 0)
  132.           ;;(repos-debug-macro "2b")
  133.           )
  134.          (t
  135.           (recenter (min ht comment-height))
  136.           ;;(repos-debug-macro "2c")
  137.           ))
  138.        ;; (recenter (min ht comment-height))
  139.        )
  140.  
  141.       ((and (> (+ line defun-depth -1) ht)
  142.         defun-line-onscreen-p)
  143.        ;; Defun runs off the bottom of the screen and the defun line
  144.        ;; is onscreen.
  145.        ;; Move the defun up.
  146.        (recenter (max 0 (1+ (- ht defun-depth)) defun-height))
  147.        ;;(repos-debug-macro "3")
  148.        )
  149.  
  150.       (t
  151.        ;; If on the bottom line and comment start is offscreen
  152.        ;; then just move all comments offscreen, or at least as
  153.        ;; far as they'll go.
  154.  
  155.        ;; Try to get as much of the comments onscreen as possible.
  156.        (if (and arg (< ht comment-height))
  157.            ;; Can't get defun line onscreen; go there and try again.
  158.            (progn (forward-line (- defun-height))
  159.               (beginning-of-line)
  160.               (reposition-window))
  161.          (recenter (min ht comment-height)))
  162.        ;;(repos-debug-macro "4")
  163.        ))))
  164.  
  165. ;;;###autoload
  166. (define-key esc-map "\C-l" 'reposition-window)
  167.  
  168. ;;; Auxiliary functions
  169.  
  170. ;; Return number of screen lines between START and END.
  171. (defun repos-count-screen-lines (start end)
  172.   (save-excursion
  173.     (save-restriction
  174.       (narrow-to-region start end)
  175.       (goto-char (point-min))
  176.       (vertical-motion (- (point-max) (point-min))))))
  177.  
  178. ;; Return number of screen lines between START and END; returns a negative
  179. ;; number if END precedes START.
  180. (defun repos-count-screen-lines-signed (start end)
  181.   (let ((lines (repos-count-screen-lines start end)))
  182.     (if (< start end)
  183.     lines
  184.       (- lines))))
  185.  
  186. ; (defmacro repos-debug-macro (case-no)
  187. ;   (` (message
  188. ;       (concat "Case " (, case-no) ": %s %s %s %s %s")
  189. ;       ht line comment-height defun-height defun-depth)))
  190.  
  191. ;;; reposition.el ends here
  192.