home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / terminfo.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  45.9 KB  |  1,296 lines

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