X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/f23d76bdefbd4c06e14d69e99e50d35ce91c8226..d9a3d80e56e26f65ca3b35b242436a2b16dbf535:/lisp/view.el diff --git a/lisp/view.el b/lisp/view.el index ad6ca9371b..1d2078a4bb 100644 --- a/lisp/view.el +++ b/lisp/view.el @@ -1,7 +1,7 @@ ;;; view.el --- peruse file or buffer without editing ;; Copyright (C) 1985, 1989, 1994, 1995, 1997, 2000, 2001, 2002, -;; 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. +;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. ;; Author: K. Shane Hartman ;; Maintainer: Inge Frick @@ -9,10 +9,10 @@ ;; This file is part of GNU Emacs. -;; GNU Emacs is free software; you can redistribute it and/or modify +;; 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 3, or (at your option) -;; any later version. +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -20,9 +20,7 @@ ;; GNU General Public License for more details. ;; 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., 51 Franklin Street, Fifth Floor, -;; Boston, MA 02110-1301, USA. +;; along with GNU Emacs. If not, see . ;;; Commentary: @@ -243,6 +241,17 @@ This is local in each buffer, once it is used.") ;;; Commands that enter or exit view mode. +;; This is used when view mode is exited, to make sure we don't try to +;; kill a buffer modified by the user. A buffer in view mode can +;; become modified if the user types C-x C-q, edits the buffer, then +;; types C-x C-q again to return to view mode. +;;;###autoload +(defun kill-buffer-if-not-modified (buf) + "Like `kill-buffer', but does nothing if the buffer is modified." + (let ((buf (get-buffer buf))) + (and buf (not (buffer-modified-p buf)) + (kill-buffer buf)))) + ;;;###autoload (defun view-file (file) "View FILE in View mode, returning to previous buffer when done. @@ -263,41 +272,50 @@ This command runs the normal hook `view-mode-hook'." (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))))) + (view-buffer buffer (and (not had-a-buf) 'kill-buffer-if-not-modified))))) ;;;###autoload (defun view-file-other-window (file) "View FILE in View mode in another window. -Return that window to its previous buffer when done. Emacs commands -editing the buffer contents are not available; instead, a special set of -commands (mostly letters and punctuation) are defined for moving around -in the buffer. +When done, return that window to its previous buffer, and kill the +buffer visiting FILE if unmodified and if it wasn't visited before. + +Emacs commands editing the buffer contents are not available; instead, +a special set of commands (mostly letters and punctuation) +are defined for moving around in the buffer. Space scrolls forward, Delete scrolls backward. For a list of all View commands, type H or h while viewing. 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)))) + (let ((had-a-buf (get-file-buffer file)) + (buf-to-view (find-file-noselect file))) + (view-buffer-other-window buf-to-view nil + (and (not had-a-buf) + 'kill-buffer-if-not-modified)))) ;;;###autoload (defun view-file-other-frame (file) "View FILE in View mode in another frame. -Maybe delete other frame and/or return to previous buffer when done. -Emacs commands editing the buffer contents are not available; instead, a -special set of commands (mostly letters and punctuation) are defined for -moving around in the buffer. +When done, kill the buffer visiting FILE if unmodified and if it wasn't +visited before; also, maybe delete other frame and/or return to previous +buffer. + +Emacs commands editing the buffer contents are not available; instead, +a special set of commands (mostly letters and punctuation) +are defined for moving around in the buffer. Space scrolls forward, Delete scrolls backward. For a list of all View commands, type H or h while viewing. 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)))) + (let ((had-a-buf (get-file-buffer file)) + (buf-to-view (find-file-noselect file))) + (view-buffer-other-frame buf-to-view nil + (and (not had-a-buf) + 'kill-buffer-if-not-modified)))) ;;;###autoload @@ -313,7 +331,12 @@ This command runs the normal hook `view-mode-hook'. Optional argument EXIT-ACTION is either nil or a function with buffer as argument. This function is called when finished viewing buffer. Use -this argument instead of explicitly setting `view-exit-action'." +this argument instead of explicitly setting `view-exit-action'. + +Do not set EXIT-ACTION to `kill-buffer' when BUFFER visits a +file: Users may suspend viewing in order to modify the buffer. +Exiting View mode will then discard the user's edits. Setting +EXIT-ACTION to `kill-buffer-if-not-modified' avoids this." (interactive "bView buffer: ") (let ((undo-window (list (window-buffer) (window-start) (window-point)))) (switch-to-buffer buffer) @@ -1075,5 +1098,5 @@ If TIMES is negative, search backwards." (provide 'view) -;;; arch-tag: 6d0ace36-1d12-4de3-8de3-1fa3231636d7 +;; arch-tag: 6d0ace36-1d12-4de3-8de3-1fa3231636d7 ;;; view.el ends here