emacs/lisp/emacs-lock.el

261 lines
10 KiB
EmacsLisp
Raw Normal View History

;;; emacs-lock.el --- protect buffers against killing or exiting -*- lexical-binding: t -*-
1996-01-14 07:34:30 +00:00
;; Copyright (C) 2011-2021 Free Software Foundation, Inc.
1996-01-14 07:34:30 +00:00
;; Author: Juanma Barranquero <lekktu@gmail.com>
;; Inspired by emacs-lock.el by Tom Wurgler <twurgler@goodyear.com>
;; Maintainer: emacs-devel@gnu.org
1997-06-10 18:18:07 +00:00
;; Keywords: extensions, processes
1996-01-14 07:34:30 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
1994-12-24 16:49:59 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
1996-01-14 07:34:30 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
1994-12-24 16:49:59 +00:00
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
1996-01-14 07:34:30 +00:00
1994-12-24 16:49:59 +00:00
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
1994-12-24 16:49:59 +00:00
;;; Commentary:
1996-01-14 07:34:30 +00:00
;; This package defines a minor mode Emacs Lock to mark a buffer as
;; protected against accidental killing, or exiting Emacs, or both.
;; Buffers associated with inferior modes, like shell or telnet, can
;; be treated specially, by auto-unlocking them if their inferior
;; processes are dead.
1994-12-24 16:49:59 +00:00
1996-01-14 07:34:30 +00:00
;;; Code:
(defgroup emacs-lock nil
"Emacs-Lock mode."
:version "24.1"
:group 'convenience)
(defcustom emacs-lock-default-locking-mode 'all
"Default locking mode of Emacs-Locked buffers.
Its value is used as the default for `emacs-lock-mode' (which
see) the first time that Emacs Lock mode is turned on in a buffer
without passing an explicit locking mode.
Possible values are:
exit -- Emacs cannot exit while the buffer is locked
kill -- the buffer cannot be killed, but Emacs can exit as usual
all -- the buffer is locked against both actions
nil -- the buffer is not locked"
:type '(choice
(const :tag "Do not allow Emacs to exit" exit)
(const :tag "Do not allow killing the buffer" kill)
(const :tag "Do not allow killing the buffer or exiting Emacs" all)
(const :tag "Do not lock the buffer" nil))
:group 'emacs-lock
:version "24.1")
;; Note: as auto-unlocking can lead to data loss, it would be better
;; to default to nil; but the value below is for compatibility with
;; the old emacs-lock.el.
(defcustom emacs-lock-unlockable-modes '((shell-mode . all)
(telnet-mode . all))
"Alist of auto-unlockable modes.
Each element is a pair (MAJOR-MODE . ACTION), where ACTION is
one of `kill', `exit' or `all'. Buffers with matching major
modes are auto-unlocked for the specific action if their
inferior processes are not alive. If this variable is t, all
buffers associated to inferior processes are auto-unlockable
for both actions (NOT RECOMMENDED)."
:type '(choice
(const :tag "All buffers with inferior processes" t)
(repeat :tag "Selected modes"
(cons :tag "Set auto-unlock for"
(symbol :tag "Major mode")
(radio
(const :tag "Allow exiting" exit)
(const :tag "Allow killing" kill)
(const :tag "Allow both" all)))))
:group 'emacs-lock
:version "24.1")
(defcustom emacs-lock-locked-buffer-functions nil
"Abnormal hook run when Emacs Lock prevents exiting Emacs, or killing a buffer.
The functions get one argument, the first locked buffer found."
:type 'hook
:group 'emacs-lock
:version "24.3")
The tedious game of whack-a-mole with compiler warnings continues * lisp/abbrev.el (edit-abbrevs-map): * lisp/emacs-lock.el (emacs-lock-from-exiting): * lisp/htmlfontify.el (hfy-optimisations): * lisp/ielm.el (inferior-emacs-lisp-mode-hook) (inferior-emacs-lisp-mode-map): * lisp/isearch.el (isearch-lazy-highlight-word): * lisp/select.el (x-select-enable-clipboard, x-select-enable-primary): * lisp/shell.el (shell-dirtrack-mode): * lisp/skeleton.el (skeleton-transformation, skeleton-filter): * lisp/startup.el (inhibit-splash-screen, inhibit-startup-message): * lisp/window.el (even-window-heights): * lisp/calendar/timeclock.el (timeclock-modeline-display): * lisp/cedet/semantic/db-mode.el (semanticdb-mode-hook) (semanticdb-global-mode): * lisp/emacs-lisp/edebug.el (gud-inhibit-global-bindings): * lisp/emacs-lisp/warnings.el (display-warning-minimum-level) (log-warning-minimum-level): * lisp/erc/erc-dcc.el (erc-dcc-chat-filter-hook): * lisp/gnus/nnspool.el (news-path): * lisp/org/org-agenda.el (org-agenda-search-view-search-words-only) (org-agenda-remove-tags-when-in-prefix) (org-agenda-align-tags-to-column, org-agenda-keymap): * lisp/org/org.el (org-special-ctrl-a) (org-log-state-notes-into-drawer) (org-agenda-multi-occur-extra-files): * lisp/progmodes/flymake-proc.el (flymake-err-line-patterns) (flymake-check-file-limit): * lisp/progmodes/make-mode.el (makefile-query-one-target-method): * lisp/progmodes/octave.el (inferior-octave-startup-hook): * lisp/progmodes/python.el (python-buffer) (python-preoutput-result): * lisp/progmodes/sql.el (sql-dialect): * lisp/textmodes/artist.el (artist-text-renderer): * lisp/textmodes/bibtex.el (bibtex-autokey-name-case-convert) (bibtex-autokey-titleword-case-convert): * lisp/textmodes/flyspell.el (flyspell-generic-check-word-p): * lisp/textmodes/ispell.el (ispell-format-word): * lisp/textmodes/rst.el (rst-preferred-decorations): * lisp/textmodes/sgml-mode.el (sgml-transformation): Move aliases before targets, to silence new compiler warning. * lisp/term/ns-win.el (ns-option-modifier, ns-right-option-modifier): Silence warning.
2018-04-20 18:34:39 -04:00
(define-obsolete-variable-alias 'emacs-lock-from-exiting
'emacs-lock-mode "24.1")
(defvar-local emacs-lock-mode nil
"If non-nil, the current buffer is locked.
It can be one of the following values:
exit -- Emacs cannot exit while the buffer is locked
kill -- the buffer cannot be killed, but Emacs can exit as usual
all -- the buffer is locked against both actions
nil -- the buffer is not locked
See also `emacs-lock-unlockable-modes', which exempts buffers under
some major modes from being locked under some circumstances.")
(put 'emacs-lock-mode 'permanent-local t)
(defvar-local emacs-lock--old-mode nil
"Most recent locking mode set on the buffer.
Internal use only.")
(put 'emacs-lock--old-mode 'permanent-local t)
(defvar-local emacs-lock--try-unlocking nil
"Non-nil if current buffer should be checked for auto-unlocking.
Internal use only.")
(put 'emacs-lock--try-unlocking 'permanent-local t)
(defun emacs-lock-live-process-p (buffer-or-name)
"Return t if BUFFER-OR-NAME is associated with a live process."
(and (process-live-p (get-buffer-process buffer-or-name)) t))
(defun emacs-lock--can-auto-unlock (action)
"Return t if the current buffer can auto-unlock for ACTION.
ACTION must be one of `kill' or `exit'.
See `emacs-lock-unlockable-modes'."
(and emacs-lock--try-unlocking
(not (emacs-lock-live-process-p (current-buffer)))
(or (eq emacs-lock-unlockable-modes t)
(let ((unlock (cdr (assq major-mode emacs-lock-unlockable-modes))))
(or (eq unlock 'all) (eq unlock action))))))
(defun emacs-lock--exit-locked-buffer ()
"Return the first exit-locked buffer found."
(save-current-buffer
(catch :found
(dolist (buffer (buffer-list))
(set-buffer buffer)
(unless (or (emacs-lock--can-auto-unlock 'exit)
(memq emacs-lock-mode '(nil kill)))
(throw :found buffer)))
nil)))
(defun emacs-lock--kill-emacs-hook ()
"Signal an error if any buffer is exit-locked.
Used from `kill-emacs-hook' (which see)."
(let ((locked (emacs-lock--exit-locked-buffer)))
(when locked
(run-hook-with-args 'emacs-lock-locked-buffer-functions locked)
(error "Emacs cannot exit because buffer %S is locked"
(buffer-name locked)))))
(defun emacs-lock--kill-emacs-query-functions ()
"Display a message if any buffer is exit-locked.
Return a value appropriate for `kill-emacs-query-functions' (which see)."
(let ((locked (emacs-lock--exit-locked-buffer)))
(if (not locked)
t
(run-hook-with-args 'emacs-lock-locked-buffer-functions locked)
(message "Emacs cannot exit because buffer %S is locked"
(buffer-name locked))
nil)))
(defun emacs-lock--kill-buffer-query-functions ()
"Display a message if the current buffer is kill-locked.
Return a value appropriate for `kill-buffer-query-functions' (which see)."
(if (or (emacs-lock--can-auto-unlock 'kill)
(memq emacs-lock-mode '(nil exit)))
t
(run-hook-with-args 'emacs-lock-locked-buffer-functions (current-buffer))
(message "Buffer %S is locked and cannot be killed" (buffer-name))
nil))
(defun emacs-lock--set-mode (mode arg)
"Setter function for `emacs-lock-mode'."
(setq emacs-lock-mode
(cond ((memq arg '(all exit kill))
;; explicit locking mode arg, use it
arg)
((and (eq arg current-prefix-arg) (consp current-prefix-arg))
;; called with C-u M-x emacs-lock-mode, so ask the user
(intern (completing-read
(format-prompt "Locking mode"
emacs-lock-default-locking-mode)
'("all" "exit" "kill")
nil t nil nil
(symbol-name emacs-lock-default-locking-mode))))
((eq mode t)
;; turn on, so use previous setting, or customized default
(or emacs-lock--old-mode emacs-lock-default-locking-mode))
(t
;; anything else (turn off)
mode))))
;;;###autoload
(define-minor-mode emacs-lock-mode
Fix minor mode docstrings for the new meaning of a nil ARG. * abbrev.el (abbrev-mode): * allout.el (allout-mode): * autoinsert.el (auto-insert-mode): * autoarg.el (autoarg-mode, autoarg-kp-mode): * autorevert.el (auto-revert-mode, auto-revert-tail-mode) (global-auto-revert-mode): * battery.el (display-battery-mode): * composite.el (global-auto-composition-mode) (auto-composition-mode): * delsel.el (delete-selection-mode): * desktop.el (desktop-save-mode): * dired-x.el (dired-omit-mode): * dirtrack.el (dirtrack-mode): * doc-view.el (doc-view-minor-mode): * double.el (double-mode): * electric.el (electric-indent-mode, electric-pair-mode): * emacs-lock.el (emacs-lock-mode): * epa-hook.el (auto-encryption-mode): * follow.el (follow-mode): * font-core.el (font-lock-mode): * frame.el (auto-raise-mode, auto-lower-mode, blink-cursor-mode): * help.el (temp-buffer-resize-mode): * hilit-chg.el (highlight-changes-mode) (highlight-changes-visible-mode): * hi-lock.el (hi-lock-mode): * hl-line.el (hl-line-mode, global-hl-line-mode): * icomplete.el (icomplete-mode): * ido.el (ido-everywhere): * image-file.el (auto-image-file-mode): * image-mode.el (image-minor-mode): * iswitchb.el (iswitchb-mode): * jka-cmpr-hook.el (auto-compression-mode): * linum.el (linum-mode): * longlines.el (longlines-mode): * master.el (master-mode): * mb-depth.el (minibuffer-depth-indicate-mode): * menu-bar.el (menu-bar-mode): * minibuf-eldef.el (minibuffer-electric-default-mode): * mouse-sel.el (mouse-sel-mode): * msb.el (msb-mode): * mwheel.el (mouse-wheel-mode): * outline.el (outline-minor-mode): * paren.el (show-paren-mode): * recentf.el (recentf-mode): * reveal.el (reveal-mode, global-reveal-mode): * rfn-eshadow.el (file-name-shadow-mode): * ruler-mode.el (ruler-mode): * savehist.el (savehist-mode): * scroll-all.el (scroll-all-mode): * scroll-bar.el (scroll-bar-mode): * server.el (server-mode): * shell.el (shell-dirtrack-mode): * simple.el (auto-fill-mode, transient-mark-mode) (visual-line-mode, overwrite-mode, binary-overwrite-mode) (line-number-mode, column-number-mode, size-indication-mode) (auto-save-mode, normal-erase-is-backspace-mode, visible-mode): * strokes.el (strokes-mode): * time.el (display-time-mode): * t-mouse.el (gpm-mouse-mode): * tool-bar.el (tool-bar-mode): * tooltip.el (tooltip-mode): * type-break.el (type-break-mode-line-message-mode) (type-break-query-mode): * view.el (view-mode): * whitespace.el (whitespace-mode, whitespace-newline-mode) (global-whitespace-mode, global-whitespace-newline-mode): * xt-mouse.el (xterm-mouse-mode): Doc fix. * emacs-lisp/easy-mmode.el (define-globalized-minor-mode): Fix autogenerated docstring.
2011-10-19 08:54:24 -04:00
"Toggle Emacs Lock mode in the current buffer.
If called with a plain prefix argument, ask for the locking mode
to be used.
Fix minor mode docstrings for the new meaning of a nil ARG. * abbrev.el (abbrev-mode): * allout.el (allout-mode): * autoinsert.el (auto-insert-mode): * autoarg.el (autoarg-mode, autoarg-kp-mode): * autorevert.el (auto-revert-mode, auto-revert-tail-mode) (global-auto-revert-mode): * battery.el (display-battery-mode): * composite.el (global-auto-composition-mode) (auto-composition-mode): * delsel.el (delete-selection-mode): * desktop.el (desktop-save-mode): * dired-x.el (dired-omit-mode): * dirtrack.el (dirtrack-mode): * doc-view.el (doc-view-minor-mode): * double.el (double-mode): * electric.el (electric-indent-mode, electric-pair-mode): * emacs-lock.el (emacs-lock-mode): * epa-hook.el (auto-encryption-mode): * follow.el (follow-mode): * font-core.el (font-lock-mode): * frame.el (auto-raise-mode, auto-lower-mode, blink-cursor-mode): * help.el (temp-buffer-resize-mode): * hilit-chg.el (highlight-changes-mode) (highlight-changes-visible-mode): * hi-lock.el (hi-lock-mode): * hl-line.el (hl-line-mode, global-hl-line-mode): * icomplete.el (icomplete-mode): * ido.el (ido-everywhere): * image-file.el (auto-image-file-mode): * image-mode.el (image-minor-mode): * iswitchb.el (iswitchb-mode): * jka-cmpr-hook.el (auto-compression-mode): * linum.el (linum-mode): * longlines.el (longlines-mode): * master.el (master-mode): * mb-depth.el (minibuffer-depth-indicate-mode): * menu-bar.el (menu-bar-mode): * minibuf-eldef.el (minibuffer-electric-default-mode): * mouse-sel.el (mouse-sel-mode): * msb.el (msb-mode): * mwheel.el (mouse-wheel-mode): * outline.el (outline-minor-mode): * paren.el (show-paren-mode): * recentf.el (recentf-mode): * reveal.el (reveal-mode, global-reveal-mode): * rfn-eshadow.el (file-name-shadow-mode): * ruler-mode.el (ruler-mode): * savehist.el (savehist-mode): * scroll-all.el (scroll-all-mode): * scroll-bar.el (scroll-bar-mode): * server.el (server-mode): * shell.el (shell-dirtrack-mode): * simple.el (auto-fill-mode, transient-mark-mode) (visual-line-mode, overwrite-mode, binary-overwrite-mode) (line-number-mode, column-number-mode, size-indication-mode) (auto-save-mode, normal-erase-is-backspace-mode, visible-mode): * strokes.el (strokes-mode): * time.el (display-time-mode): * t-mouse.el (gpm-mouse-mode): * tool-bar.el (tool-bar-mode): * tooltip.el (tooltip-mode): * type-break.el (type-break-mode-line-message-mode) (type-break-query-mode): * view.el (view-mode): * whitespace.el (whitespace-mode, whitespace-newline-mode) (global-whitespace-mode, global-whitespace-newline-mode): * xt-mouse.el (xterm-mouse-mode): Doc fix. * emacs-lisp/easy-mmode.el (define-globalized-minor-mode): Fix autogenerated docstring.
2011-10-19 08:54:24 -04:00
Initially, if the user does not pass an explicit locking mode, it
defaults to `emacs-lock-default-locking-mode' (which see);
afterwards, the locking mode most recently set on the buffer is
used instead.
When called from Elisp code, ARG can be any locking mode:
exit -- Emacs cannot exit while the buffer is locked
kill -- the buffer cannot be killed, but Emacs can exit as usual
all -- the buffer is locked against both actions
Other values are interpreted as usual.
See also `emacs-lock-unlockable-modes', which exempts buffers under
some major modes from being locked under some circumstances."
:init-value nil
:lighter (""
(emacs-lock--try-unlocking " locked:" " Locked:")
(:eval (symbol-name emacs-lock-mode)))
:group 'emacs-lock
:variable (emacs-lock-mode .
(lambda (mode)
(emacs-lock--set-mode mode arg)))
(when emacs-lock-mode
(setq emacs-lock--old-mode emacs-lock-mode)
(setq emacs-lock--try-unlocking
(and (if (eq emacs-lock-unlockable-modes t)
(emacs-lock-live-process-p (current-buffer))
(assq major-mode emacs-lock-unlockable-modes))
t))))
1994-12-24 16:49:59 +00:00
(unless noninteractive
(add-hook 'kill-buffer-query-functions 'emacs-lock--kill-buffer-query-functions)
;; We set a hook in both kill-emacs-hook and kill-emacs-query-functions because
;; we really want to use k-e-q-f to stop as soon as possible, but don't want to
;; be caught by surprise if someone calls `kill-emacs' instead.
(add-hook 'kill-emacs-hook 'emacs-lock--kill-emacs-hook)
(add-hook 'kill-emacs-query-functions 'emacs-lock--kill-emacs-query-functions))
(defun emacs-lock-unload-function ()
"Unload the Emacs Lock library."
(catch :continue
(dolist (buffer (buffer-list))
(set-buffer buffer)
(when emacs-lock-mode
(if (y-or-n-p (format "Buffer %S is locked, unlock it? " (buffer-name)))
(emacs-lock-mode -1)
(message "Unloading of feature `emacs-lock' aborted.")
(throw :continue t))))
;; continue standard unloading
nil))
1994-12-24 16:49:59 +00:00
;;; Compatibility
(defun toggle-emacs-lock ()
"Toggle `emacs-lock-from-exiting' for the current buffer."
Use declare forms, where possible, to mark obsolete functions. * lisp/allout.el (allout-passphrase-hint-string): Likewise. (allout-init): Use a declare form to mark obsolete. * lisp/calendar/calendar.el (calendar-version): * lisp/calendar/icalendar.el (icalendar-extract-ical-from-buffer) (icalendar-convert-diary-to-ical): * lisp/cus-edit.el (custom-mode): * lisp/ansi-color.el (ansi-color-unfontify-region): * lisp/international/latin1-disp.el (latin1-char-displayable-p): * lisp/progmodes/cwarn.el (turn-on-cwarn-mode): * lisp/progmodes/which-func.el (which-func-update-1): Use define-obsolete-function-alias. * lisp/bookmark.el (bookmark-jump-noselect): Use a declare form to mark this function obsolete. * lisp/calendar/cal-x.el (calendar-two-frame-setup) (calendar-only-one-frame-setup, calendar-one-frame-setup): * lisp/calendar/calendar.el (american-calendar, european-calendar) (calendar-for-loop): * lisp/comint.el (comint-dynamic-simple-complete) (comint-dynamic-complete-as-filename, comint-unquote-filename): * lisp/desktop.el (desktop-load-default): * lisp/dired-x.el (dired-omit-here-always) (dired-hack-local-variables, dired-default-directory): * lisp/emacs-lisp/derived.el (derived-mode-class): * lisp/emacs-lisp/timer.el (timer-set-time-with-usecs): * lisp/emacs-lock.el (toggle-emacs-lock): * lisp/epa.el (epa-display-verify-result): * lisp/epg.el (epg-sign-keys, epg-start-sign-keys) (epg-passphrase-callback-function): * lisp/eshell/esh-util.el (eshell-for): * lisp/eshell/eshell.el (eshell-remove-from-window-buffer-names) (eshell-add-to-window-buffer-names): * lisp/files.el (locate-file-completion): * lisp/imenu.el (imenu-example--create-c-index) (imenu-example--create-lisp-index) (imenu-example--lisp-extract-index-name) (imenu-example--name-and-position): * lisp/international/mule-cmds.el (princ-list): * lisp/international/mule-diag.el (decode-codepage-char): * lisp/international/mule-util.el (detect-coding-with-priority): * lisp/iswitchb.el (iswitchb-read-buffer): * lisp/mail/mailalias.el (mail-complete): * lisp/mail/sendmail.el (mail-sent-via): * lisp/mouse.el (mouse-popup-menubar-stuff, mouse-popup-menubar) (mouse-major-mode-menu): * lisp/password-cache.el (password-read-and-add): * lisp/pcomplete.el (pcomplete-parse-comint-arguments): * lisp/progmodes/sh-script.el (sh-maybe-here-document): * lisp/replace.el (query-replace-regexp-eval): * lisp/savehist.el (savehist-load): * lisp/simple.el (choose-completion-delete-max-match): * lisp/term.el (term-dynamic-simple-complete): * lisp/vc/ediff-init.el (ediff-check-version): * lisp/vc/ediff-wind.el (ediff-choose-window-setup-function-automatically): * lisp/vc/vc.el (vc-diff-switches-list): * lisp/view.el (view-return-to-alist-update): Likewise. * lisp/iswitchb.el (iswitchb-read-buffer): Move code of iswitchb-define-mode-map here, and delete that obsolete function. * lisp/subr.el (eval-next-after-load, makehash, insert-string) (assoc-ignore-representation, assoc-ignore-case): Use declare to mark obsolete. (mode-line-inverse-video): Variable deleted. * lisp/emacs-lisp/byte-run.el (make-obsolete): Doc fix; emphasize that this applies to functions. * lisp/erc/erc.el (erc-send-command): Use define-obsolete-function-alias. * lisp/international/mule-util.el (string-to-sequence): Remove. * lisp/net/newst-backend.el (newsticker-cache-filename): * lisp/net/newst-treeview.el (newsticker-groups-filename): Fix incorrect obsolescence declaration. * lisp/net/snmp-mode.el (snmp-font-lock-keywords-3): Don't use obsolete font-lock-reference-face. * lisp/url/url-parse.el (url-recreate-url-attributes): * lisp/url/url-util.el (url-generate-unique-filename): Use declare to mark obsolete. * src/xdisp.c (mode_line_inverse_video): Delete obsolete variable.
2012-09-25 12:13:02 +08:00
(declare (obsolete emacs-lock-mode "24.1"))
(interactive)
(call-interactively 'emacs-lock-mode))
(provide 'emacs-lock)
;;; emacs-lock.el ends here