(exec-suffixes): Initialize to a system-dependent value.
[bpt/emacs.git] / lisp / hippie-exp.el
index 4b16c71..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: 6 August 1995
-;; Version: 1.4
-;; 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."
+  :link '(custom-manual "(autotype)Hippie Expand")
+  :link '(emacs-commentary-link "hippie-exp")
+  :group 'abbrev
+  :group 'convenience)
+
 (defvar he-num -1)
 
 (defvar he-string-beg (make-marker))
 (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
+  "*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
-(defvar hippie-expand-verbose t
-  "*Non-nil makes `hippie-expand' output which function it is trying.")
+(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
-(defvar hippie-expand-max-buffers ()
+(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.")
+If nil, all buffers are searched."
+  :type '(choice (const :tag "All" nil)
+                integer)
+  :group 'hippie-expand)
 
 ;;;###autoload
-(defvar hippie-expand-ignore-buffers '("^ \\*.*\\*$" 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)")
+\(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
+\(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)
@@ -249,8 +306,7 @@ undoes the expansion."
            (if (and hippie-expand-verbose
                     (not (window-minibuffer-p (selected-window))))
                (message "Using %s"
-                        (prin1-to-string (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
@@ -264,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 ()
@@ -336,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
@@ -361,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:
@@ -445,16 +503,19 @@ otherwise."
 (defvar he-file-name-chars
   (cond ((memq system-type '(vax-vms axp-vms))
         "-a-zA-Z0-9_/.,~^#$+=:\\[\\]")
-       ((memq system-type '(ms-dos ms-windows))
+       ((memq system-type '(ms-dos windows-nt))
         "-a-zA-Z0-9_/.,~^#$+=:\\\\")
        (t                          ;; More strange file formats ?
         "-a-zA-Z0-9_/.,~^#$+="))
   "Characters that are considered part of the file name to expand.")
 
 (defun he-file-name-beg ()
-  (save-excursion
-    (skip-chars-backward he-file-name-chars)
-    (point)))
+  (let ((op (point)))
+    (save-excursion
+      (skip-chars-backward he-file-name-chars)
+      (if (> (skip-syntax-backward "w") 0)  ;; No words with non-file chars
+         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 
@@ -483,16 +544,17 @@ otherwise."
   (if (memq system-type '(vax-vms axp-vms))
       (or (file-directory-p file)
          (file-directory-p (concat file "[000000]")))
-    (file-directory-p dir-part)))
+    (file-directory-p file)))
   
 (defun he-concat-directory-file-name (dir-part name-part)
   "Try to slam together two parts of a file specification, system dependently."
-  (cond ((memq system-type '(axp-vms vax-vms))
+  (cond ((null dir-part) name-part)
+       ((memq system-type '(axp-vms vax-vms))
         (if (and (string= (substring dir-part -1) "]")
                  (string= (substring name-part 0 2) "[."))
             (concat (substring dir-part 0 -1) (substring name-part 1))
           (concat dir-part name-part)))
-       ((memq system-type '(ms-dos ms-windows))
+       ((memq system-type '(ms-dos w32))
         (if (and (string-match "\\\\" dir-part)
                  (not (string-match "/" dir-part))
                  (= (aref name-part (1- (length name-part))) ?/))
@@ -561,10 +623,9 @@ otherwise."
        t))))
 
 (defun he-lisp-symbol-beg ()
-  (let ((skips "-a-zA-Z0-9_."))
-    (save-excursion
-      (skip-chars-backward skips)
-      (point))))
+  (save-excursion
+    (skip-syntax-backward "w_")
+    (point)))
 
 (defun try-expand-line (old)
   "Try to complete the current line to an entire line in the buffer.
@@ -573,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
@@ -582,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
@@ -616,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))
@@ -633,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)))))
 
@@ -671,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))
@@ -707,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
@@ -754,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)))))
 
@@ -798,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))
@@ -855,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
@@ -902,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)))))
 
@@ -979,30 +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 (if (eq (char-syntax (aref pattern 0)) ?_)
-                   (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+")
-                 (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 (> (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.")
-
 (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 "w_") 0)
+      (if (= (skip-syntax-backward (if hippie-expand-dabbrev-as-symbol
+                                      "w_" "w"))
+            0)
          op
        (point)))))
 
@@ -1031,17 +1122,23 @@ string).  It returns t if a new completion is found, nil otherwise."
 
 (defun he-dabbrev-kill-search (pattern)
   (let ((result ())
-       (regpat (if (eq (char-syntax (aref pattern 0)) ?_)
-                   (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+")
-                 (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 (> (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))
@@ -1100,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