compare symbol names with `equal'
[bpt/emacs.git] / lisp / hippie-exp.el
index 75bc1f9..ec6007e 100644 (file)
@@ -1,9 +1,9 @@
 ;;; hippie-exp.el --- expand text trying various ways to find its expansion
 
-;; Copyright (C) 1992, 2001-201 Free Software Foundation, Inc.
+;; Copyright (C) 1992, 2001-2014 Free Software Foundation, Inc.
 
 ;; Author: Anders Holst <aho@sans.kth.se>
-;; Last change: 3 March 1998
+;; Maintainer: emacs-devel@gnu.org
 ;; Version: 1.6
 ;; Keywords: abbrev convenience
 
 ;;  opposite situation to occur, that `hippie-expand' misses some
 ;;  suggestion because it thinks it has already tried it.
 ;;
-;;  Acknowledgement
+;;  Acknowledgment
 ;;
 ;;  I want to thank Mikael Djurfeldt in discussions with whom the idea
 ;;  of this function took form.
 
 (defvar he-search-window ())
 
+;;; Autoloaded for historical reasons (bug#12982)
 ;;;###autoload
 (defcustom hippie-expand-try-functions-list
   '(try-complete-file-name-partially
@@ -217,31 +218,26 @@ or insert functions in this list."
   :type '(repeat function)
   :group 'hippie-expand)
 
-;;;###autoload
 (defcustom hippie-expand-verbose t
   "Non-nil makes `hippie-expand' output which function it is trying."
   :type 'boolean
   :group 'hippie-expand)
 
-;;;###autoload
 (defcustom hippie-expand-dabbrev-skip-space nil
   "Non-nil means tolerate trailing spaces in the abbreviation to expand."
   :group 'hippie-expand
   :type 'boolean)
 
-;;;###autoload
 (defcustom hippie-expand-dabbrev-as-symbol t
   "Non-nil means expand as symbols, i.e. syntax `_' is considered a letter."
   :group 'hippie-expand
   :type 'boolean)
 
-;;;###autoload
 (defcustom hippie-expand-no-restriction t
   "Non-nil means that narrowed buffers are widened during search."
   :group 'hippie-expand
   :type 'boolean)
 
-;;;###autoload
 (defcustom hippie-expand-max-buffers ()
   "The maximum number of buffers (apart from the current) searched.
 If nil, all buffers are searched."
@@ -249,15 +245,13 @@ If nil, all buffers are searched."
                 integer)
   :group 'hippie-expand)
 
-;;;###autoload
-(defcustom hippie-expand-ignore-buffers (list (purecopy "^ \\*.*\\*$") 'dired-mode)
+(defcustom hippie-expand-ignore-buffers '("^ \\*.*\\*$" dired-mode)
   "A list specifying which buffers not to search (if not current).
 Can contain both regexps matching buffer names (as strings) and major modes
 \(as atoms)"
   :type '(repeat (choice regexp (symbol :tag "Major Mode")))
   :group 'hippie-expand)
 
-;;;###autoload
 (defcustom hippie-expand-only-buffers ()
   "A list specifying the only buffers to search (in addition to current).
 Can contain both regexps matching buffer names (as strings) and major modes
@@ -302,7 +296,7 @@ undoes the expansion."
                  (message "No further expansions found"))
              (ding))
            (if (and hippie-expand-verbose
-                    (not (window-minibuffer-p (selected-window))))
+                    (not (window-minibuffer-p)))
                (message "Using %s"
                         (nth he-num hippie-expand-try-functions-list)))))
       (if (and (>= he-num 0)
@@ -311,7 +305,7 @@ undoes the expansion."
            (setq he-num -1)
            (he-reset-string)
            (if (and hippie-expand-verbose
-                    (not (window-minibuffer-p (selected-window))))
+                    (not (window-minibuffer-p)))
                (message "Undoing expansions"))))))
 
 ;; Initializes the region to expand (to between BEG and END).
@@ -391,7 +385,7 @@ undoes the expansion."
              lst)))
 
 ;; Check if current buffer matches any atom or regexp in LST.
-;; Atoms are interpreted as major modes, strings as regexps mathing the name.
+;; Atoms are interpreted as major modes, strings as regexps matching the name.
 (defun he-buffer-member (lst)
   (or (memq major-mode lst)
       (progn
@@ -643,15 +637,27 @@ string).  It returns t if a new completion is found, nil otherwise."
 The argument OLD has to be nil the first call of this function, and t
 for subsequent calls (for further possible completions of the same
 string).  It returns t if a new completion is found, nil otherwise."
+  (he--all-buffers
+   old
+   (lambda () (he-line-beg (and (get-buffer-process (current-buffer))
+                           comint-use-prompt-regexp
+                           comint-prompt-regexp)))
+   (lambda (string)
+     (he-line-search string
+                     (and (get-buffer-process (current-buffer))
+                          comint-use-prompt-regexp
+                          comint-prompt-regexp)
+                     nil))))
+
+(defun he--all-buffers (old beg-function search-function)
   (let ((expansion ())
-       (strip-prompt (and (get-buffer-process (current-buffer))
-                          comint-use-prompt-regexp
-                          comint-prompt-regexp))
-       (buf (current-buffer))
-       (orig-case-fold-search case-fold-search))
+        (buf (current-buffer))
+        (only-buffers hippie-expand-only-buffers)
+        (ignore-buffers hippie-expand-ignore-buffers)
+        (orig-case-fold-search case-fold-search))
     (if (not old)
-       (progn
-         (he-init-string (he-line-beg strip-prompt) (point))
+        (progn
+         (he-init-string (funcall beg-function) (point))
          (setq he-search-bufs (buffer-list))
          (setq he-searched-n-bufs 0)
          (set-marker he-search-loc 1 (car he-search-bufs))))
@@ -660,24 +666,20 @@ string).  It returns t if a new completion is found, nil otherwise."
        (while (and he-search-bufs
                    (not expansion)
                    (or (not hippie-expand-max-buffers)
-                       (< he-searched-n-bufs hippie-expand-max-buffers)))
-         (set-buffer (car he-search-bufs))
-         (if (and (not (eq (current-buffer) buf))
-                  (if hippie-expand-only-buffers
-                      (he-buffer-member hippie-expand-only-buffers)
-                    (not (he-buffer-member hippie-expand-ignore-buffers))))
-             (save-excursion
-               (save-restriction
-                 (if hippie-expand-no-restriction
+                        (< he-searched-n-bufs hippie-expand-max-buffers)))
+          (set-buffer (car he-search-bufs))
+          (if (and (not (eq (current-buffer) buf))
+                   (if only-buffers
+                       (he-buffer-member only-buffers)
+                     (not (he-buffer-member ignore-buffers))))
+              (save-excursion
+                (save-restriction
+                  (if hippie-expand-no-restriction
                      (widen))
                  (goto-char he-search-loc)
-                 (setq strip-prompt (and (get-buffer-process (current-buffer))
-                                         comint-use-prompt-regexp
-                                         comint-prompt-regexp))
                  (setq expansion
                        (let ((case-fold-search orig-case-fold-search))
-                         (he-line-search he-search-string
-                                         strip-prompt nil)))
+                         (funcall search-function he-search-string)))
                  (set-marker he-search-loc (point))
                  (if (not expansion)
                      (progn
@@ -692,9 +694,9 @@ string).  It returns t if a new completion is found, nil otherwise."
        (progn
          (if old (he-reset-string))
          ())
-       (progn
-         (he-substitute-string expansion t)
-         t))))
+      (progn
+        (he-substitute-string expansion t)
+        t))))
 
 (defun he-line-search (str strip-prompt reverse)
   (let ((result ()))
@@ -721,7 +723,7 @@ string).  It returns t if a new completion is found, nil otherwise."
 
 (defun he-line-search-regexp (pat strip-prompt)
   (if strip-prompt
-      (concat "\\(" comint-prompt-regexp "\\|^\\s-*\\)\\("
+      (concat "\\(" comint-prompt-regexp "\\|^\\s-*\\)\\(?2:"
              (regexp-quote pat)
              "[^\n]*[^ \t\n]\\)")
       (concat "^\\(\\s-*\\)\\("
@@ -775,53 +777,9 @@ string).  It returns t if a new completion is found, nil otherwise."
 The argument OLD has to be nil the first call of this function, and t
 for subsequent calls (for further possible completions of the same
 string).  It returns t if a new completion is found, nil otherwise."
-  (let ((expansion ())
-       (buf (current-buffer))
-       (orig-case-fold-search case-fold-search))
-    (if (not old)
-       (progn
-         (he-init-string (he-list-beg) (point))
-         (setq he-search-bufs (buffer-list))
-         (setq he-searched-n-bufs 0)
-         (set-marker he-search-loc 1 (car he-search-bufs))))
-
-    (if (not (equal he-search-string ""))
-       (while (and he-search-bufs
-                   (not expansion)
-                   (or (not hippie-expand-max-buffers)
-                       (< he-searched-n-bufs hippie-expand-max-buffers)))
-         (set-buffer (car he-search-bufs))
-         (if (and (not (eq (current-buffer) buf))
-                  (if hippie-expand-only-buffers
-                      (he-buffer-member hippie-expand-only-buffers)
-                    (not (he-buffer-member hippie-expand-ignore-buffers))))
-             (save-excursion
-               (save-restriction
-                 (if hippie-expand-no-restriction
-                     (widen))
-                 (goto-char he-search-loc)
-                 (setq expansion
-                       (let ((case-fold-search orig-case-fold-search))
-                         (he-list-search he-search-string nil)))
-                 (set-marker he-search-loc (point))
-                 (if (not expansion)
-                     (progn
-                       (setq he-search-bufs (cdr he-search-bufs))
-                       (setq he-searched-n-bufs (1+ he-searched-n-bufs))
-                       (set-marker he-search-loc 1 (car he-search-bufs))))))
-           (setq he-search-bufs (cdr he-search-bufs))
-           (set-marker he-search-loc 1 (car he-search-bufs)))))
+  (he--all-buffers old #'he-list-beg #'he-list-search))
 
-    (set-buffer buf)
-    (if (not expansion)
-       (progn
-         (if old (he-reset-string))
-         ())
-       (progn
-         (he-substitute-string expansion t)
-         t))))
-
-(defun he-list-search (str reverse)
+(defun he-list-search (str &optional reverse)
   (let ((result ())
        beg pos err)
     (while (and (not result)
@@ -930,51 +888,7 @@ string).  It returns t if a new expansion is found, nil otherwise."
 The argument OLD has to be nil the first call of this function, and t
 for subsequent calls (for further possible expansions of the same
 string).  It returns t if a new expansion is found, nil otherwise."
-  (let ((expansion ())
-       (buf (current-buffer))
-       (orig-case-fold-search case-fold-search))
-    (if (not old)
-       (progn
-         (he-init-string (he-dabbrev-beg) (point))
-         (setq he-search-bufs (buffer-list))
-         (setq he-searched-n-bufs 0)
-         (set-marker he-search-loc 1 (car he-search-bufs))))
-
-    (if (not (equal he-search-string ""))
-       (while (and he-search-bufs
-                   (not expansion)
-                   (or (not hippie-expand-max-buffers)
-                       (< he-searched-n-bufs hippie-expand-max-buffers)))
-         (set-buffer (car he-search-bufs))
-         (if (and (not (eq (current-buffer) buf))
-                  (if hippie-expand-only-buffers
-                      (he-buffer-member hippie-expand-only-buffers)
-                    (not (he-buffer-member hippie-expand-ignore-buffers))))
-             (save-excursion
-               (save-restriction
-                 (if hippie-expand-no-restriction
-                     (widen))
-                 (goto-char he-search-loc)
-                 (setq expansion
-                       (let ((case-fold-search orig-case-fold-search))
-                         (he-dabbrev-search he-search-string nil)))
-                 (set-marker he-search-loc (point))
-                 (if (not expansion)
-                     (progn
-                       (setq he-search-bufs (cdr he-search-bufs))
-                       (setq he-searched-n-bufs (1+ he-searched-n-bufs))
-                       (set-marker he-search-loc 1 (car he-search-bufs))))))
-           (setq he-search-bufs (cdr he-search-bufs))
-           (set-marker he-search-loc 1 (car he-search-bufs)))))
-
-    (set-buffer buf)
-    (if (not expansion)
-       (progn
-         (if old (he-reset-string))
-         ())
-       (progn
-         (he-substitute-string expansion t)
-         t))))
+  (he--all-buffers old #'he-dabbrev-beg #'he-dabbrev-search))
 
 ;; Thanks go to Jeff Dairiki <dairiki@faraday.apl.washington.edu> who
 ;; suggested this one.
@@ -984,7 +898,7 @@ The argument OLD has to be nil the first call of this function, and t
 for subsequent calls (for further possible expansions of the same
 string).  It returns t if a new expansion is found, nil otherwise."
   (let ((expansion ())
-       (flag (if (frame-visible-p (window-frame (selected-window)))
+       (flag (if (frame-visible-p (window-frame))
                  'visible t)))
     (unless old
       (he-init-string (he-dabbrev-beg) (point))