;;; abbrev.el --- abbrev mode commands for Emacs
;; Copyright (C) 1985, 1986, 1987, 1992, 2001, 2002, 2003, 2004,
-;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
;; Maintainer: FSF
;; Keywords: abbrev convenience
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
(set newsym nil) ; Make sure it won't be confused for an abbrev.
(setplist newsym (symbol-plist sym)))
(abbrev-table-put table :abbrev-table-modiff
- (1+ (abbrev-table-get table :abbrev-table-modiff)))))
+ (1+ (abbrev-table-get table :abbrev-table-modiff))))
+ ;; For backward compatibility, always return nil.
+ nil)
(defun define-abbrev (table name expansion &optional hook &rest props)
"Define an abbrev in TABLE named NAME, to expand to EXPANSION and call HOOK.
To undefine an abbrev, define it with EXPANSION = nil.
If HOOK is non-nil, it should be a function of no arguments;
it is called after EXPANSION is inserted.
-If EXPANSION is not a string, the abbrev is a special one,
- which does not expand in the usual way but only runs HOOK.
+If EXPANSION is not a string (and not nil), the abbrev is a
+ special one, which does not expand in the usual way but only
+ runs HOOK.
PROPS is a property list. The following properties are special:
- `:count': the value for the abbrev's usage-count, which is incremented each
overwrite a non-system abbreviation of the same name.
- `:case-fixed': non-nil means that abbreviations are looked up without
case-folding, and the expansion is not capitalized/upcased.
-- `:enable-function': a function of no argument which returns non-nil iff the
+- `:enable-function': a function of no argument which returns non-nil if the
abbrev should be used for a particular call of `expand-abbrev'.
An obsolete but still supported calling form is:
(goto-char pos)))
res)))
+(defun abbrev-insert (abbrev &optional name wordstart wordend)
+ "Insert abbrev ABBREV at point.
+If non-nil, NAME is the name by which this abbrev was found.
+If non-nil, WORDSTART is the place where to insert the abbrev.
+If non-nil, WORDEND the abbrev replaces the previous text between
+WORDSTART and WORDEND.
+Return ABBREV if the expansion should be considered as having taken place."
+ (unless name (setq name (symbol-name abbrev)))
+ (unless wordstart (setq wordstart (point)))
+ (unless wordend (setq wordend wordstart))
+ ;; Increment use count.
+ (abbrev-put abbrev :count (1+ (abbrev-get abbrev :count)))
+ (let ((value abbrev))
+ ;; If this abbrev has an expansion, delete the abbrev
+ ;; and insert the expansion.
+ (when (stringp (symbol-value abbrev))
+ (goto-char wordstart)
+ ;; Insert at beginning so that markers at the end (e.g. point)
+ ;; are preserved.
+ (insert (symbol-value abbrev))
+ (delete-char (- wordend wordstart))
+ (let ((case-fold-search nil))
+ ;; If the abbrev's name is different from the buffer text (the
+ ;; only difference should be capitalization), then we may want
+ ;; to adjust the capitalization of the expansion.
+ (when (and (not (equal name (symbol-name abbrev)))
+ (string-match "[[:upper:]]" name))
+ (if (not (string-match "[[:lower:]]" name))
+ ;; Abbrev was all caps. If expansion is multiple words,
+ ;; normally capitalize each word.
+ (if (and (not abbrev-all-caps)
+ (save-excursion
+ (> (progn (backward-word 1) (point))
+ (progn (goto-char wordstart)
+ (forward-word 1) (point)))))
+ (upcase-initials-region wordstart (point))
+ (upcase-region wordstart (point)))
+ ;; Abbrev included some caps. Cap first initial of expansion.
+ (let ((end (point)))
+ ;; Find the initial.
+ (goto-char wordstart)
+ (skip-syntax-forward "^w" (1- end))
+ ;; Change just that.
+ (upcase-initials-region (point) (1+ (point)))
+ (goto-char end))))))
+ ;; Now point is at the end of the expansion and the beginning is
+ ;; in last-abbrev-location.
+ (when (symbol-function abbrev)
+ (let* ((hook (symbol-function abbrev))
+ (expanded
+ ;; If the abbrev has a hook function, run it.
+ (funcall hook)))
+ ;; In addition, if the hook function is a symbol with
+ ;; a non-nil `no-self-insert' property, let the value it
+ ;; returned specify whether we consider that an expansion took
+ ;; place. If it returns nil, no expansion has been done.
+ (if (and (symbolp hook)
+ (null expanded)
+ (get hook 'no-self-insert))
+ (setq value nil))))
+ value))
+
(defvar abbrev-expand-functions nil
"Wrapper hook around `expand-abbrev'.
The functions on this special hook are called with one argument:
(setq last-abbrev-text name)
(setq last-abbrev sym)
(setq last-abbrev-location wordstart)
- ;; Increment use count.
- (abbrev-put sym :count (1+ (abbrev-get sym :count)))
;; If this abbrev has an expansion, delete the abbrev
;; and insert the expansion.
- (when (stringp (symbol-value sym))
- (goto-char wordstart)
- ;; Insert at beginning so that markers at the end (e.g. point)
- ;; are preserved.
- (insert (symbol-value sym))
- (delete-char (- wordend wordstart))
- (let ((case-fold-search nil))
- ;; If the abbrev's name is different from the buffer text (the
- ;; only difference should be capitalization), then we may want
- ;; to adjust the capitalization of the expansion.
- (when (and (not (equal name (symbol-name sym)))
- (string-match "[[:upper:]]" name))
- (if (not (string-match "[[:lower:]]" name))
- ;; Abbrev was all caps. If expansion is multiple words,
- ;; normally capitalize each word.
- (if (and (not abbrev-all-caps)
- (save-excursion
- (> (progn (backward-word 1) (point))
- (progn (goto-char wordstart)
- (forward-word 1) (point)))))
- (upcase-initials-region wordstart (point))
- (upcase-region wordstart (point)))
- ;; Abbrev included some caps. Cap first initial of expansion.
- (let ((end (point)))
- ;; Find the initial.
- (goto-char wordstart)
- (skip-syntax-forward "^w" (1- end))
- ;; Change just that.
- (upcase-initials-region (point) (1+ (point)))
- (goto-char end))))))
- ;; Now point is at the end of the expansion and the beginning is
- ;; in last-abbrev-location.
- (when (symbol-function sym)
- (let* ((hook (symbol-function sym))
- (expanded
- ;; If the abbrev has a hook function, run it.
- (funcall hook)))
- ;; In addition, if the hook function is a symbol with
- ;; a non-nil `no-self-insert' property, let the value it
- ;; returned specify whether we consider that an expansion took
- ;; place. If it returns nil, no expansion has been done.
- (if (and (symbolp hook)
- (null expanded)
- (get hook 'no-self-insert))
- (setq value nil))))
- value)))))
+ (abbrev-insert sym name wordstart wordend))))))
(defun unexpand-abbrev ()
"Undo the expansion of the last abbrev that expanded.
;; to do the expansion.
(let ((val (symbol-value last-abbrev)))
(unless (stringp val)
- (error "value of abbrev-symbol must be a string"))
- (delete-region (point) (+ (point) (length val)))
+ (error "Value of abbrev-symbol must be a string"))
;; Don't inherit properties here; just copy from old contents.
(insert last-abbrev-text)
+ ;; Delete after inserting, to better preserve markers.
+ (delete-region (point) (+ (point) (length val)))
(setq last-abbrev-text nil))))))
(defun abbrev--write (sym)
means that an abbrev can only be a single word. The submatch 1 is treated
as the potential name of an abbrev.
- `:enable-function' can be set to a function of no argument which returns
- non-nil iff the abbrevs in this table should be used for this instance
- of `expand-abbrev'."
+ non-nil if and only if the abbrevs in this table should be used for this
+ instance of `expand-abbrev'."
;; We used to manually add the docstring, but we also want to record this
;; location as the definition of the variable (in load-history), so we may
;; as well just use `defvar'.
(eval `(defvar ,tablename nil ,@(if (stringp docstring) (list docstring))))
(let ((table (if (boundp tablename) (symbol-value tablename))))
(unless table
- (setq table (make-abbrev-table props))
+ (setq table (make-abbrev-table))
(set tablename table)
(push tablename abbrev-table-name-list))
+ ;; We used to just pass them to `make-abbrev-table', but that fails
+ ;; if the table was pre-existing as is the case if it was created by
+ ;; loading the user's abbrev file.
+ (while (consp props)
+ (abbrev-table-put table (pop props) (pop props)))
(dolist (elt definitions)
(apply 'define-abbrev table elt))))
+(defun abbrev-table-menu (table &optional prompt sortfun)
+ "Return a menu that shows all abbrevs in TABLE.
+Selecting an entry runs `abbrev-insert'.
+PROMPT is the prompt to use for the keymap.
+SORTFUN is passed to `sort' to change the default ordering."
+ (unless sortfun (setq sortfun 'string-lessp))
+ (let ((entries ()))
+ (mapatoms (lambda (abbrev)
+ (when (symbol-value abbrev)
+ (let ((name (symbol-name abbrev)))
+ (push `(,(intern name) menu-item ,name
+ (lambda () (interactive)
+ (abbrev-insert ',abbrev)))
+ entries))))
+ table)
+ (nconc (make-sparse-keymap prompt)
+ (sort entries (lambda (x y)
+ (funcall sortfun (nth 2 x) (nth 2 y)))))))
+
(provide 'abbrev)
;; arch-tag: dbd6f3ae-dfe3-40ba-b00f-f9e3ff960df5