terminal.el 45 KB

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