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