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