(exec-suffixes): Initialize to a system-dependent value.
[bpt/emacs.git] / lisp / hippie-exp.el
index bc87eab..11aa652 100644 (file)
@@ -1,11 +1,11 @@
-;;; hippie-exp.el --- expand text trying various ways to find its expansion.
+;;; hippie-exp.el --- expand text trying various ways to find its expansion
 
 ;; Copyright (C) 1992 Free Software Foundation, Inc.
 
 ;; Author: Anders Holst <aho@sans.kth.se>
-;; Last change: 28 May 1997
-;; Version: 1.5
-;; Keywords: abbrev
+;; Last change: 3 March 1998
+;; Version: 1.6
+;; Keywords: abbrev convenience
 
 ;; This file is part of GNU Emacs.
 
 ;;  matching buffer names (as strings) or major modes (as atoms) of
 ;;  buffers that should not be searched by the try-functions named
 ;;  "-all-buffers".
+;;  If set, the variable `hippie-expand-only-buffers' does the opposite
+;;  of `hippie-expand-ignore-buffers', in that the search is restricted
+;;  to only the kind of buffers listed.
+;;  If the variable `hippie-expand-no-restriction' is non-nil, narrowed
+;;  buffers are widened before they are searched.
+;;  The variable `hippie-expand-dabbrev-skip-space' controls whether
+;;  trailing spaces will be included in the abbreviation to search for,
+;;  which then gives the same behavior as the original `dabbrev-expand'.
+;;  The variable `hippie-expand-dabbrev-as-symbol' controls whether
+;;  characters of syntax '_' is considered part of the words to expand
+;;  dynamically.
 ;;  See also the macro `make-hippie-expand-function' below.
 ;;  
 ;;  A short description of the current try-functions in this file:
 
 ;;; Code:
 
+(eval-when-compile (require 'comint))
+
 (defgroup hippie-expand nil
   "Expand text trying various ways to find its expansion."
-  :group 'abbrev)
+  :link '(custom-manual "(autotype)Hippie Expand")
+  :link '(emacs-commentary-link "hippie-exp")
+  :group 'abbrev
+  :group 'convenience)
 
 (defvar he-num -1)
 
 (defvar he-search-window ())
 
 ;;;###autoload
-(defvar hippie-expand-try-functions-list '(try-complete-file-name-partially
-                                          try-complete-file-name
-                                          try-expand-all-abbrevs
-                                          try-expand-list
-                                          try-expand-line
-                                          try-expand-dabbrev
-                                          try-expand-dabbrev-all-buffers
-                                          try-expand-dabbrev-from-kill
-                                          try-complete-lisp-symbol-partially
-                                          try-complete-lisp-symbol)
+(defcustom hippie-expand-try-functions-list
+  '(try-complete-file-name-partially
+    try-complete-file-name
+    try-expand-all-abbrevs
+    try-expand-list
+    try-expand-line
+    try-expand-dabbrev
+    try-expand-dabbrev-all-buffers
+    try-expand-dabbrev-from-kill
+    try-complete-lisp-symbol-partially
+    try-complete-lisp-symbol)
   "The list of expansion functions tried in order by `hippie-expand'.
 To change the behavior of `hippie-expand', remove, change the order of,
-or insert functions in this list.")
+or insert functions in this list."
+  :type '(repeat function)
+  :group 'hippie-expand)
 
 ;;;###autoload
 (defcustom hippie-expand-verbose t
@@ -206,6 +225,24 @@ or insert functions in this list.")
   :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.
@@ -222,6 +259,15 @@ Can contain both regexps matching buffer names (as strings) and major modes
   :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
+\(as atoms). If non-NIL, this variable overrides the variable
+`hippie-expand-ignore-buffers'."
+  :type '(repeat (choice regexp (symbol :tag "Major Mode")))
+  :group 'hippie-expand)
+
 ;;;###autoload
 (defun hippie-expand (arg)
   "Try to expand text before point, using multiple methods.
@@ -260,7 +306,7 @@ undoes the expansion."
            (if (and hippie-expand-verbose
                     (not (window-minibuffer-p (selected-window))))
                (message "Using %s"
-                         (nth he-num hippie-expand-try-functions-list)))))
+                        (nth he-num hippie-expand-try-functions-list)))))
       (if (and (>= he-num 0)
               (eq (marker-buffer he-string-beg) (current-buffer)))
          (progn
@@ -274,7 +320,7 @@ undoes the expansion."
 (defun he-init-string (beg end)
   (set-marker he-string-beg beg)
   (set-marker he-string-end end)
-  (setq he-search-string (buffer-substring beg end)))
+  (setq he-search-string (buffer-substring-no-properties beg end)))
 
 ;; Resets the expanded region to its original contents.
 (defun he-reset-string ()
@@ -346,14 +392,16 @@ undoes the expansion."
                str)
              lst)))
 
-;; Check if STR matches any regexp in LST.
-;; Ignore possible non-strings in LST.
-(defun he-regexp-member (str lst)
-  (while (and lst
-             (or (not (stringp (car lst)))
-                 (not (string-match (car lst) str))))
-    (setq lst (cdr lst)))
-  lst)
+;; Check if current buffer matches any atom or regexp in LST.
+;; Atoms are interpreted as major modes, strings as regexps mathing the name.
+(defun he-buffer-member (lst)
+  (or (memq major-mode lst)
+      (progn
+       (while (and lst
+                   (or (not (stringp (car lst)))
+                       (not (string-match (car lst) (buffer-name)))))
+         (setq lst (cdr lst)))
+       lst)))
 
 ;;  For the real hippie-expand enthusiast: A macro that makes it
 ;;  possible to use many functions like hippie-expand, but with
@@ -371,14 +419,14 @@ undoes the expansion."
   "Construct a function similar to `hippie-expand'.
 Make it use the expansion functions in TRY-LIST.  An optional second
 argument VERBOSE non-nil makes the function verbose."
-  (` (function (lambda (arg)
-       (, (concat 
-          "Try to expand text before point, using the following functions: \n"
-          (mapconcat 'prin1-to-string (eval try-list) ", ")))
-       (interactive "P")
-       (let ((hippie-expand-try-functions-list (, try-list))
-            (hippie-expand-verbose (, verbose)))
-        (hippie-expand arg))))))
+  `(function (lambda (arg)
+    ,(concat 
+      "Try to expand text before point, using the following functions: \n"
+      (mapconcat 'prin1-to-string (eval try-list) ", "))
+    (interactive "P")
+    (let ((hippie-expand-try-functions-list ,try-list)
+          (hippie-expand-verbose ,verbose))
+      (hippie-expand arg)))))
 
 
 ;;;  Here follows the try-functions and their requisites:
@@ -466,8 +514,8 @@ otherwise."
     (save-excursion
       (skip-chars-backward he-file-name-chars)
       (if (> (skip-syntax-backward "w") 0)  ;; No words with non-file chars
-          op
-        (point)))))
+         op
+       (point)))))
 
 ;; Thanks go to Richard Levitte <levitte@e.kth.se> who helped to make these
 ;; work under VMS, and to David Hughes <ukchugd@ukpmr.cs.philips.nl> who 
@@ -586,6 +634,7 @@ for subsequent calls (for further possible completions of the same
 string).  It returns t if a new completion is found, nil otherwise."
   (let ((expansion ())
        (strip-prompt (and (get-buffer-process (current-buffer))
+                          comint-use-prompt-regexp-instead-of-fields
                           comint-prompt-regexp)))
     (if (not old)
        (progn
@@ -595,24 +644,27 @@ string).  It returns t if a new completion is found, nil otherwise."
 
     (if (not (equal he-search-string ""))
        (save-excursion
-         ;; Try looking backward unless inhibited.
-         (if he-search-bw
-             (progn 
-               (goto-char he-search-loc)
-               (setq expansion (he-line-search he-search-string
-                                               strip-prompt t))
-               (set-marker he-search-loc (point))
-               (if (not expansion)
-                   (progn
-                     (set-marker he-search-loc he-string-end)
-                     (setq he-search-bw ())))))
-         
-         (if (not expansion) ; Then look forward.
-             (progn 
-               (goto-char he-search-loc)
-               (setq expansion (he-line-search he-search-string 
-                                               strip-prompt nil))
-               (set-marker he-search-loc (point))))))
+         (save-restriction
+           (if hippie-expand-no-restriction
+               (widen))
+           ;; Try looking backward unless inhibited.
+           (if he-search-bw
+               (progn 
+                 (goto-char he-search-loc)
+                 (setq expansion (he-line-search he-search-string
+                                                 strip-prompt t))
+                 (set-marker he-search-loc (point))
+                 (if (not expansion)
+                     (progn
+                       (set-marker he-search-loc he-string-end)
+                       (setq he-search-bw ())))))
+
+           (if (not expansion) ; Then look forward.
+               (progn 
+                 (goto-char he-search-loc)
+                 (setq expansion (he-line-search he-search-string 
+                                                 strip-prompt nil))
+                 (set-marker he-search-loc (point)))))))
 
     (if (not expansion)
        (progn
@@ -629,6 +681,7 @@ for subsequent calls (for further possible completions of the same
 string).  It returns t if a new completion is found, nil otherwise."
   (let ((expansion ())
        (strip-prompt (and (get-buffer-process (current-buffer))
+                          comint-use-prompt-regexp-instead-of-fields
                           comint-prompt-regexp))
        (buf (current-buffer))
        (orig-case-fold-search case-fold-search))
@@ -646,22 +699,27 @@ string).  It returns t if a new completion is found, nil otherwise."
                        (< he-searched-n-bufs hippie-expand-max-buffers)))
          (set-buffer (car he-search-bufs))
          (if (and (not (eq (current-buffer) buf))
-                  (not (memq major-mode hippie-expand-ignore-buffers))
-                  (not (he-regexp-member (buffer-name)
-                                         hippie-expand-ignore-buffers)))
+                  (if hippie-expand-only-buffers
+                      (he-buffer-member hippie-expand-only-buffers)
+                    (not (he-buffer-member hippie-expand-ignore-buffers))))
              (save-excursion
-               (goto-char he-search-loc)
-               (setq strip-prompt (and (get-buffer-process (current-buffer))
-                                       comint-prompt-regexp))
-               (setq expansion (let ((case-fold-search orig-case-fold-search))
-                                 (he-line-search he-search-string
-                                                 strip-prompt 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)))))
+               (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-instead-of-fields
+                                         comint-prompt-regexp))
+                 (setq expansion 
+                       (let ((case-fold-search orig-case-fold-search))
+                         (he-line-search he-search-string
+                                         strip-prompt 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)))))
 
@@ -684,7 +742,8 @@ string).  It returns t if a new completion is found, nil otherwise."
                    (re-search-forward
                     (he-line-search-regexp str strip-prompt)
                     nil t)))
-      (setq result (buffer-substring (match-beginning 2) (match-end 2)))
+      (setq result (buffer-substring-no-properties (match-end 1)
+                                                  (match-end 0)))
       (if (he-string-member result he-tried-table t)
          (setq result nil)))               ; if already in table, ignore
     result))
@@ -720,22 +779,25 @@ string).  It returns t if a new completion is found, nil otherwise."
 
     (if (not (equal he-search-string ""))
        (save-excursion
-         ;; Try looking backward unless inhibited.
-         (if he-search-bw
-             (progn 
-               (goto-char he-search-loc)
-               (setq expansion (he-list-search he-search-string t))
-               (set-marker he-search-loc (point))
-               (if (not expansion)
-                   (progn
-                     (set-marker he-search-loc he-string-end)
-                     (setq he-search-bw ())))))
-         
-         (if (not expansion) ; Then look forward.
-             (progn 
-               (goto-char he-search-loc)
-               (setq expansion (he-list-search he-search-string nil))
-               (set-marker he-search-loc (point))))))
+         (save-restriction
+           (if hippie-expand-no-restriction
+               (widen))
+           ;; Try looking backward unless inhibited.
+           (if he-search-bw
+               (progn 
+                 (goto-char he-search-loc)
+                 (setq expansion (he-list-search he-search-string t))
+                 (set-marker he-search-loc (point))
+                 (if (not expansion)
+                     (progn
+                       (set-marker he-search-loc he-string-end)
+                       (setq he-search-bw ())))))
+
+           (if (not expansion) ; Then look forward.
+               (progn 
+                 (goto-char he-search-loc)
+                 (setq expansion (he-list-search he-search-string nil))
+                 (set-marker he-search-loc (point)))))))
 
     (if (not expansion)
        (progn
@@ -767,19 +829,23 @@ string).  It returns t if a new completion is found, nil otherwise."
                        (< he-searched-n-bufs hippie-expand-max-buffers)))
          (set-buffer (car he-search-bufs))
          (if (and (not (eq (current-buffer) buf))
-                  (not (memq major-mode hippie-expand-ignore-buffers))
-                  (not (he-regexp-member (buffer-name)
-                                         hippie-expand-ignore-buffers)))
+                  (if hippie-expand-only-buffers
+                      (he-buffer-member hippie-expand-only-buffers)
+                    (not (he-buffer-member hippie-expand-ignore-buffers))))
              (save-excursion
-               (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)))))
+               (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)))))
 
@@ -811,7 +877,7 @@ string).  It returns t if a new completion is found, nil otherwise."
          (setq err t))
       (if (not err)
          (progn
-           (setq result (buffer-substring beg (point)))
+           (setq result (buffer-substring-no-properties beg (point)))
            (if (he-string-member result he-tried-table t)
                (setq result nil))))           ; if already in table, ignore
       (goto-char pos))
@@ -868,22 +934,25 @@ string).  It returns t if a new expansion is found, nil otherwise."
 
     (if (not (equal he-search-string ""))
        (save-excursion
-         ;; Try looking backward unless inhibited.
-         (if he-search-bw
-             (progn 
-               (goto-char he-search-loc)
-               (setq expansion (he-dabbrev-search he-search-string t))
-               (set-marker he-search-loc (point))
-               (if (not expansion)
-                   (progn
-                     (set-marker he-search-loc he-string-end)
-                     (setq he-search-bw ())))))
-         
-         (if (not expansion) ; Then look forward.
-             (progn 
-               (goto-char he-search-loc)
-               (setq expansion (he-dabbrev-search he-search-string nil))
-               (set-marker he-search-loc (point))))))
+         (save-restriction
+           (if hippie-expand-no-restriction
+               (widen))
+           ;; Try looking backward unless inhibited.
+           (if he-search-bw
+               (progn 
+                 (goto-char he-search-loc)
+                 (setq expansion (he-dabbrev-search he-search-string t))
+                 (set-marker he-search-loc (point))
+                 (if (not expansion)
+                     (progn
+                       (set-marker he-search-loc he-string-end)
+                       (setq he-search-bw ())))))
+
+           (if (not expansion) ; Then look forward.
+               (progn 
+                 (goto-char he-search-loc)
+                 (setq expansion (he-dabbrev-search he-search-string nil))
+                 (set-marker he-search-loc (point)))))))
        
     (if (not expansion)
        (progn
@@ -915,19 +984,23 @@ string).  It returns t if a new expansion is found, nil otherwise."
                        (< he-searched-n-bufs hippie-expand-max-buffers)))
          (set-buffer (car he-search-bufs))
          (if (and (not (eq (current-buffer) buf))
-                  (not (memq major-mode hippie-expand-ignore-buffers))
-                  (not (he-regexp-member (buffer-name)
-                                         hippie-expand-ignore-buffers)))
+                  (if hippie-expand-only-buffers
+                      (he-buffer-member hippie-expand-only-buffers)
+                    (not (he-buffer-member hippie-expand-ignore-buffers))))
              (save-excursion
-               (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)))))
+               (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)))))
 
@@ -992,38 +1065,35 @@ string).  It returns t if a new expansion is found, nil otherwise."
 
 (defun he-dabbrev-search (pattern &optional reverse limit)
   (let ((result ())
-       (regpat (cond ((not he-dabbrev-as-symbol)
-                       (concat "\\<" (regexp-quote pattern) "\\sw+"))
-                      ((eq (char-syntax (aref pattern 0)) ?_)
-                       (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+"))
-                      (t
-                       (concat "\\<" (regexp-quote pattern)
-                               "\\(\\sw\\|\\s_\\)+")))))
+       (regpat (cond ((not hippie-expand-dabbrev-as-symbol)
+                      (concat "\\<" (regexp-quote pattern) "\\sw+"))
+                     ((eq (char-syntax (aref pattern 0)) ?_)
+                      (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+"))
+                     (t
+                      (concat "\\<" (regexp-quote pattern)
+                              "\\(\\sw\\|\\s_\\)+")))))
     (while (and (not result) 
                (if reverse
                     (re-search-backward regpat limit t)
                     (re-search-forward regpat limit t)))
-      (setq result (buffer-substring (match-beginning 0) (match-end 0)))
-      (if (or (and he-dabbrev-as-symbol
-                   (> (match-beginning 0) (point-min))
+      (setq result (buffer-substring-no-properties (match-beginning 0)
+                                                  (match-end 0)))
+      (if (or (and hippie-expand-dabbrev-as-symbol
+                  (> (match-beginning 0) (point-min))
                   (memq (char-syntax (char-after (1- (match-beginning 0))))
                         '(?_ ?w)))
              (he-string-member result he-tried-table t))
          (setq result nil)))     ; ignore if bad prefix or already in table
     result))
 
-(defvar he-dabbrev-skip-space ()
-  "Non-NIL means tolerate trailing spaces in the abbreviation to expand.")
-
-(defvar he-dabbrev-as-symbol t
-  "Non-NIL means expand as symbols, i.e. syntax '_' is considered a letter.")
-
 (defun he-dabbrev-beg ()
   (let ((op (point)))
     (save-excursion
-      (if he-dabbrev-skip-space
+      (if hippie-expand-dabbrev-skip-space
          (skip-syntax-backward ". "))
-      (if (= (skip-syntax-backward (if he-dabbrev-as-symbol "w_" "w")) 0)
+      (if (= (skip-syntax-backward (if hippie-expand-dabbrev-as-symbol
+                                      "w_" "w"))
+            0)
          op
        (point)))))
 
@@ -1052,22 +1122,23 @@ string).  It returns t if a new completion is found, nil otherwise."
 
 (defun he-dabbrev-kill-search (pattern)
   (let ((result ())
-       (regpat (cond ((not he-dabbrev-as-symbol)
-                       (concat "\\<" (regexp-quote pattern) "\\sw+"))
-                      ((eq (char-syntax (aref pattern 0)) ?_)
-                       (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+"))
-                      (t
-                       (concat "\\<" (regexp-quote pattern)
-                               "\\(\\sw\\|\\s_\\)+"))))
+       (regpat (cond ((not hippie-expand-dabbrev-as-symbol)
+                      (concat "\\<" (regexp-quote pattern) "\\sw+"))
+                     ((eq (char-syntax (aref pattern 0)) ?_)
+                      (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+"))
+                     (t
+                      (concat "\\<" (regexp-quote pattern)
+                              "\\(\\sw\\|\\s_\\)+"))))
        (killstr (car he-expand-list)))
     (while (and (not result) 
                he-expand-list)
       (while (and (not result)
                  (string-match regpat killstr he-search-loc2))
        (setq result (substring killstr (match-beginning 0) (match-end 0)))
+       (set-text-properties 0 (length result) () result)
        (setq he-search-loc2 (1+ (match-beginning 0)))
-       (if (or (and he-dabbrev-as-symbol
-                     (> (match-beginning 0) 0)
+       (if (or (and hippie-expand-dabbrev-as-symbol
+                    (> (match-beginning 0) 0)
                     (memq (char-syntax (aref killstr (1- (match-beginning 0))))
                           '(?_ ?w)))
                (he-string-member result he-tried-table t))
@@ -1126,7 +1197,9 @@ string).  It returns t if a new completion is found, nil otherwise."
                   (eq (char-after (- (point) pos)) (aref killstr 0))
                   (search-backward (substring killstr 0 pos)
                                    (- (point) pos) t))
-             (setq result (substring killstr pos))))
+             (progn
+               (setq result (substring killstr pos))
+               (set-text-properties 0 (length result) () result))))
        (if (and result
                 (he-string-member result he-tried-table))
            (setq result nil)))     ; ignore if already in table