Subject: bug#5937: 23.1.95; why saving empty abbrev tables

From: Leo <[email-masked]>
Date: Tue, 29 Mar 2011 12:41:44 +0800

On 2011-03-29 11:31 +0800, Stefan Monnier wrote:
> Oh, I didn't notice this part of the patch.  So I guess what I'm
> suggesting is to create a new command for use in C-x C-w which calls
> edit-abbrevs-redefine and then calls interactively write-abbrev-file.
> The issue with write-abbrev-file is that it's called from
> save-some-buffers so we can't/shouldn't bind it to edit-abbrevs.

Thanks for that. I forgot write-abbrev-file is also called elsewhere.
How about this patch?

diff --git a/lisp/abbrev.el b/lisp/abbrev.el
index 0a3e1fd1..b1d9e024 100644
--- a/lisp/abbrev.el
+++ b/lisp/abbrev.el
@@ -73,7 +73,8 @@ (put 'abbrev-mode 'safe-local-variable 'booleanp)
 
 (defvar edit-abbrevs-map
   (let ((map (make-sparse-keymap)))
-    (define-key map "\C-x\C-s" 'edit-abbrevs-redefine)
+    (define-key map "\C-x\C-s" 'abbrev-save-buffer)
+    (define-key map "\C-x\C-w" 'abbrev-save-to-file)
     (define-key map "\C-c\C-c" 'edit-abbrevs-redefine)
     map)
   "Keymap used in `edit-abbrevs'.")
@@ -224,13 +225,15 @@ (defun quietly-read-abbrev-file (&optional file)
 					;(interactive "fRead abbrev file: ")
   (read-abbrev-file file t))
 
-(defun write-abbrev-file (&optional file)
+(defun write-abbrev-file (&optional file verbose)
   "Write all user-level abbrev definitions to a file of Lisp code.
 This does not include system abbrevs; it includes only the abbrev tables
 listed in listed in `abbrev-table-name-list'.
 The file written can be loaded in another session to define the same abbrevs.
 The argument FILE is the file name to write.  If omitted or nil, the file
-specified in `abbrev-file-name' is used."
+specified in `abbrev-file-name' is used.
+If VERBOSE is non-nil, display a message indicating where abbrevs
+have been saved."
   (interactive
    (list
     (read-file-name "Write abbrev file: "
@@ -260,7 +263,28 @@ (defun write-abbrev-file (&optional file)
 		'emacs-mule)))
       (goto-char (point-min))
       (insert (format ";;-*-coding: %s;-*-\n" coding-system-for-write))
-      (write-region nil nil file nil 0))))
+      (write-region nil nil file nil (and (not verbose) 0)))))
+
+(defun abbrev-save-buffer ()
+  "Save all user-level abbrev definitions in current buffer.
+The saved abbrevs are written to the file specified by
+`abbrev-file-name'."
+  (interactive)
+  (and (derived-mode-p 'edit-abbrevs-mode)
+       (edit-abbrevs-redefine))
+  (write-abbrev-file abbrev-file-name t))
+
+(defun abbrev-save-to-file (file)
+  "Save all user-level abbrev definitions in current buffer to FILE."
+  (interactive
+   (list (read-file-name "Save abbrevs to file: "
+			 (file-name-directory
+			  (expand-file-name abbrev-file-name))
+			 abbrev-file-name)))
+  (and (derived-mode-p 'edit-abbrevs-mode)
+       (edit-abbrevs-redefine))
+  (write-abbrev-file file t))
+
 
 (defun add-mode-abbrev (arg)
   "Define mode-specific abbrev for last word(s) before point.

> Maybe another solution is to change edit-abbrevs-mode so that
> save-buffer and write-file work in it by calling internally
> write-abbrev-file.  Note sure if that can be made to work without major
> surgery, tho (write-region-annotate-functions could do some of it, but
> I think the lack of buffer-file-name setting will get in the way, so
> we'd have to change edit-abbrevs so that it sets buffer-file-name and
> is really turned into a major mode to edit the .abbrev-defs file, which
> seems like a fairly different beast).
>
>
>         Stefan

This isn't cleaner at the moment. So probably leave it for future
improvement.

Leo