home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / modes / new-view.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  17.5 KB  |  453 lines

  1. ;From utkcs2!emory!sol.ctr.columbia.edu!cica!tut.cis.ohio-state.edu!somewhere!aks Mon Jun  4 12:57:25 EDT 1990
  2. ;Article 2874 of gnu.emacs:
  3. ;Path: utkcs2!emory!sol.ctr.columbia.edu!cica!tut.cis.ohio-state.edu!somewhere!aks
  4. ;>From: aks@somewhere (Alan Stebbens)
  5. ;Newsgroups: gnu.emacs
  6. ;Subject: New view.el
  7. ;Message-ID: <9006011029.AA01818@somewhere>
  8. ;Date: 1 Jun 90 10:29:08 GMT
  9. ;Sender: daemon@tut.cis.ohio-state.edu
  10. ;Distribution: gnu
  11. ;Organization: GNUs Not Usenet
  12. ;Lines: 437
  13. ;
  14. ;Rather than reinvent the wheel, I decided to modify the existing code in
  15. ;view.el to Do The Right Thing, which is:
  16. ;
  17. ;  o  NOT automatically exit view-mode when scrolling off the end of the
  18. ;     buffer.
  19. ;
  20. ;  o  a new command function: electric-view-buffer, which pops up a
  21. ;     window, on a buffer, runs view-mode, then restores the previous
  22. ;     window configuration.
  23. ;
  24. ;  o  a lot of documentation formatting and cleanup, which, admittedly
  25. ;     inducing differences, does make it nicer to read.
  26. ;
  27. ;Because of the last, I decided to submit the entire source, rather than
  28. ;the diffs.  You are welcome to create your own diffs :^)
  29. ;
  30. ;Comments, suggestions, and fixes are welcome.
  31. ;
  32. ;Alan Stebbens        <aks@hub.ucsb.edu>             (805) 961-3221
  33. ;     Center for Computational Sciences and Engineering (CCSE)
  34. ;          University of California, Santa Barbara (UCSB)
  35. ;           3111 Engineering I, Santa Barbara, CA 93106
  36. ;
  37. ;============================= cut here ===================================
  38. ;; View: Peruse file or buffer without editing.
  39. ;; Copyright (C) 1985-89, 1990 Free Software Foundation, Inc.
  40. ;; Principal author K. Shane Hartman
  41. ;; Modifications by Alan K. Stebbens
  42.  
  43. ;; This file is part of GNU Emacs.
  44.  
  45. ;; GNU Emacs is distributed in the hope that it will be useful,
  46. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  47. ;; accepts responsibility to anyone for the consequences of using it
  48. ;; or for whether it serves any particular purpose or works at all,
  49. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  50. ;; License for full details.
  51.  
  52. ;; Everyone is granted permission to copy, modify and redistribute
  53. ;; GNU Emacs, but only under the conditions described in the
  54. ;; GNU Emacs General Public License.   A copy of this license is
  55. ;; supposed to have been given to you along with GNU Emacs so you
  56. ;; can know your rights and responsibilities.  It should be in a
  57. ;; file named COPYING.  Among other things, the copyright notice
  58. ;; and this notice must be preserved on all copies.
  59.  
  60. ;; Last Edited:
  61. ;; 
  62. ;; Thu May 31 18:27:07 1990 by Alan Stebbens (aks at somewhere.ucsb.edu)
  63. ;;
  64. ;;     Created electric-view-buffer.
  65. ;;
  66. ;;      Added View-scroll-auto-exit variable to en/dis-able the automatic
  67. ;;      exit when scrolling off the end of the buffer.
  68. ;;
  69. ;;     Made view-mode the major-mode temporarily, so C-h m would work 
  70. ;;       correctly.
  71. ;;
  72. ;;     Reformatted the keymap to improve readability.
  73. ;;
  74. ;;     Changed the definition of \C-z to be the same as the global def, in
  75. ;;      case the user has "tailored" his suspend-emacs function, or
  76. ;;      disabled it.
  77. ;;
  78. ;;     Changed the "\e" keymaps to "\M-" in case the meta-prefix character
  79. ;;      is _ever_ changed.
  80. ;;
  81. ;;      Thought about enabling the commented-out stuff which supports last
  82. ;;      command repeats (like "." in `vi'?), but didn't: it looked too hairy.
  83.  
  84. (provide 'view)
  85.  
  86. (defvar View-scroll-auto-exit nil "\
  87. *If non-nil, automatically exit view-mode when scrolling past either end
  88. of the buffer.  If nil, attempting to scroll past the end of the buffer
  89. does nothing.")
  90.  
  91. (defvar view-mode-map nil)
  92. (if view-mode-map
  93.     nil
  94.   (setq view-mode-map (make-keymap))
  95.   (fillarray view-mode-map 'View-undefined)
  96.   (define-key view-mode-map "\C-c"    'exit-recursive-edit)
  97.   (define-key view-mode-map "\C-z"    (global-key-binding "\C-z"))
  98.   (define-key view-mode-map "q"        'exit-recursive-edit)
  99.   (define-key view-mode-map "-"        'negative-argument)
  100.   (define-key view-mode-map "0"        'digit-argument)
  101.   (define-key view-mode-map "1"        'digit-argument)
  102.   (define-key view-mode-map "2"        'digit-argument)
  103.   (define-key view-mode-map "3"        'digit-argument)
  104.   (define-key view-mode-map "4"        'digit-argument)
  105.   (define-key view-mode-map "5"        'digit-argument)
  106.   (define-key view-mode-map "6"        'digit-argument)
  107.   (define-key view-mode-map "7"        'digit-argument)
  108.   (define-key view-mode-map "8"        'digit-argument)
  109.   (define-key view-mode-map "9"        'digit-argument)
  110.   (define-key view-mode-map "\C-u"    'universal-argument)
  111.   (define-key view-mode-map "\e"     nil)
  112.   (define-key view-mode-map "\C-x"    'Control-X-prefix)
  113.   (define-key view-mode-map "\M--"    'negative-argument)
  114.   (define-key view-mode-map "\M-0"    'digit-argument)
  115.   (define-key view-mode-map "\M-1"    'digit-argument)
  116.   (define-key view-mode-map "\M-2"    'digit-argument)
  117.   (define-key view-mode-map "\M-3"    'digit-argument)
  118.   (define-key view-mode-map "\M-4"    'digit-argument)
  119.   (define-key view-mode-map "\M-5"    'digit-argument)
  120.   (define-key view-mode-map "\M-6"    'digit-argument)
  121.   (define-key view-mode-map "\M-7"    'digit-argument)
  122.   (define-key view-mode-map "\M-8"    'digit-argument)
  123.   (define-key view-mode-map "\M-9"    'digit-argument)
  124.   (define-key view-mode-map "<"        'beginning-of-buffer)
  125.   (define-key view-mode-map ">"        'end-of-buffer)
  126.   (define-key view-mode-map "\M-v"    'View-scroll-lines-backward)
  127.   (define-key view-mode-map "\C-v"    'View-scroll-lines-forward)
  128.   (define-key view-mode-map " "        'View-scroll-lines-forward)
  129.   (define-key view-mode-map "\177"    'View-scroll-lines-backward)
  130.   (define-key view-mode-map "\n"    'View-scroll-one-more-line)
  131.   (define-key view-mode-map "\r"    'View-scroll-one-more-line)
  132.   (define-key view-mode-map "\C-l"    'recenter)
  133.   (define-key view-mode-map "z"        'View-scroll-lines-forward-set-scroll-size)
  134.   (define-key view-mode-map "g"        'View-goto-line)
  135.   (define-key view-mode-map "="        'what-line)
  136.   (define-key view-mode-map "."        'set-mark-command)
  137.   (define-key view-mode-map "\C-@"    'set-mark-command)
  138.   (define-key view-mode-map "'"        'View-back-to-mark)
  139.   (define-key view-mode-map "@"        'View-back-to-mark)  
  140.   (define-key view-mode-map "x"        'exchange-point-and-mark)
  141.   (define-key view-mode-map "h"        'Helper-describe-bindings)
  142.   (define-key view-mode-map "?"        'Helper-describe-bindings)
  143.   (define-key view-mode-map "\C-h"    'Helper-help)
  144.   (define-key view-mode-map "\C-n"    'next-line)
  145.   (define-key view-mode-map "\C-p"    'previous-line)
  146.   (define-key view-mode-map "\C-s"    'isearch-forward)
  147.   (define-key view-mode-map "\C-r"    'isearch-backward)
  148.   (define-key view-mode-map "s"        'isearch-forward)
  149.   (define-key view-mode-map "r"        'isearch-backward)
  150.   (define-key view-mode-map "/"        'View-search-regexp-forward)
  151.   (define-key view-mode-map "\\"    'View-search-regexp-backward)
  152.   ;; This conflicts with the standard binding of isearch-regexp-forward
  153.   (define-key view-mode-map "\M-\C-s"    'View-search-regexp-forward)
  154.   (define-key view-mode-map "\M-\C-r"    'View-search-regexp-backward)  
  155.   (define-key view-mode-map "n"        'View-search-last-regexp-forward)
  156.   (define-key view-mode-map "p"        'View-search-last-regexp-backward)
  157.   )
  158.  
  159. ;; view-file
  160.  
  161. (defun view-file (file-name)
  162.   "View FILE in View mode, returning to previous buffer when done.
  163. The usual Emacs commands are not available; instead,
  164. a special set of commands (mostly letters and punctuation)
  165. are defined for moving around in the buffer.
  166. Space scrolls forward, Delete scrolls backward.
  167. For list of all View commands, type ? or h while viewing.
  168.  
  169. Calls the value of  view-hook  if that is non-nil."
  170.   (interactive "fView file: ")
  171.   (let ((had-a-buf (get-file-buffer file-name))
  172.     (buf-to-view nil))
  173.     (unwind-protect
  174.     (view-mode (prog1 (current-buffer)
  175.              (switch-to-buffer
  176.               (setq buf-to-view (find-file-noselect file-name)) t)))
  177.       (and (not had-a-buf) buf-to-view (not (buffer-modified-p buf-to-view))
  178.        (kill-buffer buf-to-view)))))
  179.  
  180. ;; view-buffer
  181.  
  182. (defun view-buffer (buffer-name) "\
  183. View BUFFER in View mode, returning to previous buffer when done.  The
  184. usual Emacs commands are not available; instead, a special set of
  185. commands (mostly letters and punctuation) are defined for moving
  186. around in the buffer.  Space scrolls forward, Delete scrolls backward.
  187. For list of all View commands, type ? or h while viewing.
  188. Calls the value of  view-hook  if that is non-nil."
  189.   (interactive "bView buffer: ")
  190.   (view-mode (prog1 (current-buffer) (switch-to-buffer buffer-name))))
  191.  
  192. ;; electric-view-buffer
  193.  
  194. (defun electric-view-buffer (buffer) "\
  195. View BUFFER in View mode in a new window, restoring the previous window
  196. configuration when done."
  197.   (interactive "bView buffer: ")
  198.   (save-window-excursion
  199.     (save-excursion
  200.       (let ((pop-up-windows t)
  201.         lines height)
  202.     (pop-to-buffer buffer))
  203.       (if (< (setq lines (count-lines (point-min) (point-max)))
  204.          (setq height (1- (window-height))))
  205.       (shrink-window (- height lines)))
  206.       (view-mode))))
  207.       
  208. ;; view-mode
  209.  
  210. (defun view-mode (&optional view-return-to-buffer)
  211.   "Major mode for viewing text but not editing it.  Letters do not insert
  212. themselves.  Instead these commands are provided.  Most commands take
  213. prefix arguments.  Commands dealing with lines default to \"scroll
  214. size\" lines (initially size of window).  Search commands default to a
  215. repeat count of one.
  216.  
  217. M-< or <    move to beginning of buffer.
  218. M-> or >    move to end of buffer.
  219. C-v or Space    scroll forward lines.
  220. M-v or DEL    scroll backward lines.
  221. CR or LF    scroll forward one line (backward with prefix argument).
  222. z        like Space except set number of lines for further
  223.            scrolling commands to scroll by.
  224. C-u and Digits    provide prefix arguments.  `-' denotes negative argument.
  225. =        prints the current line number.
  226. g        goes to line given by prefix argument.
  227. / or M-C-s    searches forward for regular expression
  228. \\ or M-C-r    searches backward for regular expression.
  229. n        searches forward for last regular expression.
  230. p        searches backward for last regular expression.
  231. C-@ or .    set the mark.
  232. x        exchanges point and mark.
  233. C-s or s    do forward incremental search.
  234. C-r or r    do reverse incremental search.
  235. @ or '        return to mark and pops mark ring.
  236.           Mark ring is pushed at start of every
  237.           successful search and when jump to line to occurs.
  238.           The mark is set on jump to buffer start or end.
  239. ? or h        provide help message (list of commands).
  240. C-h        provides help (list of commands or description of a command).
  241. C-n        moves down lines vertically.
  242. C-p        moves upward lines vertically.
  243. C-l        recenters the screen.
  244. q or C-c    exit view-mode and return to previous buffer.
  245.  
  246. Entry to this mode calls the value of  view-hook  if non-nil.
  247. \\{view-mode-map}"
  248. ;  Not interactive because dangerous things happen
  249. ;  if you call it without passing a buffer as argument
  250. ;  and they are not easy to fix.
  251. ;  (interactive)
  252.   (let* ((view-buffer-window (selected-window))
  253.      (view-scroll-size nil))
  254.     (unwind-protect
  255.     (let ((buffer-read-only t)
  256.           (mode-line-buffer-identification
  257.            (list
  258.         (if (buffer-file-name)
  259.             "Viewing %f"
  260.           "Viewing %b")))
  261.           (mode-name "View")
  262.           (major-mode 'view-mode))
  263.       (beginning-of-line)
  264.       (catch 'view-mode-exit (view-mode-command-loop)))
  265.       (if view-return-to-buffer
  266.       (switch-to-buffer view-return-to-buffer)))))
  267.  
  268. (defun view-helpful-message ()
  269.   (message
  270.    (if (and (eq (key-binding "\C-h") 'Helper-help)
  271.         (eq (key-binding "?") 'Helper-describe-bindings)
  272.         (eq (key-binding "\C-c") 'exit-recursive-edit))
  273.        "Type C-h for help, ? for commands, C-c to quit"
  274.      (substitute-command-keys
  275.       "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[exit-recursive-edit] to quit."))))
  276.  
  277. (defun View-undefined ()
  278.   (interactive)
  279.   (ding)
  280.   (view-helpful-message))
  281.  
  282. (defun view-window-size () (1- (window-height view-buffer-window)))
  283.  
  284. (defun view-scroll-size ()
  285.   (min (view-window-size) (or view-scroll-size (view-window-size))))
  286.  
  287. (defvar view-hook nil
  288.   "If non-nil, its value is called when viewing buffer or file.")
  289.  
  290. (defun view-mode-command-loop ()
  291.   (push-mark)
  292.   (let ((old-local-map (current-local-map))
  293.     (mark-ring)
  294. ;    (view-last-command)
  295. ;    (view-last-command-entry)
  296. ;    (view-last-command-argument)
  297.     (view-last-regexp)
  298.     (Helper-return-blurb
  299.      (format "continue viewing %s"
  300.          (if (buffer-file-name)
  301.              (file-name-nondirectory (buffer-file-name))
  302.              (buffer-name))))
  303.     (goal-column 0)
  304.     (view-buffer (buffer-name)))
  305.     (unwind-protect
  306.     (progn
  307.       (use-local-map view-mode-map)
  308.       (run-hooks 'view-hook)
  309.       (view-helpful-message)
  310.       (recursive-edit))
  311.       (save-excursion
  312.     (set-buffer view-buffer)
  313.     (use-local-map old-local-map))))
  314.   (pop-mark))
  315.  
  316. ;(defun view-last-command (&optional who what)
  317. ;  (setq view-last-command-entry this-command)
  318. ;  (setq view-last-command who)
  319. ;  (setq view-last-command-argument what))
  320.  
  321. ;(defun View-repeat-last-command ()
  322. ;  "Repeat last command issued in View mode."
  323. ;  (interactive)
  324. ;  (if (and view-last-command
  325. ;       (eq view-last-command-entry last-command))
  326. ;      (funcall view-last-command view-last-command-argument))
  327. ;  (setq this-command view-last-command-entry))
  328.  
  329. (defun View-goto-line (&optional line) "\
  330. Move to LINE in View mode.  Display is centered at LINE.  Sets mark at
  331. starting position and pushes mark ring."
  332.   (interactive "p")
  333.   (push-mark)
  334.   (goto-line (or line 1))
  335.   (recenter (/ (view-window-size) 2)))
  336.  
  337. (defun View-scroll-lines-forward (&optional lines) "\
  338. Scroll forward in View mode, or exit if end of text is visible.  No arg
  339. means whole window full, or number of lines set by
  340. \\[View-scroll-lines-forward-set-scroll-size].  Arg is number of lines
  341. to scroll."
  342.   (interactive "P")
  343.   (setq lines (if lines (prefix-numeric-value lines) (view-scroll-size)))
  344.   (if (and (pos-visible-in-window-p (point-max))
  345.        (>= lines 0))
  346.       (if View-scroll-auto-exit
  347.       (exit-recursive-edit)
  348.     (ding)))
  349. ; (view-last-command 'View-scroll-lines-forward lines)
  350.   (if (>= lines (view-window-size))
  351.       (scroll-up nil)
  352.     (if (>= (- lines) (view-window-size))
  353.     (scroll-down nil)
  354.       (scroll-up lines)))
  355.   (cond ((pos-visible-in-window-p (point-max))
  356.      (goto-char (point-max))
  357.      (recenter -1)
  358.      (message (substitute-command-keys
  359.         "End.  Type \\[exit-recursive-edit] to quit viewing."))))
  360.   (move-to-window-line -1)
  361.   (beginning-of-line))
  362.  
  363. (defun View-scroll-lines-forward-set-scroll-size (&optional lines)
  364.   "Scroll forward LINES lines in View mode, setting the \"scroll size\".
  365. This is the number of lines which \\[View-scroll-lines-forward] and
  366. \\[View-scroll-lines-backward] scroll by default.  The absolute value of
  367. LINES is used, so this command can be used to scroll backwards (but
  368. \"scroll size\" is always positive).  If LINES is greater than window
  369. height or omitted, then window height is assumed.  If LINES is less than
  370. window height then scrolling context is provided from previous screen."
  371.   (interactive "P")
  372.   (if (not lines)
  373.       (setq view-scroll-size (view-window-size))
  374.     (setq lines (prefix-numeric-value lines))
  375.     (setq view-scroll-size
  376.       (min (if (> lines 0) lines (- lines)) (view-window-size))))
  377.   (View-scroll-lines-forward lines))
  378.  
  379. (defun View-scroll-one-more-line (&optional arg)
  380.   "Scroll one more line up in View mode.  With ARG scroll one line down."
  381.   (interactive "P")
  382.   (View-scroll-lines-forward (if (not arg) 1 -1)))
  383.  
  384. (defun View-scroll-lines-backward (&optional lines)  "\
  385. Scroll backward in View mode.  No arg means whole window full, or number
  386. of lines set by \\[View-scroll-lines-forward-set-scroll-size].  Arg is number
  387. of lines to scroll."
  388.   (interactive "P")
  389.   (View-scroll-lines-forward (if lines
  390.                  (- (prefix-numeric-value lines))
  391.                    (- (view-scroll-size)))))
  392.   
  393. (defun View-search-regexp-forward (times regexp)
  394.   "Search forward for NTH occurrence of REGEXP in View mode.  Displays line
  395. found at center of window.  REGEXP is remembered for searching with
  396. \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].
  397. Sets mark at starting position and pushes mark ring."
  398.   (interactive "p\nsSearch forward (regexp): ")
  399.   (if (> (length regexp) 0)
  400.       (progn
  401.        ;(view-last-command 'View-search-last-regexp-forward times)
  402.     (view-search times regexp))))
  403.  
  404. (defun View-search-regexp-backward (times regexp)
  405.   "Search backward from window start for NTH instance of REGEXP in View
  406. mode.  Displays line found at center of window.  REGEXP is remembered
  407. for searching with \\[View-search-last-regexp-forward] and
  408. \\[View-search-last-regexp-backward].  Sets mark at starting position
  409. and pushes mark ring."
  410.   (interactive "p\nsSearch backward (regexp): ")
  411.   (View-search-regexp-forward (- times) regexp))
  412.  
  413. (defun View-search-last-regexp-forward (times)
  414.   "Search forward from window end for NTH instance of last regexp in View mode.
  415. Displays line found at center of window.  Sets mark at starting position
  416. and pushes mark ring."
  417.   (interactive "p")
  418.   (View-search-regexp-forward times view-last-regexp))
  419.  
  420. (defun View-search-last-regexp-backward (times)
  421.   "Search backward from window start for NTH instance of last regexp in View mode.
  422. Displays line found at center of window.  Sets mark at starting position and
  423. pushes mark ring."
  424.   (interactive "p")
  425.   (View-search-regexp-backward times view-last-regexp))
  426.  
  427. (defun View-back-to-mark (&optional ignore)
  428.   "Return to last mark set in View mode, else beginning of file.
  429. Displays line at center of window.  Pops mark ring so successive
  430. invocations return to earlier marks."
  431.   (interactive)
  432.   (goto-char (or (mark) (point-min)))
  433.   (pop-mark)
  434.   (recenter (/ (view-window-size) 2)))
  435.          
  436. (defun view-search (times regexp)
  437.   (setq view-last-regexp regexp)
  438.   (let (where)
  439.     (save-excursion
  440.       (move-to-window-line (if (< times 0) 0 -1))
  441.       (if (re-search-forward regexp nil t times)
  442.       (setq where (point))))
  443.     (if where
  444.     (progn
  445.       (push-mark)
  446.       (goto-char where)
  447.       (beginning-of-line)
  448.       (recenter (/ (view-window-size) 2)))
  449.       (message "Can't find occurrence %d of %s" times regexp)
  450.       (sit-for 4))))
  451.  
  452.  
  453.