home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / terminal.el < prev    next >
Encoding:
Text File  |  1995-03-25  |  40.3 KB  |  1,197 lines

  1. ;;; terminal.el --- terminal emulator for GNU Emacs.
  2. ;; Keywords: comm, terminals
  3.  
  4. ;; Copyright (C) 1986, 1987, 1988, 1989, 1993 Free Software Foundation, Inc.
  5. ;; Written by Richard Mlynarik, November 1986.
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  21. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;#### TODO
  24. ;;#### terminfo?
  25.  
  26. ;;#### One probably wants to do setenv MORE -c when running with
  27. ;;####   more-processing enabled.
  28.  
  29. (provide 'terminal)
  30. (require 'ehelp)
  31.  
  32. (defvar terminal-escape-char ?\C-^
  33.   "*All characters except for this are passed verbatim through the
  34. terminal-emulator.  This character acts as a prefix for commands
  35. to the emulator program itself.  Type this character twice to send
  36. it through the emulator.  Type ? after typing it for a list of
  37. possible commands.
  38. This variable is local to each terminal-emulator buffer.")
  39.  
  40. (defvar terminal-scrolling t
  41.   "*If non-nil, the terminal-emulator will `scroll' when output occurs
  42. past the bottom of the screen.  If nil, output will `wrap' to the top
  43. of the screen.
  44. This variable is local to each terminal-emulator buffer.")
  45.  
  46. (defvar terminal-more-processing t
  47.   "*If non-nil, do more-processing.
  48. This variable is local to each terminal-emulator buffer.")
  49.  
  50. ;; If you are the sort of loser who uses scrolling without more breaks
  51. ;; and expects to actually see anything, you should probably set this to
  52. ;; around 400
  53. (defvar terminal-redisplay-interval 5000
  54.   "*Maximum number of characters which will be processed by the
  55. terminal-emulator before a screen redisplay is forced.
  56. Set this to a large value for greater throughput,
  57. set it smaller for more frequent updates but overall slower
  58. performance.")
  59.  
  60. (defvar terminal-more-break-insertion
  61.   "*** More break -- Press space to continue ***")
  62.  
  63. (defvar terminal-escape-map nil)
  64. (defvar terminal-map nil)
  65. (defvar terminal-more-break-map nil)
  66. (if terminal-map
  67.     nil
  68.   (let ((map (make-keymap)))
  69.     (set-keymap-name map 'terminal-map)
  70.  
  71.     (let ((meta-prefix-char -1)
  72.           (s (make-string 1 0))
  73.           (i 0))
  74.       (while (< i 256)
  75.         (aset s 0 i)
  76.         (define-key map s 'te-pass-through)
  77.         (setq i (1+ i))))
  78.  
  79.     ;(define-key map "\C-l"
  80.     ;  '(lambda () (interactive) (te-pass-through) (redraw-display)))
  81.     (setq terminal-map map)))
  82.  
  83. (if terminal-escape-map
  84.     nil
  85.   (let ((map (make-keymap)))
  86.     (set-keymap-name map 'terminal-escape-map)
  87.     (let ((s (make-string 1 ?0)))
  88.       (while (<= (aref s 0) ?9)
  89.     (define-key map s 'digit-argument)
  90.     (aset s 0 (1+ (aref s 0)))))
  91.     (define-key map "b" 'switch-to-buffer)
  92.     (define-key map "o" 'other-window)
  93.     (define-key map "e" 'te-set-escape-char)
  94.     (define-key map "\C-l" 'redraw-display)
  95.     (define-key map "\C-o" 'te-flush-pending-output)
  96.     (define-key map "m" 'te-toggle-more-processing)
  97.     (define-key map "x" 'te-escape-extended-command)
  98.     (define-key map "?" 'te-escape-help)
  99.     (define-key map (char-to-string help-char) 'te-escape-help)
  100.     (setq terminal-escape-map map)))
  101.  
  102. (defvar te-escape-command-alist '())
  103. (if te-escape-command-alist
  104.     nil
  105.   (setq te-escape-command-alist
  106.     '(("Set Escape Character" . te-set-escape-char)
  107.       ("Refresh" . redraw-display)
  108.       ("Record Output" . te-set-output-log)
  109.       ("Photo" . te-set-output-log)
  110.       ("Tofu" . te-tofu) ;; confuse the uninitiated
  111.       ("Stuff Input" . te-stuff-string)
  112.       ("Flush Pending Output" . te-flush-pending-output)
  113.       ("Enable More Processing" . te-enable-more-processing)
  114.       ("Disable More Processing" . te-disable-more-processing)
  115.       ("Scroll at end of page" . te-do-scrolling)
  116.       ("Wrap at end of page" . te-do-wrapping)
  117.       ("Switch To Buffer" . switch-to-buffer)
  118.       ("Other Window" . other-window)
  119.       ("Kill Buffer" . kill-buffer)
  120.       ("Help" . te-escape-help)
  121.       ("Set Redisplay Interval" . te-set-redisplay-interval)
  122.       )))
  123.  
  124. ;(setq terminal-more-break-map nil)
  125. (if terminal-more-break-map
  126.     nil
  127.   (let ((map (make-keymap)))
  128.     (set-keymap-name map 'terminal-more-break-map)
  129.  
  130.     (let ((meta-prefix-char -1)
  131.           (s (make-string 1 0))
  132.           (i 0))
  133.       (while (< i 256)
  134.         (aset s 0 i)
  135.         (define-key map s 'te-more-break-unwind)
  136.         (setq i (1+ i))))
  137.  
  138.     (define-key map (char-to-string help-char) 'te-more-break-help)
  139.     (define-key map " " 'te-more-break-resume)
  140.     (define-key map "\C-l" 'redraw-display)
  141.     (define-key map "\C-o" 'te-more-break-flush-pending-output)
  142.     ;;#### this isn't right
  143.     ;(define-key map "\^?" 'te-more-break-flush-pending-output) ;DEL
  144.     (define-key map "\r" 'te-more-break-advance-one-line)
  145.  
  146.     (setq terminal-more-break-map map)))
  147.   
  148. (defvar te-width)
  149. (defvar te-height)
  150. (defvar te-process)
  151. (defvar te-pending-output)
  152. (defvar te-saved-point)
  153. (defvar te-pending-output-info)
  154. (defvar te-log-buffer)
  155. (defvar te-more-count)
  156. (defvar te-redisplay-count)
  157.  
  158.  
  159.  
  160. ;;;;  escape map
  161.  
  162. (defun te-escape-p (event)
  163.   (cond ((eventp terminal-escape-char)
  164.          (cond ((key-press-event-p event)
  165.                 (and (key-press-event-p terminal-escape-char)
  166.                      (= (event-modifier-bits event)
  167.                         (event-modifier-bits terminal-escape-char))
  168.                      (eq (event-key event)
  169.                          (event-key terminal-escape-char))))
  170.                ((button-press-event-p event)
  171.                 (and (button-press-event-p terminal-escape-char)
  172.                      (= (event-modifier-bits event)
  173.                         (event-modifier-bits terminal-escape-char))
  174.                      (eq (event-button event)
  175.                          (event-button terminal-escape-char))))
  176.                (t nil)))
  177.         ((numberp terminal-escape-char)
  178.          (let ((c (event-to-character event nil t nil)))
  179.            (and c (= c terminal-escape-char))))
  180.         (t
  181.          nil)))
  182.  
  183.  
  184. (defun te-escape ()
  185.   (interactive)
  186.   (let ((c (let ((cursor-in-echo-area t)
  187.                  (prompt (if prefix-arg
  188.                              (format "Emacs Terminal escape> %d "
  189.                                      (prefix-numeric-value prefix-arg))
  190.                              "Emacs Terminal escape> ")))
  191.              (message "%s" prompt)
  192.              (let ((e (next-command-event)))
  193.                (while (button-release-event-p e)
  194.                  (setq e (next-command-event e)))
  195.                (if (te-escape-p e)
  196.                    e
  197.                    (progn
  198.                      (setq unread-command-event e)
  199.                      (lookup-key terminal-escape-map
  200.                                  (read-key-sequence prompt))))))))
  201.     (cond ((eventp c)
  202.            (message nil)
  203.            (copy-event c last-command-event)
  204.            (let ((terminal-escape-char -259))
  205.              (te-pass-through)))
  206.           (c
  207.            (call-interactively c)))))
  208.  
  209. (defun te-escape-help ()
  210.   "Provide help on commands available after terminal-escape-char is typed."
  211.   (interactive)
  212.   (message "Terminal emulator escape help...")
  213.   (let ((char (single-key-description terminal-escape-char)))
  214.     (with-electric-help
  215.       (function (lambda ()
  216.      (princ (format "Terminal-emulator escape, invoked by \"%s\"
  217. Type \"%s\" twice to send a single \"%s\" through.
  218.  
  219. Other chars following \"%s\" are interpreted as follows:\n"
  220.             char char char char))
  221.  
  222.      (princ (substitute-command-keys "\\{terminal-escape-map}\n"))
  223.      (princ (format "\nSubcommands of \"%s\" (%s)\n"
  224.             (where-is-internal 'te-escape-extended-command
  225.                        terminal-escape-map t)
  226.             'te-escape-extended-command))
  227.      (let ((l (if (fboundp 'sortcar)
  228.               (sortcar (copy-sequence te-escape-command-alist)
  229.                    'string<)
  230.               (sort (copy-sequence te-escape-command-alist)
  231.                 (function (lambda (a b)
  232.                               (string< (car a) (car b))))))))
  233.        (while l
  234.          (let ((doc (or (documentation (cdr (car l)))
  235.                 "Not documented")))
  236.            (if (string-match "\n" doc)
  237.            ;; just use first line of documentation
  238.            (setq doc (substring doc 0 (match-beginning 0))))
  239.            (princ "  \"")
  240.            (princ (car (car l)))
  241.            (princ "\":\n     ")
  242.            (princ doc)
  243.            (write-char ?\n))
  244.          (setq l (cdr l))))
  245.      nil)))))
  246.  
  247.             
  248.  
  249. (defun te-escape-extended-command ()
  250.   (interactive)
  251.   (let ((c (let ((completion-ignore-case t))
  252.          (completing-read "terminal command: "
  253.                   te-escape-command-alist
  254.                   nil t))))
  255.     (if c
  256.     (catch 'foo
  257.       (setq c (downcase c))
  258.       (let ((l te-escape-command-alist))
  259.         (while l
  260.           (if (string= c (downcase (car (car l))))
  261.           (throw 'foo (call-interactively (cdr (car l))))
  262.         (setq l (cdr l)))))))))
  263.  
  264. ;; not used.
  265. (defun te-escape-extended-command-unread ()
  266.   (interactive)
  267.   (setq unread-command-event last-command-event)
  268.   (te-escape-extended-command))
  269.  
  270. (defun te-set-escape-char (c)
  271.   "Change the terminal-emulator escape character."
  272.   (interactive (list (let ((cursor-in-echo-area t))
  273.                        (message "Set escape character to: ")
  274.                        (let ((e (next-command-event)))
  275.                          (while (button-release-event-p e)
  276.                            (setq e (next-command-event e)))
  277.                          e))))
  278.   (cond ((te-escape-p c)
  279.          (message "\"%s\" is escape char"))
  280.         ((and (eventp terminal-escape-char)
  281.               (event-to-character terminal-escape-char nil t nil))
  282.          (message "\"%s\" is now escape; \"%s\" passes though"
  283.                   (single-key-description c)
  284.                   (single-key-description terminal-escape-char)))
  285.         (t
  286.          (message "\"%s\" is now escape"
  287.                   (single-key-description c))
  288.          ;; Let mouse-events, for example, go back to looking at global map
  289.          (local-unset-key (vector terminal-escape-char))))
  290.   (local-set-key (vector c) 'te-escape) ;ensure it's defined
  291.   (setq terminal-escape-char c))
  292.  
  293.  
  294. (defun te-stuff-string (string)
  295.   "Read a string to send to through the terminal emulator
  296. as though that string had been typed on the keyboard.
  297.  
  298. Very poor man's file transfer protocol."
  299.   (interactive "sStuff string: ")
  300.   (process-send-string te-process string))
  301.  
  302. (defun te-set-output-log (name)
  303.   "Record output from the terminal emulator in a buffer."
  304.   (interactive (list (if te-log-buffer
  305.              nil
  306.                (read-buffer "Record output in buffer: "
  307.                     (format "%s output-log"
  308.                         (buffer-name (current-buffer)))
  309.                     nil))))
  310.   (if (or (null name) (equal name ""))
  311.       (progn (setq te-log-buffer nil)
  312.          (message "Output logging off."))
  313.     (if (get-buffer name)
  314.     nil
  315.       (save-excursion
  316.     (set-buffer (get-buffer-create name))
  317.     (fundamental-mode)
  318.     (buffer-disable-undo (current-buffer))
  319.     (erase-buffer)))
  320.     (setq te-log-buffer (get-buffer name))
  321.     (message "Recording terminal emulator output into buffer \"%s\""
  322.          (buffer-name te-log-buffer))))
  323.  
  324. (defun te-tofu ()
  325.   "Discontinue output log."
  326.   (interactive)
  327.   (te-set-output-log nil))
  328.   
  329.  
  330. (defun te-toggle (sym arg)
  331.   (set sym (cond ((not (numberp arg)) arg)
  332.          ((= arg 1) (not (symbol-value sym)))
  333.          ((< arg 0) nil)
  334.          (t t))))
  335.  
  336. (defun te-toggle-more-processing (arg)
  337.   (interactive "p")
  338.   (message (if (te-toggle 'terminal-more-processing arg)
  339.            "More processing on" "More processing off"))
  340.   (if terminal-more-processing (setq te-more-count -1)))
  341.  
  342. (defun te-toggle-scrolling (arg)
  343.   (interactive "p")
  344.   (message (if (te-toggle 'terminal-scrolling arg)
  345.            "Scroll at end of page" "Wrap at end of page")))
  346.  
  347. (defun te-enable-more-processing ()
  348.   "Enable ** MORE ** processing"
  349.   (interactive)
  350.   (te-toggle-more-processing t))
  351.  
  352. (defun te-disable-more-processing ()
  353.   "Disable ** MORE ** processing"
  354.   (interactive)
  355.   (te-toggle-more-processing nil))
  356.  
  357. (defun te-do-scrolling ()
  358.   "Scroll at end of page (yuck)"
  359.   (interactive)
  360.   (te-toggle-scrolling t))
  361.  
  362. (defun te-do-wrapping ()
  363.   "Wrap to top of window at end of page"
  364.   (interactive)
  365.   (te-toggle-scrolling nil))
  366.  
  367.  
  368. (defun te-set-redisplay-interval (arg)
  369.   "Set the maximum interval (in output characters) between screen updates.
  370. Set this number to large value for greater throughput,
  371. set it smaller for more frequent updates (but overall slower performance."
  372.   (interactive "NMax number of output chars between redisplay updates: ")
  373.   (setq arg (max arg 1))
  374.   (setq terminal-redisplay-interval arg
  375.     te-redisplay-count 0))
  376.  
  377. ;;;; more map
  378.  
  379. ;; every command -must- call te-more-break-unwind
  380. ;; or grave lossage will result
  381.  
  382. (put 'te-more-break-unread 'suppress-keymap t)
  383. (defun te-more-break-unread ()
  384.   (interactive)
  385.   (if (te-escape-p last-command-event)
  386.       (call-interactively 'te-escape)
  387.     (message "Continuing from more break (\"%s\" typed, %d chars output pending...)"
  388.          (single-key-description last-command-event)
  389.          (te-pending-output-length))
  390.     (setq te-more-count 259259)
  391.     (te-more-break-unwind)
  392.     (let ((terminal-more-processing nil))
  393.       (te-pass-through))))
  394.  
  395. (defun te-more-break-resume ()
  396.   "Proceed past the **MORE** break,
  397. allowing the next page of output to appear"
  398.   (interactive)
  399.   (message "Continuing from more break")
  400.   (te-more-break-unwind))
  401.  
  402. (defun te-more-break-help ()
  403.   "Provide help on commands available in a terminal-emulator **MORE** break"
  404.   (interactive)
  405.   (message "Terminal-emulator more break help...")
  406.   (sit-for 0)
  407.   (with-electric-help
  408.     (function (lambda ()
  409.       (princ "Terminal-emulator more break.\n\n")
  410.       (princ (format "Type \"%s\" (te-more-break-resume)\n%s\n"
  411.              (where-is-internal 'te-more-break-resume
  412.                     terminal-more-break-map t)
  413.              (documentation 'te-more-break-resume)))
  414.       (princ (substitute-command-keys "\\{terminal-more-break-map}\n"))
  415.       (princ "Any other key is passed through to the program
  416. running under the terminal emulator and disables more processing until
  417. all pending output has been dealt with.")
  418.       nil))))
  419.  
  420.  
  421. (defun te-more-break-advance-one-line ()
  422.   "Allow one more line of text to be output before doing another more break."
  423.   (interactive)
  424.   (setq te-more-count 1)
  425.   (te-more-break-unwind))
  426.  
  427. (defun te-more-break-flush-pending-output ()
  428.   "Discard any output which has been received by the terminal emulator but
  429. not yet proceesed and then proceed from the more break."
  430.   (interactive)
  431.   (te-more-break-unwind)
  432.   (te-flush-pending-output))
  433.  
  434. (defun te-flush-pending-output ()
  435.   "Discard any as-yet-unprocessed output which has been received by
  436. the terminal emulator."
  437.   (interactive)
  438.   ;; this could conceivably be confusing in the presence of
  439.   ;; escape-sequences spanning process-output chunks
  440.   (if (null (cdr te-pending-output))
  441.       (message "(There is no output pending)")
  442.     (let ((length (te-pending-output-length)))
  443.       (message "Flushing %d chars of pending output" length)
  444.       (setq te-pending-output
  445.         (list 0 (format "\n*** %d chars of pending output flushed ***\n"
  446.                 length)))
  447.       (te-update-pending-output-display)
  448.       (te-process-output nil)
  449.       (sit-for 0))))
  450.  
  451.  
  452. (defun te-pass-through ()
  453.   "Send the last character typed through the terminal-emulator
  454. without any interpretation"
  455.   (interactive)
  456.   (if (te-escape-p last-command-event)
  457.       (call-interactively 'te-escape)
  458.     (and terminal-more-processing
  459.      (null (cdr te-pending-output))
  460.      (te-set-more-count nil))
  461.     (let ((c (event-to-character last-command-event nil t nil)))
  462.       (if c (process-send-string te-process (make-string 1 c))))
  463.     (te-process-output t)))
  464.  
  465. (defun te-set-window-start ()
  466.   (let* ((w (get-buffer-window (current-buffer)))
  467.      (h (if w (window-height w))))
  468.     (cond ((not w)) ; buffer not displayed
  469.       ((>= h (/ (- (point) (point-min)) (1+ te-width)))
  470.        ;; this is the normal case
  471.        (set-window-start w (point-min)))
  472.       ;; this happens if some vandal shrinks our window.
  473.       ((>= h (/ (- (point-max) (point)) (1+ te-width)))
  474.        (set-window-start w (- (point-max) (* h (1+ te-width)) -1)))
  475.       ;; I give up.
  476.       (t nil))))
  477.  
  478. (defun te-pending-output-length ()
  479.   (let ((length (car te-pending-output))
  480.     (tem (cdr te-pending-output)))
  481.     (while tem
  482.       (setq length (+ length (length (car tem))) tem (cdr tem)))
  483.     length))
  484.  
  485. ;;;; more break hair
  486.  
  487. (defun te-more-break ()
  488.   (te-set-more-count t)
  489.   (make-local-variable 'te-more-old-point)
  490.   (setq te-more-old-point (point))
  491.   (make-local-variable 'te-more-old-local-map)
  492.   (setq te-more-old-local-map (current-local-map))
  493.   (use-local-map terminal-more-break-map)
  494.   (make-local-variable 'te-more-old-filter)
  495.   (setq te-more-old-filter (process-filter te-process))
  496.   (make-local-variable 'te-more-old-mode-line-format)
  497.   (setq te-more-old-mode-line-format mode-line-format
  498.     mode-line-format (list "--   **MORE**  "
  499.                    mode-line-buffer-identification
  500.                    "%-"))
  501.   (set-process-filter te-process
  502.     (function (lambda (process string)
  503.         (save-excursion
  504.           (set-buffer (process-buffer process))
  505.           (setq te-pending-output (nconc te-pending-output
  506.                          (list string))))
  507.           (te-update-pending-output-display))))
  508.   (te-update-pending-output-display)
  509.   (if (eq (window-buffer (selected-window)) (current-buffer))
  510.       (message "More break "))
  511.   (or (eobp)
  512.       (null terminal-more-break-insertion)
  513.       (save-excursion
  514.     (forward-char 1)
  515.     (delete-region (point) (+ (point) te-width))
  516.     (insert terminal-more-break-insertion)))
  517.   (run-hooks 'terminal-more-break-hook)
  518.   (sit-for 0) ;get display to update
  519.   (throw 'te-process-output t))
  520.  
  521. (defun te-more-break-unwind ()
  522.   (use-local-map te-more-old-local-map)
  523.   (set-process-filter te-process te-more-old-filter)
  524.   (goto-char te-more-old-point)
  525.   (setq mode-line-format te-more-old-mode-line-format)
  526.   (set-buffer-modified-p (buffer-modified-p))
  527.   (let ((buffer-read-only nil))
  528.     (cond ((eobp))
  529.       (terminal-more-break-insertion
  530.        (forward-char 1)
  531.        (delete-region (point)
  532.               (+ (point) (length terminal-more-break-insertion)))
  533.        (insert-char ?\  te-width)
  534.        (goto-char te-more-old-point)))
  535.     (setq te-more-old-point nil)
  536.     (let ((te-more-count 259259))
  537.       (te-newline)))
  538.   ;(sit-for 0)
  539.   (te-process-output t))
  540.  
  541. (defun te-set-more-count (newline)
  542.   (let ((line (/ (- (point) (point-min)) (1+ te-width))))
  543.     (if newline (setq line (1+ line)))
  544.     (cond ((= line te-height)
  545.        (setq te-more-count te-height))
  546.       ;#### something is strange.  Investigate this!
  547.       ((= line (1- te-height))
  548.        (setq te-more-count te-height))
  549.       ((or (< line (/ te-height 2))
  550.            (> (- te-height line) 10))
  551.        ;; break at end of this page
  552.        (setq te-more-count (- te-height line)))
  553.       (t
  554.        ;; migrate back towards top (ie bottom) of screen.
  555.        (setq te-more-count (- te-height
  556.                   (if (> te-height 10) 2 1)))))))
  557.  
  558.  
  559. ;;;; More or less straight-forward terminal escapes
  560.  
  561. ;; ^j, meaning `newline' to non-display programs.
  562. ;; (Who would think of ever writing a system which doesn't understand
  563. ;;  display terminals natively?  Un*x:  The Operating System of the Future.)
  564. (defun te-newline ()
  565.   "Move down a line, optionally do more processing, perhaps wrap/scroll,
  566. move to start of new line, clear to end of line."
  567.   (end-of-line)
  568.   (cond ((not terminal-more-processing))
  569.     ((< (setq te-more-count (1- te-more-count)) 0)
  570.      (te-set-more-count t))
  571.     ((eq te-more-count 0)
  572.      ;; this doesn't return
  573.      (te-more-break)))
  574.   (if (eobp)
  575.       (progn
  576.     (delete-region (point-min) (+ (point-min) te-width))
  577.     (goto-char (point-min))
  578.     (if terminal-scrolling
  579.         (progn (delete-char 1)
  580.            (goto-char (point-max))
  581.            (insert ?\n))))
  582.     (forward-char 1)
  583.     (delete-region (point) (+ (point) te-width)))
  584.   (insert-char ?\  te-width)
  585.   (beginning-of-line)
  586.   (te-set-window-start))
  587.  
  588. ;; ^p ^j
  589. ;; Handle the `do' or `nl' termcap capability.
  590. ;;#### I am not sure why this broken, obsolete, capability is here.
  591. ;;#### Perhaps it is for VIle.  No comment was made about why it
  592. ;;#### was added (in "Sun Dec  6 01:22:27 1987  Richard Stallman")
  593. (defun te-down-vertically-or-scroll ()
  594.   "Move down a line vertically, or scroll at bottom."
  595.   (let ((column (current-column)))
  596.     (end-of-line)
  597.     (if (eobp)
  598.     (progn
  599.       (delete-region (point-min) (+ (point-min) te-width))
  600.       (goto-char (point-min))
  601.       (delete-char 1)
  602.       (goto-char (point-max))
  603.       (insert ?\n)
  604.       (insert-char ?\  te-width)
  605.       (beginning-of-line))
  606.       (forward-line 1))
  607.     (move-to-column column))
  608.   (te-set-window-start))
  609.  
  610. ; ^p = x+32 y+32
  611. (defun te-move-to-position ()
  612.   ;; must offset by #o40 since cretinous unix won't send a 004 char through
  613.   (let ((y (- (te-get-char) 32))
  614.     (x (- (te-get-char) 32)))
  615.     (if (or (> x te-width)
  616.         (> y te-height))
  617.     () ;(error "fucked %d %d" x y)
  618.       (goto-char (+ (point-min) x (* y (1+ te-width))))
  619.       ;(te-set-window-start?)
  620.       ))
  621.   (setq te-more-count -1))
  622.  
  623.  
  624.  
  625. ;; ^p c
  626. (defun te-clear-rest-of-line ()
  627.   (save-excursion
  628.     (let ((n (- (point) (progn (end-of-line) (point)))))
  629.       (delete-region (point) (+ (point) n))
  630.       (insert-char ?\  (- n)))))
  631.  
  632.  
  633. ;; ^p C
  634. (defun te-clear-rest-of-screen ()
  635.   (save-excursion
  636.     (te-clear-rest-of-line)
  637.     (while (progn (end-of-line) (not (eobp)))
  638.       (forward-char 1) (end-of-line)
  639.       (delete-region (- (point) te-width) (point))
  640.       (insert-char ?\  te-width))))
  641.       
  642.  
  643. ;; ^p ^l
  644. (defun te-clear-screen ()
  645.   ;; regenerate buffer to compensate for (nonexistent!!) bugs.
  646.   (erase-buffer)
  647.   (let ((i 0))
  648.     (while (< i te-height)
  649.       (setq i (1+ i))
  650.       (insert-char ?\  te-width)
  651.       (insert ?\n)))
  652.   (delete-region (1- (point-max)) (point-max))
  653.   (goto-char (point-min))
  654.   (setq te-more-count -1))
  655.  
  656.  
  657. ;; ^p ^o count+32
  658. (defun te-insert-lines ()
  659.   (if (not (bolp))
  660.       ();(error "fooI")
  661.     (save-excursion
  662.       (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
  663.          (n (min (- (te-get-char) ?\ ) line))
  664.          (i 0))
  665.     (delete-region (- (point-max) (* n (1+ te-width))) (point-max))
  666.     (if (eq (point) (point-max)) (insert ?\n))
  667.     (while (< i n)
  668.       (setq i (1+ i))
  669.       (insert-char ?\  te-width)
  670.       (or (eq i line) (insert ?\n))))))
  671.   (setq te-more-count -1))
  672.  
  673.  
  674. ;; ^p ^k count+32
  675. (defun te-delete-lines ()
  676.   (if (not (bolp))
  677.       ();(error "fooD")
  678.     (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
  679.        (n (min (- (te-get-char) ?\ ) line))
  680.        (i 0))
  681.       (delete-region (point)
  682.              (min (+ (point) (* n (1+ te-width))) (point-max)))
  683.       (save-excursion
  684.     (goto-char (point-max))
  685.     (while (< i n)
  686.       (setq i (1+ i))
  687.       (insert-char ?\  te-width)
  688.       (or (eq i line) (insert ?\n))))))
  689.   (setq te-more-count -1))
  690.  
  691. ;; ^p ^a
  692. (defun te-beginning-of-line ()
  693.   (beginning-of-line))
  694.  
  695. ;; ^p ^b
  696. (defun te-backward-char ()
  697.   (if (not (bolp))
  698.       (backward-char 1)))
  699.  
  700. ;; ^p ^f
  701. (defun te-forward-char ()
  702.   (if (not (eolp))
  703.       (forward-char 1)))
  704.  
  705.  
  706. ;; 0177
  707. (defun te-delete ()
  708.   (if (bolp)
  709.       ()
  710.     (delete-region (1- (point)) (point))
  711.     (insert ?\ )
  712.     (forward-char -1)))
  713.  
  714. ;; ^p ^g
  715. (defun te-beep ()
  716.   (beep))
  717.  
  718.  
  719. ;; ^p _ count+32
  720. (defun te-insert-spaces ()
  721.   (let* ((p (point))
  722.      (n (min (- (te-get-char) 32)
  723.          (- (progn (end-of-line) (point)) p))))
  724.     (if (<= n 0)
  725.     nil
  726.       (delete-char (- n))
  727.       (goto-char p)
  728.       (insert-char ?\  n))
  729.     (goto-char p)))
  730.  
  731. ;; ^p d count+32  (should be ^p ^d but cretinous un*x won't send ^d chars!!!)
  732. (defun te-delete-char ()
  733.   (let* ((p (point))
  734.      (n (min (- (te-get-char) 32)
  735.          (- (progn (end-of-line) (point)) p))))
  736.     (if (<= n 0)
  737.     nil
  738.       (insert-char ?\  n)
  739.       (goto-char p)
  740.       (delete-char n))
  741.     (goto-char p)))
  742.  
  743.  
  744.  
  745. ;; disgusting unix-required shit
  746. ;;  Are we living twenty years in the past yet?
  747.  
  748. (defun te-losing-unix ()
  749.   ;(what lossage)
  750.   ;(message "fucking-unix: %d" char)
  751.   )
  752.  
  753. ;; ^i
  754. (defun te-output-tab ()
  755.   (let* ((p (point))
  756.      (x (- p (progn (beginning-of-line) (point))))
  757.      (l (min (- 8 (logand x 7))
  758.          (progn (end-of-line) (- (point) p)))))
  759.     (goto-char (+ p l))))
  760.  
  761. ;; Also:
  762. ;;  ^m => beginning-of-line (for which it -should- be using ^p ^a, right?!!)
  763. ;;  ^g => te-beep (for which it should use ^p ^g)
  764. ;;  ^h => te-backward-char (for which it should use ^p ^b)
  765.  
  766.  
  767.  
  768. (defun te-filter (process string)
  769.   (let* ((obuf (current-buffer)))
  770.     ;; can't use save-excursion, as that preserves point, which we don't want
  771.     (unwind-protect
  772.     (progn
  773.       (set-buffer (process-buffer process))
  774.       (goto-char te-saved-point)
  775.       (and (bufferp te-log-buffer)
  776.            (if (null (buffer-name te-log-buffer))
  777.            ;; killed
  778.            (setq te-log-buffer nil)
  779.          (set-buffer te-log-buffer)
  780.          (goto-char (point-max))
  781.          (insert-before-markers string)
  782.          (set-buffer (process-buffer process))))
  783.       (setq te-pending-output (nconc te-pending-output (list string)))
  784.       (te-update-pending-output-display)
  785.           (te-process-output (eq (current-buffer)
  786.                                  (window-buffer (selected-window))))
  787.       (set-buffer (process-buffer process))
  788.       (setq te-saved-point (point)))
  789.       (set-buffer obuf))))
  790.  
  791. ;; fucking unix has -such- braindamaged lack of tty control...
  792. (defun te-process-output (preemptable)
  793.   ;;#### There seems no good reason to ever disallow preemption
  794.   (setq preemptable t)
  795.   (catch 'te-process-output
  796.     (let ((buffer-read-only nil)
  797.       (string nil) ostring start char (matchpos nil))
  798.       (while (cdr te-pending-output)
  799.     (setq ostring string
  800.           start (car te-pending-output)
  801.           string (car (cdr te-pending-output))
  802.           char (aref string start))
  803.     (if (eq (setq start (1+ start)) (length string))
  804.         (progn (setq te-pending-output
  805.                (cons 0 (cdr (cdr te-pending-output)))
  806.              start 0
  807.              string (car (cdr te-pending-output)))
  808.            (te-update-pending-output-display))
  809.         (setcar te-pending-output start))
  810.     (if (and (> char ?\037) (< char ?\377))
  811.         (cond ((eolp)
  812.            ;; unread char
  813.            (if (eq start 0)
  814.                (setq te-pending-output
  815.                  (cons 0 (cons (make-string 1 char)
  816.                        (cdr te-pending-output))))
  817.                (setcar te-pending-output (1- start)))
  818.            (te-newline))
  819.           ((null string)
  820.            (delete-char 1) (insert char)
  821.            (te-redisplay-if-necessary 1))
  822.           (t
  823.            (let ((end (or (and (eq ostring string) matchpos)
  824.                   (setq matchpos (string-match
  825.                            "[\000-\037\177-\377]"
  826.                            string start))
  827.                   (length string))))
  828.              (delete-char 1) (insert char)
  829.              (setq char (point)) (end-of-line)
  830.              (setq end (min end (+ start (- (point) char))))
  831.              (goto-char char)
  832.              (if (eq end matchpos) (setq matchpos nil))
  833.              (delete-region (point) (+ (point) (- end start)))
  834.              (insert (if (and (eq start 0)
  835.                       (eq end (length string)))
  836.                  string
  837.                      (substring string start end)))
  838.              (if (eq end (length string))
  839.              (setq te-pending-output
  840.                    (cons 0 (cdr (cdr te-pending-output))))
  841.                  (setcar te-pending-output end))
  842.              (te-redisplay-if-necessary (1+ (- end start))))))
  843.       ;; I suppose if I split the guts of this out into a separate
  844.       ;;  function we could trivially emulate different terminals
  845.       ;; Who cares in any case?  (Apart from stupid losers using rlogin)
  846.       (funcall
  847.         (if (eq char ?\^p)
  848.             (or (cdr (assq (te-get-char)
  849.                    '((?= . te-move-to-position)
  850.                  (?c . te-clear-rest-of-line)
  851.                  (?C . te-clear-rest-of-screen)
  852.                  (?\C-o . te-insert-lines)
  853.                  (?\C-k . te-delete-lines)
  854.                  ;; not necessary, but help sometimes.
  855.                  (?\C-a . te-beginning-of-line)
  856.                  (?\C-b . te-backward-char)
  857.                  ;; should be C-d, but un*x
  858.                  ;;  pty's won't send \004 through!
  859.                                  ;; Can you believe this?
  860.                  (?d . te-delete-char)
  861.                  (?_ . te-insert-spaces)
  862.                  ;; random
  863.                  (?\C-f . te-forward-char)
  864.                  (?\C-g . te-beep)
  865.                  (?\C-j . te-down-vertically-or-scroll)
  866.                  (?\C-l . te-clear-screen)
  867.                  )))
  868.             'te-losing-unix)
  869.             (or (cdr (assq char
  870.                    '((?\C-j . te-newline)
  871.                  (?\177 . te-delete)
  872.                  ;; Did I ask to be sent these characters?
  873.                  ;; I don't remember doing so, either.
  874.                  ;; (Perhaps some operating system or
  875.                  ;; other is completely incompetent...)
  876.                  (?\C-m . te-beginning-of-line) ;fuck me harder
  877.                  (?\C-g . te-beep)             ;again and again!
  878.                  (?\C-h . te-backward-char)     ;wa12id!!
  879.                  (?\C-i . te-output-tab))))     ;(spiked)
  880.             'te-losing-unix)))              ;That feels better
  881.       (te-redisplay-if-necessary 1))
  882.     (and preemptable
  883.          (input-pending-p)
  884.          ;; preemptable output!  Oh my!!
  885.          (throw 'te-process-output t)))))
  886.   ;; We must update window-point in every window displaying our buffer
  887.   (let* ((s (selected-window))
  888.      (w s))
  889.     (while (not (eq s (setq w (next-window w))))
  890.       (if (eq (window-buffer w) (current-buffer))
  891.       (set-window-point w (point))))))
  892.  
  893. (defun te-get-char ()
  894.   (if (cdr te-pending-output)
  895.       (let ((start (car te-pending-output))
  896.         (string (car (cdr te-pending-output))))
  897.     (prog1 (aref string start)
  898.       (if (eq (setq start (1+ start)) (length string))
  899.           (setq te-pending-output (cons 0 (cdr (cdr te-pending-output))))
  900.           (setcar te-pending-output start))))
  901.     (catch 'char
  902.       (let ((filter (process-filter te-process)))
  903.     (unwind-protect
  904.         (progn
  905.           (set-process-filter te-process
  906.                   (function (lambda (p s)
  907.                                     (or (eq (length s) 1)
  908.                                         (setq te-pending-output (list 1 s)))
  909.                                     (throw 'char (aref s 0)))))
  910.           (accept-process-output te-process))
  911.       (set-process-filter te-process filter))))))
  912.  
  913.  
  914. (defun te-redisplay-if-necessary (length)
  915.   (and (<= (setq te-redisplay-count (- te-redisplay-count length)) 0)
  916.        (eq (current-buffer) (window-buffer (selected-window)))
  917.        (waiting-for-user-input-p)
  918.        (progn (te-update-pending-output-display)
  919.           (sit-for 0)
  920.           (setq te-redisplay-count terminal-redisplay-interval))))
  921.  
  922. (defun te-update-pending-output-display ()
  923.   (if (null (cdr te-pending-output))
  924.       (setq te-pending-output-info "")      
  925.     (let ((length (te-pending-output-length)))
  926.       (if (< length 1500)
  927.       (setq te-pending-output-info "")
  928.     (setq te-pending-output-info (format "(%dK chars output pending) "
  929.                          (/ (+ length 512) 1024))))))
  930.   ;; update mode line
  931.   (set-buffer-modified-p (buffer-modified-p)))
  932.  
  933.  
  934. (defun te-sentinel (process message)
  935.   (cond ((eq (process-status process) 'run))
  936.     ((null (buffer-name (process-buffer process)))) ;deleted
  937.     (t (let ((b (current-buffer)))
  938.          (save-excursion
  939.            (set-buffer (process-buffer process))
  940.            (setq buffer-read-only nil)
  941.            (fundamental-mode)
  942.            (goto-char (point-max))
  943.            (delete-blank-lines)
  944.            (delete-horizontal-space)
  945.            (insert "\n*******\n" message "*******\n"))
  946.          (if (and (eq b (process-buffer process))
  947.               (waiting-for-user-input-p))
  948.          (progn (goto-char (point-max))
  949.             (recenter -1)))))))
  950.  
  951. (defvar te-stty-string "stty -nl new dec echo"
  952.   "Command string (to be interpreted by \"sh\") which sets the modes
  953. of the virtual terminal to be appropriate for interactive use.")
  954.  
  955. (defvar explicit-shell-file-name nil
  956.   "*If non-nil, is file name to use for explicitly requested inferior shell.")
  957.  
  958. ;;;###autoload
  959. (defun terminal-emulator (buffer program args &optional width height)
  960.   "Under a display-terminal emulator in BUFFER, run PROGRAM on arguments ARGS.
  961. ARGS is a list of argument-strings.  Remaining arguments are WIDTH and HEIGHT.
  962. BUFFER's contents are made an image of the display generated by that program,
  963. and any input typed when BUFFER is the current Emacs buffer is sent to that
  964. program an keyboard input.
  965.  
  966. Interactively, BUFFER defaults to \"*terminal*\" and PROGRAM and ARGS
  967. are parsed from an input-string using your usual shell.
  968. WIDTH and HEIGHT are determined from the size of the current window
  969. -- WIDTH will be one less than the window's width, HEIGHT will be its height.
  970.  
  971. To switch buffers and leave the emulator, or to give commands
  972. to the emulator itself (as opposed to the program running under it),
  973. type Control-^.  The following character is an emulator command.
  974. Type Control-^ twice to send it to the subprogram.
  975. This escape character may be changed using the variable `terminal-escape-char'.
  976.  
  977. `Meta' characters may not currently be sent through the terminal emulator.
  978.  
  979. Here is a list of some of the variables which control the behaviour
  980. of the emulator -- see their documentation for more information:
  981. terminal-escape-char, terminal-scrolling, terminal-more-processing,
  982. terminal-redisplay-interval.
  983.  
  984. This function calls the value of terminal-mode-hook if that exists
  985. and is non-nil after the terminal buffer has been set up and the
  986. subprocess started.
  987.  
  988. Presently with `termcap' only; if somebody sends us code to make this
  989. work with `terminfo' we will try to use it."
  990.   (interactive
  991.     (cons (save-excursion
  992.         (set-buffer (get-buffer-create "*terminal*"))
  993.         (buffer-name (if (or (not (boundp 'te-process))
  994.                  (null te-process)
  995.                  (not (eq (process-status te-process)
  996.                       'run)))
  997.                  (current-buffer)
  998.                (generate-new-buffer "*terminal*"))))
  999.       (append
  1000.         (let* ((default-s
  1001.              ;; Default shell is same thing M-x shell uses.
  1002.              (or explicit-shell-file-name
  1003.              (getenv "ESHELL")
  1004.              (getenv "SHELL")
  1005.              "/bin/sh"))
  1006.            (s (read-shell-command
  1007.                (format "Run program in emulator: (default %s) "
  1008.                    default-s))))
  1009.           (if (equal s "")
  1010.           (list default-s '())
  1011.                   (te-parse-program-and-args s))))))
  1012.   (switch-to-buffer buffer)
  1013.   (if (null width) (setq width (- (window-width (selected-window)) 1)))
  1014.   (if (null height) (setq height (- (window-height (selected-window)) 1)))
  1015.   (terminal-mode)
  1016.   (setq te-width width te-height height)
  1017.   (setq mode-line-buffer-identification
  1018.     (list (format "Emacs terminal %dx%d: %%b  " te-width te-height)
  1019.           'te-pending-output-info))
  1020.   (let ((buffer-read-only nil))
  1021.     (te-clear-screen))
  1022.   (let (process)
  1023.     (while (setq process (get-buffer-process (current-buffer)))
  1024.       (if (y-or-n-p (format "Kill process %s? " (process-name process)))
  1025.       (delete-process process)
  1026.     (error "Process %s not killed" (process-name process)))))
  1027.   (condition-case err
  1028.       (let ((termcap
  1029.              ;; Because of Unix Brain Death(tm), we can't change
  1030.              ;;  the terminal type of a running process, and so
  1031.              ;;  terminal size and scrollability are wired-down
  1032.              ;;  at this point.  ("Detach?  What's that?")
  1033.              (concat (format "emacs-virtual:co#%d:li#%d:%s:km:"
  1034.                              ;; Sigh.  These can't be dynamically changed.
  1035.                              te-width te-height (if terminal-scrolling
  1036.                                                     "" "ns:"))
  1037.                      ;;-- Basic things
  1038.                      ;; cursor-motion, bol, forward/backward char
  1039.                      "cm=^p=%+ %+ :cr=^p^a:le=^p^b:nd=^p^f:"
  1040.                      ;; newline, clear eof/eof, audible bell
  1041.                      "nw=^j:ce=^pc:cd=^pC:cl=^p^l:bl=^p^g:"
  1042.                      ;; insert/delete char/line
  1043.                      "IC=^p_%+ :DC=^pd%+ :AL=^p^o%+ :DL=^p^k%+ :"
  1044.                      ;;-- Not-widely-known (ie nonstandard) flags, which mean
  1045.                      ;; o writing in the last column of the last line
  1046.                      ;;   doesn't cause idiotic scrolling, and
  1047.                      ;; o don't use idiotische c-s/c-q sogenannte
  1048.                      ;;   ``flow control'' auf keinen Fall.
  1049.                      "LP:NF:"
  1050.                      ;;-- For stupid or obsolete programs
  1051.                      "ic=^p_!:dc=^pd!:al=^p^o!:dl=^p^k!:ho=^p=  :"
  1052.                      ;;-- For disgusting programs.
  1053.                      ;; (VI? What losers need these, I wonder?)
  1054.                      "im=:ei=:dm=:ed=:mi:do=^p^j:nl=^p^j:bs:")))
  1055.     (if (fboundp 'start-subprocess)
  1056.         ;; this winning function would do everything, except that
  1057.         ;;  rms doesn't want it.
  1058.         (setq te-process (start-subprocess "terminal-emulator"
  1059.                    program args
  1060.                    'channel-type 'terminal
  1061.                    'filter 'te-filter
  1062.                    'buffer (current-buffer)
  1063.                    'sentinel 'te-sentinel
  1064.                    'modify-environment
  1065.                      (list (cons "TERM" "emacs-virtual")
  1066.                        (cons "TERMCAP" termcap))))
  1067.       ;; so instead we resort to this...
  1068.       (setq te-process (start-process "terminal-emulator" (current-buffer)
  1069.                  "/bin/sh" "-c"
  1070.                  ;; Yuck!!! Start a shell to set some terminal
  1071.                  ;; control characteristics.  Then start the
  1072.                  ;; "env" program to setup the terminal type
  1073.                  ;; Then finally start the program we wanted.
  1074.                  (format "%s; exec %s TERM=emacs-virtual %s %s"
  1075.                                      te-stty-string
  1076.                      (te-quote-arg-for-sh
  1077.                        (concat exec-directory "env"))
  1078.                      (te-quote-arg-for-sh
  1079.                        (concat "TERMCAP=" termcap))
  1080.                      (mapconcat 'te-quote-arg-for-sh
  1081.                         (cons program args) " "))))
  1082.       (set-process-filter te-process 'te-filter)
  1083.       (set-process-sentinel te-process 'te-sentinel)))
  1084.     (error (fundamental-mode)
  1085.        (signal (car err) (cdr err))))
  1086.   ;; sigh
  1087.   (setq inhibit-quit t)            ;sport death
  1088.   (use-local-map terminal-map)
  1089.   (run-hooks 'terminal-mode-hook)
  1090.   (message "Entering emacs terminal-emulator...  Type %s %s for help"
  1091.        (single-key-description terminal-escape-char)
  1092.        (mapconcat 'single-key-description
  1093.               (where-is-internal 'te-escape-help
  1094.                      terminal-escape-map
  1095.                      t)
  1096.               " ")))
  1097.  
  1098.  
  1099. (defun te-parse-program-and-args (s)
  1100.   (cond ((string-match "\\`\\([a-zA-Z0-9-+=_.@/:]+[ \t]*\\)+\\'" s)
  1101.      (let ((l ()) (p 0))
  1102.        (while p
  1103.          (setq l (cons (if (string-match
  1104.                 "\\([a-zA-Z0-9-+=_.@/:]+\\)\\([ \t]+\\)*"
  1105.                 s p)
  1106.                    (prog1 (substring s p (match-end 1))
  1107.                  (setq p (match-end 0))
  1108.                  (if (eq p (length s)) (setq p nil)))
  1109.                    (prog1 (substring s p)
  1110.                  (setq p nil)))
  1111.                l)))
  1112.        (setq l (nreverse l))
  1113.        (list (car l) (cdr l))))
  1114.     ((and (string-match "[ \t]" s) (not (file-exists-p s)))
  1115.      (list shell-file-name (list "-c" (concat "exec " s))))
  1116.     (t (list s ()))))
  1117.  
  1118. (put 'terminal-mode 'mode-class 'special)
  1119. ;; This is only separated out from function terminal-emulator
  1120. ;; to keep the latter a little more managable.
  1121. (defun terminal-mode ()
  1122.   "Set up variables for use f the terminal-emualtor.
  1123. One should not call this -- it is an internal function
  1124. of the terminal-emulator"
  1125.   (kill-all-local-variables)
  1126.   (buffer-disable-undo (current-buffer))
  1127.   (setq major-mode 'terminal-mode)
  1128.   (setq mode-name "terminal")
  1129. ; (make-local-variable 'Helper-return-blurb)
  1130. ; (setq Helper-return-blurb "return to terminal simulator")
  1131.   (setq mode-line-process '(": %s"))
  1132.   (setq buffer-read-only t)
  1133.   (setq truncate-lines t)
  1134.   (make-local-variable 'terminal-escape-char)
  1135.   (setq terminal-escape-char (default-value 'terminal-escape-char))
  1136.   (make-local-variable 'terminal-scrolling)
  1137.   (setq terminal-scrolling (default-value 'terminal-scrolling))
  1138.   (make-local-variable 'terminal-more-processing)
  1139.   (setq terminal-more-processing (default-value 'terminal-more-processing))
  1140.   (make-local-variable 'terminal-redisplay-interval)
  1141.   (setq terminal-redisplay-interval (default-value 'terminal-redisplay-interval))
  1142.   (make-local-variable 'te-width)
  1143.   (make-local-variable 'te-height)
  1144.   (make-local-variable 'te-process)
  1145.   (make-local-variable 'te-pending-output)
  1146.   (setq te-pending-output (list 0))
  1147.   (make-local-variable 'te-saved-point)
  1148.   (setq te-saved-point (point-min))
  1149.   (make-local-variable 'te-pending-output-info) ;for the mode line
  1150.   (setq te-pending-output-info "")
  1151.   (make-local-variable 'inhibit-quit)
  1152.   ;(setq inhibit-quit t)
  1153.   (make-local-variable 'te-log-buffer)
  1154.   (setq te-log-buffer nil)
  1155.   (make-local-variable 'te-more-count)
  1156.   (setq te-more-count -1)
  1157.   (make-local-variable 'te-redisplay-count)
  1158.   (setq te-redisplay-count terminal-redisplay-interval)
  1159.   ;(use-local-map terminal-mode-map)
  1160.   ;; terminal-mode-hook is called above in function terminal-emulator
  1161.   (make-local-variable 'meta-prefix-char)
  1162.   (setq meta-prefix-char -1)            ;death to ASCII lossage
  1163.   )
  1164.  
  1165. ;;;; what a complete loss
  1166.  
  1167. (defun te-quote-arg-for-sh (fuckme)
  1168.   (cond ((string-match "\\`[a-zA-Z0-9-+=_.@/:]+\\'"
  1169.                fuckme)
  1170.      fuckme)
  1171.     ((not (string-match "[$]" fuckme))
  1172.      ;; "[\"\\]" are special to sh and the lisp reader in the same way
  1173.      (prin1-to-string fuckme))
  1174.     (t
  1175.      (let ((harder "")
  1176.            (cretin 0)
  1177.            (stupid 0))
  1178.        (while (cond ((>= cretin (length fuckme))
  1179.              nil)
  1180.             ;; this is the set of chars magic with "..." in `sh'
  1181.             ((setq stupid (string-match "[\"\\$]"
  1182.                             fuckme cretin))
  1183.              t)
  1184.             (t (setq harder (concat harder
  1185.                         (substring fuckme cretin)))
  1186.                nil))
  1187.          (setq harder (concat harder (substring fuckme cretin stupid)
  1188.                                   ;; Can't use ?\\ since `concat'
  1189.                                   ;; unfortunately does prin1-to-string
  1190.                                   ;; on fixna.  Amazing.
  1191.                   "\\"
  1192.                   (substring fuckme
  1193.                          stupid
  1194.                          (1+ stupid)))
  1195.            cretin (1+ stupid)))
  1196.        (concat "\"" harder "\"")))))
  1197.