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