reposition.el 7.1 KB

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