]> code.delx.au - gnu-emacs/blobdiff - lisp/dabbrev.el
CC Mode: truncate the semi-nonlit cache when applying syntax-table to a quote
[gnu-emacs] / lisp / dabbrev.el
index 825402228e1ba2d73d07d72ba599d89481ef9461..d9f36b152905cf971da126a1d7c6d5969eb17cc1 100644 (file)
@@ -1,7 +1,7 @@
 ;;; dabbrev.el --- dynamic abbreviation package  -*- lexical-binding: t -*-
 
-;; Copyright (C) 1985-1986, 1992, 1994, 1996-1997, 2000-2012
-;;   Free Software Foundation, Inc.
+;; Copyright (C) 1985-1986, 1992, 1994, 1996-1997, 2000-2016 Free
+;; Software Foundation, Inc.
 
 ;; Author: Don Morrison
 ;;     Lars Lindberg
 
 Example: Set this to \"\\\\$\" for programming languages
 in which variable names may appear with or without a leading `$'.
-\(For example, in Makefiles.\)
+\(For example, in Makefiles.)
 
 Set this to nil if no characters should be skipped."
   :type '(choice regexp
@@ -285,15 +285,13 @@ A mode setting this variable should make it buffer local."
 If this variable is non-nil, dabbrev will only look in these buffers.
 It will not even look in the current buffer if it is not a member of
 this list."
+  :type '(choice (const nil) (repeat :tag "List of buffers" string))
   :group 'dabbrev)
 
 ;;----------------------------------------------------------------
 ;; Internal variables
 ;;----------------------------------------------------------------
 
-;; Last obarray of completions in `dabbrev-completion'
-(defvar dabbrev--last-obarray nil)
-
 ;; Table of expansions seen so far
 (defvar dabbrev--last-table nil)
 
@@ -321,9 +319,6 @@ this list."
 ;; The buffer we found the expansion last time.
 (defvar dabbrev--last-buffer-found nil)
 
-;; The buffer we last did a completion in.
-(defvar dabbrev--last-completion-buffer nil)
-
 ;; If non-nil, a function to use when copying successive words.
 ;; It should be `upcase' or `downcase'.
 (defvar dabbrev--last-case-pattern nil)
@@ -367,6 +362,13 @@ this list."
 ;;??? Do we want this?
 ;;;###autoload (define-key esc-map [?\C-/] 'dabbrev-completion)
 
+(defun dabbrev--ignore-case-p (abbrev)
+  (and (if (eq dabbrev-case-fold-search 'case-fold-search)
+           case-fold-search
+         dabbrev-case-fold-search)
+       (or (not dabbrev-upcase-means-case-search)
+           (string= abbrev (downcase abbrev)))))
+
 ;;;###autoload
 (defun dabbrev-completion (&optional arg)
   "Completion on current word.
@@ -387,53 +389,40 @@ then it searches *all* buffers."
         (abbrev (dabbrev--abbrev-at-point))
          (beg (progn (search-backward abbrev) (point)))
          (end (progn (search-forward abbrev) (point)))
-        (ignore-case-p
-          (and (if (eq dabbrev-case-fold-search 'case-fold-search)
-                   case-fold-search
-                 dabbrev-case-fold-search)
-               (or (not dabbrev-upcase-means-case-search)
-                   (string= abbrev (downcase abbrev)))))
-        (my-obarray dabbrev--last-obarray)
+        (ignore-case-p (dabbrev--ignore-case-p abbrev))
+        (list 'uninitialized)
          (table
-          (completion-table-dynamic
-           (let ((initialized nil))
-             (lambda (abbrev)
-               (unless initialized
-                (setq initialized t)
-                 (save-excursion
-                   ;;--------------------------------
-                   ;; New abbreviation to expand.
-                   ;;--------------------------------
-                   (setq dabbrev--last-abbreviation abbrev)
-                   ;; Find all expansion
-                   (let ((completion-list
-                          (dabbrev--find-all-expansions abbrev ignore-case-p))
-                         (completion-ignore-case ignore-case-p))
-                     ;; Make an obarray with all expansions
-                     (setq my-obarray (make-vector (length completion-list) 0))
-                     (or (> (length my-obarray) 0)
-                         (error "No dynamic expansion for \"%s\" found%s"
-                                abbrev
-                                (if dabbrev--check-other-buffers
-                                    "" " in this-buffer")))
-                     (cond
-                      ((not (and ignore-case-p
-                                 dabbrev-case-replace))
-                       (dolist (string completion-list)
-                         (intern string my-obarray)))
-                      ((string= abbrev (upcase abbrev))
-                       (dolist (string completion-list)
-                         (intern (upcase string) my-obarray)))
-                      ((string= (substring abbrev 0 1)
-                                (upcase (substring abbrev 0 1)))
-                       (dolist (string completion-list)
-                         (intern (capitalize string) my-obarray)))
-                      (t
-                       (dolist (string completion-list)
-                         (intern (downcase string) my-obarray))))
-                     (setq dabbrev--last-obarray my-obarray)
-                     (setq dabbrev--last-completion-buffer (current-buffer)))))
-               my-obarray)))))
+          (lambda (s p a)
+            (if (eq a 'metadata)
+                `(metadata (cycle-sort-function . ,#'identity)
+                           (category . dabbrev))
+              (when (eq list 'uninitialized)
+                (save-excursion
+                  ;;--------------------------------
+                  ;; New abbreviation to expand.
+                  ;;--------------------------------
+                  (setq dabbrev--last-abbreviation abbrev)
+                  ;; Find all expansion
+                  (let ((completion-list
+                         (dabbrev--find-all-expansions abbrev ignore-case-p))
+                        (completion-ignore-case ignore-case-p))
+                    (or (consp completion-list)
+                        (user-error "No dynamic expansion for \"%s\" found%s"
+                                    abbrev
+                                    (if dabbrev--check-other-buffers
+                                        "" " in this-buffer")))
+                    (setq list
+                          (cond
+                           ((not (and ignore-case-p dabbrev-case-replace))
+                            completion-list)
+                           ((string= abbrev (upcase abbrev))
+                            (mapcar #'upcase completion-list))
+                           ((string= (substring abbrev 0 1)
+                                     (upcase (substring abbrev 0 1)))
+                            (mapcar #'capitalize completion-list))
+                           (t
+                            (mapcar #'downcase completion-list)))))))
+              (complete-with-action a list s p)))))
     (completion-in-region beg end table)))
 
 ;;;###autoload
@@ -469,7 +458,7 @@ See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
               (markerp dabbrev--last-abbrev-location)
               (marker-position dabbrev--last-abbrev-location)
               (or (eq last-command this-command)
-                  (and (window-minibuffer-p (selected-window))
+                  (and (window-minibuffer-p)
                        (= dabbrev--last-abbrev-location
                           (point)))))
          ;; Find a different expansion for the same abbrev as last time.
@@ -528,11 +517,7 @@ See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
          (setq expansion
                (dabbrev--find-expansion
                  abbrev direction
-                 (and (if (eq dabbrev-case-fold-search 'case-fold-search)
-                          case-fold-search
-                        dabbrev-case-fold-search)
-                      (or (not dabbrev-upcase-means-case-search)
-                          (string= abbrev (downcase abbrev))))))))
+                 (dabbrev--ignore-case-p abbrev)))))
     (cond
      ((not expansion)
       (dabbrev--reset-global-variables)
@@ -543,13 +528,13 @@ See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
            (search-backward old)
            (insert abbrev)
            (delete-region (point) (+ (point) (length old)))))
-      (error "No%s dynamic expansion for `%s' found"
-            (if old " further" "") abbrev))
+      (user-error "No%s dynamic expansion for `%s' found"
+                  (if old " further" "") abbrev))
      (t
       (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
                   (minibuffer-window-active-p (selected-window))))
          (progn
-           (message "Expansion found in '%s'"
+           (message "Expansion found in `%s'"
                     (buffer-name dabbrev--last-buffer))
            (setq dabbrev--last-buffer-found dabbrev--last-buffer))
        (message nil))
@@ -561,8 +546,8 @@ See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
                (copy-marker dabbrev--last-expansion-location)))
       ;; Success: stick it in and return.
       (setq buffer-undo-list (cons orig-point buffer-undo-list))
-      (dabbrev--substitute-expansion old abbrev expansion
-                                    record-case-pattern)
+      (setq expansion (dabbrev--substitute-expansion old abbrev expansion
+                                                     record-case-pattern))
 
       ;; Save state for re-expand.
       (setq dabbrev--last-expansion expansion)
@@ -601,7 +586,7 @@ all skip characters."
   "Extract the symbol at point to serve as abbreviation."
   ;; Check for error
   (if (bobp)
-      (error "No possible abbreviation preceding point"))
+      (user-error "No possible abbreviation preceding point"))
   ;; Return abbrev at point
   (save-excursion
     ;; Record the end of the abbreviation.
@@ -619,7 +604,7 @@ all skip characters."
                                      "\\sw\\|\\s_")
                                  nil t)
              (forward-char 1)
-           (error "No possible abbreviation preceding point"))))
+           (user-error "No possible abbreviation preceding point"))))
     ;; Now find the beginning of that one.
     (dabbrev--goto-start-of-abbrev)
     (buffer-substring-no-properties
@@ -627,8 +612,6 @@ all skip characters."
 
 (defun dabbrev--reset-global-variables ()
   "Initialize all global variables."
-  ;; dabbrev--last-obarray and dabbrev--last-completion-buffer
-  ;; must not be reset here.
   (setq dabbrev--last-table nil
        dabbrev--last-abbreviation nil
        dabbrev--last-abbrev-location nil
@@ -811,7 +794,7 @@ of the start of the occurrence."
     ;; In a minibuffer, search the buffer it was activated from,
     ;; first after the minibuffer itself.  Unless we aren't supposed
     ;; to search the current buffer either.
-    (if (and (window-minibuffer-p (selected-window))
+    (if (and (window-minibuffer-p)
             (not dabbrev-search-these-buffers-only))
        (setq list
              (cons (dabbrev--minibuffer-origin)
@@ -836,11 +819,7 @@ RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
 to record whether we upcased the expansion, downcased it, or did neither."
   ;;(undo-boundary)
   (let ((use-case-replace
-         (and (if (eq dabbrev-case-fold-search 'case-fold-search)
-                  case-fold-search
-                dabbrev-case-fold-search)
-              (or (not dabbrev-upcase-means-case-search)
-                  (string= abbrev (downcase abbrev)))
+         (and (dabbrev--ignore-case-p abbrev)
               (if (eq dabbrev-case-replace 'case-replace)
                   case-replace
                 dabbrev-case-replace))))
@@ -923,7 +902,9 @@ to record whether we upcased the expansion, downcased it, or did neither."
     ;; and (2) the replacement itself is all lower case.
     (dabbrev--safe-replace-match expansion
                                 (not use-case-replace)
-                                t)))
+                                t))
+  ;; Return the expansion actually used.
+  expansion)
 
 
 ;;;----------------------------------------------------------------
@@ -996,11 +977,6 @@ Leaves point at the location of the start of the expansion."
                (cons found-string dabbrev--last-table))
          result)))))
 
-(dolist (mess '("^No dynamic expansion for .* found"
-               "^No further dynamic expansion for .* found$"
-               "^No possible abbreviation preceding point$"))
-  (add-to-list 'debug-ignored-errors mess))
-
 (provide 'dabbrev)
 
 ;;; dabbrev.el ends here