+ (set-buffer other-buffer)
+ major-mode)))
+
+;;; Back over all abbrev type characters and then moves forward over
+;;; all skip characters.
+(defun dabbrev--goto-start-of-abbrev ()
+ ;; Move backwards over abbrev chars
+ (save-match-data
+ (when (> (point) (minibuffer-prompt-end))
+ (forward-char -1)
+ (while (and (looking-at dabbrev--abbrev-char-regexp)
+ (> (point) (minibuffer-prompt-end))
+ (not (= (point) (field-beginning (point) nil
+ (1- (point))))))
+ (forward-char -1))
+ (or (looking-at dabbrev--abbrev-char-regexp)
+ (forward-char 1)))
+ (and dabbrev-abbrev-skip-leading-regexp
+ (while (looking-at dabbrev-abbrev-skip-leading-regexp)
+ (forward-char 1)))))
+
+;;; Extract the symbol at point to serve as abbreviation.
+(defun dabbrev--abbrev-at-point ()
+ ;; Check for error
+ (if (bobp)
+ (error "No possible abbreviation preceding point"))
+ ;; Return abbrev at point
+ (save-excursion
+ ;; Record the end of the abbreviation.
+ (setq dabbrev--last-abbrev-location (point))
+ ;; If we aren't right after an abbreviation,
+ ;; move point back to just after one.
+ ;; This is so the user can get successive words
+ ;; by typing the punctuation followed by M-/.
+ (save-match-data
+ (if (save-excursion
+ (forward-char -1)
+ (not (looking-at (concat "\\("
+ (or dabbrev-abbrev-char-regexp
+ "\\sw\\|\\s_")
+ "\\)+"))))
+ (if (re-search-backward (or dabbrev-abbrev-char-regexp
+ "\\sw\\|\\s_")
+ nil t)
+ (forward-char 1)
+ (error "No possible abbreviation preceding point"))))
+ ;; Now find the beginning of that one.
+ (dabbrev--goto-start-of-abbrev)
+ (buffer-substring-no-properties
+ dabbrev--last-abbrev-location (point))))
+
+;;; Initializes all global variables
+(defun dabbrev--reset-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
+ dabbrev--last-direction nil
+ dabbrev--last-expansion nil
+ dabbrev--last-expansion-location nil
+ dabbrev--friend-buffer-list nil
+ dabbrev--last-buffer nil
+ dabbrev--last-buffer-found nil
+ dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
+ "\\sw\\|\\s_")
+ dabbrev--check-other-buffers dabbrev-check-other-buffers))
+
+(defun dabbrev--select-buffers ()
+ "Return a list of other buffers to search for a possible abbrev.
+The current buffer is not included in the list.
+
+This function makes a list of all the buffers returned by `buffer-list',
+then discards buffers whose names match `dabbrev-ignored-buffer-names'
+or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
+`dabbrev-friend-buffer-function', if it is bound, returns nil when called
+with the buffer as argument.
+It returns the list of the buffers that are not discarded."
+ (dabbrev-filter-elements
+ buffer (buffer-list)
+ (and (not (eq (current-buffer) buffer))
+ (not (dabbrev--ignore-buffer-p buffer))
+ (boundp 'dabbrev-friend-buffer-function)
+ (funcall dabbrev-friend-buffer-function buffer))))
+
+(defun dabbrev--try-find (abbrev reverse n ignore-case)
+ "Search for ABBREV, backwards if REVERSE, N times.
+If IGNORE-CASE is non-nil, ignore case while searching.
+Return the expansion found, and save the location of the start
+of the expansion in `dabbrev--last-expansion-location'."
+ (save-excursion
+ (save-restriction
+ (widen)
+ (let ((expansion nil))
+ (and dabbrev--last-expansion-location
+ (goto-char dabbrev--last-expansion-location))
+ (let ((case-fold-search ignore-case)
+ (count n))
+ (while (and (> count 0)
+ (setq expansion (dabbrev--search abbrev
+ reverse
+ (and ignore-case
+ (if (eq dabbrev-case-distinction 'case-replace)
+ case-replace
+ dabbrev-case-distinction))
+ )))
+ (setq count (1- count))))
+ (and expansion
+ (setq dabbrev--last-expansion-location (point)))
+ expansion))))
+
+(defun dabbrev--find-all-expansions (abbrev ignore-case)
+ "Return a list of all possible expansions of ABBREV.
+If IGNORE-CASE is non-nil, accept matches which differ in case."
+ (let ((all-expansions nil)
+ expansion)
+ (save-excursion
+ (goto-char (point-min))
+ (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
+ (setq all-expansions (cons expansion all-expansions))))
+ all-expansions))
+
+(defun dabbrev--scanning-message ()
+ (unless (window-minibuffer-p (selected-window))
+ (message "Scanning `%s'" (buffer-name (current-buffer)))))
+
+(defun dabbrev--ignore-buffer-p (buffer)
+ "Return non-nil if BUFFER should be ignored by dabbrev."
+ (let ((bn (buffer-name buffer)))
+ (or (member bn dabbrev-ignored-buffer-names)
+ (let ((tail dabbrev-ignored-buffer-regexps)
+ (match nil))
+ (while (and tail (not match))
+ (setq match (string-match (car tail) bn)
+ tail (cdr tail)))
+ match))))
+
+(defun dabbrev--find-expansion (abbrev direction ignore-case)
+ "Find one occurrence of ABBREV, and return the expansion.
+DIRECTION > 0 means look that many times backwards.
+DIRECTION < 0 means look that many times forward.
+DIRECTION = 0 means try both backward and forward.
+IGNORE-CASE non-nil means ignore case when searching.
+This sets `dabbrev--last-direction' to 1 or -1 according
+to the direction in which the occurrence was actually found.
+It sets `dabbrev--last-expansion-location' to the location
+of the start of the occurrence."
+ (save-excursion
+ ;; If we were scanning something other than the current buffer,
+ ;; continue scanning there.
+ (when dabbrev--last-buffer
+ (set-buffer dabbrev--last-buffer)
+ (dabbrev--scanning-message))
+ (or
+ ;; ------------------------------------------
+ ;; Look backward in current buffer.
+ ;; ------------------------------------------
+ (and (not dabbrev-search-these-buffers-only)
+ (>= direction 0)
+ (setq dabbrev--last-direction (min 1 direction))
+ (dabbrev--try-find abbrev t
+ (max 1 direction)
+ ignore-case))
+ ;; ------------------------------------------
+ ;; Look forward in current buffer
+ ;; or whatever buffer we were last scanning.
+ ;; ------------------------------------------
+ (and (or (not dabbrev-search-these-buffers-only)
+ dabbrev--last-buffer)
+ (<= direction 0)
+ (setq dabbrev--last-direction -1)
+ (dabbrev--try-find abbrev nil
+ (max 1 (- direction))
+ ignore-case))
+ ;; ------------------------------------------
+ ;; Look in other buffers.
+ ;; Always start at (point-min) and look forward.
+ ;; ------------------------------------------
+ (progn
+ (setq dabbrev--last-direction -1)
+ (unless dabbrev--last-buffer
+ ;; If we have just now begun to search other buffers,
+ ;; determine which other buffers we should check.
+ ;; Put that list in dabbrev--friend-buffer-list.
+ (or dabbrev--friend-buffer-list
+ (setq dabbrev--friend-buffer-list
+ (dabbrev--make-friend-buffer-list))))
+ ;; Walk through the buffers till we find a match.
+ (let (expansion)
+ (while (and (not expansion) dabbrev--friend-buffer-list)
+ (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list))
+ (set-buffer dabbrev--last-buffer)
+ (dabbrev--scanning-message)
+ (setq dabbrev--last-expansion-location (point-min))
+ (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
+ expansion)))))
+
+;; Compute the list of buffers to scan.
+;; If dabbrev-search-these-buffers-only, then the current buffer
+;; is included in this list if it should be searched.
+;; Otherwise, the current buffer is searched first specially.,
+;; and it is not included in this list.
+(defun dabbrev--make-friend-buffer-list ()
+ (let ((list (mapcar (function get-buffer)
+ dabbrev-search-these-buffers-only)))
+ (when (and (null dabbrev-search-these-buffers-only)
+ dabbrev--check-other-buffers
+ (or (eq dabbrev--check-other-buffers t)
+ (setq dabbrev--check-other-buffers
+ (y-or-n-p "Scan other buffers also? "))))
+ (setq list (funcall dabbrev-select-buffers-function))
+ ;; If dabbrev-check-all-buffers, tack on all the other
+ ;; buffers at the end of the list, except those which are
+ ;; specifically to be ignored.
+ (if dabbrev-check-all-buffers
+ (setq list
+ (append list
+ (dabbrev-filter-elements
+ buffer (buffer-list)
+ (and (not (memq buffer list))
+ (not (dabbrev--ignore-buffer-p buffer)))))))
+ ;; Remove the current buffer.
+ (setq list (delq (current-buffer) list)))
+ ;; Move buffers in the list that are visible on the screen
+ ;; to the front of the list, but don't add anything to the list.
+ (if list
+ (walk-windows (lambda (w)
+ (unless (eq w (selected-window))
+ (if (memq (window-buffer w) list)
+ (setq list
+ (cons (window-buffer w)
+ (delq (window-buffer w)
+ list))))))))
+ ;; 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))
+ (not dabbrev-search-these-buffers-only))
+ (setq list
+ (cons (dabbrev--minibuffer-origin)
+ (delq (dabbrev--minibuffer-origin) list))))
+ list))
+
+(defun dabbrev--safe-replace-match (string &optional fixedcase literal)
+ (if (eq major-mode 'picture-mode)
+ (with-no-warnings
+ (picture-replace-match string fixedcase literal))
+ (replace-match string fixedcase literal)))
+
+;;;----------------------------------------------------------------
+(defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
+ "Replace OLD with EXPANSION in the buffer.
+OLD is text currently in the buffer, perhaps the abbreviation
+or perhaps another expansion that was tried previously.
+ABBREV is the abbreviation we are expanding.
+It is \" \" if we are copying subsequent words.
+EXPANSION is the expansion substring to be used this time.
+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)))
+ (if (eq dabbrev-case-replace 'case-replace)
+ case-replace
+ dabbrev-case-replace))))
+
+ ;; If we upcased or downcased the original expansion,
+ ;; do likewise for the subsequent words when we copy them.
+ ;; Don't do any of the usual case processing, though.
+ (when (equal abbrev " ")
+ (if dabbrev--last-case-pattern
+ (setq expansion
+ (funcall dabbrev--last-case-pattern expansion)))
+ (setq use-case-replace nil))
+
+ ;; If the expansion has mixed case
+ ;; and it is not simply a capitalized word,
+ ;; or if the abbrev has mixed case,
+ ;; and if the given abbrev's case pattern
+ ;; matches the start of the expansion,
+ ;; copy the expansion's case
+ ;; instead of downcasing all the rest.
+ ;;
+ ;; Treat a one-capital-letter (possibly with preceding non-letter
+ ;; characters) abbrev as "not all upper case", so as to force
+ ;; preservation of the expansion's pattern if the expansion starts
+ ;; with a capital letter.
+ (let ((expansion-rest (substring expansion 1))
+ (first-letter-position (string-match "[[:alpha:]]" abbrev)))
+ (if (or (null first-letter-position)
+ (and (not (and (or (string= expansion-rest (downcase expansion-rest))
+ (string= expansion-rest (upcase expansion-rest)))
+ (or (string= abbrev (downcase abbrev))
+ (and (string= abbrev (upcase abbrev))
+ (> (- (length abbrev) first-letter-position)
+ 1)))))
+ (string= abbrev
+ (substring expansion 0 (length abbrev)))))
+ (setq use-case-replace nil)))
+
+ ;; If the abbrev and the expansion are both all-lower-case
+ ;; then don't do any conversion. The conversion would be a no-op
+ ;; for this replacement, but it would carry forward to subsequent words.
+ ;; The goal of this is to prevent that carrying forward.
+ (if (and (string= expansion (downcase expansion))
+ (string= abbrev (downcase abbrev)))
+ (setq use-case-replace nil))
+
+ (if use-case-replace
+ (setq expansion (downcase expansion)))
+
+ ;; In case we insert subsequent words,
+ ;; record if we upcased or downcased the first word,
+ ;; in order to do likewise for subsequent words.
+ (and record-case-pattern
+ (setq dabbrev--last-case-pattern
+ (and use-case-replace
+ (cond ((equal abbrev (upcase abbrev)) 'upcase)
+ ((equal abbrev (downcase abbrev)) 'downcase)))))
+
+ ;; Convert whitespace to single spaces.
+ (if dabbrev--eliminate-newlines
+ ;; Start searching at end of ABBREV so that any whitespace
+ ;; carried over from the existing text is not changed.
+ (let ((pos (length abbrev)))
+ (while (string-match "[\n \t]+" expansion pos)
+ (setq pos (1+ (match-beginning 0)))
+ (setq expansion (replace-match " " nil nil expansion)))))
+
+ (if old