From 0c495c215a149245fd00cf26f375b8cfa3977ef6 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Wed, 5 May 2010 22:53:56 -0400 Subject: [PATCH] Define auto-save-mode with define-minor-mode. * emacs-lisp/easy-mmode.el (define-minor-mode): Make :variable more flexible. * files.el (auto-save-mode): Use it to define using define-minor-mode. --- lisp/ChangeLog | 14 ++++++++++---- lisp/emacs-lisp/easy-mmode.el | 34 +++++++++++++++++++++++++--------- lisp/files.el | 32 ++++++++++++++------------------ 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index d35340beb10..88c41ea84c2 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,9 @@ +2010-05-06 Stefan Monnier + + * emacs-lisp/easy-mmode.el (define-minor-mode): + Make :variable more flexible. + * files.el (auto-save-mode): Use it to define using define-minor-mode. + 2010-05-05 Juri Linkov Add `slow' and `history' tags to the desktop data. @@ -20,8 +26,8 @@ (ange-ftp-delete-file): Add FORCE arg. (ange-ftp-rename-remote-to-remote) (ange-ftp-rename-local-to-remote, ange-ftp-rename-remote-to-local) - (ange-ftp-load, ange-ftp-compress, ange-ftp-uncompress): Force - file deletion. + (ange-ftp-load, ange-ftp-compress, ange-ftp-uncompress): + Force file deletion. * net/tramp-compat.el (tramp-compat-delete-file): New defun. @@ -39,8 +45,8 @@ (tramp-fish-handle-make-symbolic-link) (tramp-fish-handle-process-file): Use `tramp-compat-delete-file'. - * net/tramp-ftp.el (tramp-ftp-file-name-handler): Use - `tramp-compat-delete-file'. + * net/tramp-ftp.el (tramp-ftp-file-name-handler): + Use `tramp-compat-delete-file'. * net/tramp-gvfs.el (tramp-gvfs-handle-delete-file): Add FORCE arg. (tramp-gvfs-handle-write-region): Use `tramp-compat-delete-file'. diff --git a/lisp/emacs-lisp/easy-mmode.el b/lisp/emacs-lisp/easy-mmode.el index 238f2fa551a..5a21946183e 100644 --- a/lisp/emacs-lisp/easy-mmode.el +++ b/lisp/emacs-lisp/easy-mmode.el @@ -117,7 +117,10 @@ BODY contains code to execute each time the mode is activated or deactivated. :keymap MAP Same as the KEYMAP argument. :require SYM Same as in `defcustom'. :variable PLACE The location (as can be used with `setf') to use instead - of the variable MODE to store the state of the mode. + of the variable MODE to store the state of the mode. PLACE + can also be of the form (GET . SET) where GET is an expression + that returns the current state and SET is a function that takes + a new state and sets it. For example, you could write (define-minor-mode foo-mode \"If enabled, foo on you!\" @@ -149,8 +152,9 @@ For example, you could write (type nil) (extra-args nil) (extra-keywords nil) - (variable nil) - (modefun mode) + (variable nil) ;The PLACE where the state is stored. + (setter nil) ;The function (if any) to set the mode var. + (modefun mode) ;The minor mode function name we're defining. (require t) (hook (intern (concat mode-name "-hook"))) (hook-on (intern (concat mode-name "-on-hook"))) @@ -171,7 +175,12 @@ For example, you could write (:type (setq type (list :type (pop body)))) (:require (setq require (pop body))) (:keymap (setq keymap (pop body))) - (:variable (setq variable (setq mode (pop body)))) + (:variable (setq variable (pop body)) + (if (not (functionp (cdr-safe variable))) + ;; PLACE is not of the form (GET . SET). + (setq mode variable) + (setq mode (car variable)) + (setq setter (cdr variable)))) (t (push keyw extra-keywords) (push (pop body) extra-keywords)))) (setq keymap-sym (if (and keymap (symbolp keymap)) keymap @@ -230,7 +239,8 @@ With zero or negative ARG turn mode off. ;; repeat-command still does the toggling correctly. (interactive (list (or current-prefix-arg 'toggle))) (let ((,last-message (current-message))) - (,(if (symbolp mode) 'setq 'setf) ,mode + (,@(if setter (list setter) + (list (if (symbolp mode) 'setq 'setf) mode)) (if (eq arg 'toggle) (not ,mode) ;; A nil argument also means ON now. @@ -240,7 +250,8 @@ With zero or negative ARG turn mode off. (run-hooks ',hook (if ,mode ',hook-on ',hook-off)) (if (called-interactively-p 'any) (progn - ,(if globalp `(customize-mark-as-set ',mode)) + ,(if (and globalp (symbolp mode)) + `(customize-mark-as-set ',mode)) ;; Avoid overwriting a message shown by the body, ;; but do overwrite previous messages. (unless (and (current-message) @@ -265,10 +276,15 @@ With zero or negative ARG turn mode off. (t (error "Invalid keymap %S" ,keymap)))) ,(format "Keymap for `%s'." mode-name))) - ,(unless variable - `(add-minor-mode ',mode ',lighter + ,(if (not (symbolp mode)) + (if (or lighter keymap) + (error ":lighter and :keymap unsupported with mode expression %s" mode)) + `(with-no-warnings + (add-minor-mode ',mode ',lighter ,(if keymap keymap-sym - `(if (boundp ',keymap-sym) ,keymap-sym))))))) + `(if (boundp ',keymap-sym) ,keymap-sym)) + nil + ,(unless (eq mode modefun) 'modefun))))))) ;;; ;;; make global minor mode diff --git a/lisp/files.el b/lisp/files.el index 138261b64dd..9090f8afc88 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -5150,29 +5150,25 @@ The optional second argument indicates whether to kill internal buffers too." (kill-buffer-ask buffer))))) -(defun auto-save-mode (arg) +(define-minor-mode auto-save-mode "Toggle auto-saving of contents of current buffer. With prefix argument ARG, turn auto-saving on if positive, else off." - (interactive "P") - (setq buffer-auto-save-file-name - (and (if (null arg) - (or (not buffer-auto-save-file-name) - ;; If auto-save is off because buffer has shrunk, - ;; then toggling should turn it on. - (< buffer-saved-size 0)) - (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0)))) - (if (and buffer-file-name auto-save-visited-file-name - (not buffer-read-only)) - buffer-file-name - (make-auto-save-file-name)))) + :variable ((and buffer-auto-save-file-name + ;; If auto-save is off because buffer has shrunk, + ;; then toggling should turn it on. + (>= buffer-saved-size 0)) + . (lambda (val) + (setq buffer-auto-save-file-name + (cond + ((null val) nil) + ((and buffer-file-name auto-save-visited-file-name + (not buffer-read-only)) + buffer-file-name) + (t (make-auto-save-file-name)))))) ;; If -1 was stored here, to temporarily turn off saving, ;; turn it back on. (and (< buffer-saved-size 0) - (setq buffer-saved-size 0)) - (if (called-interactively-p 'interactive) - (message "Auto-save %s (in this buffer)" - (if buffer-auto-save-file-name "on" "off"))) - buffer-auto-save-file-name) + (setq buffer-saved-size 0))) (defun rename-auto-save-file () "Adjust current buffer's auto save file name for current conditions. -- 2.39.2