terminal.el 45 KB

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