emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] Changes to emacs/lisp/abbrev.el,v


From: Stefan Monnier
Subject: [Emacs-diffs] Changes to emacs/lisp/abbrev.el,v
Date: Thu, 17 Apr 2008 19:54:02 +0000

CVSROOT:        /sources/emacs
Module name:    emacs
Changes by:     Stefan Monnier <monnier>        08/04/17 19:54:00

Index: lisp/abbrev.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/abbrev.el,v
retrieving revision 1.70
retrieving revision 1.71
diff -u -b -r1.70 -r1.71
--- lisp/abbrev.el      17 Apr 2008 17:40:52 -0000      1.70
+++ lisp/abbrev.el      17 Apr 2008 19:54:00 -0000      1.71
@@ -719,48 +719,32 @@
           (goto-char pos)))
       res)))
 
-(defvar abbrev-expand-functions nil
-  "Wrapper hook around `expand-abbrev'.
-The functions on this special hook are called with one argument:
-a function that performs the abbrev expansion.  It should return
-the abbrev symbol if expansion took place.")
-
-(defun expand-abbrev ()
-  "Expand the abbrev before point, if there is an abbrev there.
-Effective when explicitly called even when `abbrev-mode' is nil.
-Returns the abbrev symbol, if expansion took place."
-  (interactive)
-  (run-hooks 'pre-abbrev-expand-hook)
-  (abbrev-with-wrapper-hook abbrev-expand-functions
-    (destructuring-bind (&optional sym name wordstart wordend)
-        (abbrev--before-point)
-      (when sym
-        (let ((value sym))
-          (unless (or ;; executing-kbd-macro
-                   noninteractive
-                   (window-minibuffer-p (selected-window)))
-            ;; Add an undo boundary, in case we are doing this for
-            ;; a self-inserting command which has avoided making one so far.
-            (undo-boundary))
-          ;; Now sym is the abbrev symbol.
-          (setq last-abbrev-text name)
-          (setq last-abbrev sym)
-          (setq last-abbrev-location wordstart)
+(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 sym :count (1+ (abbrev-get sym :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 sym))
+    (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 sym))
+      (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 sym)))
+        (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,
@@ -782,8 +766,8 @@
                     (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))
+    (when (symbol-function abbrev)
+      (let* ((hook (symbol-function abbrev))
                    (expanded
                     ;; If the abbrev has a hook function, run it.
                     (funcall hook)))
@@ -795,7 +779,38 @@
                        (null expanded)
                        (get hook 'no-self-insert))
                   (setq value nil))))
-          value)))))
+    value))
+
+(defvar abbrev-expand-functions nil
+  "Wrapper hook around `expand-abbrev'.
+The functions on this special hook are called with one argument:
+a function that performs the abbrev expansion.  It should return
+the abbrev symbol if expansion took place.")
+
+(defun expand-abbrev ()
+  "Expand the abbrev before point, if there is an abbrev there.
+Effective when explicitly called even when `abbrev-mode' is nil.
+Returns the abbrev symbol, if expansion took place."
+  (interactive)
+  (run-hooks 'pre-abbrev-expand-hook)
+  (abbrev-with-wrapper-hook abbrev-expand-functions
+    (destructuring-bind (&optional sym name wordstart wordend)
+        (abbrev--before-point)
+      (when sym
+        (let ((value sym))
+          (unless (or ;; executing-kbd-macro
+                   noninteractive
+                   (window-minibuffer-p (selected-window)))
+            ;; Add an undo boundary, in case we are doing this for
+            ;; a self-inserting command which has avoided making one so far.
+            (undo-boundary))
+          ;; Now sym is the abbrev symbol.
+          (setq last-abbrev-text name)
+          (setq last-abbrev sym)
+          (setq last-abbrev-location wordstart)
+          ;; If this abbrev has an expansion, delete the abbrev
+          ;; and insert the expansion.
+          (abbrev-insert sym name wordstart wordend))))))
 
 (defun unexpand-abbrev ()
   "Undo the expansion of the last abbrev that expanded.
@@ -905,6 +920,25 @@
     (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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]