home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / modes / tpu-edt-3.0 / tpu-edt-extras.el < prev    next >
Encoding:
Text File  |  1993-06-24  |  17.3 KB  |  490 lines

  1. ;;; tpu-edt-extras.el --- Scroll margins and free cursor mode for TPU-edt
  2.  
  3. ;; Copyright (C) 1993 Rob Riepel.
  4.  
  5. ;; Author: Rob Riepel <riepel@networking.stanford.edu>
  6. ;; Maintainer: Rob Riepel <riepel@networking.stanford.edu>
  7. ;; Keywords: tpu-edt
  8.  
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  11. ;; accepts responsibility to anyone for the consequences of using it
  12. ;; or for whether it serves any particular purpose or works at all,
  13. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  14. ;; License for full details.
  15.  
  16. ;; Everyone is granted permission to copy, modify and redistribute
  17. ;; GNU Emacs, but only under the conditions described in the
  18. ;; GNU Emacs General Public License.   A copy of this license is
  19. ;; supposed to have been given to you along with GNU Emacs so you
  20. ;; can know your rights and responsibilities.  It should be in a
  21. ;; file named COPYING.  Among other things, the copyright notice
  22. ;; and this notice must be preserved on all copies.
  23. ;;
  24.  
  25. ;;; Revision: $Id: tpu-edt-extras.el,v 3.2 1993/06/20 19:06:19 riepel Exp $
  26.  
  27. ;;; Commentary:
  28.  
  29. ;;  The functions contained in this file implement scroll margins and free
  30. ;;  cursor mode.  The following keys and commands are affected.
  31.  
  32. ;;       key/command   function                        scroll   cursor
  33.  
  34. ;;       Up-Arrow      previous line                     x        x
  35. ;;       Down-Arrow    next line                         x        x
  36. ;;       Right-Arrow   next character                             x
  37. ;;       Left-Arrow    previous character                         x
  38. ;;       KP0           next or previous line             x
  39. ;;       KP7           next or previous page             x
  40. ;;       KP8           next or previous screen           x
  41. ;;       KP2           next or previous end-of-line      x        x
  42. ;;       Control-e     current end-of-line                        x
  43. ;;       Control-h     previous beginning-of-line        x
  44. ;;       Next Scr      next screen                       x
  45. ;;       Prev Scr      previous screen                   x
  46. ;;       Search        find a string                     x
  47. ;;       Replace       find and replace a string         x
  48. ;;       Newline       insert a newline                  x
  49. ;;       Paragraph     next or previous paragraph        x
  50. ;;       Auto-Fill     break lines on spaces             x
  51.  
  52. ;;  These functions are not part of the base TPU-edt for the following
  53. ;;  reasons:
  54.  
  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.  
  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.  
  72. ;;  I don't know for a fact that the overhead associated with scroll
  73. ;;  margin support is significant.  If you find that it is, please send me
  74. ;;  a note describing the extent of the performance degradation.  Be sure
  75. ;;  to include a description of the platform where you're running TPU-edt.
  76. ;;  Send your note to the address provided by Gold-V.
  77.  
  78. ;;  Even with these differences and limitations, these functions implement
  79. ;;  important aspects of the real TPU/edt.  Those who miss free cursor mode
  80. ;;  and/or scroll margins will appreciate these implementations.
  81.  
  82. ;;; Usage:
  83.  
  84. ;;  To use this file, simply load it after loading TPU-edt.  After that,
  85. ;;  customize TPU-edt to your tastes by setting scroll margins and/or
  86. ;;  turning on free cursor mode.  Here's an example for your .emacs file.
  87.  
  88. ;;     (load "tpu-edt")                      ; Load the base TPU-edt
  89. ;;     (load "tpu-edt-extras")               ;   and the extras.
  90. ;;     (tpu-set-scroll-margins "10%" "15%")  ; Set scroll margins.
  91.  
  92. ;;  Once the extras are loaded, scroll margins and cursor binding can be
  93. ;;  changed with the following commands:
  94.  
  95. ;;     tpu-set-scroll-margins  or   set scroll margins
  96. ;;     tpu-set-cursor-bound    or   set cursor bound
  97. ;;     tpu-set-cursor-free     or   set cursor free
  98.  
  99. ;;  Additionally, Gold-F toggles between bound and free cursor modes.
  100.  
  101. ;;  Note that switching out of free cursor mode or exiting TPU-edt while in
  102. ;;  free cursor mode strips trailing whitespace from every line in the file.
  103.  
  104. ;;; Code:
  105.  
  106.  
  107. ;;;  Revision Information
  108.  
  109. (defconst tpu-extras-revision "$Revision: 3.2 $"
  110.   "Revision number of the TPU-edt extras.")
  111.  
  112.  
  113. ;;;  Customization variables
  114.  
  115. (defconst tpu-top-scroll-margin 0
  116.   "*Scroll margin at the top of the screen.
  117. Interpreted as a percent of the current window size.")
  118. (defconst tpu-bottom-scroll-margin 0
  119.   "*Scroll margin at the bottom of the screen.
  120. Interpreted as a percent of the current window size.")
  121.  
  122. (defvar tpu-backward-char-like-tpu t
  123.   "*If non-nil, in free cursor mode backward-char (left-arrow) works
  124. just like TPU/edt.  Otherwise, backward-char will move to the end of
  125. the previous line when starting from a line beginning.")
  126.  
  127.  
  128. ;;;  Global variables
  129.  
  130. (defvar tpu-cursor-free nil
  131.   "If non-nil, let the cursor roam free.")
  132.  
  133.  
  134. ;;;  Hooks  --  Set cursor free in picture mode.
  135. ;;;             Clean up when writing a file from cursor free mode.
  136.  
  137. (setq edit-picture-hook 'tpu-set-cursor-free)
  138.  
  139. (defun tpu-write-file-hook nil
  140.   "Eliminate whitespace at ends of lines, if the cursor is free."
  141.   (if (and (buffer-modified-p) tpu-cursor-free) (picture-clean)))
  142.  
  143. (or (memq 'tpu-write-file-hook write-file-hooks)
  144.     (setq write-file-hooks
  145.       (cons 'tpu-write-file-hook write-file-hooks)))
  146.  
  147.  
  148. ;;;  Utility routines for implementing scroll margins
  149.  
  150. (defun tpu-top-check (beg lines)
  151.   "Enforce scroll margin at the top of screen."
  152.   (let ((margin     (/ (* (window-height) tpu-top-scroll-margin) 100)))
  153.     (cond ((< beg margin) (recenter beg))
  154.       ((< (- beg lines) margin) (recenter margin)))))
  155.  
  156. (defun tpu-bottom-check (beg lines)
  157.   "Enforce scroll margin at the bottom of screen."
  158.   (let* ((height (window-height))
  159.      (margin (+ 1 (/ (* height tpu-bottom-scroll-margin) 100)))
  160.      ;; subtract 1 from height because it includes mode line
  161.      (difference (- height margin 1)))
  162.     (cond ((> beg difference) (recenter beg))
  163.       ((> (+ beg lines) difference) (recenter (- margin))))))
  164.  
  165.  
  166. ;;;  Movement by character
  167.  
  168. (defun tpu-forward-char (num)
  169.   "Move right ARG characters (left if ARG is negative)."
  170.   (interactive "p")
  171.   (if tpu-cursor-free (picture-forward-column num) (forward-char num)))
  172.  
  173. (defun tpu-backward-char (num)
  174.   "Move left ARG characters (right if ARG is negative)."
  175.   (interactive "p")
  176.   (cond ((not tpu-cursor-free)
  177.      (backward-char num))
  178.     (tpu-backward-char-like-tpu
  179.      (picture-backward-column num))
  180.     ((bolp)
  181.      (backward-char 1)
  182.      (picture-end-of-line)
  183.      (picture-backward-column (1- num)))
  184.     (t
  185.      (picture-backward-column num))))
  186.  
  187.  
  188. ;;;  Movement by line
  189.  
  190. (defun tpu-next-line (num)
  191.   "Move to next line.
  192. Prefix argument serves as a repeat count."
  193.   (interactive "p")
  194.   (let ((beg (tpu-current-line)))
  195.     (if tpu-cursor-free (or (eobp) (picture-move-down num))
  196.       (next-line-internal num))
  197.     (tpu-bottom-check beg num)
  198.     (setq this-command 'next-line)))
  199.  
  200. (defun tpu-previous-line (num)
  201.   "Move to previous line.
  202. Prefix argument serves as a repeat count."
  203.   (interactive "p")
  204.   (let ((beg (tpu-current-line)))
  205.     (if tpu-cursor-free (picture-move-up num) (next-line-internal (- num)))
  206.     (tpu-top-check beg num)
  207.     (setq this-command 'previous-line)))
  208.  
  209. (defun tpu-next-beginning-of-line (num)
  210.   "Move to beginning of line; if at beginning, move to beginning of next line.
  211. Accepts a prefix argument for the number of lines to move."
  212.   (interactive "p")
  213.   (let ((beg (tpu-current-line)))
  214.     (backward-char 1)
  215.     (forward-line (- 1 num))
  216.     (tpu-top-check beg num)))
  217.  
  218. (defun tpu-next-end-of-line (num)
  219.   "Move to end of line; if at end, move to end of next line.
  220. Accepts a prefix argument for the number of lines to move."
  221.   (interactive "p")
  222.   (let ((beg (tpu-current-line)))
  223.     (cond (tpu-cursor-free
  224.        (let ((beg (point)))
  225.          (if (< 1 num) (forward-line num))
  226.          (picture-end-of-line)
  227.          (if (<= (point) beg) (progn (forward-line) (picture-end-of-line)))))
  228.       (t
  229.        (forward-char)
  230.        (end-of-line num)))
  231.     (tpu-bottom-check beg num)))
  232.  
  233. (defun tpu-previous-end-of-line (num)
  234.   "Move EOL upward.
  235. Accepts a prefix argument for the number of lines to move."
  236.   (interactive "p")
  237.   (let ((beg (tpu-current-line)))
  238.     (cond (tpu-cursor-free
  239.        (picture-end-of-line (- 1 num)))
  240.       (t
  241.        (end-of-line (- 1 num))))
  242.     (tpu-top-check beg num)))
  243.  
  244. (defun tpu-current-end-of-line nil
  245.   "Move point to end of current line."
  246.   (interactive)
  247.   (let ((beg (point)))
  248.     (if tpu-cursor-free (picture-end-of-line) (end-of-line))
  249.     (if (= beg (point)) (message "You are already at the end of a line."))))
  250.  
  251. (defun tpu-forward-line (num)
  252.   "Move to beginning of next line.
  253. Prefix argument serves as a repeat count."
  254.   (interactive "p")
  255.   (let ((beg (tpu-current-line)))
  256.     (next-line-internal num)
  257.     (tpu-bottom-check beg num)
  258.     (beginning-of-line)))
  259.  
  260. (defun tpu-backward-line (num)
  261.   "Move to beginning of previous line.
  262. Prefix argument serves as repeat count."
  263.   (interactive "p")
  264.   (let ((beg (tpu-current-line)))
  265.     (next-line-internal (- num))
  266.     (tpu-top-check beg num)
  267.     (beginning-of-line)))
  268.  
  269.  
  270. ;;;  Movement by paragraph
  271.  
  272. (defun tpu-paragraph (num)
  273.   "Move to the next paragraph in the current direction.
  274. A repeat count means move that many paragraphs."
  275.   (interactive "p")
  276.   (let* ((left nil)
  277.      (beg (tpu-current-line))
  278.      (height (window-height))
  279.      (top-percent
  280.       (if (= 0 tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
  281.      (bottom-percent
  282.       (if (= 0 tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
  283.      (top-margin (/ (* height top-percent) 100))
  284.      (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
  285.      (bottom-margin (max beg (- height bottom-up-margin 1)))
  286.      (top (save-excursion (move-to-window-line top-margin) (point)))
  287.      (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
  288.      (far (save-excursion
  289.         (goto-char bottom) (forward-line (- height 2)) (point))))
  290.     (cond (tpu-advance
  291.        (tpu-next-paragraph num)
  292.        (cond((> (point) far)
  293.          (setq left (save-excursion (forward-line height)))
  294.          (if (= 0 left) (recenter top-margin)
  295.            (recenter (- left bottom-up-margin))))
  296.         (t
  297.          (and (> (point) bottom) (recenter bottom-margin)))))
  298.       (t
  299.        (tpu-previous-paragraph num)
  300.        (and (< (point) top) (recenter (min beg top-margin)))))))
  301.  
  302.  
  303. ;;;  Movement by page
  304.  
  305. (defun tpu-page (num)
  306.   "Move to the next page in the current direction.
  307. A repeat count means move that many pages."
  308.   (interactive "p")
  309.   (let* ((left nil)
  310.      (beg (tpu-current-line))
  311.      (height (window-height))
  312.      (top-percent
  313.       (if (= 0 tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
  314.      (bottom-percent
  315.       (if (= 0 tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
  316.      (top-margin (/ (* height top-percent) 100))
  317.      (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
  318.      (bottom-margin (max beg (- height bottom-up-margin 1)))
  319.      (top (save-excursion (move-to-window-line top-margin) (point)))
  320.      (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
  321.      (far (save-excursion
  322.         (goto-char bottom) (forward-line (- height 2)) (point))))
  323.     (cond (tpu-advance
  324.        (forward-page num)
  325.        (cond((> (point) far)
  326.          (setq left (save-excursion (forward-line height)))
  327.          (if (= 0 left) (recenter top-margin)
  328.            (recenter (- left bottom-up-margin))))
  329.         (t
  330.          (and (> (point) bottom) (recenter bottom-margin)))))
  331.       (t
  332.        (backward-page num)
  333.        (and (< (point) top) (recenter (min beg top-margin)))))))
  334.  
  335.  
  336. ;;;  Scrolling
  337.  
  338. (defun tpu-scroll-window-down (num)
  339.   "Scroll the display down to the next section.
  340. A repeat count means scroll that many sections."
  341.   (interactive "p")
  342.   (let* ((beg (tpu-current-line))
  343.      (height (1- (window-height)))
  344.      (lines (* num (/ (* height tpu-percent-scroll) 100))))
  345.     (next-line-internal (- lines))
  346.     (tpu-top-check beg lines)))
  347.  
  348. (defun tpu-scroll-window-up (num)
  349.   "Scroll the display up to the next section.
  350. A repeat count means scroll that many sections."
  351.   (interactive "p")
  352.   (let* ((beg (tpu-current-line))
  353.      (height (1- (window-height)))
  354.      (lines (* num (/ (* height tpu-percent-scroll) 100))))
  355.     (next-line-internal lines)
  356.     (tpu-bottom-check beg lines)))
  357.  
  358.  
  359. ;;;  Replace the TPU-edt internal search function
  360.  
  361. (defun tpu-search-internal (pat &optional quiet)
  362.   "Search for a string or regular expression."
  363.   (let* ((left nil)
  364.      (beg (tpu-current-line))
  365.      (height (window-height))
  366.      (top-percent
  367.       (if (= 0 tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
  368.      (bottom-percent
  369.       (if (= 0 tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
  370.      (top-margin (/ (* height top-percent) 100))
  371.      (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
  372.      (bottom-margin (max beg (- height bottom-up-margin 1)))
  373.      (top (save-excursion (move-to-window-line top-margin) (point)))
  374.      (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
  375.      (far (save-excursion
  376.         (goto-char bottom) (forward-line (- height 2)) (point))))
  377.     (tpu-search-internal-core pat quiet)
  378.     (if searching-forward
  379.     (cond((> (point) far)
  380.           (setq left (save-excursion (forward-line height)))
  381.           (if (= 0 left) (recenter top-margin)
  382.         (recenter (- left bottom-up-margin))))
  383.          (t
  384.           (and (> (point) bottom) (recenter bottom-margin))))
  385.       (and (< (point) top) (recenter (min beg top-margin))))))
  386.  
  387.  
  388.  
  389. ;;;  Replace the newline, newline-and-indent, and do-auto-fill functions
  390.  
  391. (or (fboundp 'tpu-old-newline)
  392.     (fset 'tpu-old-newline (symbol-function 'newline)))
  393. (or (fboundp 'tpu-old-do-auto-fill)
  394.     (fset 'tpu-old-do-auto-fill (symbol-function 'do-auto-fill)))
  395. (or (fboundp 'tpu-old-newline-and-indent)
  396.     (fset 'tpu-old-newline-and-indent (symbol-function 'newline-and-indent)))
  397.  
  398. (defun newline (&optional num)
  399.   "Insert a newline.  With arg, insert that many newlines.
  400. In Auto Fill mode, can break the preceding line if no numeric arg.
  401. This is the TPU-edt version that respects the bottom scroll margin."
  402.   (interactive "p")
  403.   (let ((beg (tpu-current-line)))
  404.     (or num (setq num 1))
  405.     (tpu-old-newline num)
  406.     (tpu-bottom-check beg num)))
  407.  
  408. (defun newline-and-indent nil
  409.   "Insert a newline, then indent according to major mode.
  410. Indentation is done using the current indent-line-function.
  411. In programming language modes, this is the same as TAB.
  412. In some text modes, where TAB inserts a tab, this indents
  413. to the specified left-margin column.  This is the TPU-edt
  414. version that respects the bottom scroll margin."
  415.   (interactive)
  416.   (let ((beg (tpu-current-line)))
  417.     (tpu-old-newline-and-indent)
  418.     (tpu-bottom-check beg 1)))
  419.  
  420. (defun do-auto-fill nil
  421.   "TPU-edt version that respects the bottom scroll margin."
  422.   (let ((beg (tpu-current-line)))
  423.     (tpu-old-do-auto-fill)
  424.     (tpu-bottom-check beg 1)))
  425.  
  426.  
  427. ;;;  Function to set scroll margins
  428.  
  429. (defun tpu-set-scroll-margins (top bottom)
  430.   "Set scroll margins."
  431.   (interactive
  432.    "sEnter top scroll margin (N lines or N%% or RETURN for current value): \
  433. \nsEnter bottom scroll margin (N lines or N%% or RETURN for current value): ")
  434.   ;; set top scroll margin
  435.   (or (string= top "")
  436.       (if (string= "%" (substring top -1))
  437.       (setq tpu-top-scroll-margin (string-to-int top))
  438.     (setq tpu-top-scroll-margin
  439.           (/ (1- (+ (* (string-to-int top) 100) (window-height)))
  440.          (window-height)))))
  441.   ;; set bottom scroll margin
  442.   (or (string= bottom "")
  443.       (if (string= "%" (substring bottom -1))
  444.       (setq tpu-bottom-scroll-margin (string-to-int bottom))
  445.     (setq tpu-bottom-scroll-margin
  446.           (/ (1- (+ (* (string-to-int bottom) 100) (window-height)))
  447.          (window-height)))))
  448.   ;; report scroll margin settings if running interactively
  449.   (and (interactive-p)
  450.        (message "Scroll margins set.  Top = %s%%, Bottom = %s%%"
  451.         tpu-top-scroll-margin tpu-bottom-scroll-margin)))
  452.  
  453. (fset 'set\ scroll\ margins 'tpu-set-scroll-margins)
  454. (fset 'SET\ SCROLL\ MARGINS 'tpu-set-scroll-margins)
  455.  
  456.  
  457. ;;;  Functions to set cursor bound or free
  458.  
  459. (defun tpu-set-cursor-free nil
  460.   "Allow the cursor to move freely about the screen."
  461.   (interactive)
  462.   (setq tpu-cursor-free t)
  463.   (substitute-key-definition 'tpu-set-cursor-free
  464.                  'tpu-set-cursor-bound
  465.                  GOLD-map)
  466.   (message "The cursor will now move freely about the screen."))
  467.  
  468. (defun tpu-set-cursor-bound nil
  469.   "Constrain the cursor to the flow of the text."
  470.   (interactive)
  471.   (picture-clean)
  472.   (setq tpu-cursor-free nil)
  473.   (substitute-key-definition 'tpu-set-cursor-bound
  474.                  'tpu-set-cursor-free
  475.                  GOLD-map)
  476.   (message "The cursor is now bound to the flow of your text."))
  477.  
  478. (fset 'set\ cursor\ bound 'tpu-set-cursor-bound)
  479. (fset 'SET\ CURSOR\ BOUND 'tpu-set-cursor-bound)
  480. (fset 'set\ cursor\ free 'tpu-set-cursor-free)
  481. (fset 'SET\ CURSOR\ FREE 'tpu-set-cursor-free)
  482.  
  483.  
  484. ;;;  Keypad Mapping
  485.  
  486. (define-key GOLD-map "F" 'tpu-set-cursor-free)            ; F
  487. (define-key GOLD-map "f" 'tpu-set-cursor-free)            ; f
  488.  
  489. ;;; tpu-edt-extras.el ends here
  490.