annotate lisp/abbrev.el @ 209:41ff10fd062f r20-4b3

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