1 ;;; view-less.el --- Minor mode for browsing files with keybindings like `less'
3 ;; Copyright (C) 1994, 1995 Tinker Systems and INS Engineering Corp.
5 ;; Author: Jonathan Stigelman <stig@hackvan.com>
6 ;; Maintainer: XEmacs Development Team
9 ;; This file is part of XEmacs.
11 ;; XEmacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2 of the License, or
14 ;; (at your option) any later version.
16 ;; XEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; if not, write to the Free Software
23 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;;; Synched up with: Not in FSF.
29 ;; This mode is for browsing files without changing them. Keybindings
30 ;; similar to those used by the less(1) program are used.
32 ;; Originally written for v18 by David Gudeman (gudeman@arizona.edu)
33 ;; Mods by Bengt Martensson, to closely resemble less (July 1987)
35 ;; If you would like all write-protected files to be visited in view-mode,
36 ;; then add the following to your .emacs file:
38 ;; (add-hook 'find-file-hooks 'auto-view-mode)
42 (defvar view-search-string ""
43 "Last string searched for with view-search functions.")
45 (defvar view-search-arg 1
46 "Argument to last view search.")
48 (defvar view-default-lines 10
49 "Default value for the \"d\" and \"u\" commands in view-mode")
51 (defvar view-minor-mode nil
52 "Non-nil when view-mode is active. Call `view-mode' to toggle.")
53 (make-variable-buffer-local 'view-minor-mode)
56 (defvar view-minor-mode-map
57 (let ((map (make-keymap)))
58 (set-keymap-name map 'view-minor-mode-map)
60 (define-key map "-" 'negative-argument)
61 (define-key map " " 'scroll-up)
62 (define-key map "f" 'scroll-up)
63 (define-key map "b" 'scroll-down)
64 (define-key map 'backspace 'scroll-down)
65 (define-key map 'delete 'scroll-down)
66 (define-key map "\r" 'view-scroll-lines-up)
67 (define-key map "\n" 'view-scroll-lines-up)
68 (define-key map "e" 'view-scroll-lines-up)
69 (define-key map "j" 'view-scroll-lines-up)
70 (define-key map "y" 'view-scroll-lines-down)
71 (define-key map "k" 'view-scroll-lines-down)
72 (define-key map "d" 'view-scroll-some-lines-up)
73 (define-key map "u" 'view-scroll-some-lines-down)
74 (define-key map "r" 'recenter)
75 (define-key map "t" 'toggle-truncate-lines)
76 (define-key map "N" 'view-buffer)
77 (define-key map "E" 'view-file)
78 (define-key map "P" 'view-buffer)
79 (define-key map "!" 'shell-command)
80 (define-key map "|" 'shell-command-on-region)
81 (define-key map "=" 'what-line)
82 (define-key map "?" 'view-search-backward)
83 (define-key map "h" 'view-mode-describe)
84 (define-key map "s" 'view-repeat-search)
85 (define-key map "n" 'view-repeat-search)
86 (define-key map "/" 'view-search-forward)
87 (define-key map "\\" 'view-search-backward)
88 (define-key map "g" 'view-goto-line)
89 (define-key map "G" 'view-last-windowful)
90 (define-key map "%" 'view-goto-percent)
91 (define-key map "p" 'view-goto-percent)
92 (define-key map "m" 'point-to-register)
93 (define-key map "'" 'register-to-point)
94 (define-key map "C" 'view-cleanup-backspaces)
95 (define-key map "\C-c\C-c" 'view-quit)
96 ;; #### - should this use substitute-command-keys?
97 (define-key map "\C-x\C-q" 'view-quit-toggle-ro)
98 (define-key map "q" 'view-quit)
102 (add-minor-mode 'view-minor-mode " View" view-minor-mode-map)
105 (defvar view-mode-map
106 (let ((map (copy-keymap view-minor-mode-map)))
107 (set-keymap-name map 'view-mode-map)
111 (defun view-file (file &optional other-p)
112 "Find FILE, enter view mode. With prefix arg OTHER-P, use other window."
113 (interactive "fView File: \nP")
114 (let ((old-p (get-file-buffer file))
115 (obuf (current-buffer)))
117 (find-file-other-window file)
119 (view-mode (if other-p nil obuf)
120 (if old-p nil 'kill-buffer))
124 (defun view-buffer (buf &optional other-p)
125 "Switch to BUF, enter view mode. With prefix arg use other window."
126 (interactive "bView Buffer: \nP")
127 (let ((obuf (current-buffer)))
129 (switch-to-buffer-other-window buf)
130 (switch-to-buffer buf))
131 (view-mode (if other-p nil obuf) (if other-p nil 'bury-buffer))))
134 (defun view-file-other-window (file)
135 "Find FILE in other window, and enter view mode."
136 (interactive "fView File: ")
140 (defun view-buffer-other-window (buffer)
141 "Switch to BUFFER in another window, and enter view mode."
142 (interactive "bView Buffer: ")
143 (view-buffer buffer t))
145 (defun view-brief-help ()
147 (substitute-command-keys
148 "\\<view-minor-mode-map>\\[scroll-up] = page forward; \\[scroll-down] = page back; \
149 \\[view-mode-describe] = help; \\[view-quit] = quit.")))
151 (defvar view-exit-position)
152 (defvar view-prev-buffer)
153 (defvar view-exit-action)
154 (defvar view-old-buffer-read-only)
157 (defun view-minor-mode (&optional prev-buffer exit-action)
158 "Minor mode for viewing text, with bindings like `less'.
160 \\<view-minor-mode-map>
163 \\[scroll-up] page forward
164 \\[scroll-down] page back
165 \\[view-scroll-lines-up] scroll prefix-arg lines forward, default 1.
166 \\[view-scroll-lines-down] scroll prefix-arg lines backward, default 1.
167 \\[view-scroll-some-lines-down] scroll prefix-arg lines backward, default 10.
168 \\[view-scroll-some-lines-up] scroll prefix-arg lines forward, default 10.
169 \\[what-line] print line number
170 \\[view-mode-describe] print this help message
171 \\[view-search-forward] regexp search, uses previous string if you just hit RET
172 \\[view-search-backward] as above but searches backward
173 \\[view-repeat-search] repeat last search
174 \\[view-goto-line] goto line prefix-arg, default 1
175 \\[view-last-windowful] goto line prefix-arg, default last line
176 \\[view-goto-percent] goto a position by percentage
177 \\[toggle-truncate-lines] toggle truncate-lines
178 \\[view-file] view another file
179 \\[view-buffer] view another buffer
180 \\[view-cleanup-backspaces] cleanup backspace constructions
181 \\[shell-command] execute a shell command
182 \\[shell-command-on-region]\
183 execute a shell command with the region as input
184 \\[view-quit] exit view-mode, and bury the current buffer.
186 If invoked with the optional (prefix) arg non-nil, view-mode cleans up
187 backspace constructions.
190 \\{view-minor-mode-map}"
193 (make-local-variable 'view-default-lines)
194 (set (make-local-variable 'view-exit-position) (point))
195 (set (make-local-variable 'view-prev-buffer) prev-buffer)
196 (set (make-local-variable 'view-exit-action) exit-action)
197 (set (make-local-variable 'view-old-buffer-read-only) buffer-read-only)
198 (add-hook (make-local-variable 'change-major-mode-hook)
199 'view-fixup-read-only)
200 (setq view-minor-mode t
205 (defun view-mode (&optional prev-buffer exit-action clean-bs)
206 "View the current buffer using view-minor-mode. This exists to be 99.9%
207 compatible with the implementations of `view-mode' in view.el and older
208 versions of view-less.el."
209 (interactive (list nil 'bury-buffer current-prefix-arg))
210 ;; #### - The first two arguments provide compatibility with view.el (and
211 ;; thus FSFmacs), while the third argument as a prefix argument maintains
212 ;; interactive compatibility with older versions of view-less. --Stig
213 (if clean-bs (cleanup-backspaces))
214 (view-minor-mode prev-buffer exit-action))
217 (defun view-major-mode (&optional prev-buffer exit-action clean-bs)
218 "View the current buffer using view-mode, as a major mode.
219 This function has a nonstandard name because `view-mode' is wrongly
220 named but is like this for compatibility reasons."
221 ;; #### - The first two arguments provide compatibility with view.el (and
222 ;; thus FSFmacs), while the third argument as a prefix argument maintains
223 ;; interactive compatibility with older versions of view-less. --Stig
224 (interactive (list nil 'bury-buffer current-prefix-arg))
225 (kill-all-local-variables)
226 (use-local-map view-mode-map)
227 (setq major-mode 'view-mode)
228 (set (make-local-variable 'view-exit-position) (point))
229 (set (make-local-variable 'view-prev-buffer) prev-buffer)
230 (set (make-local-variable 'view-exit-action) exit-action)
231 (set (make-local-variable 'view-old-buffer-read-only) buffer-read-only)
232 (set (make-local-variable 'view-major-mode) t)
233 (setq buffer-read-only t)
234 (if clean-bs (cleanup-backspaces))
235 (run-hooks 'view-mode-hook))
238 (defun auto-view-mode ()
239 "If the file of the current buffer is not writable, call view-mode.
240 This is meant to be added to `find-file-hooks'."
241 (or (file-writable-p buffer-file-name)
244 (defun view-fixup-read-only ()
245 ;; doing M-x normal mode should NOT leave the buffer read-only
246 (and (boundp 'view-old-buffer-read-only)
247 (progn (setq buffer-read-only view-old-buffer-read-only)
248 (kill-local-variable 'view-old-buffer-read-only))))
250 (defun view-quit-toggle-ro ()
251 "Exit view mode and execute the global binding of the key that invoked this
252 command. Normally, this will toggle the state of `buffer-read-only', perhaps
253 invoking some version-control mechanism."
255 (setq view-exit-position nil)
256 ;; Kludge so this works as advertised. Stig, why can't you write
258 (let ((buffer-read-only buffer-read-only))
260 ;; no longer in view-minor-mode, so the keymap has changed...
261 (call-interactively (key-binding (this-command-keys))))
263 (defun view-quit (&optional no-exit-action)
264 "Exit view mode. With prefix argument, keep the current buffer selected."
266 (view-fixup-read-only)
267 (setq view-minor-mode nil)
268 (if view-exit-position (goto-char view-exit-position))
269 (if (and (boundp 'view-major-mode) view-major-mode)
271 (let ((pbuf view-prev-buffer)
272 (exitact view-exit-action))
275 (if exitact (funcall exitact (current-buffer)))
276 (if pbuf (switch-to-buffer pbuf))))))
278 ;; #### - similar to what's in man.el and this ought to be written in C anyway... --Stig
279 (defun cleanup-backspaces ()
280 "Cleanup backspace constructions.
281 _^H and ^H_ sequences are deleted. x^Hx sequences are turned into x for all
282 characters x. ^^H| and |^H^ sequences are turned into ^. +^Ho and o^H+ are
286 (goto-char (point-min))
287 (while (= (following-char) ?\C-h)
289 (while (search-forward "\C-h" nil t)
291 (cond ((looking-at "_\C-h\\|\\(.\\)\C-h\\1\\||\C-h\\^")
293 ((looking-at ".\C-h_\\|\\^\C-h|")
296 ((looking-at "+\C-ho\\|o\C-h+")
299 ((looking-at "|\C-h-")
302 (t (forward-char 2))))))
304 (defun view-cleanup-backspaces ()
305 "Cleanup backspaces and if buffer is currently unmodified, don't flag it
306 as a modified buffer. This works even if the buffer is read-only."
308 (let ((buffer-read-only)
309 (buf-mod (buffer-modified-p)))
311 ;; #### - THIS IS PROBABLY A REALLY DANGEROUS THING TO DO IN A MINOR MODE!!
312 (set-buffer-modified-p buf-mod)))
314 (defun toggle-truncate-lines (&optional p)
315 "Toggles the values of truncate-lines.
316 Positive prefix arg sets, negative disables."
318 (setq truncate-lines (if p
319 (> (prefix-numeric-value p) 0)
320 (not truncate-lines)))
323 (defun view-scroll-lines-up (p)
324 "Scroll up prefix-arg lines, default 1."
328 (defun view-scroll-lines-down (p)
329 "Scroll down prefix-arg lines, default 1."
333 (defun view-scroll-some-lines-down (&optional n)
334 "Scroll down prefix-arg lines, default 10, or last argument."
336 (if (> n 1) (setq view-default-lines n))
337 (scroll-down view-default-lines))
339 (defun view-scroll-some-lines-up (&optional n)
340 "Scroll up prefix-arg lines, default 10, or last argument."
342 (if (> n 1) (setq view-default-lines n))
343 (scroll-up view-default-lines))
345 (defun view-goto-line (&optional n)
346 "Goto prefix arg line N. N = 1 by default.."
350 (defun view-last-windowful (&optional n)
351 "Goto prefix arg line N or the first line of the last windowful in buffer."
353 (if current-prefix-arg
357 (move-to-window-line 0)))
359 (defun view-goto-percent (&optional percent)
360 "Set mark and go to a position PERCENT way into the current buffer."
362 (set-mark-command nil)
363 (goto-char (+ (point-min) (/ (* percent (- (point-max) (point-min))) 100)))
366 (defun view-mode-describe ()
368 (let ((mode-name "View")
369 (major-mode 'view-mode))
372 (defun view-search-forward (s p)
373 "Search forward for REGEXP. If regexp is empty, use last search string.
374 With prefix ARG, search forward that many occurrences."
375 (interactive "sView search: \np")
378 (if (string-equal "" s) view-search-string s) nil nil p)
379 (setq view-search-arg p)
380 (or (string-equal "" s)
381 (setq view-search-string s))))
383 (defun view-search-backward (s p)
384 "Search backward for REGEXP. If regexp is empty, use last search string.
385 With prefix ARG, search forward that many occurrences."
386 (interactive "sView search backward: \np")
387 (view-search-forward s (- p)))
389 (defun view-repeat-search (p)
390 "Repeat last view search command. If a prefix arg is given, use that
391 instead of the previous arg, if the prefix is just a -, then take the
392 negative of the last prefix arg."
396 (cond ((null p) view-search-arg)
397 ((eq p '-) (- view-search-arg))
398 (t (prefix-numeric-value p)))))
403 ;;; view-less.el ends here