tpu-extras.el 16 KB

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