-;;; view.el --- peruse file or buffer without editing.
+;;; view.el --- peruse file or buffer without editing
-;; Copyright (C) 1985, 1989, 1994, 1995, 1997, 2000
-;; Free Software Foundation, Inc.
+;; Copyright (C) 1985, 1989, 1994, 1995, 1997, 2000, 2001, 2002,
+;; 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
;; Author: K. Shane Hartman
;; Maintainer: Inge Frick <inge@nada.kth.se>
+;; Keywords: files
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; You could also bind view-file, view-buffer, view-buffer-other-window and
;; view-buffer-other-frame to keys.
-
+\f
;;; Code:
(defgroup view nil
:group 'wp
:group 'editing)
-(defcustom view-read-only nil
- "*Non-nil means buffers visiting files read-only, do it in view mode."
- :type 'boolean
- :group 'view)
-
(defcustom view-highlight-face 'highlight
"*The face used for highlighting the match found by View mode search."
:type 'face
;; is now controlled by the presence of a value in `view-return-to-alist'.
(defcustom view-scroll-auto-exit nil
"*Non-nil means scrolling past the end of buffer exits View mode.
-nil means attempting to scroll past the end of the buffer,
+A value of nil means attempting to scroll past the end of the buffer,
only rings the bell and gives a message on how to leave."
:type 'boolean
:group 'view)
(defcustom view-try-extend-at-buffer-end nil
- "*Non-nil means try load more of file when reaching end of buffer.
+ "*Non-nil means try to load more of file when reaching end of buffer.
This variable is mainly intended to be temporarily set to non-nil by
the F command in view-mode, but you can set it to t if you want the action
for all scroll commands in view mode."
:type 'boolean
:group 'view)
+(defcustom view-inhibit-help-message nil
+ "*Non-nil inhibits the help message shown upon entering View mode."
+ :type 'boolean
+ :group 'view
+ :version "22.1")
+
;;;###autoload
(defvar view-mode nil
"Non-nil if View mode is enabled.
"Normal hook run when starting to view a buffer or file."
:type 'hook
:group 'view)
-
+\f
(defvar view-old-buffer-read-only nil)
(make-variable-buffer-local 'view-old-buffer-read-only)
(defvar view-old-Helper-return-blurb)
(make-variable-buffer-local 'view-old-Helper-return-blurb)
+;; Just to avoid warnings.
+(defvar Helper-return-blurb)
+
(defvar view-page-size nil
"Default number of lines to scroll by View page commands.
-If nil then the local value of this is initially set to window size.")
+If nil that means use the window size.")
(make-variable-buffer-local 'view-page-size)
(defvar view-half-page-size nil
"Default number of lines to scroll by View half page commands.
-If nil then the local value of this is initially set to half window size.")
+If nil that means use half the window size.")
(make-variable-buffer-local 'view-half-page-size)
(defvar view-last-regexp nil)
See RETURN-TO-ALIST argument of function `view-mode-exit' for the format of
`view-return-to-alist'.")
(make-variable-buffer-local 'view-return-to-alist)
+(put 'view-return-to-alist 'permanent-local t)
(defvar view-exit-action nil
"nil or a function with one argument (a buffer) called when finished viewing.
This is local in each buffer, once it is used.")
(make-variable-buffer-local 'view-overlay)
-(or (assq 'view-mode minor-mode-alist)
- (setq minor-mode-alist
- (cons (list 'view-mode
- (propertize " View"
- 'help-echo "mouse-2: exit View mode"
- 'local-map (make-mode-line-mouse2-map
- #'view-mode)))
- minor-mode-alist)))
-
+(unless (assq 'view-mode minor-mode-alist)
+ (setq minor-mode-alist
+ (cons (list 'view-mode
+ (propertize " View"
+ 'local-map mode-line-minor-mode-keymap
+ 'help-echo "mouse-3: minor mode menu"))
+ minor-mode-alist)))
+\f
;; Define keymap inside defvar to make it easier to load changes.
;; Some redundant "less"-like key bindings below have been commented out.
(defvar view-mode-map
(or (assq 'view-mode minor-mode-map-alist)
(setq minor-mode-map-alist
(cons (cons 'view-mode view-mode-map) minor-mode-map-alist)))
-
+\f
;;; Commands that enter or exit view mode.
;;;###autoload
This command runs the normal hook `view-mode-hook'."
(interactive "fView file: ")
- (let ((had-a-buf (get-file-buffer file)))
- (view-buffer (find-file-noselect file)
- (and (not had-a-buf) 'kill-buffer))))
+ (unless (file-exists-p file) (error "%s does not exist" file))
+ (let ((had-a-buf (get-file-buffer file))
+ (buffer (find-file-noselect file)))
+ (if (eq (with-current-buffer buffer
+ (get major-mode 'mode-class))
+ 'special)
+ (progn
+ (switch-to-buffer buffer)
+ (message "Not using View mode because the major mode is special"))
+ (view-buffer buffer (and (not had-a-buf) 'kill-buffer)))))
;;;###autoload
(defun view-file-other-window (file)
This command runs the normal hook `view-mode-hook'."
(interactive "fIn other window view file: ")
+ (unless (file-exists-p file) (error "%s does not exist" file))
(let ((had-a-buf (get-file-buffer file)))
(view-buffer-other-window (find-file-noselect file) nil
(and (not had-a-buf) 'kill-buffer))))
This command runs the normal hook `view-mode-hook'."
(interactive "fIn other frame view file: ")
+ (unless (file-exists-p file) (error "%s does not exist" file))
(let ((had-a-buf (get-file-buffer file)))
(view-buffer-other-frame (find-file-noselect file) nil
(and (not had-a-buf) 'kill-buffer))))
(switch-to-buffer-other-frame buffer)
(view-mode-enter (and return-to (cons (selected-window) return-to))
exit-action)))
-
+\f
;;;###autoload
(defun view-mode (&optional arg)
;; In the following documentation string we have to use some explicit key
;; bindings instead of using the \\[] construction. The reason for this
;; is that most commands have more than one key binding.
"Toggle View mode, a minor mode for viewing text but not editing it.
-With ARG, turn View mode on iff ARG is positive.
+With prefix argument ARG, turn View mode on if ARG is positive, otherwise
+turn it off.
Emacs commands that do not change the buffer contents are available as usual.
Kill commands insert text in kill buffers but do not delete. Other commands
\\ searches backward for regular expression, starting before current page.
\\[View-search-last-regexp-forward] searches forward for last regular expression.
p searches backward for last regular expression.
-\\[View-quit] quit View mode, trying to restore window and buffer to previous state.
+\\[View-quit] quit View mode, restoring this window and buffer to previous state.
\\[View-quit] is the normal way to leave view mode.
\\[View-exit] exit View mode but stay in current buffer. Use this if you started
viewing a buffer (file) and find out you want to edit it.
-\\[View-exit-and-edit] exit View mode and make the current buffer editable.
-\\[View-quit-all] quit View mode, trying to restore windows and buffer to previous state.
+ This command restores the previous read-only status of the buffer.
+\\[View-exit-and-edit] exit View mode, and make the current buffer editable
+ even if it was not editable before entry to View mode.
+\\[View-quit-all] quit View mode, restoring all windows to previous state.
\\[View-leave] quit View mode and maybe switch buffers, but don't kill this buffer.
\\[View-kill-and-leave] quit View mode, kill current buffer and go back to other buffer.
-The effect of \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
-entered by view-file, view-file-other-window or view-file-other-frame
-\(\\[view-file], \\[view-file-other-window], \\[view-file-other-frame] or the dired mode v command), then \\[View-quit] will
-try to kill the current buffer. If view-mode was entered from another buffer
-as is done by View-buffer, View-buffer-other-window, View-buffer-other frame,
-View-file, View-file-other-window or View-file-other-frame then \\[View-leave] , \\[View-quit] and \\[View-kill-and-leave]
-will return to that buffer.
+The effect of \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] depends on how view-mode was entered. If it was
+entered by view-file, view-file-other-window, view-file-other-frame, or
+\\[dired-view-file] \(\\[view-file], \\[view-file-other-window],
+\\[view-file-other-frame], or the Dired mode v command),
+then \\[View-quit] will try to kill the current buffer.
+If view-mode was entered from another buffer, by \\[view-buffer],
+\\[view-buffer-other-window], \\[view-buffer-other frame], \\[view-file],
+\\[view-file-other-window], or \\[view-file-other-frame],
+then \\[View-leave], \\[View-quit] and \\[View-kill-and-leave] will return to that buffer.
Entry to view-mode runs the normal hook `view-mode-hook'."
(interactive "P")
(if (> (prefix-numeric-value arg) 0) view-mode (not view-mode)))
(if view-mode (view-mode-disable)
(view-mode-enable))))
-
+\f
(defun view-mode-enable ()
"Turn on View mode."
;; Always leave view mode before changing major mode.
;; This is to guarantee that the buffer-read-only variable is restored.
- (make-local-hook 'change-major-mode-hook)
(add-hook 'change-major-mode-hook 'view-mode-disable nil t)
(setq view-mode t
- view-page-size (view-page-size-default view-page-size)
- view-half-page-size (or view-half-page-size (/ (view-window-size) 2))
+ view-page-size nil
+ view-half-page-size nil
view-old-buffer-read-only buffer-read-only
buffer-read-only t
view-old-Helper-return-blurb (and (boundp 'Helper-return-blurb)
(unless view-mode ; Do nothing if already in view mode.
(view-mode-enable)
(force-mode-line-update)
- (message "%s"
- (substitute-command-keys "\
-Type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit."))))
-
+ (unless view-inhibit-help-message
+ (message "%s"
+ (substitute-command-keys "\
+View mode: type \\[help-command] for help, \\[describe-mode] for commands, \\[View-quit] to quit.")))))
+\f
(defun view-mode-exit (&optional return-to-alist exit-action all-win)
"Exit View mode in various ways, depending on optional arguments.
RETURN-TO-ALIST, EXIT-ACTION and ALL-WIN determine what to do after exit.
(setq view-exit-action nil)
(funcall exit-action buffer))
(force-mode-line-update))))
-
+\f
(defun View-exit ()
"Exit View mode but stay in current buffer."
(interactive)
"Quit View mode, kill current buffer and return to previous buffer."
(interactive)
(view-mode-exit view-return-to-alist (or view-exit-action 'kill-buffer) t))
-
+\f
;;; Some help routines.
(defun view-set-half-page-size-default (lines)
;; Get and maybe set half page size.
- (if (not lines) view-half-page-size
+ (if (not lines) (or view-half-page-size
+ (/ (view-window-size) 2))
(setq view-half-page-size
(if (zerop (setq lines (prefix-numeric-value lines)))
(/ (view-window-size) 2)
; (goto-char (point-max))
; (beginning-of-line))
; (view-recenter))
-
+
(defun View-goto-line (&optional line)
"Move to first (or prefix LINE) line in View mode.
Display is centered at LINE.
(goto-line line)
(view-recenter))
-(defun View-scroll-to-buffer-end ()
- "Scroll backward or forward so that buffer end is at last line of window."
+(defun View-back-to-mark (&optional ignore)
+ "Return to last mark set in View mode, else beginning of file.
+Display that line at the center of the window.
+This command pops the mark ring, so that successive
+invocations return to earlier marks."
(interactive)
- (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
- (goto-char (point-max))
- (recenter -1)
- (and p (goto-char p))))
-
+ (goto-char (or (mark t) (point-min)))
+ (pop-mark)
+ (view-recenter))
+\f
(defun view-scroll-lines (lines backward default maxdefault)
;; This function does the job for all the scrolling commands.
;; Scroll forward LINES lines. If BACKWARD is true scroll backwards.
(if view-scroll-auto-exit "\\[View-scroll-page-forward]"
"\\[View-quit]")))
(message "End of buffer")))
+\f
+(defun View-scroll-to-buffer-end ()
+ "Scroll backward or forward so that buffer end is at last line of window."
+ (interactive)
+ (let ((p (if (pos-visible-in-window-p (point-max)) (point))))
+ (goto-char (point-max))
+ (recenter -1)
+ (and p (goto-char p))))
(defun View-scroll-page-forward (&optional lines)
"Scroll \"page size\" or prefix LINES lines forward in View mode.
\\[View-scroll-page-backward-set-page-size].
If LINES is more than a window-full, only the last window-full is shown."
(interactive "P")
- (view-scroll-lines lines nil view-page-size nil))
+ (view-scroll-lines lines nil (view-page-size-default view-page-size) nil))
(defun View-scroll-page-backward (&optional lines)
"Scroll \"page size\" or prefix LINES lines backward in View mode.
See also `View-scroll-page-forward'."
(interactive "P")
- (view-scroll-lines lines t view-page-size nil))
-
+ (view-scroll-lines lines t (view-page-size-default view-page-size) nil))
+
(defun View-scroll-page-forward-set-page-size (&optional lines)
"Scroll forward LINES lines in View mode, setting the \"page size\".
This is the number of lines which \\[View-scroll-page-forward] and
(interactive "P")
(let ((view-scroll-auto-exit nil)
(view-try-extend-at-buffer-end t))
- (view-scroll-lines lines nil view-page-size nil)))
-
-(defun View-back-to-mark (&optional ignore)
- "Return to last mark set in View mode, else beginning of file.
-Display that line at the center of the window.
-This command pops the mark ring, so that successive
-invocations return to earlier marks."
- (interactive)
- (goto-char (or (mark t) (point-min)))
- (pop-mark)
- (view-recenter))
-
+ (view-scroll-lines lines nil (view-page-size-default view-page-size) nil)))
+\f
(defun View-search-regexp-forward (n regexp)
"Search forward for first (or prefix Nth) occurrence of REGEXP in View mode.
times (if no "no " "") regexp)
(sit-for 4))))
+;; This is the dumb approach, looking at each line. The original
+;; version of this function looked like it might have been trying to
+;; do something clever, but not succeeding:
+;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-09/msg00073.html
(defun view-search-no-match-lines (times regexp)
- ;; Search for the TIMESt occurrence of line with no match for REGEXP.
- (let ((back (and (< times 0) (setq times (- times)) -1))
- n)
- (while (> times 0)
- (save-excursion (beginning-of-line (if back (- times) (1+ times)))
- (setq n (point)))
- (setq times
- (cond
- ((< (count-lines (point) n) times) -1) ; Not enough lines.
- ((or (null (re-search-forward regexp nil t back))
- (if back (and (< (match-end 0) n)
- (> (count-lines (match-end 0) n) 1))
- (and (< n (match-beginning 0))
- (> (count-lines n (match-beginning 0)) 1))))
- 0) ; No match within lines.
- (back (count-lines (max n (match-beginning 0)) (match-end 0)))
- (t (count-lines (match-beginning 0) (min n (match-end 0))))))
- (goto-char n))
- (and (zerop times) (looking-at "^.*$"))))
-
+ "Search for the TIMESth occurrence of a line with no match for REGEXP.
+If such a line is found, return non-nil and set the match-data to that line.
+If TIMES is negative, search backwards."
+ (let ((step (if (>= times 0) 1
+ (setq times (- times))
+ -1)))
+ ;; Note that we do not check the current line.
+ (while (and (> times 0)
+ (zerop (forward-line step)))
+ ;; (forward-line 1) returns 0 on moving within the last line.
+ (if (eobp)
+ (setq times -1)
+ (or (re-search-forward regexp (line-end-position) t)
+ (setq times (1- times))))))
+ (and (zerop times)
+ (looking-at ".*")))
(provide 'view)
+;;; arch-tag: 6d0ace36-1d12-4de3-8de3-1fa3231636d7
;;; view.el ends here