(c-require-final-newline): Added a variable to make the initialization of
[bpt/emacs.git] / lisp / term.el
index e8e584d..eecd2ab 100644 (file)
@@ -2,9 +2,9 @@
 
 ;;; Copyright (C) 1988, 1990, 1992, 1994, 1995 Free Software Foundation, Inc.
 
-;;; Author: Per Bothner <bothner@cygnus.com>
-;;; Based on comint mode written by: Olin Shivers <shivers@cs.cmu.edu>
-;;; Keyword: processes
+;; Author: Per Bothner <bothner@cygnus.com>
+;; Based on comint mode written by: Olin Shivers <shivers@cs.cmu.edu>
+;; Keywords: processes
 
 ;; This file is part of GNU Emacs.
 
 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 ;; Boston, MA 02111-1307, USA.
 
+;;; Marck 13 2001
+;;; Fixes for CJK support by Yong Lu <lyongu@yahoo.com>.
+
 ;;; Dir/Hostname tracking and ANSI colorization by
 ;;; Marco Melgazzi <marco@techie.com>.
 
 ;;; To see what I've modified and where it came from search for '-mm'
 
+;;; Commentary:
+
 ;;; Speed considerations and a few caveats
 ;;; --------------------------------------
 ;;;
 ;; term-mode-hook is the term mode hook.  Basically for your keybindings.
 ;; term-load-hook is run after loading in this package.
 
-;; Code:
+;;; Code:
 
 ;; This is passed to the inferior in the EMACS environment variable,
 ;; so it is important to increase it if there are protocol-relevant changes.
 (defconst term-protocol-version "0.95")
 
+(eval-when-compile
+  (require 'ange-ftp))
 (require 'ring)
 (require 'ehelp)
 
@@ -651,10 +658,6 @@ Buffer local variable.")
 (put 'term-scroll-show-maximum-output 'permanent-local t)
 (put 'term-ptyp 'permanent-local t)
 
-;; Do FORMS if running under Emacs 19 or later.
-(defmacro term-if-emacs19 (&rest forms)
-  (if (string-match "^\\(19\\|[2-9][0-9]\\)" emacs-version)
-      (cons 'progn forms)))
 ;; True if running under XEmacs (previously Lucid Emacs).
 (defmacro term-is-xemacs ()  '(string-match "Lucid" emacs-version))
 ;; Do FORM if running under XEmacs (previously Lucid Emacs).
@@ -719,6 +722,7 @@ Buffer local variable.")
 
 (when (fboundp 'make-face)
 ;;; --- Simple faces ---
+  (copy-face 'default 'term-default)
   (make-face 'term-default-fg)
   (make-face 'term-default-bg)
   (make-face 'term-default-fg-inv)
@@ -728,31 +732,22 @@ Buffer local variable.")
   (make-face 'term-invisible)
   (make-face 'term-invisible-inv)
 
-  (copy-face 'default 'term-default-fg)
-  (copy-face 'default 'term-default-bg)
   (term-ignore-error
    (set-face-foreground 'term-default-fg term-default-fg-color))
   (term-ignore-error
    (set-face-background 'term-default-bg term-default-bg-color))
 
-  (copy-face 'default 'term-default-fg-inv)
-  (copy-face 'default 'term-default-bg-inv)
   (term-ignore-error
    (set-face-foreground 'term-default-fg-inv term-default-bg-color))
   (term-ignore-error
    (set-face-background 'term-default-bg-inv term-default-fg-color))
 
-  (copy-face 'default 'term-invisible)
   (term-ignore-error
    (set-face-background 'term-invisible term-default-bg-color))
 
-  (copy-face 'default 'term-invisible-inv)
   (term-ignore-error
    (set-face-background 'term-invisible-inv term-default-fg-color))
 
-  (copy-face 'default 'term-bold)
-  (copy-face 'default 'term-underline)
-
   ;; Set the colors of the new faces.
   (term-ignore-error
    (make-face-bold 'term-bold))
@@ -770,28 +765,20 @@ Buffer local variable.")
   (make-face 'term-cyan)
   (make-face 'term-white)
 
-  (copy-face 'default 'term-black)
   (term-ignore-error
    (set-face-foreground 'term-black "black"))
-  (copy-face 'default 'term-red)
   (term-ignore-error
    (set-face-foreground 'term-red "red"))
-  (copy-face 'default 'term-green)
   (term-ignore-error
    (set-face-foreground 'term-green "green"))
-  (copy-face 'default 'term-yellow)
   (term-ignore-error
    (set-face-foreground 'term-yellow "yellow"))
-  (copy-face 'default 'term-blue)
   (term-ignore-error
    (set-face-foreground 'term-blue "blue"))
-  (copy-face 'default 'term-magenta)
   (term-ignore-error
    (set-face-foreground 'term-magenta "magenta"))
-  (copy-face 'default 'term-cyan)
   (term-ignore-error
    (set-face-foreground 'term-cyan "cyan"))
-  (copy-face 'default 'term-white)
   (term-ignore-error
    (set-face-foreground 'term-white "white"))
 
@@ -805,28 +792,20 @@ Buffer local variable.")
   (make-face 'term-cyanbg)
   (make-face 'term-whitebg)
 
-  (copy-face 'default 'term-blackbg)
   (term-ignore-error
    (set-face-background 'term-blackbg "black"))
-  (copy-face 'default 'term-redbg)
   (term-ignore-error
    (set-face-background 'term-redbg "red"))
-  (copy-face 'default 'term-greenbg)
   (term-ignore-error
    (set-face-background 'term-greenbg "green"))
-  (copy-face 'default 'term-yellowbg)
   (term-ignore-error
    (set-face-background 'term-yellowbg "yellow"))
-  (copy-face 'default 'term-bluebg)
   (term-ignore-error
    (set-face-background 'term-bluebg "blue"))
-  (copy-face 'default 'term-magentabg)
   (term-ignore-error
    (set-face-background 'term-magentabg "magenta"))
-  (copy-face 'default 'term-cyanbg)
   (term-ignore-error
    (set-face-background 'term-cyanbg "cyan"))
-  (copy-face 'default 'term-whitebg)
   (term-ignore-error
    (set-face-background 'term-whitebg "white")))
 
@@ -1056,7 +1035,7 @@ Entry to this mode runs the hooks on term-mode-hook"
 
 ;; Menu bars:
 (term-ifnot-xemacs
- (term-if-emacs19
+ (progn
 
   ;; terminal:
   (let (newmap)
@@ -1202,20 +1181,22 @@ without any interpretation."
 
 (defun term-send-raw-meta ()
   (interactive)
-  (if (symbolp last-input-char)
+  (let ((char last-input-char))
+    (when (symbolp last-input-char)
       ;; Convert `return' to C-m, etc.
-      (let ((tmp (get last-input-char 'event-symbol-elements)))
-       (if tmp
-           (setq last-input-char (car tmp)))
-       (if (symbolp last-input-char)
-           (progn
-             (setq tmp (get last-input-char 'ascii-character))
-             (if tmp (setq last-input-char tmp))))))
-  (term-send-raw-string (if (and (numberp last-input-char)
-                                (> last-input-char 127)
-                                (< last-input-char 256))
-                           (make-string 1 last-input-char)
-                         (format "\e%c" last-input-char))))
+      (let ((tmp (get char 'event-symbol-elements)))
+       (when tmp
+         (setq char (car tmp)))
+       (when (symbolp char)
+         (setq tmp (get char 'ascii-character))
+         (when tmp
+           (setq char tmp)))))
+    (setq char (event-basic-type char))
+    (term-send-raw-string (if (and (numberp char)
+                                  (> char 127)
+                                  (< char 256))
+                             (make-string 1 char)
+                           (format "\e%c" char)))))
 
 (defun term-mouse-paste (click arg)
   "Insert the last stretch of killed text at the position clicked on."
@@ -1264,7 +1245,9 @@ without any interpretation."
   (define-key term-raw-escape-map "\C-q" 'term-pager-toggle)
   ;; The keybinding for term-char-mode is needed by the menubar code.
   (define-key term-raw-escape-map "\C-k" 'term-char-mode)
-  (define-key term-raw-escape-map "\C-j" 'term-line-mode))
+  (define-key term-raw-escape-map "\C-j" 'term-line-mode)
+  ;; It's convenient to have execute-extended-command here.
+  (define-key term-raw-escape-map [?\M-x] 'execute-extended-command))
 
 (defun term-char-mode ()
   "Switch to char (\"raw\") sub-mode of term mode.
@@ -1279,6 +1262,8 @@ intervention from Emacs, except for the escape character (usually C-c)."
          (define-key map (make-string 1 i) 'term-send-raw)
          (define-key esc-map (make-string 1 i) 'term-send-raw-meta)
          (setq i (1+ i)))
+       (dolist (elm (generic-character-list))
+         (define-key map (vector elm) 'term-send-raw))
        (define-key map "\e" esc-map)
        (setq term-raw-map map)
        (setq term-raw-escape-map
@@ -1286,7 +1271,7 @@ intervention from Emacs, except for the escape character (usually C-c)."
 
 ;;; Added nearly all the 'grey keys' -mm
 
-       (term-if-emacs19
+       (progn
         (term-if-xemacs
          (define-key term-raw-map [button2] 'term-mouse-paste))
         (term-ifnot-xemacs
@@ -1391,7 +1376,7 @@ buffer.  The hook term-exec-hook is run after each exec."
     ;; Crank up a new process
     (let ((proc (term-exec-1 name buffer command switches)))
       (make-local-variable 'term-ptyp)
-      (setq term-ptyp process-connection-type) ; T if pty, NIL if pipe.
+      (setq term-ptyp process-connection-type) ; t if pty, nil if pipe.
       ;; Jump to the end, and set the process mark.
       (goto-char (point-max))
       (set-marker (process-mark proc) (point))
@@ -1445,6 +1430,7 @@ buffer.  The hook term-exec-hook is run after each exec."
               (format "TERMINFO=%s" data-directory)
            (format term-termcap-format "TERMCAP="
                    term-term-name term-height term-width))
+          ;; Breaks `./configure' of w3 and url which try to run $EMACS.
           (format "EMACS=%s (term:%s)" emacs-version term-protocol-version)
           (format "LINES=%d" term-height)
           (format "COLUMNS=%d" term-width))
@@ -2005,13 +1991,13 @@ If the interpreter is the csh,
         initial string matching regexp term-prompt-regexp.
     term-input-filter-functions monitors input for \"cd\", \"pushd\", and
        \"popd\" commands.  When it sees one, it cd's the buffer.
-    term-input-filter is the default: returns T if the input isn't all white
+    term-input-filter is the default: returns t if the input isn't all white
        space.
 
 If the term is Lucid Common Lisp,
     term-get-old-input snarfs the sexp ending at point.
     term-input-filter-functions does nothing.
-    term-input-filter returns NIL if the input matches input-filter-regexp,
+    term-input-filter returns nil if the input matches input-filter-regexp,
         which matches (1) all whitespace (2) :a, :c, etc.
 
 Similarly for Soar, Scheme, etc."
@@ -2389,7 +2375,7 @@ See `term-prompt-regexp'."
 ;;; your cursor over a string that's a filename and have it taken as default.
 ;;;
 ;;; If the command is given in a file buffer whose major mode is in
-;;; SOURCE-MODES, then the the filename is the default file, and the
+;;; SOURCE-MODES, then the filename is the default file, and the
 ;;; file's directory is the default directory.
 ;;;
 ;;; If the buffer isn't a source file buffer (e.g., it's the process buffer),
@@ -2599,10 +2585,10 @@ See `term-prompt-regexp'."
   (cond (term-current-column)
        ((setq term-current-column (current-column)))))
 
-;;; Move DELTA column right (or left if delta < 0).
+;;; Move DELTA column right (or left if delta < 0 limiting at column 0).
 
 (defun term-move-columns (delta)
-  (setq term-current-column (+ (term-current-column) delta))
+  (setq term-current-column (max 0 (+ (term-current-column) delta)))
   (move-to-column term-current-column t))
 
 ;; Insert COUNT copies of CHAR in the default face.
@@ -2779,19 +2765,21 @@ See `term-prompt-regexp'."
                                   (setq term-current-column nil)
                                   (setq term-start-line-column nil)))
                            (setq old-point (point))
-                           ;; In the common case that we're at the end of
-                           ;; the buffer, we can save a little work.
-                           (cond ((/= (point) (point-max))
-                                  (if term-insert-mode
-                                      ;; Inserting spaces, then deleting them,
-                                      ;; then inserting the actual text is
-                                      ;; inefficient, but it is simple, and
-                                      ;; the actual overhead is miniscule.
-                                      (term-insert-spaces count))
-                                  (term-move-columns count)
-                                  (delete-region old-point (point)))
-       (t (setq term-current-column (+ (term-current-column) count))))
-                           (insert (substring str i funny))
+
+                           ;; Insert a string, check how many columns
+                           ;; we moved, then delete that many columns
+                           ;; following point if not eob nor insert-mode.
+                           (let ((old-column (current-column))
+                                 columns pos)
+                             (insert (substring str i funny))
+                             (setq term-current-column (current-column)
+                                   columns (- term-current-column old-column))
+                             (when (not (or (eobp) term-insert-mode))
+                               (setq pos (point))
+                               (term-move-columns columns)
+                               (delete-region pos (point))))
+                           (setq term-current-column nil)
+                                                        
                            (put-text-property old-point (point)
                                               'face term-current-face)
                            ;; If the last char was written in last column,
@@ -2839,11 +2827,11 @@ See `term-prompt-regexp'."
                           ((eq char ?\^G)
                            (beep t)) ; Bell
                           ((eq char ?\032)
-                           (let ((end (string-match "\n" str i)))
+                           (let ((end (string-match "\r?$" str i)))
                              (if end
-                                 (progn (funcall term-command-hook
-                                                 (substring str (1+ i) (1- end)))
-                                        (setq i end))
+                                 (funcall term-command-hook
+                                          (prog1 (substring str (1+ i) end)
+                                            (setq i (match-end 0))))
                                (setq term-terminal-parameter
                                      (substring str i))
                                (setq term-terminal-state 4)
@@ -3352,7 +3340,7 @@ The top-most line is line 0."
        (define-key map ">" 'term-pager-eob)
 
        ;; Add menu bar.
-       (term-if-emacs19
+       (progn
         (term-ifnot-xemacs
          (define-key map [menu-bar terminal] term-terminal-menu)
          (define-key map [menu-bar signals] term-signals-menu)