home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / view.el < prev    next >
Encoding:
Text File  |  1993-01-15  |  14.3 KB  |  373 lines

  1. ;; View: Peruse file or buffer without editing.
  2. ;; Copyright (C) 1985-1993 Free Software Foundation, Inc.
  3. ;; Principal author K. Shane Hartman
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11.  
  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.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  19. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  
  22. (defvar view-mode-map nil)
  23. (if view-mode-map
  24.     nil
  25.   (setq view-mode-map (make-keymap))
  26.   (set-keymap-name view-mode-map 'view-mode-map)
  27.   (suppress-keymap view-mode-map)
  28.   (define-key view-mode-map "\C-c" 'view-exit)
  29.   (define-key view-mode-map "\C-z" 'suspend-emacs)
  30.   (define-key view-mode-map "q" 'view-exit)
  31.   (define-key view-mode-map "<" 'beginning-of-buffer)
  32.   (define-key view-mode-map ">" 'end-of-buffer)
  33.   (define-key view-mode-map "\M-v" 'View-scroll-lines-backward)
  34.   (define-key view-mode-map "\C-v" 'View-scroll-lines-forward)
  35.   (define-key view-mode-map " " 'View-scroll-lines-forward)
  36.   (define-key view-mode-map "\177" 'View-scroll-lines-backward)
  37.   (define-key view-mode-map 'backspace 'View-scroll-lines-backward)
  38.   (define-key view-mode-map "\n" 'View-scroll-one-more-line)
  39.   (define-key view-mode-map "\r" 'View-scroll-one-more-line)
  40.   (define-key view-mode-map "z" 'View-scroll-lines-forward-set-scroll-size)
  41.   (define-key view-mode-map "g" 'View-goto-line)
  42.   (define-key view-mode-map "=" 'what-line)
  43.   (define-key view-mode-map "." 'set-mark-command)
  44.   (define-key view-mode-map "'" 'View-back-to-mark)
  45.   (define-key view-mode-map "@" 'View-back-to-mark)  
  46.   (define-key view-mode-map "x" 'exchange-point-and-mark)
  47.   (define-key view-mode-map "h" 'Helper-describe-bindings)
  48.   (define-key view-mode-map "?" 'Helper-describe-bindings)
  49. ;;  (define-key view-mode-map "\C-h" 'Helper-help)
  50.   (define-key view-mode-map '(control h) 'Helper-help)
  51.   (define-key view-mode-map "s" 'isearch-forward)
  52.   (define-key view-mode-map "r" 'isearch-backward)
  53.   (define-key view-mode-map "/" 'View-search-regexp-forward)
  54.   (define-key view-mode-map "\\" 'View-search-regexp-backward)
  55.   ;; This conflicts with the standard binding of isearch-regexp-forward
  56.   (define-key view-mode-map "\M-\C-s" 'View-search-regexp-forward)
  57.   (define-key view-mode-map "\M-\C-r" 'View-search-regexp-backward)  
  58.   (define-key view-mode-map "n" 'View-search-last-regexp-forward)
  59.   (define-key view-mode-map "p" 'View-search-last-regexp-backward)
  60.   )
  61.  
  62. ;; view-mode buffer-local vars
  63. (defvar view-old-mode-line-buffer-identification)
  64. (defvar view-old-buffer-read-only)
  65. (defvar view-old-mode-name)
  66. (defvar view-old-major-mode)
  67. (defvar view-old-local-map)
  68. (defvar view-old-Helper-return-blurb)
  69. (defvar view-exit-action)
  70. (defvar view-prev-buffer)
  71. (defvar view-exit-position)
  72. (defvar view-scroll-size)
  73. (defvar view-last-regexp)
  74.  
  75. (defun view-file (file-name)
  76.   "View FILE in View mode, returning to previous buffer when done.
  77. The usual Emacs commands are not available; instead,
  78. a special set of commands (mostly letters and punctuation)
  79. are defined for moving around in the buffer.
  80. Space scrolls forward, Delete scrolls backward.
  81. For list of all View commands, type ? or h while viewing.
  82.  
  83. Calls the value of  view-hook  if that is non-nil."
  84.   (interactive "fView file: ")
  85.   (let ((old-buf (current-buffer))
  86.     (had-a-buf (get-file-buffer file-name))
  87.     (buf-to-view (find-file-noselect file-name)))
  88.     (switch-to-buffer buf-to-view t)
  89.     (view-mode old-buf
  90.            (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
  91.             'kill-buffer))))
  92.  
  93. (defun view-buffer (buffer-name)
  94.   "View BUFFER in View mode, returning to previous buffer when done.
  95. The usual Emacs commands are not available; instead,
  96. a special set of commands (mostly letters and punctuation)
  97. are defined for moving around in the buffer.
  98. Space scrolls forward, Delete scrolls backward.
  99. For list of all View commands, type ? or h while viewing.
  100.  
  101. Calls the value of  view-hook  if that is non-nil."
  102.   (interactive "bView buffer: ")
  103.   (let ((old-buf (current-buffer)))
  104.     (switch-to-buffer buffer-name t)
  105.     (view-mode old-buf nil)))
  106.  
  107. (defun view-mode (&optional prev-buffer action)
  108.   "Major mode for viewing text but not editing it.
  109. Letters do not insert themselves.  Instead these commands are provided.
  110. Most commands take prefix arguments.  Commands dealing with lines
  111. default to \"scroll size\" lines (initially size of window).
  112. Search commands default to a repeat count of one.
  113. M-< or <    move to beginning of buffer.
  114. M-> or >    move to end of buffer.
  115. C-v or Space    scroll forward lines.
  116. M-v or DEL    scroll backward lines.
  117. CR or LF    scroll forward one line (backward with prefix argument).
  118. z        like Space except set number of lines for further
  119.            scrolling commands to scroll by.
  120. C-u and Digits    provide prefix arguments.  `-' denotes negative argument.
  121. =        prints the current line number.
  122. g        goes to line given by prefix argument.
  123. / or M-C-s    searches forward for regular expression
  124. \\ or M-C-r    searches backward for regular expression.
  125. n        searches forward for last regular expression.
  126. p        searches backward for last regular expression.
  127. C-@ or .    set the mark.
  128. x        exchanges point and mark.
  129. C-s or s    do forward incremental search.
  130. C-r or r    do reverse incremental search.
  131. @ or '        return to mark and pops mark ring.
  132.           Mark ring is pushed at start of every
  133.           successful search and when jump to line to occurs.
  134.           The mark is set on jump to buffer start or end.
  135. ? or h        provide help message (list of commands).
  136. C-h        provides help (list of commands or description of a command).
  137. C-n        moves down lines vertically.
  138. C-p        moves upward lines vertically.
  139. C-l        recenters the screen.
  140. q or C-c    exit view-mode and return to previous buffer.
  141.  
  142. Entry to this mode calls the value of  view-hook  if non-nil.
  143. \\{view-mode-map}"
  144. ;  Not interactive because dangerous things happen
  145. ;  if you call it without passing a buffer as argument
  146. ;  and they are not easy to fix.
  147. ;  (interactive)
  148.   (make-local-variable 'view-old-mode-line-buffer-identification)
  149.   (setq view-old-mode-line-buffer-identification
  150.     mode-line-buffer-identification)
  151.   (make-local-variable 'view-old-buffer-read-only)
  152.   (setq view-old-buffer-read-only buffer-read-only)
  153.   (make-local-variable 'view-old-mode-name)
  154.   (setq view-old-mode-name mode-name)
  155.   (make-local-variable 'view-old-major-mode)
  156.   (setq view-old-major-mode major-mode)
  157.   (make-local-variable 'view-old-local-map)
  158.   (setq view-old-local-map (current-local-map))
  159.   (make-local-variable 'view-old-Helper-return-blurb)
  160.   (setq view-old-Helper-return-blurb
  161.     (and (boundp 'Helper-return-blurb) Helper-return-blurb))
  162.  
  163.   (setq buffer-read-only t)
  164.   (setq mode-line-buffer-identification
  165.     (list
  166.      (if (buffer-file-name)
  167.          "Viewing %f"
  168.        "Viewing %b")))
  169.   (setq mode-name "View")
  170.   (setq major-mode 'view-mode)
  171.   (setq Helper-return-blurb
  172.     (format "continue viewing %s"
  173.         (if (buffer-file-name)
  174.             (file-name-nondirectory (buffer-file-name))
  175.             (buffer-name))))
  176.  
  177.   (make-local-variable 'view-exit-action)
  178.   (setq view-exit-action action)
  179.   (make-local-variable 'view-prev-buffer)
  180.   (setq view-prev-buffer prev-buffer)
  181.   (make-local-variable 'view-exit-position)
  182.   (setq view-exit-position (point-marker))
  183.  
  184.   (make-local-variable 'view-scroll-size)
  185.   (setq view-scroll-size nil)
  186.   (make-local-variable 'view-last-regexp)
  187.   (setq view-last-regexp nil)
  188.  
  189.   (beginning-of-line)
  190.   (setq goal-column nil)
  191.  
  192.   (use-local-map view-mode-map)
  193.   (run-hooks 'view-hook)
  194.   (view-helpful-message))
  195.  
  196. (defun view-exit ()
  197.   "Exit from view-mode.
  198. If you viewed an existing buffer, that buffer returns to its previous mode.
  199. If you viewed a file that was not present in Emacs, its buffer is killed."
  200.   (interactive)
  201.   (setq mode-line-buffer-identification
  202.     view-old-mode-line-buffer-identification)
  203.   (setq major-mode view-old-major-mode)
  204.   (setq mode-name view-old-mode-name)
  205.   (use-local-map (current-local-map))
  206.   (setq buffer-read-only view-old-buffer-read-only)
  207.  
  208.   (goto-char view-exit-position)
  209.   (set-marker view-exit-position nil)
  210.  
  211.   ;; Now do something to the buffer that we were viewing
  212.   ;; (such as kill it).
  213.   (let ((viewed-buffer (current-buffer))
  214.     (action view-exit-action))
  215.     (switch-to-buffer view-prev-buffer)
  216.     (if action (funcall action viewed-buffer))))
  217.  
  218. (defun view-helpful-message ()
  219.   (message
  220.    (if (and (eq (key-binding "\C-h") 'Helper-help)
  221.         (eq (key-binding "?") 'Helper-describe-bindings)
  222.         (eq (key-binding "\C-c") 'view-exit))
  223.        "Type C-h for help, ? for commands, C-c to quit"
  224.      (substitute-command-keys
  225.       "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[exit-recursive-edit] to quit."))))
  226.  
  227. ;(defun View-undefined ()
  228. ;  (interactive)
  229. ;  (ding)
  230. ;  (view-helpful-message))
  231.  
  232. (defun view-window-size () (1- (window-height)))
  233.  
  234. (defun view-scroll-size ()
  235.   (min (view-window-size) (or view-scroll-size (view-window-size))))
  236.  
  237. (defvar view-hook nil
  238.   "If non-nil, its value is called when viewing buffer or file.")
  239.  
  240. ;(defun view-last-command (&optional who what)
  241. ;  (setq view-last-command-entry this-command)
  242. ;  (setq view-last-command who)
  243. ;  (setq view-last-command-argument what))
  244.  
  245. ;(defun View-repeat-last-command ()
  246. ;  "Repeat last command issued in View mode."
  247. ;  (interactive)
  248. ;  (if (and view-last-command
  249. ;       (eq view-last-command-entry last-command))
  250. ;      (funcall view-last-command view-last-command-argument))
  251. ;  (setq this-command view-last-command-entry))
  252.  
  253. (defun View-goto-line (&optional line)
  254.   "Move to LINE in View mode.
  255. Display is centered at LINE.  Sets mark at starting position and pushes
  256. mark ring."
  257.   (interactive "p")
  258.   (push-mark)
  259.   (goto-line (or line 1))
  260.   (recenter (/ (view-window-size) 2)))
  261.  
  262. (defun View-scroll-lines-forward (&optional lines)
  263.   "Scroll forward in View mode, or exit if end of text is visible.
  264. No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
  265. Arg is number of lines to scroll."
  266.   (interactive "P")
  267.   (if (pos-visible-in-window-p (point-max))
  268.       (exit-recursive-edit))
  269.   (setq lines
  270.     (if lines (prefix-numeric-value lines)
  271.       (view-scroll-size)))
  272. ; (view-last-command 'View-scroll-lines-forward lines)
  273.   (if (>= lines (view-window-size))
  274.       (scroll-up nil)
  275.     (if (>= (- lines) (view-window-size))
  276.     (scroll-down nil)
  277.       (scroll-up lines)))
  278.   (cond ((pos-visible-in-window-p (point-max))
  279.      (goto-char (point-max))
  280.      (recenter -1)
  281.      (message (substitute-command-keys
  282.         "End.  Type \\[exit-recursive-edit] to quit viewing."))))
  283.   (move-to-window-line -1)
  284.   (beginning-of-line))
  285.  
  286. (defun View-scroll-lines-forward-set-scroll-size (&optional lines)
  287.   "Scroll forward LINES lines in View mode, setting the \"scroll size\".
  288. This is the number of lines which \\[View-scroll-lines-forward] and \\[View-scroll-lines-backward] scroll by default.
  289. The absolute value of LINES is used, so this command can be used to scroll
  290. backwards (but \"scroll size\" is always positive).  If LINES is greater than
  291. window height or omitted, then window height is assumed.  If LINES is less
  292. than window height then scrolling context is provided from previous screen."
  293.   (interactive "P")
  294.   (if (not lines)
  295.       (setq view-scroll-size (view-window-size))
  296.     (setq lines (prefix-numeric-value lines))
  297.     (setq view-scroll-size
  298.       (min (if (> lines 0) lines (- lines)) (view-window-size))))
  299.   (View-scroll-lines-forward lines))
  300.  
  301. (defun View-scroll-one-more-line (&optional arg)
  302.   "Scroll one more line up in View mode.
  303. With ARG scroll one line down."
  304.   (interactive "P")
  305.   (View-scroll-lines-forward (if (not arg) 1 -1)))
  306.  
  307. (defun View-scroll-lines-backward (&optional lines)
  308.   "Scroll backward in View mode.
  309. No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
  310. Arg is number of lines to scroll."
  311.   (interactive "P")
  312.   (View-scroll-lines-forward (if lines
  313.                  (- (prefix-numeric-value lines))
  314.                    (- (view-scroll-size)))))
  315.   
  316. (defun View-search-regexp-forward (times regexp)
  317.   "Search forward for NTH occurrence of REGEXP in View mode.
  318. Displays line found at center of window.  REGEXP is remembered for
  319. searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring."
  320.   (interactive "p\nsSearch forward (regexp): ")
  321.   (if (> (length regexp) 0)
  322.       (progn
  323.        ;(view-last-command 'View-search-last-regexp-forward times)
  324.     (view-search times regexp))))
  325.  
  326. (defun View-search-regexp-backward (times regexp)
  327.   "Search backward from window start for NTH instance of REGEXP in View mode.
  328. Displays line found at center of window.  REGEXP is remembered for
  329. searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring."
  330.   (interactive "p\nsSearch backward (regexp): ")
  331.   (View-search-regexp-forward (- times) regexp))
  332.  
  333. (defun View-search-last-regexp-forward (times)
  334.   "Search forward from window end for NTH instance of last regexp in View mode.
  335. Displays line found at center of window.  Sets mark at starting position
  336. and pushes mark ring."
  337.   (interactive "p")
  338.   (View-search-regexp-forward times view-last-regexp))
  339.  
  340. (defun View-search-last-regexp-backward (times)
  341.   "Search backward from window start for NTH instance of last regexp in View mode.
  342. Displays line found at center of window.  Sets mark at starting position and
  343. pushes mark ring."
  344.   (interactive "p")
  345.   (View-search-regexp-backward times view-last-regexp))
  346.  
  347. (defun View-back-to-mark (&optional ignore)
  348.   "Return to last mark set in View mode, else beginning of file.
  349. Displays line at center of window.  Pops mark ring so successive
  350. invocations return to earlier marks."
  351.   (interactive)
  352.   (goto-char (or (mark) (point-min)))
  353.   (pop-mark)
  354.   (recenter (/ (view-window-size) 2)))
  355.          
  356. (defun view-search (times regexp)
  357.   (setq view-last-regexp regexp)
  358.   (let (where)
  359.     (save-excursion
  360.       (move-to-window-line (if (< times 0) 0 -1))
  361.       (if (re-search-forward regexp nil t times)
  362.       (setq where (point))))
  363.     (if where
  364.     (progn
  365.       (push-mark)
  366.       (goto-char where)
  367.       (beginning-of-line)
  368.       (recenter (/ (view-window-size) 2)))
  369.       (message "Can't find occurrence %d of %s" times regexp)
  370.       (sit-for 4))))
  371.  
  372. (provide 'view)
  373.