diff options
| author | Allen Li | 2018-09-29 15:19:04 -0700 |
|---|---|---|
| committer | Eli Zaretskii | 2018-10-12 11:44:15 +0300 |
| commit | f5896e2cbf0e537ec6b79ba139220239f934c840 (patch) | |
| tree | 51604349e1190449caf75043d689694a84212441 | |
| parent | 5bd8cfc14d4b0c78c07e65a583f42a10c4cbc06d (diff) | |
| download | emacs-f5896e2cbf0e537ec6b79ba139220239f934c840.tar.gz emacs-f5896e2cbf0e537ec6b79ba139220239f934c840.zip | |
Rework empty abbrev table omitting
There were two problems with the original implementation:
1. It changed the behavior of insert-abbrev-table-description when
READABLE is nil to sometimes insert one Emacs Lisp expression and
sometimes insert nothing.
2. It broke the tests.
This commit reworks this so that insert-abbrev-table-description
always inserts an expressions even if no abbrevs need to be saved and
making only write-abbrev-file check that a table has any abbrevs to
save before calling insert-abbrev-table-description. This duplicates
the work of filtering the table for savable abbrevs, but the benefit
of keeping the API is worth it.
* doc/lispref/abbrevs.texi (Abbrev Tables): Update documentation.
* lisp/abbrev.el (write-abbrev-file): Skip tables without user abbrevs
(insert-abbrev-table-description): Always insert the define
expression.
(abbrev--table-symbols): New function.
* test/lisp/abbrev-tests.el (abbrev--table-symbols-test):
Add test for abbrev--table-symbols.
| -rw-r--r-- | doc/lispref/abbrevs.texi | 7 | ||||
| -rw-r--r-- | etc/NEWS | 7 | ||||
| -rw-r--r-- | lisp/abbrev.el | 52 | ||||
| -rw-r--r-- | test/lisp/abbrev-tests.el | 8 |
4 files changed, 43 insertions, 31 deletions
diff --git a/doc/lispref/abbrevs.texi b/doc/lispref/abbrevs.texi index 4c9e653cb19..1e9471ba27a 100644 --- a/doc/lispref/abbrevs.texi +++ b/doc/lispref/abbrevs.texi | |||
| @@ -122,9 +122,7 @@ System abbrevs are listed and identified as such. Otherwise the | |||
| 122 | description is a Lisp expression---a call to @code{define-abbrev-table} | 122 | description is a Lisp expression---a call to @code{define-abbrev-table} |
| 123 | that would define @var{name} as it is currently defined, but without | 123 | that would define @var{name} as it is currently defined, but without |
| 124 | the system abbrevs. (The mode or package using @var{name} is supposed | 124 | the system abbrevs. (The mode or package using @var{name} is supposed |
| 125 | to add these to @var{name} separately.) If the Lisp expression would | 125 | to add these to @var{name} separately.) |
| 126 | not define any abbrevs (i.e.@: it defines an empty abbrev table), this | ||
| 127 | function inserts nothing. | ||
| 128 | @end defun | 126 | @end defun |
| 129 | 127 | ||
| 130 | @node Defining Abbrevs | 128 | @node Defining Abbrevs |
| @@ -234,7 +232,8 @@ Emacs commands to offer to save your abbrevs. | |||
| 234 | Save all abbrev definitions (except system abbrevs), for all abbrev | 232 | Save all abbrev definitions (except system abbrevs), for all abbrev |
| 235 | tables listed in @code{abbrev-table-name-list}, in the file | 233 | tables listed in @code{abbrev-table-name-list}, in the file |
| 236 | @var{filename}, in the form of a Lisp program that when loaded will | 234 | @var{filename}, in the form of a Lisp program that when loaded will |
| 237 | define the same abbrevs. If @var{filename} is @code{nil} or omitted, | 235 | define the same abbrevs. Tables that do not have any abbrevs to save |
| 236 | are omitted. If @var{filename} is @code{nil} or omitted, | ||
| 238 | @code{abbrev-file-name} is used. This function returns @code{nil}. | 237 | @code{abbrev-file-name} is used. This function returns @code{nil}. |
| 239 | @end deffn | 238 | @end deffn |
| 240 | 239 | ||
| @@ -258,10 +258,9 @@ case does not match. | |||
| 258 | for abbrevs that have them. | 258 | for abbrevs that have them. |
| 259 | 259 | ||
| 260 | +++ | 260 | +++ |
| 261 | ** 'insert-abbrev-table-description' skips empty tables. | 261 | ** 'write-abbrev-file' skips empty tables. |
| 262 | 'insert-abbrev-table-description' skips inserting empty tables when | 262 | 'write-abbrev-file' now skips inserting a 'define-abbrev-table' form for |
| 263 | inserting non-readable tables. By extension, this makes | 263 | tables which do not have any non-system abbrevs to save. |
| 264 | 'write-abbrev-file' skip writing empty tables. | ||
| 265 | 264 | ||
| 266 | +++ | 265 | +++ |
| 267 | ** The new functions and commands 'text-property-search-forward' and | 266 | ** The new functions and commands 'text-property-search-forward' and |
diff --git a/lisp/abbrev.el b/lisp/abbrev.el index e1fd366ba9e..20a967d7d61 100644 --- a/lisp/abbrev.el +++ b/lisp/abbrev.el | |||
| @@ -251,7 +251,8 @@ have been saved." | |||
| 251 | (lambda (s1 s2) | 251 | (lambda (s1 s2) |
| 252 | (string< (symbol-name s1) | 252 | (string< (symbol-name s1) |
| 253 | (symbol-name s2))))) | 253 | (symbol-name s2))))) |
| 254 | (insert-abbrev-table-description table nil)) | 254 | (if (abbrev--table-symbols table) |
| 255 | (insert-abbrev-table-description table nil))) | ||
| 255 | (when (unencodable-char-position (point-min) (point-max) 'utf-8) | 256 | (when (unencodable-char-position (point-min) (point-max) 'utf-8) |
| 256 | (setq coding-system-for-write | 257 | (setq coding-system-for-write |
| 257 | (if (> emacs-major-version 24) | 258 | (if (> emacs-major-version 24) |
| @@ -937,33 +938,38 @@ is inserted. | |||
| 937 | If READABLE is nil, an expression is inserted. The expression is | 938 | If READABLE is nil, an expression is inserted. The expression is |
| 938 | a call to `define-abbrev-table' that when evaluated will define | 939 | a call to `define-abbrev-table' that when evaluated will define |
| 939 | the abbrev table NAME exactly as it is currently defined. | 940 | the abbrev table NAME exactly as it is currently defined. |
| 940 | Abbrevs marked as \"system abbrevs\" are ignored. If the | 941 | Abbrevs marked as \"system abbrevs\" are ignored." |
| 941 | resulting expression would not define any abbrevs, nothing is | 942 | (let ((table (symbol-value name)) |
| 942 | inserted." | 943 | (symbols (abbrev--table-symbols name readable))) |
| 944 | (setq symbols (sort symbols 'string-lessp)) | ||
| 945 | (let ((standard-output (current-buffer))) | ||
| 946 | (if readable | ||
| 947 | (progn | ||
| 948 | (insert "(") | ||
| 949 | (prin1 name) | ||
| 950 | (insert ")\n\n") | ||
| 951 | (mapc 'abbrev--describe symbols) | ||
| 952 | (insert "\n\n")) | ||
| 953 | (insert "(define-abbrev-table '") | ||
| 954 | (prin1 name) | ||
| 955 | (if (null symbols) | ||
| 956 | (insert " '())\n\n") | ||
| 957 | (insert "\n '(\n") | ||
| 958 | (mapc 'abbrev--write symbols) | ||
| 959 | (insert " ))\n\n"))) | ||
| 960 | nil))) | ||
| 961 | |||
| 962 | (defun abbrev--table-symbols (name &optional system) | ||
| 963 | "Return the user abbrev symbols in the abbrev table named NAME. | ||
| 964 | NAME is a symbol whose value is an abbrev table. System abbrevs | ||
| 965 | are omitted unless SYSTEM is non-nil." | ||
| 943 | (let ((table (symbol-value name)) | 966 | (let ((table (symbol-value name)) |
| 944 | (symbols ())) | 967 | (symbols ())) |
| 945 | (mapatoms (lambda (sym) | 968 | (mapatoms (lambda (sym) |
| 946 | (if (and (symbol-value sym) (or readable (not (abbrev-get sym :system)))) | 969 | (if (and (symbol-value sym) (or system (not (abbrev-get sym :system)))) |
| 947 | (push sym symbols))) | 970 | (push sym symbols))) |
| 948 | table) | 971 | table) |
| 949 | (when symbols | 972 | symbols)) |
| 950 | (setq symbols (sort symbols 'string-lessp)) | ||
| 951 | (let ((standard-output (current-buffer))) | ||
| 952 | (if readable | ||
| 953 | (progn | ||
| 954 | (insert "(") | ||
| 955 | (prin1 name) | ||
| 956 | (insert ")\n\n") | ||
| 957 | (mapc 'abbrev--describe symbols) | ||
| 958 | (insert "\n\n")) | ||
| 959 | (insert "(define-abbrev-table '") | ||
| 960 | (prin1 name) | ||
| 961 | (if (null symbols) | ||
| 962 | (insert " '())\n\n") | ||
| 963 | (insert "\n '(\n") | ||
| 964 | (mapc 'abbrev--write symbols) | ||
| 965 | (insert " ))\n\n"))) | ||
| 966 | nil)))) | ||
| 967 | 973 | ||
| 968 | (defun define-abbrev-table (tablename definitions | 974 | (defun define-abbrev-table (tablename definitions |
| 969 | &optional docstring &rest props) | 975 | &optional docstring &rest props) |
diff --git a/test/lisp/abbrev-tests.el b/test/lisp/abbrev-tests.el index facf097815e..e50f931cef5 100644 --- a/test/lisp/abbrev-tests.el +++ b/test/lisp/abbrev-tests.el | |||
| @@ -64,6 +64,14 @@ | |||
| 64 | (should (= (length table) obarray-default-size)) | 64 | (should (= (length table) obarray-default-size)) |
| 65 | (should (eq (abbrev-table-get table 'foo) 'bar)))) | 65 | (should (eq (abbrev-table-get table 'foo) 'bar)))) |
| 66 | 66 | ||
| 67 | (ert-deftest abbrev--table-symbols-test () | ||
| 68 | (let ((ert-test-abbrevs (setup-test-abbrev-table))) | ||
| 69 | (define-abbrev ert-test-abbrevs "sys" "system abbrev" nil :system t) | ||
| 70 | (should (equal (mapcar #'symbol-name (abbrev--table-symbols 'ert-test-abbrevs)) | ||
| 71 | '("a-e-t"))) | ||
| 72 | (should (equal (mapcar #'symbol-name (abbrev--table-symbols 'ert-test-abbrevs t)) | ||
| 73 | '("a-e-t" "sys"))))) | ||
| 74 | |||
| 67 | (ert-deftest abbrev-table-get-put-test () | 75 | (ert-deftest abbrev-table-get-put-test () |
| 68 | (let ((table (make-abbrev-table))) | 76 | (let ((table (make-abbrev-table))) |
| 69 | (should-not (abbrev-table-get table 'foo)) | 77 | (should-not (abbrev-table-get table 'foo)) |