window.sl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % Window.SL - Commands and Functions for manipulating windows.
  4. %
  5. % Author: Alan Snyder
  6. % Hewlett-Packard/CRC
  7. % Date: 24 August 1982
  8. % Revised: 30 December 1982
  9. %
  10. % 30-Dec-82 Alan Snyder
  11. % Change scrolling commands to Ding if no scrolling is actually done. Fix bug
  12. % in backwards scroll by pages that failed to preserve relative cursor
  13. % position. Change behavior of scroll-by-pages upon excessive request.
  14. %
  15. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  16. (CompileTime (load objects fast-int))
  17. (fluid '(nmode-current-window
  18. nmode-command-argument
  19. nmode-command-number-given
  20. nmode-command-argument-given
  21. nmode-layout-mode
  22. ))
  23. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24. (de current-window-height ()
  25. % Return the number of text lines displayable on the current window.
  26. (=> nmode-current-window height))
  27. (de current-window-top-line ()
  28. % Return the index of the buffer line at the top of the current window.
  29. (=> nmode-current-window buffer-top)
  30. )
  31. (de current-window-set-top-line (new-top-line)
  32. % Change which buffer line displays at the top of the current window.
  33. (=> nmode-current-window set-buffer-top new-top-line)
  34. )
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36. % Window Scrolling Functions
  37. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  38. (de scroll-window-according-to-command (w)
  39. % Scroll the contents of the specified window according to the command
  40. % argument. If the command argument was set by C-U or C-U -, then scroll the
  41. % contents of the window up or down one page. Otherwise, scroll the window up
  42. % or down the specified number of lines.
  43. (if (and (or (= nmode-command-argument 1) (= nmode-command-argument -1))
  44. (not nmode-command-number-given))
  45. (scroll-window-by-pages w nmode-command-argument)
  46. (scroll-window-by-lines w nmode-command-argument)
  47. ))
  48. (de scroll-window-by-lines (w n)
  49. % Scroll the contents of the window up (n > 0) or down (n < 0) by |n| lines.
  50. % The "window position" may be adjusted to keep it within the window. Ding if
  51. % the window contents does not move.
  52. (let* ((old-top-line (=> w buffer-top))
  53. (new-top-line (+ old-top-line n))
  54. )
  55. % adjust to keep something in the window
  56. (let ((buffer-last-line (- (=> (=> w buffer) visible-size) 1)))
  57. (cond
  58. ((< new-top-line 0) (setf new-top-line 0))
  59. ((> new-top-line buffer-last-line) (setf new-top-line buffer-last-line))
  60. ))
  61. % adjust "window position" if no longer in window
  62. (let ((line (=> w line-position))
  63. (max (+ new-top-line (- (=> w height) 1)))
  64. )
  65. (cond
  66. ((< line new-top-line) (=> w set-line-position new-top-line))
  67. ((> line max) (=> w set-line-position max))
  68. ))
  69. (if (~= old-top-line new-top-line)
  70. (=> w set-buffer-top new-top-line)
  71. (Ding)
  72. )))
  73. (de scroll-window-by-pages (w n)
  74. % Scroll the contents of the window up (n > 0) or down (n < 0) by |n|
  75. % screenfuls. The "window position" may be adjusted to keep it within the
  76. % window. Ding if the window contents does not move.
  77. (let* ((old-top-line (=> w buffer-top))
  78. (window-height (=> w height))
  79. (buffer-last-line (- (=> (=> w buffer) visible-size) 1))
  80. (new-top-line old-top-line)
  81. )
  82. (if (>= n 0)
  83. % moving towards the end of the buffer
  84. (for (from i 1 n) % do as many complete screenfuls as possible
  85. (do (let ((next-top-line (+ new-top-line window-height)))
  86. (if (<= next-top-line buffer-last-line)
  87. (setf new-top-line next-top-line)
  88. (exit)
  89. ))))
  90. % moving towards the beginning of the buffer
  91. (setf new-top-line (max 0 (+ new-top-line (* n window-height))))
  92. )
  93. (if (~= new-top-line old-top-line)
  94. % keep the cursor at the same relative location in the window!
  95. (let ((delta (- new-top-line old-top-line)))
  96. (=> w set-line-position
  97. (min (+ (=> w line-position) delta) (+ buffer-last-line 1)))
  98. (=> w set-buffer-top new-top-line)
  99. )
  100. % otherwise (no change)
  101. (Ding)
  102. )))
  103. (de scroll-window-horizontally (w n)
  104. % Scroll the contents of the specified window left (n > 0) or right (n < 0)
  105. % by |n| columns.
  106. (let ((old-buffer-left (=> w buffer-left)))
  107. (=> w set-buffer-left (+ old-buffer-left n))
  108. (if (= old-buffer-left (=> w buffer-left)) (Ding))
  109. ))
  110. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  111. % Window Scrolling Commands
  112. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  113. (de next-screen-command ()
  114. (scroll-window-according-to-command nmode-current-window)
  115. )
  116. (de previous-screen-command ()
  117. (setf nmode-command-argument (- 0 nmode-command-argument))
  118. (scroll-window-according-to-command nmode-current-window)
  119. )
  120. (de scroll-other-window-command ()
  121. (selectq nmode-layout-mode
  122. (1 (Ding))
  123. (2 (scroll-window-according-to-command (nmode-other-window)))
  124. ))
  125. (de scroll-window-up-line-command ()
  126. (scroll-window-by-lines nmode-current-window nmode-command-argument)
  127. )
  128. (de scroll-window-down-line-command ()
  129. (scroll-window-by-lines nmode-current-window (- nmode-command-argument))
  130. )
  131. (de scroll-window-up-page-command ()
  132. (scroll-window-by-pages nmode-current-window nmode-command-argument)
  133. )
  134. (de scroll-window-down-page-command ()
  135. (scroll-window-by-pages nmode-current-window (- nmode-command-argument))
  136. )
  137. (de scroll-window-right-command ()
  138. (scroll-window-horizontally nmode-current-window nmode-command-argument)
  139. )
  140. (de scroll-window-left-command ()
  141. (scroll-window-horizontally nmode-current-window (- nmode-command-argument))
  142. )
  143. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  144. % Window Adjusting Commands
  145. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  146. (de nmode-adjust-window (w)
  147. % Adjust BUFFER-TOP to show current position.
  148. (=> w adjust-window)
  149. )
  150. (de move-to-screen-edge-command ()
  151. (let* ((n nmode-command-argument)
  152. (line (current-line-pos))
  153. (top (current-window-top-line))
  154. (height (current-window-height))
  155. )
  156. (set-line-pos (+ top
  157. (cond ((not nmode-command-argument-given) (/ height 2))
  158. ((>= n 0) n)
  159. (t (+ height n))
  160. )))))