]> code.delx.au - gnu-emacs/blobdiff - lisp/comint.el
(compilation-error-regexp-alist): Make separate
[gnu-emacs] / lisp / comint.el
index 11e49d4fdb7965cd01ef53934fdbb22329e4c63e..d8c51121de07728e297071364b457c9ad32fa124 100644 (file)
@@ -69,8 +69,8 @@
 ;;; m-s     comint-next-matching-input      Next input that matches
 ;;; m-c-l   comint-show-output             Show last batch of process output
 ;;; return  comint-send-input
-;;; c-a     comint-bol                      Beginning of line; skip prompt
 ;;; c-d            comint-delchar-or-maybe-eof     Delete char unless at end of buff
+;;; c-c c-a comint-bol                      Beginning of line; skip prompt
 ;;; c-c c-u comint-kill-input              ^u
 ;;; c-c c-w backward-kill-word             ^w
 ;;; c-c c-c comint-interrupt-subjob        ^c
@@ -219,6 +219,10 @@ appears in the buffer.
 
 This variable is buffer-local.")
 
+(defvar comint-password-prompt-regexp "\\b[Pp]assword:\\s *\\'"
+  "*Regexp matching prompts for passwords in the inferior process.
+This is used by comint-watch-for-password-prompt.")
+
 ;;; Here are the per-interpreter hooks.
 (defvar comint-get-old-input (function comint-get-old-input-default)
   "Function that returns old text in comint mode.
@@ -342,12 +346,14 @@ Entry to this mode runs the hooks on `comint-mode-hook'."
   (kill-all-local-variables)
   (setq major-mode 'comint-mode)
   (setq mode-name "Comint")
-  (setq mode-line-process '(": %s"))
+  (setq mode-line-process '(":%s"))
   (use-local-map comint-mode-map)
   (make-local-variable 'comint-last-input-start)
   (setq comint-last-input-start (make-marker))
+  (set-marker comint-last-input-start (point-min))
   (make-local-variable 'comint-last-input-end)
   (setq comint-last-input-end (make-marker))
+  (set-marker comint-last-input-end (point-min))
   (make-local-variable 'comint-last-output-start)
   (setq comint-last-output-start (make-marker))
   (make-local-variable 'comint-prompt-regexp)        ; Don't set; default
@@ -394,7 +400,7 @@ Entry to this mode runs the hooks on `comint-mode-hook'."
   (define-key comint-mode-map "\e\C-l" 'comint-show-output)
   (define-key comint-mode-map "\C-m" 'comint-send-input)
   (define-key comint-mode-map "\C-d" 'comint-delchar-or-maybe-eof)
-  (define-key comint-mode-map "\C-a" 'comint-bol)
+  (define-key comint-mode-map "\C-c\C-a" 'comint-bol)
   (define-key comint-mode-map "\C-c\C-u" 'comint-kill-input)
   (define-key comint-mode-map "\C-c\C-w" 'backward-kill-word)
   (define-key comint-mode-map "\C-c\C-c" 'comint-interrupt-subjob)
@@ -404,7 +410,7 @@ Entry to this mode runs the hooks on `comint-mode-hook'."
   (define-key comint-mode-map "\C-c\C-o" 'comint-kill-output)
   (define-key comint-mode-map "\C-c\C-r" 'comint-show-output)
   (define-key comint-mode-map "\C-c\C-e" 'comint-show-maximum-output)
-  (define-key comint-mode-map "\C-c\C-h" 'comint-dynamic-list-input-ring)
+  (define-key comint-mode-map "\C-c\C-l" 'comint-dynamic-list-input-ring)
   (define-key comint-mode-map "\C-c\C-n" 'comint-next-prompt)
   (define-key comint-mode-map "\C-c\C-p" 'comint-previous-prompt)
   (define-key comint-mode-map "\C-c\C-d" 'comint-send-eof)
@@ -553,9 +559,20 @@ buffer.  The hook `comint-exec-hook' is run after each exec."
 
 (defun comint-exec-1 (name buffer command switches)
   (let ((process-environment
-        (nconc (list "EMACS=t" "TERM=emacs"
-                     (format "TERMCAP=emacs:co#%d:tc=unknown" (frame-width)))
-               process-environment)))
+        (nconc
+         ;; If using termcap, we specify `emacs' as the terminal type
+         ;; because that lets us specify a width.
+         ;; If using terminfo, we specify `unknown' because that is
+         ;; a defined terminal type.  `emacs' is not a defined terminal type
+         ;; and there is no way for us to define it here.
+         ;; Some programs that use terminfo get very confused
+         ;; if TERM is not a valid terminal type.
+         (if (and (boundp 'system-uses-terminfo) system-uses-terminfo)
+             (list "EMACS=t" "TERM=unknown"
+                   (format "COLUMNS=%d" (frame-width)))
+           (list "EMACS=t" "TERM=emacs"
+                 (format "TERMCAP=emacs:co#%d:tc=unknown" (frame-width))))
+         process-environment)))
     (apply 'start-process name buffer command switches)))
 \f
 ;;; Input history processing in a buffer
@@ -603,27 +620,30 @@ See also `comint-input-ignoredups' and `comint-write-input-ring'."
             (message "Cannot read history file %s"
                      comint-input-ring-file-name)))
        (t
-        (let ((history-buf (get-file-buffer comint-input-ring-file-name))
+        (let ((history-buf (get-buffer-create " *temp*"))
+              (file comint-input-ring-file-name)
+              (count 0)
               (ring (make-ring comint-input-ring-size)))
-          (save-excursion
-            (set-buffer (or history-buf
-                            (find-file-noselect comint-input-ring-file-name)))
-            ;; Save restriction in case file is already visited...
-            ;; Watch for those date stamps in history files!
-            (save-excursion
-              (save-restriction
+          (unwind-protect
+              (save-excursion
+                (set-buffer history-buf)
                 (widen)
-                (goto-char (point-min))
-                (while (re-search-forward "^\\s *\\([^#].*\\)\\s *$" nil t)
+                (erase-buffer)
+                (insert-file-contents file)
+                ;; Save restriction in case file is already visited...
+                ;; Watch for those date stamps in history files!
+                (goto-char (point-max))
+                (while (and (< count comint-input-ring-size)
+                            (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$"
+                                                nil t))
                   (let ((history (buffer-substring (match-beginning 1)
                                                    (match-end 1))))
                     (if (or (null comint-input-ignoredups)
                             (ring-empty-p ring)
                             (not (string-equal (ring-ref ring 0) history)))
-                        (ring-insert ring history)))))
-              ;; Kill buffer unless already visited.
-              (if (null history-buf)
-                  (kill-buffer nil))))
+                        (ring-insert-at-beginning ring history)))
+                  (setq count (1+ count))))
+            (kill-buffer history-buf))
           (setq comint-input-ring ring
                 comint-input-ring-index nil)))))
 
@@ -850,15 +870,17 @@ See `comint-magic-space' and `comint-replace-by-expanded-history-before-point'.
 Returns t if successful."
   (interactive)
   (if (and comint-input-autoexpand
-          (string-match "[!^]" (funcall comint-get-old-input)))
+          (string-match "[!^]" (funcall comint-get-old-input))
+          (save-excursion (beginning-of-line)
+                          (looking-at comint-prompt-regexp)))
       ;; Looks like there might be history references in the command.
       (let ((previous-modified-tick (buffer-modified-tick)))
        (message "Expanding history references...")
-       (comint-replace-by-expanded-history-before-point)
+       (comint-replace-by-expanded-history-before-point silent)
        (/= previous-modified-tick (buffer-modified-tick)))))
 
 
-(defun comint-replace-by-expanded-history-before-point ()
+(defun comint-replace-by-expanded-history-before-point (silent)
   "Expand directory stack reference before point.
 See `comint-replace-by-expanded-history'.  Returns t if successful."
   (save-excursion
@@ -920,10 +942,11 @@ See `comint-replace-by-expanded-history'.  Returns t if successful."
                             (comint-previous-matching-input-string-position
                              (concat pref (regexp-quote exp)) 1))))
                 (if (null pos)
-                    (or silent
-                        (progn (message "Not found")
-                               (goto-char (match-end 0))
-                               (ding)))
+                    (progn
+                      (goto-char (match-end 0))
+                      (or silent
+                          (progn (message "Not found")
+                                 (ding))))
                   (setq comint-input-ring-index pos)
                   (replace-match
                    (comint-args (ring-ref comint-input-ring pos)
@@ -1118,18 +1141,14 @@ Similarly for Soar, Scheme, etc."
            (while functions
              (funcall (car functions) (concat input "\n"))
              (setq functions (cdr functions))))
-         (funcall comint-input-sender proc input)
          (setq comint-input-ring-index nil)
+         ;; Update the markers before we send the input
+         ;; in case we get output amidst sending the input.
          (set-marker comint-last-input-start pmark)
          (set-marker comint-last-input-end (point))
          (set-marker (process-mark proc) (point))
-         ;; A kludge to prevent the delay between insert and process output
-         ;; affecting the display.  A case for a comint-send-input-hook?
-         (if (eq (process-filter proc) 'comint-output-filter)
-             (let ((functions comint-output-filter-functions))
-               (while functions
-                 (funcall (car functions) (concat input "\n"))
-                 (setq functions (cdr functions)))))))))
+         (funcall comint-input-sender proc input)
+         (comint-output-filter proc "")))))
 
 ;; The purpose of using this filter for comint processes
 ;; is to keep comint-last-input-end from moving forward
@@ -1296,10 +1315,7 @@ set the hook `comint-input-sender'."
 If prefix argument is given (\\[universal-argument]) the prompt is not skipped. 
 
 The prompt skip is done by skipping text matching the regular expression
-`comint-prompt-regexp', a buffer local variable.
-
-If you don't like this command, bind C-a to `beginning-of-line' 
-in your hook, `comint-mode-hook'."
+`comint-prompt-regexp', a buffer local variable."
   (interactive "P")
   (beginning-of-line)
   (if (null arg) (comint-skip-prompt)))
@@ -1374,10 +1390,11 @@ Security bug: your string can still be temporarily recovered with
 (defun comint-watch-for-password-prompt (string) 
   "Prompt in the minibuffer for password and send without echoing.
 This function uses `send-invisible' to read and send a password to the buffer's
-process if STRING contains a password prompt (matches \"^[Pp]assword:\\\\s *\\\\'\").
+process if STRING contains a password prompt defined by 
+`comint-password-prompt-regexp'.
 
 This function could be in the list `comint-output-filter-functions'."
-  (if (string-match "^[Pp]assword:\\s *\\'" string)
+  (if (string-match comint-password-prompt-regexp string)
       (send-invisible nil)))
 \f
 ;;; Low-level process communication
@@ -1414,14 +1431,19 @@ your process from hanging on long inputs.  See `comint-send-string'."
   "Kill all output from interpreter since last input.
 Does not delete the prompt."
   (interactive)
-  (let ((pmark (progn (goto-char
-                      (process-mark (get-buffer-process (current-buffer))))
-                     (beginning-of-line nil)
-                     (point-marker))))
-    (kill-region comint-last-input-end pmark)
-    (insert "*** output flushed ***\n")
-    (comint-skip-prompt)
-    (set-marker pmark (point))))
+  (let ((proc (get-buffer-process (current-buffer)))
+       (replacement nil))
+    (save-excursion
+      (let ((pmark (progn (goto-char (process-mark proc))
+                         (beginning-of-line nil)
+                         (point-marker))))
+       (delete-region comint-last-input-end pmark)
+       (comint-skip-prompt)
+       (setq replacement (concat "*** output flushed ***\n"
+                                 (buffer-substring pmark (point))))
+       (delete-region pmark (point))))
+    ;; Output message and put back prompt
+    (comint-output-filter proc replacement)))
 
 (defun comint-show-output ()
   "Display start of this batch of interpreter output at top of window.
@@ -1822,7 +1844,8 @@ completions listing is dependent on the value of `comint-completion-autolist'.
 Returns t if successful."
   (interactive)
   (if (comint-match-partial-filename)
-      (prog2 (message "Completing file name...")
+      (prog2 (or (eq (selected-window) (minibuffer-window))
+                (message "Completing file name..."))
          (comint-dynamic-complete-as-filename))))
 
 
@@ -1833,40 +1856,40 @@ See `comint-dynamic-complete-filename'.  Returns t if successful."
         (completion-ignored-extensions comint-completion-fignore)
         (success t)
         (filename (or (comint-match-partial-filename) ""))
-         (pathdir (file-name-directory filename))
-         (pathnondir (file-name-nondirectory filename))
-         (directory (if pathdir (comint-directory pathdir) default-directory))
-        (completion (file-name-completion pathnondir directory)))
+        (pathdir (file-name-directory filename))
+        (pathnondir (file-name-nondirectory filename))
+        (directory (if pathdir (comint-directory pathdir) default-directory))
+        (completion (file-name-completion pathnondir directory))
+        (mini-flag (eq (selected-window) (minibuffer-window))))
     (cond ((null completion)
            (message "No completions of %s" filename)
           (setq success nil))
           ((eq completion t)            ; Means already completed "file".
            (if comint-completion-addsuffix (insert " "))
-           (message "Sole completion"))
+           (or mini-flag (message "Sole completion")))
           ((string-equal completion "") ; Means completion on "directory/".
            (comint-dynamic-list-filename-completions))
           (t                            ; Completion string returned.
            (let ((file (concat (file-name-as-directory directory) completion)))
-             (goto-char (match-end 0))
              (insert (substring (directory-file-name completion)
                                 (length pathnondir)))
              (cond ((symbolp (file-name-completion completion directory))
                     ;; We inserted a unique completion.
                     (if comint-completion-addsuffix
                         (insert (if (file-directory-p file) "/" " ")))
-                    (message "Completed"))
+                    (or mini-flag (message "Completed")))
                    ((and comint-completion-recexact comint-completion-addsuffix
                          (string-equal pathnondir completion)
                          (file-exists-p file))
                     ;; It's not unique, but user wants shortest match.
                     (insert (if (file-directory-p file) "/" " "))
-                    (message "Completed shortest"))
+                    (or mini-flag (message "Completed shortest")))
                    ((or comint-completion-autolist
                         (string-equal pathnondir completion))
                     ;; It's not unique, list possible completions.
                     (comint-dynamic-list-filename-completions))
                    (t
-                    (message "Partially completed"))))))
+                    (or mini-flag (message "Partially completed")))))))
     success))
 
 
@@ -1949,17 +1972,17 @@ See also `comint-dynamic-complete-filename'."
   "List in help buffer sorted COMPLETIONS.
 Typing SPC flushes the help buffer."
   (let ((conf (current-window-configuration)))
-    (with-output-to-temp-buffer " *Completions*"
+    (with-output-to-temp-buffer "*Completions*"
       (display-completion-list (sort completions 'string-lessp)))
     (message "Hit space to flush")
     (let (key first)
       (if (save-excursion
-           (set-buffer (get-buffer " *Completions*"))
+           (set-buffer (get-buffer "*Completions*"))
            (setq key (read-key-sequence nil)
                  first (aref key 0))
            (and (consp first)
                 (eq (window-buffer (posn-window (event-start first)))
-                    (get-buffer " *Completions*"))
+                    (get-buffer "*Completions*"))
                 (eq (key-binding key) 'mouse-choose-completion)))
          ;; If the user does mouse-choose-completion with the mouse,
          ;; execute the command, then delete the completion window.
@@ -1968,7 +1991,7 @@ Typing SPC flushes the help buffer."
            (set-window-configuration conf))
        (if (eq first ?\ )
            (set-window-configuration conf)
-         (setq unread-command-events (append key nil)))))))
+         (setq unread-command-events (listify-key-sequence key)))))))
 \f
 ;;; Converting process modes to use comint mode
 ;;; ===========================================================================
@@ -2025,7 +2048,7 @@ Typing SPC flushes the help buffer."
 ;;; 
 ;;; (defvar shell-mode-map '())
 ;;; (cond ((not shell-mode-map)
-;;;        (setq shell-mode-map (full-copy-sparse-keymap comint-mode-map))
+;;;        (setq shell-mode-map (copy-keymap comint-mode-map))
 ;;;        (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command)
 ;;;        (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command)
 ;;;        (define-key shell-mode-map "\t" 'comint-dynamic-complete)