tpu-extras.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. ;;; tpu-extras.el --- scroll margins and free cursor mode for TPU-edt
  2. ;; Copyright (C) 1993-1995, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Rob Riepel <riepel@networking.stanford.edu>
  4. ;; Maintainer: Rob Riepel <riepel@networking.stanford.edu>
  5. ;; Keywords: emulations
  6. ;; Package: tpu-edt
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  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. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Use the functions defined here to customize TPU-edt to your tastes by
  20. ;; setting scroll margins and/or turning on free cursor mode. Here's an
  21. ;; example for your .emacs file.
  22. ;; (tpu-set-cursor-free) ; Set cursor free.
  23. ;; (tpu-set-scroll-margins "10%" "15%") ; Set scroll margins.
  24. ;; Scroll margins and cursor binding can be changed from within emacs using
  25. ;; the following commands:
  26. ;; tpu-set-scroll-margins or set scroll margins
  27. ;; tpu-set-cursor-bound or set cursor bound
  28. ;; tpu-set-cursor-free or set cursor free
  29. ;; Additionally, Gold-F toggles between bound and free cursor modes.
  30. ;; Note that switching out of free cursor mode or exiting TPU-edt while in
  31. ;; free cursor mode strips trailing whitespace from every line in the file.
  32. ;;; Details:
  33. ;; The functions contained in this file implement scroll margins and free
  34. ;; cursor mode. The following keys and commands are affected.
  35. ;; key/command function scroll cursor
  36. ;; Up-Arrow previous line x x
  37. ;; Down-Arrow next line x x
  38. ;; Right-Arrow next character x
  39. ;; Left-Arrow previous character x
  40. ;; KP0 next or previous line x
  41. ;; KP7 next or previous page x
  42. ;; KP8 next or previous screen x
  43. ;; KP2 next or previous end-of-line x x
  44. ;; Control-e current end-of-line x
  45. ;; Control-h previous beginning-of-line x
  46. ;; Next Scr next screen x
  47. ;; Prev Scr previous screen x
  48. ;; Search find a string x
  49. ;; Replace find and replace a string x
  50. ;; Newline insert a newline x
  51. ;; Paragraph next or previous paragraph x
  52. ;; Auto-Fill break lines on spaces x
  53. ;; These functions are not part of the base TPU-edt for the following
  54. ;; reasons:
  55. ;; Free cursor mode is implemented with the emacs picture-mode functions.
  56. ;; These functions support moving the cursor all over the screen, however,
  57. ;; when the cursor is moved past the end of a line, spaces or tabs are
  58. ;; appended to the line - even if no text is entered in that area. In
  59. ;; order for a free cursor mode to work exactly like TPU/edt, this trailing
  60. ;; whitespace needs to be dealt with in every function that might encounter
  61. ;; it. Such global changes are impractical, however, free cursor mode is
  62. ;; too valuable to abandon completely, so it has been implemented in those
  63. ;; functions where it serves best.
  64. ;; The implementation of scroll margins adds overhead to previously
  65. ;; simple and often used commands. These commands are now responsible
  66. ;; for their normal operation and part of the display function. There
  67. ;; is a possibility that this display overhead could adversely affect the
  68. ;; performance of TPU-edt on slower computers. In order to support the
  69. ;; widest range of computers, scroll margin support is optional.
  70. ;; It's actually not known whether the overhead associated with scroll
  71. ;; margin support is significant. If you find that it is, please send
  72. ;; a note describing the extent of the performance degradation. Be sure
  73. ;; to include a description of the platform where you're running TPU-edt.
  74. ;; Send your note to the address provided by Gold-V.
  75. ;; Even with these differences and limitations, these functions implement
  76. ;; important aspects of the real TPU/edt. Those who miss free cursor mode
  77. ;; and/or scroll margins will appreciate these implementations.
  78. ;;; Code:
  79. ;;; Gotta have tpu-edt
  80. (require 'tpu-edt)
  81. ;;; Customization variables
  82. (defcustom tpu-top-scroll-margin 0
  83. "Scroll margin at the top of the screen.
  84. Interpreted as a percent of the current window size."
  85. :type 'integer
  86. :group 'tpu)
  87. (defcustom tpu-bottom-scroll-margin 0
  88. "Scroll margin at the bottom of the screen.
  89. Interpreted as a percent of the current window size."
  90. :type 'integer
  91. :group 'tpu)
  92. (defcustom tpu-backward-char-like-tpu t
  93. "If non-nil, in free cursor mode backward-char (left-arrow) works
  94. just like TPU/edt. Otherwise, backward-char will move to the end of
  95. the previous line when starting from a line beginning."
  96. :type 'boolean
  97. :group 'tpu)
  98. ;;; Global variables
  99. ;;;###autoload
  100. (define-minor-mode tpu-cursor-free-mode
  101. "Minor mode to allow the cursor to move freely about the screen.
  102. With a prefix argument ARG, enable the mode if ARG is positive,
  103. and disable it otherwise. If called from Lisp, enable the mode
  104. if ARG is omitted or nil."
  105. :init-value nil
  106. (if (not tpu-cursor-free-mode)
  107. (tpu-trim-line-ends))
  108. (if (not tpu-cursor-free-mode)
  109. (message "The cursor is now bound to the flow of your text.")
  110. (message "The cursor will now move freely about the screen.")))
  111. ;;; Hooks -- Set cursor free in picture mode.
  112. ;;; Clean up when writing a file from cursor free mode.
  113. (add-hook 'picture-mode-hook 'tpu-set-cursor-free)
  114. (defun tpu-trim-line-ends-if-needed ()
  115. "Eliminate whitespace at ends of lines, if the cursor is free."
  116. (if (and (buffer-modified-p) tpu-cursor-free-mode) (tpu-trim-line-ends)))
  117. (add-hook 'before-save-hook 'tpu-trim-line-ends-if-needed)
  118. ;;; Utility routines for implementing scroll margins
  119. (defun tpu-top-check (beg lines)
  120. "Enforce scroll margin at the top of screen."
  121. (let ((margin (/ (* (window-height) tpu-top-scroll-margin) 100)))
  122. (cond ((< beg margin) (recenter beg))
  123. ((< (- beg lines) margin) (recenter margin)))))
  124. (defun tpu-bottom-check (beg lines)
  125. "Enforce scroll margin at the bottom of screen."
  126. (let* ((height (window-height))
  127. (margin (+ 1 (/ (* height tpu-bottom-scroll-margin) 100)))
  128. ;; subtract 1 from height because it includes mode line
  129. (difference (- height margin 1)))
  130. (cond ((> beg difference) (recenter beg))
  131. ((> (+ beg lines) difference) (recenter (- margin))))))
  132. ;;; Movement by character
  133. (defun tpu-forward-char (num)
  134. "Move right ARG characters (left if ARG is negative)."
  135. (interactive "p")
  136. (if tpu-cursor-free-mode (picture-forward-column num) (forward-char num)))
  137. (defun tpu-backward-char (num)
  138. "Move left ARG characters (right if ARG is negative)."
  139. (interactive "p")
  140. (cond ((not tpu-cursor-free-mode)
  141. (backward-char num))
  142. (tpu-backward-char-like-tpu
  143. (picture-backward-column num))
  144. ((bolp)
  145. (backward-char 1)
  146. (picture-end-of-line)
  147. (picture-backward-column (1- num)))
  148. (t
  149. (picture-backward-column num))))
  150. ;;; Movement by line
  151. (defun tpu-next-line (num)
  152. "Move to next line.
  153. Prefix argument serves as a repeat count."
  154. (interactive "p")
  155. (let ((beg (tpu-current-line)))
  156. (if tpu-cursor-free-mode (or (eobp) (picture-move-down num))
  157. (line-move num))
  158. (tpu-bottom-check beg num)
  159. (setq this-command 'next-line)))
  160. (defun tpu-previous-line (num)
  161. "Move to previous line.
  162. Prefix argument serves as a repeat count."
  163. (interactive "p")
  164. (let ((beg (tpu-current-line)))
  165. (if tpu-cursor-free-mode (picture-move-up num) (line-move (- num)))
  166. (tpu-top-check beg num)
  167. (setq this-command 'previous-line)))
  168. (defun tpu-next-beginning-of-line (num)
  169. "Move to beginning of line; if at beginning, move to beginning of next line.
  170. Accepts a prefix argument for the number of lines to move."
  171. (interactive "p")
  172. (let ((beg (tpu-current-line)))
  173. (backward-char 1)
  174. (forward-visible-line (- 1 num))
  175. (tpu-top-check beg num)))
  176. (defun tpu-next-end-of-line (num)
  177. "Move to end of line; if at end, move to end of next line.
  178. Accepts a prefix argument for the number of lines to move."
  179. (interactive "p")
  180. (let ((beg (tpu-current-line)))
  181. (cond (tpu-cursor-free-mode
  182. (let ((beg (point)))
  183. (if (< 1 num) (forward-line num))
  184. (picture-end-of-line)
  185. (if (<= (point) beg) (progn (forward-line) (picture-end-of-line)))))
  186. (t
  187. (forward-char)
  188. (end-of-line num)))
  189. (tpu-bottom-check beg num)))
  190. (defun tpu-previous-end-of-line (num)
  191. "Move EOL upward.
  192. Accepts a prefix argument for the number of lines to move."
  193. (interactive "p")
  194. (let ((beg (tpu-current-line)))
  195. (cond (tpu-cursor-free-mode
  196. (picture-end-of-line (- 1 num)))
  197. (t
  198. (end-of-line (- 1 num))))
  199. (tpu-top-check beg num)))
  200. (defun tpu-current-end-of-line ()
  201. "Move point to end of current line."
  202. (interactive)
  203. (let ((beg (point)))
  204. (if tpu-cursor-free-mode (picture-end-of-line) (end-of-line))
  205. (if (= beg (point)) (message "You are already at the end of a line."))))
  206. (defun tpu-forward-line (num)
  207. "Move to beginning of next line.
  208. Prefix argument serves as a repeat count."
  209. (interactive "p")
  210. (let ((beg (tpu-current-line)))
  211. (forward-line num)
  212. (tpu-bottom-check beg num)))
  213. (defun tpu-backward-line (num)
  214. "Move to beginning of previous line.
  215. Prefix argument serves as repeat count."
  216. (interactive "p")
  217. (let ((beg (tpu-current-line)))
  218. (or (bolp) (>= 0 num) (setq num (- num 1)))
  219. (forward-line (- num))
  220. (tpu-top-check beg num)))
  221. ;;; Movement by paragraph
  222. ;; Cf edt-with-position.
  223. (defmacro tpu-with-position (&rest body)
  224. "Execute BODY with some position-related variables bound."
  225. `(let* ((left nil)
  226. (beg (tpu-current-line))
  227. (height (window-height))
  228. (top-percent
  229. (if (zerop tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
  230. (bottom-percent
  231. (if (zerop tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
  232. (top-margin (/ (* height top-percent) 100))
  233. (bottom-up-margin (1+ (/ (* height bottom-percent) 100)))
  234. (bottom-margin (max beg (- height bottom-up-margin 1)))
  235. (top (save-excursion (move-to-window-line top-margin) (point)))
  236. (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
  237. (far (save-excursion
  238. (goto-char bottom)
  239. (point-at-bol (1- height)))))
  240. ,@body))
  241. (defun tpu-paragraph (num)
  242. "Move to the next paragraph in the current direction.
  243. A repeat count means move that many paragraphs."
  244. (interactive "p")
  245. (tpu-with-position
  246. (if tpu-advance
  247. (progn
  248. (tpu-next-paragraph num)
  249. (if (> (point) far)
  250. (if (zerop (setq left (save-excursion (forward-line height))))
  251. (recenter top-margin)
  252. (recenter (- left bottom-up-margin)))
  253. (and (> (point) bottom) (recenter bottom-margin))))
  254. (tpu-previous-paragraph num)
  255. (and (< (point) top) (recenter (min beg top-margin))))))
  256. ;;; Movement by page
  257. (defun tpu-page (num)
  258. "Move to the next page in the current direction.
  259. A repeat count means move that many pages."
  260. (interactive "p")
  261. (tpu-with-position
  262. (if tpu-advance
  263. (progn
  264. (forward-page num)
  265. (if (> (point) far)
  266. (if (zerop (setq left (save-excursion (forward-line height))))
  267. (recenter top-margin)
  268. (recenter (- left bottom-up-margin)))
  269. (and (> (point) bottom) (recenter bottom-margin))))
  270. (backward-page num)
  271. (and (< (point) top) (recenter (min beg top-margin))))))
  272. ;;; Scrolling
  273. (defun tpu-scroll-window-down (num)
  274. "Scroll the display down to the next section.
  275. A repeat count means scroll that many sections."
  276. (interactive "p")
  277. (let* ((beg (tpu-current-line))
  278. (height (1- (window-height)))
  279. (lines (* num (/ (* height tpu-percent-scroll) 100))))
  280. (line-move (- lines))
  281. (tpu-top-check beg lines)))
  282. (defun tpu-scroll-window-up (num)
  283. "Scroll the display up to the next section.
  284. A repeat count means scroll that many sections."
  285. (interactive "p")
  286. (let* ((beg (tpu-current-line))
  287. (height (1- (window-height)))
  288. (lines (* num (/ (* height tpu-percent-scroll) 100))))
  289. (line-move lines)
  290. (tpu-bottom-check beg lines)))
  291. ;;; Replace the TPU-edt internal search function
  292. (defun tpu-search-internal (pat &optional quiet)
  293. "Search for a string or regular expression."
  294. (tpu-with-position
  295. (tpu-search-internal-core pat quiet)
  296. (if tpu-searching-forward
  297. (progn
  298. (if (> (point) far)
  299. (if (zerop (setq left (save-excursion (forward-line height))))
  300. (recenter top-margin)
  301. (recenter (- left bottom-up-margin)))
  302. (and (> (point) bottom) (recenter bottom-margin))))
  303. (and (< (point) top) (recenter (min beg top-margin))))))
  304. ;; Advise the newline, newline-and-indent, and do-auto-fill functions.
  305. (defadvice newline (around tpu-respect-bottom-scroll-margin activate disable)
  306. "Respect `tpu-bottom-scroll-margin'."
  307. (let ((beg (tpu-current-line))
  308. (num (prefix-numeric-value (ad-get-arg 0))))
  309. ad-do-it
  310. (tpu-bottom-check beg num)))
  311. (defadvice newline-and-indent (around tpu-respect-bottom-scroll-margin)
  312. "Respect `tpu-bottom-scroll-margin'."
  313. (let ((beg (tpu-current-line)))
  314. ad-do-it
  315. (tpu-bottom-check beg 1)))
  316. (defadvice do-auto-fill (around tpu-respect-bottom-scroll-margin)
  317. "Respect `tpu-bottom-scroll-margin'."
  318. (let ((beg (tpu-current-line)))
  319. ad-do-it
  320. (tpu-bottom-check beg 1)))
  321. ;;; Function to set scroll margins
  322. ;;;###autoload
  323. (defun tpu-set-scroll-margins (top bottom)
  324. "Set scroll margins."
  325. (interactive
  326. "sEnter top scroll margin (N lines or N%% or RETURN for current value): \
  327. \nsEnter bottom scroll margin (N lines or N%% or RETURN for current value): ")
  328. ;; set top scroll margin
  329. (or (string= top "")
  330. (setq tpu-top-scroll-margin
  331. (if (string= "%" (substring top -1))
  332. (string-to-number top)
  333. (/ (1- (+ (* (string-to-number top) 100) (window-height)))
  334. (window-height)))))
  335. ;; set bottom scroll margin
  336. (or (string= bottom "")
  337. (setq tpu-bottom-scroll-margin
  338. (if (string= "%" (substring bottom -1))
  339. (string-to-number bottom)
  340. (/ (1- (+ (* (string-to-number bottom) 100) (window-height)))
  341. (window-height)))))
  342. (dolist (f '(newline newline-and-indent do-auto-fill))
  343. (ad-enable-advice f 'around 'tpu-respect-bottom-scroll-margin)
  344. (ad-activate f))
  345. ;; report scroll margin settings if running interactively
  346. (and (called-interactively-p 'interactive)
  347. (message "Scroll margins set. Top = %s%%, Bottom = %s%%"
  348. tpu-top-scroll-margin tpu-bottom-scroll-margin)))
  349. ;;; Functions to set cursor bound or free
  350. ;;;###autoload
  351. (defun tpu-set-cursor-free ()
  352. "Allow the cursor to move freely about the screen."
  353. (interactive)
  354. (tpu-cursor-free-mode 1))
  355. ;;;###autoload
  356. (defun tpu-set-cursor-bound ()
  357. "Constrain the cursor to the flow of the text."
  358. (interactive)
  359. (tpu-cursor-free-mode -1))
  360. ;; Local Variables:
  361. ;; generated-autoload-file: "tpu-edt.el"
  362. ;; End:
  363. ;;; tpu-extras.el ends here