From 1510152169b6cc8b5cb1d0daa59578cec1373f13 Mon Sep 17 00:00:00 2001 From: Allen Li Date: Sun, 31 Dec 2017 20:33:21 -0800 Subject: [PATCH] Skip writing empty abbrev tables Fixes bug#29923 insert-abbrev-table-description with a non-nil READABLE inserts Lisp forms suitable for evaluation to restore the defined abbrevs. We don't have to insert a form for tables that do not have any abbrevs. To implement this, we need to filter out system abbrevs before checking if a table is empty, because system abbrevs were previously skipped in the abbrev--write call, at which point we would already have started inserting the beginning of a table definition form. * lisp/abbrev.el (insert-abbrev-table-description): Skip inserting empty tables when READABLE is non-nil. Clarify behavior in documentation string. * lisp/abbrev.el (abbrev--write): Remove system abbrev check. --- doc/lispref/abbrevs.texi | 16 ++++---- etc/NEWS | 6 +++ lisp/abbrev.el | 85 +++++++++++++++++++++------------------- 3 files changed, 60 insertions(+), 47 deletions(-) diff --git a/doc/lispref/abbrevs.texi b/doc/lispref/abbrevs.texi index 087e694520..32d1d8d036 100644 --- a/doc/lispref/abbrevs.texi +++ b/doc/lispref/abbrevs.texi @@ -112,17 +112,19 @@ Abbrev Tables @code{define-abbrev-table} adds the new abbrev table name to this list. @end defvar -@defun insert-abbrev-table-description name &optional human +@defun insert-abbrev-table-description name &optional readable This function inserts before point a description of the abbrev table named @var{name}. The argument @var{name} is a symbol whose value is an abbrev table. @c The return value is always @code{nil}. -If @var{human} is non-@code{nil}, the description is human-oriented. -System abbrevs are listed and identified as such. Otherwise the -description is a Lisp expression---a call to @code{define-abbrev-table} -that would define @var{name} as it is currently defined, but without -the system abbrevs. (The mode or package using @var{name} is supposed -to add these to @var{name} separately.) +If @var{readable} is non-@code{nil}, the description is +human-oriented. System abbrevs are listed and identified as such. +Otherwise the description is a Lisp expression, a call to +@code{define-abbrev-table}, that would define @var{name} as it is +currently defined, but without the system abbrevs (the mode or package +using @var{name} is supposed to add these to @var{name} separately). +However, if the resulting expression would not define any abbrevs, +nothing is inserted. @end defun @node Defining Abbrevs diff --git a/etc/NEWS b/etc/NEWS index bc6791b05b..9032f90643 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -246,6 +246,12 @@ case does not match. 'write-abbrev-file' now writes special properties like ':case-fixed' for abbrevs that have them. ++++ +** 'insert-abbrev-table-description' skips empty tables. +'insert-abbrev-table-description' skips inserting empty tables when +inserting non-readable tables. By extension, this makes +'write-abbrev-file' skip writing empty tables. + +++ ** The new functions and commands 'text-property-search-forward' and 'text-property-search-backward' have been added. These provide an diff --git a/lisp/abbrev.el b/lisp/abbrev.el index cddce8f529..e1fd366ba9 100644 --- a/lisp/abbrev.el +++ b/lisp/abbrev.el @@ -896,24 +896,22 @@ unexpand-abbrev (defun abbrev--write (sym) "Write the abbrev in a `read'able form. -Only writes the non-system abbrevs. Presumes that `standard-output' points to `current-buffer'." - (unless (or (null (symbol-value sym)) (abbrev-get sym :system)) - (insert " (") - (prin1 (symbol-name sym)) - (insert " ") - (prin1 (symbol-value sym)) - (insert " ") - (prin1 (symbol-function sym)) - (insert " :count ") - (prin1 (abbrev-get sym :count)) - (when (abbrev-get sym :case-fixed) - (insert " :case-fixed ") - (prin1 (abbrev-get sym :case-fixed))) - (when (abbrev-get sym :enable-function) - (insert " :enable-function ") - (prin1 (abbrev-get sym :enable-function))) - (insert ")\n"))) + (insert " (") + (prin1 (symbol-name sym)) + (insert " ") + (prin1 (symbol-value sym)) + (insert " ") + (prin1 (symbol-function sym)) + (insert " :count ") + (prin1 (abbrev-get sym :count)) + (when (abbrev-get sym :case-fixed) + (insert " :case-fixed ") + (prin1 (abbrev-get sym :case-fixed))) + (when (abbrev-get sym :enable-function) + (insert " :enable-function ") + (prin1 (abbrev-get sym :enable-function))) + (insert ")\n")) (defun abbrev--describe (sym) (when (symbol-value sym) @@ -934,31 +932,38 @@ insert-abbrev-table-description "Insert before point a full description of abbrev table named NAME. NAME is a symbol whose value is an abbrev table. If optional 2nd arg READABLE is non-nil, a human-readable description -is inserted. Otherwise the description is an expression, -a call to `define-abbrev-table', which would -define the abbrev table NAME exactly as it is currently defined. - -Abbrevs marked as \"system abbrevs\" are omitted." +is inserted. + +If READABLE is nil, an expression is inserted. The expression is +a call to `define-abbrev-table' that when evaluated will define +the abbrev table NAME exactly as it is currently defined. +Abbrevs marked as \"system abbrevs\" are ignored. If the +resulting expression would not define any abbrevs, nothing is +inserted." (let ((table (symbol-value name)) (symbols ())) - (mapatoms (lambda (sym) (if (symbol-value sym) (push sym symbols))) table) - (setq symbols (sort symbols 'string-lessp)) - (let ((standard-output (current-buffer))) - (if readable - (progn - (insert "(") - (prin1 name) - (insert ")\n\n") - (mapc 'abbrev--describe symbols) - (insert "\n\n")) - (insert "(define-abbrev-table '") - (prin1 name) - (if (null symbols) - (insert " '())\n\n") - (insert "\n '(\n") - (mapc 'abbrev--write symbols) - (insert " ))\n\n"))) - nil))) + (mapatoms (lambda (sym) + (if (and (symbol-value sym) (or readable (not (abbrev-get sym :system)))) + (push sym symbols))) + table) + (when symbols + (setq symbols (sort symbols 'string-lessp)) + (let ((standard-output (current-buffer))) + (if readable + (progn + (insert "(") + (prin1 name) + (insert ")\n\n") + (mapc 'abbrev--describe symbols) + (insert "\n\n")) + (insert "(define-abbrev-table '") + (prin1 name) + (if (null symbols) + (insert " '())\n\n") + (insert "\n '(\n") + (mapc 'abbrev--write symbols) + (insert " ))\n\n"))) + nil)))) (defun define-abbrev-table (tablename definitions &optional docstring &rest props) -- 2.19.0