Disable "in-tree" builds.
[sxemacs] / lisp / abbrev.el
1 ;;; abbrev.el --- abbrev mode commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1997 Free Software Foundation, Inc.
4
5 ;; Maintainer: SXEmacs Development Team
6 ;; Keywords: abbrev, dumped
7
8 ;; This file is part of SXEmacs.
9
10 ;; SXEmacs is free software: you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by the
12 ;; Free Software Foundation, either version 3 of the License, or (at your
13 ;; option) any later version.
14
15 ;; SXEmacs is distributed in the hope that it will be
16 ;; useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23
24 ;;; Synched up with: FSF 19.34 (With some additions)
25
26 ;;; Commentary:
27
28 ;; This file is dumped with XEmacs.
29
30 ;; This facility is documented in the Emacs Manual.
31
32 ;;; Code:
33
34 (defgroup abbrev nil
35   "Abbreviation handling, typing shortcuts, macros."
36   :tag "Abbreviations"
37   :group 'editing)
38
39 (defgroup abbrev-mode nil
40   "Word abbreviations mode."
41   :group 'abbrev)
42
43 ;jwz: this is preloaded so don't ;;;###autoload
44 (defcustom only-global-abbrevs nil
45   "*Non-nil means user plans to use global abbrevs only.
46 Makes the commands to define mode-specific abbrevs define global ones instead."
47   :type 'boolean
48   :group 'abbrev)
49
50 ;;; XEmacs: the following block of code is not in FSF
51 (defvar abbrev-table-name-list '()
52   "List of symbols whose values are abbrev tables.")
53
54 (defvar abbrevs-changed nil
55   "Set non-nil by defining or altering any word abbrevs.
56 This causes `save-some-buffers' to offer to save the abbrevs.")
57
58 (defun make-abbrev-table ()
59   "Return a new, empty abbrev table object."
60   (make-vector 59 0)) ; 59 is prime
61
62 (defun clear-abbrev-table (table)
63   "Undefine all abbrevs in abbrev table TABLE, leaving it empty."
64   (fillarray table 0)
65   (setq abbrevs-changed t)
66   nil)
67
68
69 (defun define-abbrev-table (table-name definitions)
70   "Define TABLE-NAME (a symbol) as an abbrev table name.
71 Define abbrevs in it according to DEFINITIONS, which is a list of elements
72 of the form (ABBREVNAME EXPANSION HOOK USECOUNT)."
73   (let ((table (and (boundp table-name) (symbol-value table-name))))
74     (cond ((vectorp table))
75           ((not table)
76            (setq table (make-abbrev-table))
77            (set table-name table)
78            (setq abbrev-table-name-list (cons table-name abbrev-table-name-list)))
79           (t
80            (setq table (wrong-type-argument 'vectorp table))
81            (set table-name table)))
82     (while definitions
83       (apply (function define-abbrev) table (car definitions))
84       (setq definitions (cdr definitions)))))
85
86 (defun define-abbrev (table name &optional expansion hook count)
87   "Define an abbrev in TABLE named NAME, to expand to EXPANSION or call HOOK.
88 NAME and EXPANSION are strings.  Hook is a function or `nil'.
89 To undefine an abbrev, define it with an expansion of `nil'."
90   (check-type expansion (or null string))
91   (check-type count (or null integer))
92   (check-type table vector)
93   (let* ((sym (intern name table))
94          (oexp (and (boundp sym) (symbol-value sym)))
95          (ohook (and (fboundp sym) (symbol-function sym))))
96     (unless (and (equal ohook hook)
97                  (stringp oexp)
98                  (stringp expansion)
99                  (string-equal oexp expansion))
100       (setq abbrevs-changed t)
101       ;; If there is a non-word character in the string, set the flag.
102       (if (string-match "\\W" name)
103           (set (intern " " table) nil)))
104     (set sym expansion)
105     (fset sym hook)
106     (setplist sym (or count 0))
107     name))
108
109
110 ;; Fixup stuff from bootstrap def of define-abbrev-table in subr.el
111 (let ((l abbrev-table-name-list))
112   (while l
113     (let ((fixup (car l)))
114       (if (consp fixup)
115           (progn
116             (setq abbrev-table-name-list (delq fixup abbrev-table-name-list))
117             (define-abbrev-table (car fixup) (cdr fixup))))
118       (setq l (cdr l))))
119   ;; These are no longer initialized by C code
120   (if (not global-abbrev-table)
121       (progn
122         (setq global-abbrev-table (make-abbrev-table))
123         (setq abbrev-table-name-list (cons 'global-abbrev-table
124                                            abbrev-table-name-list))))
125   (if (not fundamental-mode-abbrev-table)
126       (progn
127         (setq fundamental-mode-abbrev-table (make-abbrev-table))
128         (setq abbrev-table-name-list (cons 'fundamental-mode-abbrev-table
129                                            abbrev-table-name-list))))
130   (and (eq major-mode 'fundamental-mode)
131        (not local-abbrev-table)
132        (setq local-abbrev-table fundamental-mode-abbrev-table)))
133
134
135 (defun define-global-abbrev (name expansion)
136   "Define ABBREV as a global abbreviation for EXPANSION."
137   (interactive "sDefine global abbrev: \nsExpansion for %s: ")
138   (define-abbrev global-abbrev-table
139                  (downcase name) expansion nil 0))
140
141 (defun define-mode-abbrev (name expansion)
142   "Define ABBREV as a mode-specific abbreviation for EXPANSION."
143   (interactive "sDefine mode abbrev: \nsExpansion for %s: ")
144   (define-abbrev (or local-abbrev-table
145                      (error "Major mode has no abbrev table"))
146                  (downcase name) expansion nil 0))
147
148 (defun abbrev-symbol (abbrev &optional table)
149   "Return the symbol representing abbrev named ABBREV.
150 This symbol's name is ABBREV, but it is not the canonical symbol of that name;
151 it is interned in an abbrev-table rather than the normal obarray.
152 The value is nil if that abbrev is not defined.
153 Optional second arg TABLE is abbrev table to look it up in.
154 The default is to try buffer's mode-specific abbrev table, then global table."
155   (let ((frob (function (lambda (table)
156                 (let ((sym (intern-soft abbrev table)))
157                   (if (and (boundp sym)
158                            (stringp (symbol-value sym)))
159                       sym
160                       nil))))))
161     (if table
162         (funcall frob table)
163         (or (and local-abbrev-table
164                  (funcall frob local-abbrev-table))
165             (funcall frob global-abbrev-table)))))
166
167 (defun abbrev-expansion (abbrev &optional table)
168   "Return the string that ABBREV expands into in the current buffer.
169 Optionally specify an abbrev table as second arg;
170 then ABBREV is looked up in that table only."
171   (let ((sym (abbrev-symbol abbrev table)))
172     (if sym
173         (symbol-value sym)
174         nil)))
175
176 (defun unexpand-abbrev ()
177   "Undo the expansion of the last abbrev that expanded.
178 This differs from ordinary undo in that other editing done since then
179 is not undone."
180   (interactive)
181   (if (or (< last-abbrev-location (point-min))
182           (> last-abbrev-location (point-max))
183           (not (stringp last-abbrev-text)))
184       nil
185     (let* ((opoint (point))
186            (val (symbol-value last-abbrev))
187            (adjust (length val)))
188       ;; This isn't correct if (symbol-function last-abbrev-text)
189       ;;  was used to do the expansion
190       (goto-char last-abbrev-location)
191       (delete-region last-abbrev-location (+ last-abbrev-location adjust))
192       (insert last-abbrev-text)
193       (setq adjust (- adjust (length last-abbrev-text)))
194       (setq last-abbrev-text nil)
195       (if (< last-abbrev-location opoint)
196           (goto-char (- opoint adjust))
197           (goto-char opoint)))))
198
199 \f
200
201 (defun insert-abbrev-table-description (name &optional human-readable)
202   "Insert before point a full description of abbrev table named NAME.
203 NAME is a symbol whose value is an abbrev table.
204 If optional second argument HUMAN-READABLE is non-nil, insert a
205 human-readable description. Otherwise the description is an
206 expression, a call to `define-abbrev-table', which would define the
207 abbrev table NAME exactly as it is currently defined."
208   (let ((table (symbol-value name))
209         (stream (current-buffer)))
210     (message "Abbrev-table %s..." name)
211     (if human-readable
212         (progn
213           (prin1 (list name) stream)
214           ;; Need two terpri's or cretinous edit-abbrevs blows out
215           (terpri stream)
216           (terpri stream)
217           (mapatoms (function (lambda (sym)
218                       (if (symbol-value sym)
219                           (let* ((n (prin1-to-string (symbol-name sym)))
220                                  (pos (length n)))
221                             (princ n stream)
222                             (while (< pos 14)
223                               (write-char ?\  stream)
224                               (setq pos (1+ pos)))
225                             (princ (format " %-5S " (symbol-plist sym))
226                                    stream)
227                             (if (not (symbol-function sym))
228                                 (prin1 (symbol-value sym) stream)
229                               (progn
230                                 (setq n (prin1-to-string (symbol-value sym))
231                                       pos (+ pos 6 (length n)))
232                                 (princ n stream)
233                                 (while (< pos 45)
234                                   (write-char ?\  stream)
235                                   (setq pos (1+ pos)))
236                                 (prin1 (symbol-function sym) stream)))
237                             (terpri stream)))))
238                     table)
239           (terpri stream))
240         (progn
241           (princ "\(define-abbrev-table '" stream)
242           (prin1 name stream)
243           (princ " '\(\n" stream)
244           (mapatoms (function (lambda (sym)
245                       (if (symbol-value sym)
246                           (progn
247                             (princ "    " stream)
248                             (prin1 (list (symbol-name sym)
249                                          (symbol-value sym)
250                                          (symbol-function sym)
251                                          (symbol-plist sym))
252                                    stream)
253                             (terpri stream)))))
254                     table)
255           (princ "    \)\)\n" stream)))
256     (terpri stream))
257   (message ""))
258 ;;; End code not in FSF
259
260 (defun abbrev-mode (arg)
261   "Toggle abbrev mode.
262 With argument ARG, enable abbrev mode if ARG is positive, else disable.
263 In abbrev mode, inserting an abbreviation causes it to expand
264 and be replaced by its expansion."
265   (interactive "P")
266   (setq abbrev-mode
267         (if (null arg) (not abbrev-mode)
268           (> (prefix-numeric-value arg) 0)))
269   ;; XEmacs change
270   (redraw-modeline))
271
272 \f
273 (defvar edit-abbrevs-map nil
274   "Keymap used in edit-abbrevs.")
275 (if edit-abbrevs-map
276     nil
277   (setq edit-abbrevs-map (make-sparse-keymap))
278   ;; XEmacs change
279   (set-keymap-name edit-abbrevs-map 'edit-abbrevs-map)
280   (define-key edit-abbrevs-map "\C-x\C-s" 'edit-abbrevs-redefine)
281   (define-key edit-abbrevs-map "\C-c\C-c" 'edit-abbrevs-redefine))
282
283 (defun kill-all-abbrevs ()
284   "Undefine all defined abbrevs."
285   (interactive)
286   (let ((tables abbrev-table-name-list))
287     (while tables
288       (clear-abbrev-table (symbol-value (car tables)))
289       (setq tables (cdr tables)))))
290
291 (defun insert-abbrevs ()
292   "Insert after point a description of all defined abbrevs.
293 Mark is set after the inserted text."
294   (interactive)
295   (push-mark
296    (save-excursion
297     (let ((tables abbrev-table-name-list))
298       (while tables
299         (insert-abbrev-table-description (car tables) t)
300         (setq tables (cdr tables))))
301     (point))))
302
303 (defun list-abbrevs ()
304   "Display a list of all defined abbrevs."
305   (interactive)
306   (display-buffer (prepare-abbrev-list-buffer)))
307
308 (defun prepare-abbrev-list-buffer ()
309   (save-excursion
310     (set-buffer (get-buffer-create "*Abbrevs*"))
311     (erase-buffer)
312     (let ((tables abbrev-table-name-list))
313       (while tables
314         (insert-abbrev-table-description (car tables) t)
315         (setq tables (cdr tables))))
316     (goto-char (point-min))
317     (set-buffer-modified-p nil)
318     (edit-abbrevs-mode))
319   (get-buffer-create "*Abbrevs*"))
320
321 (defun edit-abbrevs-mode ()
322   "Major mode for editing the list of abbrev definitions.
323 \\{edit-abbrevs-map}"
324   (interactive)
325   (setq major-mode 'edit-abbrevs-mode)
326   (setq mode-name "Edit-Abbrevs")
327   (use-local-map edit-abbrevs-map))
328
329 (defun edit-abbrevs ()
330   "Alter abbrev definitions by editing a list of them.
331 Selects a buffer containing a list of abbrev definitions.
332 You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
333 according to your editing.
334 Buffer contains a header line for each abbrev table,
335  which is the abbrev table name in parentheses.
336 This is followed by one line per abbrev in that table:
337 NAME   USECOUNT   EXPANSION   HOOK
338 where NAME and EXPANSION are strings with quotes,
339 USECOUNT is an integer, and HOOK is any valid function
340 or may be omitted (it is usually omitted)."
341   (interactive)
342   (switch-to-buffer (prepare-abbrev-list-buffer)))
343
344 (defun edit-abbrevs-redefine ()
345   "Redefine abbrevs according to current buffer contents."
346   (interactive)
347   (define-abbrevs t)
348   (set-buffer-modified-p nil))
349
350 (defun define-abbrevs (&optional arg)
351   "Define abbrevs according to current visible buffer contents.
352 See documentation of `edit-abbrevs' for info on the format of the
353 text you must have in the buffer.
354 With argument, eliminate all abbrev definitions except
355 the ones defined from the buffer now."
356   (interactive "P")
357   (if arg (kill-all-abbrevs))
358   (save-excursion
359    (goto-char (point-min))
360    (while (and (not (eobp)) (re-search-forward "^(" nil t))
361      (let* ((buf (current-buffer))
362             (table (read buf))
363             abbrevs name hook exp count)
364        (forward-line 1)
365        (while (progn (forward-line 1)
366                      (not (eolp)))
367          (setq name (read buf) count (read buf) exp (read buf))
368          (skip-chars-backward " \t\n\f")
369          (setq hook (if (not (eolp)) (read buf)))
370          (skip-chars-backward " \t\n\f")
371          (setq abbrevs (cons (list name exp hook count) abbrevs)))
372        (define-abbrev-table table abbrevs)))))
373
374 (defun read-abbrev-file (&optional file quietly)
375   "Read abbrev definitions from file written with `write-abbrev-file'.
376 Optional argument FILE is the name of the file to read;
377 it defaults to the value of `abbrev-file-name'.
378 Optional second argument QUIETLY non-nil means don't print anything."
379   (interactive "fRead abbrev file: ")
380   (load (if (and file (> (length file) 0)) file abbrev-file-name)
381         nil quietly)
382   (setq save-abbrevs t abbrevs-changed nil))
383
384 (defun quietly-read-abbrev-file (&optional file)
385   "Read abbrev definitions from file written with `write-abbrev-file'.
386 Optional argument FILE is the name of the file to read;
387 it defaults to the value of `abbrev-file-name'.
388 Does not print anything."
389   ;(interactive "fRead abbrev file: ")
390   (read-abbrev-file file t))
391
392 (defun write-abbrev-file (file)
393   "Write all abbrev definitions to a file of Lisp code.
394 The file written can be loaded in another session to define the same abbrevs.
395 The argument FILE is the file name to write."
396   (interactive
397    (list
398     (read-file-name "Write abbrev file: "
399                     (file-name-directory (expand-file-name abbrev-file-name))
400                     abbrev-file-name)))
401   (or (and file (> (length file) 0))
402       (setq file abbrev-file-name))
403   (save-excursion
404    (set-buffer (get-buffer-create " write-abbrev-file"))
405    (erase-buffer)
406    (let ((tables abbrev-table-name-list))
407      (while tables
408        (insert-abbrev-table-description (car tables) nil)
409        (setq tables (cdr tables))))
410    (write-region 1 (point-max) file)
411    (erase-buffer)))
412 \f
413 (defun abbrev-string-to-be-defined (arg)
414   "Return the string for which an abbrev will be defined.
415 ARG is the argument to `add-global-abbrev' or `add-mode-abbrev'."
416   (if (and (not arg) (region-active-p)) (setq arg 0)
417     (setq arg (prefix-numeric-value arg)))
418   (and (>= arg 0)
419        (buffer-substring
420         (point)
421         (if (= arg 0) (mark)
422           (save-excursion (backward-word arg) (point))))))
423
424 (defun add-mode-abbrev (arg)
425   "Define mode-specific abbrev for last word(s) before point.
426 Argument is how many words before point form the expansion;
427 or zero means the region is the expansion.
428 A negative argument means to undefine the specified abbrev.
429 Reads the abbreviation in the minibuffer.
430
431 Don't use this function in a Lisp program; use `define-abbrev' instead."
432   ;; XEmacs change:
433   (interactive "P")
434   (add-abbrev
435    (if only-global-abbrevs
436        global-abbrev-table
437      (or local-abbrev-table
438          (error "No per-mode abbrev table")))
439    "Mode" arg))
440
441 (defun add-global-abbrev (arg)
442   "Define global (all modes) abbrev for last word(s) before point.
443 The prefix argument specifies the number of words before point that form the
444 expansion; or zero means the region is the expansion.
445 A negative argument means to undefine the specified abbrev.
446 This command uses the minibuffer to read the abbreviation.
447
448 Don't use this function in a Lisp program; use `define-abbrev' instead."
449   ;; XEmacs change:
450   (interactive "P")
451   (add-abbrev global-abbrev-table "Global" arg))
452
453 (defun add-abbrev (table type arg)
454   "Add an abbreviation to abbrev table TABLE.
455 TYPE is a string describing in English the kind of abbrev this will be
456 (typically, \"global\" or \"mode-specific\"); this is used in
457 prompting the user.  ARG is the number of words in the expansion.
458
459 Return the symbol that internally represents the new abbrev, or nil if
460 the user declines to confirm redefining an existing abbrev."
461   ;; XEmacs change:
462   (let ((exp (abbrev-string-to-be-defined arg))
463         name)
464     (setq name
465           (read-string (format (if exp "%s abbrev for \"%s\": "
466                                  "Undefine %s abbrev: ")
467                                type exp)))
468     (set-text-properties 0 (length name) nil name)
469     (if (or (null exp)
470             (not (abbrev-expansion name table))
471             (y-or-n-p (format "%s expands to \"%s\"; redefine? "
472                               name (abbrev-expansion name table))))
473         (define-abbrev table (downcase name) exp))))
474
475 (defun inverse-abbrev-string-to-be-defined (arg)
476   "Return the string for which an inverse abbrev will be defined.
477 ARG is the argument to `inverse-add-global-abbrev' or
478 `inverse-add-mode-abbrev'."
479   (save-excursion
480     (backward-word arg)
481     (buffer-substring (point) (progn (forward-word 1) (point)))))
482
483 (defun inverse-add-mode-abbrev (arg)
484   "Define last word before point as a mode-specific abbrev.
485 With prefix argument N, defines the Nth word before point.
486 This command uses the minibuffer to read the expansion.
487 Expands the abbreviation after defining it."
488   (interactive "p")
489   (inverse-add-abbrev
490    (if only-global-abbrevs
491        global-abbrev-table
492      (or local-abbrev-table
493          (error "No per-mode abbrev table")))
494    "Mode" arg))
495
496 (defun inverse-add-global-abbrev (arg)
497   "Define last word before point as a global (mode-independent) abbrev.
498 With prefix argument N, defines the Nth word before point.
499 This command uses the minibuffer to read the expansion.
500 Expands the abbreviation after defining it."
501   (interactive "p")
502   (inverse-add-abbrev global-abbrev-table "Global" arg))
503
504 (defun inverse-add-abbrev (table type arg)
505   (let (name nameloc exp)
506     (save-excursion
507      (backward-word arg)
508      (setq name (buffer-substring (point) (progn (forward-word 1)
509                                                (setq nameloc (point))))))
510     (set-text-properties 0 (length name) nil name)
511     (setq exp (read-string (format "%s expansion for \"%s\": "
512                                    type name)))
513     (if (or (not (abbrev-expansion name table))
514             (y-or-n-p (format "%s expands to \"%s\"; redefine? "
515                               name (abbrev-expansion name table))))
516         (progn
517          (define-abbrev table (downcase name) exp)
518          (save-excursion
519           (goto-char nameloc)
520           (expand-abbrev))))))
521
522 (defun abbrev-prefix-mark (&optional arg)
523   "Mark current point as the beginning of an abbrev.
524 Abbrev to be expanded starts here rather than at beginning of word.
525 This way, you can expand an abbrev with a prefix: insert the prefix,
526 use this command, then insert the abbrev."
527   (interactive "P")
528   (or arg (expand-abbrev))
529   (setq abbrev-start-location (point-marker)
530         abbrev-start-location-buffer (current-buffer))
531   (let ((e (make-extent (point) (point))))
532     (set-extent-begin-glyph e (make-glyph [string :data "-"]))))
533
534 (defun expand-region-abbrevs (start end &optional noquery)
535   "For abbrev occurrence in the region, offer to expand it.
536 The user is asked to type y or n for each occurrence.
537 A prefix argument means don't query; expand all abbrevs.
538 If called from a Lisp program, arguments are START END &optional NOQUERY."
539   (interactive "r\nP")
540   (save-excursion
541     (goto-char start)
542     (let ((lim (- (point-max) end))
543           pnt string)
544       (while (and (not (eobp))
545                   (progn (forward-word 1)
546                          (<= (setq pnt (point)) (- (point-max) lim))))
547         (if (abbrev-expansion
548              (setq string
549                    (buffer-substring
550                     (save-excursion (backward-word) (point))
551                     pnt)))
552             (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
553                 (expand-abbrev)))))))
554
555 ;;; abbrev.el ends here