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