terminal.el 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. ;; Terminal emulator for GNU Emacs.
  2. ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. ;;>>TODO
  18. ;;>> terminfo?
  19. ;;>> ** Nothing can be done about emacs' meta-lossage **
  20. ;;>> (without redoing keymaps `sanely' -- ask mly for details)
  21. ;;>> One probably wants to do setenv MORE -c when running with
  22. ;;>> more-processing enabled.
  23. (provide 'terminal)
  24. (require 'ehelp)
  25. (defvar terminal-escape-char ?\C-^
  26. "*All characters except for this are passed verbatim through the
  27. terminal-emulator. This character acts as a prefix for commands
  28. to the emulator program itself. Type this character twice to send
  29. it through the emulator. Type ? after typing it for a list of
  30. possible commands.
  31. This variable is local to each terminal-emulator buffer.")
  32. (defvar terminal-scrolling nil
  33. "*If non-nil, the terminal-emulator will `scroll' when output occurs
  34. past the bottom of the screen. If nil, output will `wrap' to the top
  35. of the screen.
  36. This variable is local to each terminal-emulator buffer.")
  37. (defvar terminal-more-processing t
  38. "*If non-nil, do more-processing.
  39. This variable is local to each terminal-emulator buffer.")
  40. ;; If you are the sort of loser who uses scrolling without more breaks
  41. ;; and expects to actually see anything, you should probably set this to
  42. ;; around 400
  43. (defvar terminal-redisplay-interval 5000
  44. "*Maximum number of characters which will be processed by the
  45. terminal-emulator before a screen redisplay is forced.
  46. Set this to a large value for greater throughput,
  47. set it smaller for more frequent updates but overall slower
  48. performance.")
  49. (defvar terminal-more-break-insertion
  50. "*** More break -- Press space to continue ***")
  51. (defvar terminal-escape-map nil)
  52. (defvar terminal-map nil)
  53. (defvar terminal-more-break-map nil)
  54. (if terminal-map
  55. nil
  56. (let ((map (make-keymap)))
  57. (fillarray map 'te-pass-through)
  58. ;(define-key map "\C-l"
  59. ; '(lambda () (interactive) (te-pass-through) (redraw-display)))
  60. (setq terminal-map map)))
  61. ;(setq terminal-escape-map nil)
  62. (if terminal-escape-map
  63. nil
  64. (let ((map (make-keymap)))
  65. ;(fillarray map 'te-escape-extended-command-unread)
  66. (fillarray map 'undefined)
  67. (let ((s "0"))
  68. (while (<= (aref s 0) ?9)
  69. (define-key map s 'digit-argument)
  70. (aset s 0 (1+ (aref s 0)))))
  71. (define-key map "b" 'switch-to-buffer)
  72. (define-key map "o" 'other-window)
  73. (define-key map "e" 'te-set-escape-char)
  74. (define-key map "\C-l" 'redraw-display)
  75. (define-key map "\C-o" 'te-flush-pending-output)
  76. (define-key map "m" 'te-toggle-more-processing)
  77. (define-key map "x" 'te-escape-extended-command)
  78. (define-key map "?" 'te-escape-help)
  79. (define-key map (char-to-string help-char) 'te-escape-help)
  80. (setq terminal-escape-map map)))
  81. (defvar te-escape-command-alist ())
  82. ;(setq te-escape-command-alist ())
  83. (if te-escape-command-alist
  84. nil
  85. (setq te-escape-command-alist
  86. '(("Set Escape Character" . te-set-escape-char)
  87. ("Refresh" . redraw-display)
  88. ("Record Output" . te-set-output-log)
  89. ("Photo" . te-set-output-log)
  90. ("Tofu" . te-tofu) ;; confuse the uninitiated
  91. ("Stuff Input" . te-stuff-string)
  92. ("Flush Pending Output" . te-flush-pending-output)
  93. ("Enable More Processing" . te-enable-more-processing)
  94. ("Disable More Processing" . te-disable-more-processing)
  95. ("Scroll at end of page" . te-do-scrolling)
  96. ("Wrap at end of page" . te-do-wrapping)
  97. ("Switch To Buffer" . switch-to-buffer)
  98. ("Other Window" . other-window)
  99. ("Kill Buffer" . kill-buffer)
  100. ("Help" . te-escape-help)
  101. ("Set Redisplay Interval" . te-set-redisplay-interval)
  102. )))
  103. ;(setq terminal-more-break-map nil)
  104. (if terminal-more-break-map
  105. nil
  106. (let ((map (make-keymap)))
  107. (fillarray map 'te-more-break-unread)
  108. (define-key map (char-to-string help-char) 'te-more-break-help)
  109. (define-key map " " 'te-more-break-resume)
  110. (define-key map "\C-l" 'redraw-display)
  111. (define-key map "\C-o" 'te-more-break-flush-pending-output)
  112. ;;>>> this isn't right
  113. ;(define-key map "\^?" 'te-more-break-flush-pending-output) ;DEL
  114. (define-key map "\r" 'te-more-break-advance-one-line)
  115. (setq terminal-more-break-map map)))
  116. ;;;; escape map
  117. (defun te-escape ()
  118. (interactive)
  119. (let (s
  120. (local (current-local-map))
  121. (global (current-global-map)))
  122. (unwind-protect
  123. (progn
  124. (use-global-map terminal-escape-map)
  125. (use-local-map terminal-escape-map)
  126. (setq s (read-key-sequence
  127. (if prefix-arg
  128. (format "Emacs Terminal escape> %d "
  129. (prefix-numeric-value prefix-arg))
  130. "Emacs Terminal escape> "))))
  131. (use-global-map global)
  132. (use-local-map local))
  133. (message "")
  134. (cond ((string= s (make-string 1 terminal-escape-char))
  135. (setq last-command-char terminal-escape-char)
  136. (let ((terminal-escape-char -259))
  137. (te-pass-through)))
  138. ((setq s (lookup-key terminal-escape-map s))
  139. (call-interactively s)))))
  140. (defun te-escape-help ()
  141. "Provide help on commands available after terminal-escape-char is typed."
  142. (interactive)
  143. (message "Terminal emulator escape help...")
  144. (let ((char (single-key-description terminal-escape-char)))
  145. (with-electric-help
  146. (function (lambda ()
  147. (princ (format "Terminal-emulator escape, invoked by \"%s\"
  148. Type \"%s\" twice to send a single \"%s\" through.
  149. Other chars following \"%s\" are interpreted as follows:\n"
  150. char char char char))
  151. (princ (substitute-command-keys "\\{terminal-escape-map}\n"))
  152. (princ (format "\nSubcommands of \"%s\" (%s)\n"
  153. (where-is-internal 'te-escape-extended-command
  154. terminal-escape-map t)
  155. 'te-escape-extended-command))
  156. (let ((l (if (fboundp 'sortcar)
  157. (sortcar (copy-sequence te-escape-command-alist)
  158. 'string<)
  159. (sort (copy-sequence te-escape-command-alist)
  160. '(lambda (a b) (string< (car a) (car b)))))))
  161. (while l
  162. (let ((doc (or (documentation (cdr (car l)))
  163. "Not documented")))
  164. (if (string-match "\n" doc)
  165. ;; just use first line of documentation
  166. (setq doc (substring doc 0 (match-beginning 0))))
  167. (princ " \"")
  168. (princ (car (car l)))
  169. (princ "\":\n ")
  170. (princ doc)
  171. (write-char ?\n))
  172. (setq l (cdr l))))
  173. nil)))))
  174. (defun te-escape-extended-command ()
  175. (interactive)
  176. (let ((c (let ((completion-ignore-case t))
  177. (completing-read "terminal command: "
  178. te-escape-command-alist
  179. nil t))))
  180. (if c
  181. (catch 'foo
  182. (setq c (downcase c))
  183. (let ((l te-escape-command-alist))
  184. (while l
  185. (if (string= c (downcase (car (car l))))
  186. (throw 'foo (call-interactively (cdr (car l))))
  187. (setq l (cdr l)))))))))
  188. ;; not used.
  189. (defun te-escape-extended-command-unread ()
  190. (interactive)
  191. (setq unread-command-char last-input-char)
  192. (te-escape-extended-command))
  193. (defun te-set-escape-char (c)
  194. "Change the terminal-emulator escape character."
  195. (interactive "cSet escape character to: ")
  196. (let ((o terminal-escape-char))
  197. (message (if (= o c)
  198. "\"%s\" is escape char"
  199. "\"%s\" is now escape; \"%s\" passes though")
  200. (single-key-description c)
  201. (single-key-description o))
  202. (setq terminal-escape-char c)))
  203. (defun te-stuff-string (string)
  204. "Read a string to send to through the terminal emulator
  205. as though that string had been typed on the keyboard.
  206. Very poor man's file transfer protocol."
  207. (interactive "sStuff string: ")
  208. (process-send-string te-process string))
  209. (defun te-set-output-log (name)
  210. "Record output from the terminal emulator in a buffer."
  211. (interactive (list (if te-log-buffer
  212. nil
  213. (read-buffer "Record output in buffer: "
  214. (format "%s output-log"
  215. (buffer-name (current-buffer)))
  216. nil))))
  217. (if (or (null name) (equal name ""))
  218. (progn (setq te-log-buffer nil)
  219. (message "Output logging off."))
  220. (if (get-buffer name)
  221. nil
  222. (save-excursion
  223. (set-buffer (get-buffer-create name))
  224. (fundamental-mode)
  225. (buffer-flush-undo (current-buffer))
  226. (erase-buffer)))
  227. (setq te-log-buffer (get-buffer name))
  228. (message "Recording terminal emulator output into buffer \"%s\""
  229. (buffer-name te-log-buffer))))
  230. (defun te-tofu ()
  231. "Discontinue output log."
  232. (interactive)
  233. (te-set-output-log nil))
  234. (defun te-toggle (sym arg)
  235. (set sym (cond ((not (numberp arg)) arg)
  236. ((= arg 1) (not (symbol-value sym)))
  237. ((< arg 0) nil)
  238. (t t))))
  239. (defun te-toggle-more-processing (arg)
  240. (interactive "p")
  241. (message (if (te-toggle 'terminal-more-processing arg)
  242. "More processing on" "More processing off"))
  243. (if terminal-more-processing (setq te-more-count -1)))
  244. (defun te-toggle-scrolling (arg)
  245. (interactive "p")
  246. (message (if (te-toggle 'terminal-scrolling arg)
  247. "Scroll at end of page" "Wrap at end of page")))
  248. (defun te-enable-more-processing ()
  249. "Enable ** MORE ** processing"
  250. (interactive)
  251. (te-toggle-more-processing t))
  252. (defun te-disable-more-processing ()
  253. "Disable ** MORE ** processing"
  254. (interactive)
  255. (te-toggle-more-processing nil))
  256. (defun te-do-scrolling ()
  257. "Scroll at end of page (yuck)"
  258. (interactive)
  259. (te-toggle-scrolling t))
  260. (defun te-do-wrapping ()
  261. "Wrap to top of window at end of page"
  262. (interactive)
  263. (te-toggle-scrolling nil))
  264. (defun te-set-redisplay-interval (arg)
  265. "Set the maximum interval (in output characters) between screen updates.
  266. Set this number to large value for greater throughput,
  267. set it smaller for more frequent updates (but overall slower performance."
  268. (interactive "NMax number of output chars between redisplay updates: ")
  269. (setq arg (max arg 1))
  270. (setq terminal-redisplay-interval arg
  271. te-redisplay-count 0))
  272. ;;;; more map
  273. ;; every command -must- call te-more-break-unwind
  274. ;; or grave lossage will result
  275. (put 'te-more-break-unread 'suppress-keymap t)
  276. (defun te-more-break-unread ()
  277. (interactive)
  278. (if (= last-input-char terminal-escape-char)
  279. (call-interactively 'te-escape)
  280. (message "Continuing from more break (\"%s\" typed, %d chars output pending...)"
  281. (single-key-description last-input-char)
  282. (te-pending-output-length))
  283. (setq te-more-count 259259)
  284. (te-more-break-unwind)
  285. (let ((terminal-more-processing nil))
  286. (te-pass-through))))
  287. (defun te-more-break-resume ()
  288. "Proceed past the **MORE** break,
  289. allowing the next page of output to appear"
  290. (interactive)
  291. (message "Continuing from more break")
  292. (te-more-break-unwind))
  293. (defun te-more-break-help ()
  294. "Provide help on commands available in a terminal-emulator **MORE** break"
  295. (interactive)
  296. (message "Terminal-emulator more break help...")
  297. (sit-for 0)
  298. (with-electric-help
  299. (function (lambda ()
  300. (princ "Terminal-emulator more break.\n\n")
  301. (princ (format "Type \"%s\" (te-more-break-resume)\n%s\n"
  302. (where-is-internal 'te-more-break-resume
  303. terminal-more-break-map t)
  304. (documentation 'te-more-break-resume)))
  305. (princ (substitute-command-keys "\\{terminal-more-break-map}\n"))
  306. (princ "Any other key is passed through to the program
  307. running under the terminal emulator and disables more processing until
  308. all pending output has been dealt with.")
  309. nil))))
  310. (defun te-more-break-advance-one-line ()
  311. "Allow one more line of text to be output before doing another more break."
  312. (interactive)
  313. (setq te-more-count 1)
  314. (te-more-break-unwind))
  315. (defun te-more-break-flush-pending-output ()
  316. "Discard any output which has been received by the terminal emulator but
  317. not yet proceesed and then proceed from the more break."
  318. (interactive)
  319. (te-more-break-unwind)
  320. (te-flush-pending-output))
  321. (defun te-flush-pending-output ()
  322. "Discard any as-yet-unprocessed output which has been received by
  323. the terminal emulator."
  324. (interactive)
  325. ;; this could conceivably be confusing in the presence of
  326. ;; escape-sequences spanning process-output chunks
  327. (if (null (cdr te-pending-output))
  328. (message "(There is no output pending)")
  329. (let ((length (te-pending-output-length)))
  330. (message "Flushing %d chars of pending output" length)
  331. (setq te-pending-output
  332. (list 0 (format "\n*** %d chars of pending output flushed ***\n"
  333. length)))
  334. (te-update-pending-output-display)
  335. (te-process-output nil)
  336. (sit-for 0))))
  337. (defun te-pass-through ()
  338. "Send the last character typed through the terminal-emulator
  339. without any interpretation"
  340. (interactive)
  341. (if (eql last-input-char terminal-escape-char)
  342. (call-interactively 'te-escape)
  343. (and terminal-more-processing
  344. (null (cdr te-pending-output))
  345. (te-set-more-count nil))
  346. (send-string te-process (make-string 1 last-input-char))
  347. (te-process-output t)))
  348. (defun te-set-window-start ()
  349. (let* ((w (get-buffer-window (current-buffer)))
  350. (h (window-height w)))
  351. (cond ((>= h (/ (- (point) (point-min)) (1+ te-width)))
  352. ;; this is the normal case
  353. (set-window-start w (point-min)))
  354. ;; this happens if some vandal shrinks our window.
  355. ((>= h (/ (- (point-max) (point)) (1+ te-width)))
  356. (set-window-start w (- (point-max) (* h (1+ te-width)) -1)))
  357. ;; I give up.
  358. (t nil))))
  359. (defun te-pending-output-length ()
  360. (let ((length (car te-pending-output))
  361. (tem (cdr te-pending-output)))
  362. (while tem
  363. (setq length (+ length (length (car tem))) tem (cdr tem)))
  364. length))
  365. ;;;; more break hair
  366. (defun te-more-break ()
  367. (te-set-more-count t)
  368. (make-local-variable 'te-more-old-point)
  369. (setq te-more-old-point (point))
  370. (make-local-variable 'te-more-old-local-map)
  371. (setq te-more-old-local-map (current-local-map))
  372. (use-local-map terminal-more-break-map)
  373. (make-local-variable 'te-more-old-filter)
  374. (setq te-more-old-filter (process-filter te-process))
  375. (make-local-variable 'te-more-old-mode-line-format)
  376. (setq te-more-old-mode-line-format mode-line-format
  377. mode-line-format (list "-- **MORE** "
  378. mode-line-buffer-identification
  379. "%-"))
  380. (set-process-filter te-process
  381. (function (lambda (process string)
  382. (save-excursion
  383. (set-buffer (process-buffer process))
  384. (setq te-pending-output (nconc te-pending-output
  385. (list string))))
  386. (te-update-pending-output-display))))
  387. (te-update-pending-output-display)
  388. (if (eq (window-buffer (selected-window)) (current-buffer))
  389. (message "More break "))
  390. (or (eobp)
  391. (null terminal-more-break-insertion)
  392. (save-excursion
  393. (forward-char 1)
  394. (delete-region (point) (+ (point) te-width))
  395. (insert terminal-more-break-insertion)))
  396. (run-hooks 'terminal-more-break-hook)
  397. (sit-for 0) ;get display to update
  398. (throw 'te-process-output t))
  399. (defun te-more-break-unwind ()
  400. (use-local-map te-more-old-local-map)
  401. (set-process-filter te-process te-more-old-filter)
  402. (goto-char te-more-old-point)
  403. (setq mode-line-format te-more-old-mode-line-format)
  404. (set-buffer-modified-p (buffer-modified-p))
  405. (let ((buffer-read-only nil))
  406. (cond ((eobp))
  407. (terminal-more-break-insertion
  408. (forward-char 1)
  409. (delete-region (point)
  410. (+ (point) (length terminal-more-break-insertion)))
  411. (insert-char ?\ te-width)
  412. (goto-char te-more-old-point)))
  413. (setq te-more-old-point nil)
  414. (let ((te-more-count 259259))
  415. (te-newline)))
  416. ;(sit-for 0)
  417. (te-process-output t))
  418. (defun te-set-more-count (newline)
  419. (let ((line (/ (- (point) (point-min)) (1+ te-width))))
  420. (if newline (setq line (1+ line)))
  421. (cond ((= line te-height)
  422. (setq te-more-count te-height))
  423. ;>>>> something is strange. Investigate this!
  424. ((= line (1- te-height))
  425. (setq te-more-count te-height))
  426. ((or (< line (/ te-height 2))
  427. (> (- te-height line) 10))
  428. ;; break at end of this page
  429. (setq te-more-count (- te-height line)))
  430. (t
  431. ;; migrate back towards top (ie bottom) of screen.
  432. (setq te-more-count (- te-height
  433. (if (> te-height 10) 2 1)))))))
  434. ;;;; More or less straight-forward terminal escapes
  435. ; ^j (Yeah, even though the civilized world uses a different character
  436. ; for newline from that used by The Operating System of the Future)
  437. (defun te-newline ()
  438. "Move down a line, optionally do more processing, perhaps wrap/scroll,
  439. move to start of new line, clear to end of line."
  440. (end-of-line)
  441. (cond ((not terminal-more-processing))
  442. ((< (setq te-more-count (1- te-more-count)) 0)
  443. (te-set-more-count t))
  444. ((eql te-more-count 0)
  445. ;; this doesn't return
  446. (te-more-break)))
  447. (if (eobp)
  448. (progn
  449. (delete-region (point-min) (+ (point-min) te-width))
  450. (goto-char (point-min))
  451. (if terminal-scrolling
  452. (progn (delete-char 1)
  453. (goto-char (point-max))
  454. (insert ?\n))))
  455. (forward-char 1)
  456. (delete-region (point) (+ (point) te-width)))
  457. (insert-char ?\ te-width)
  458. (beginning-of-line)
  459. (if (bobp)
  460. (set-window-start (get-buffer-window (current-buffer))
  461. (point-min) t)
  462. (te-set-window-start)))
  463. ; ^p = x+32 y+32
  464. (defun te-move-to-position ()
  465. ;; must offset by #o40 since cretinous unix won't send a 004 char through
  466. (let ((y (- (te-get-char) 32))
  467. (x (- (te-get-char) 32)))
  468. (if (or (> x te-width)
  469. (> y te-height))
  470. () ;(error "fucked %d %d" x y)
  471. (goto-char (+ (point-min) x (* y (1+ te-width))))
  472. ;(te-set-window-start?)
  473. ))
  474. (setq te-more-count -1))
  475. ;; ^p c
  476. (defun te-clear-rest-of-line ()
  477. (save-excursion
  478. (let ((n (- (point) (progn (end-of-line) (point)))))
  479. (delete-region (point) (+ (point) n))
  480. (insert-char ?\ (- n)))))
  481. ;; ^p C
  482. (defun te-clear-rest-of-screen ()
  483. (save-excursion
  484. (te-clear-rest-of-line)
  485. (while (progn (end-of-line) (not (eobp)))
  486. (forward-char 1) (end-of-line)
  487. (delete-region (- (point) te-width) (point))
  488. (insert-char ?\ te-width))))
  489. ;; ^p ^l
  490. (defun te-clear-screen ()
  491. ;; regenerate buffer to compensate for (nonexistent!!) bugs.
  492. (erase-buffer)
  493. (let ((i 0))
  494. (while (< i te-height)
  495. (setq i (1+ i))
  496. (insert-char ?\ te-width)
  497. (insert ?\n)))
  498. (delete-region (1- (point-max)) (point-max))
  499. (goto-char (point-min))
  500. (setq te-more-count -1))
  501. ;; ^p ^o count+32
  502. (defun te-insert-lines ()
  503. (if (not (bolp))
  504. ();(error "fooI")
  505. (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
  506. (n (min (- (te-get-char) ?\ ) line))
  507. (i 0))
  508. (delete-region (- (point-max) (* n (1+ te-width))) (point-max))
  509. (if (eql (point) (point-max)) (insert ?\n))
  510. (while (< i n)
  511. (setq i (1+ i))
  512. (insert-char ?\ te-width)
  513. (or (eql i line) (insert ?\n)))))
  514. (setq te-more-count -1))
  515. ;; ^p ^k count+32
  516. (defun te-delete-lines ()
  517. (if (not (bolp))
  518. ();(error "fooD")
  519. (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
  520. (n (min (- (te-get-char) ?\ ) line))
  521. (i 0))
  522. (delete-region (point)
  523. (min (+ (point) (* n (1+ te-width))) (point-max)))
  524. (save-excursion
  525. (goto-char (point-max))
  526. (while (< i n)
  527. (setq i (1+ i))
  528. (insert-char ?\ te-width)
  529. (or (eql i line) (insert ?\n))))))
  530. (setq te-more-count -1))
  531. ;; ^p ^a
  532. (defun te-beginning-of-line ()
  533. (beginning-of-line))
  534. ;; ^p ^b
  535. (defun te-backward-char ()
  536. (if (not (bolp))
  537. (backward-char 1)))
  538. ;; 0177
  539. (defun te-delete ()
  540. (if (bolp)
  541. ()
  542. (delete-region (1- (point)) (point))
  543. (insert ?\ )
  544. (forward-char -1)))
  545. ;; ^p ^g
  546. (defun te-beep ()
  547. (beep))
  548. ;; ^p _ count+32
  549. (defun te-insert-spaces ()
  550. (let* ((p (point))
  551. (n (min (- (te-get-char) 32)
  552. (- (progn (end-of-line) (point)) p))))
  553. (if (<= n 0)
  554. nil
  555. (delete-char (- n))
  556. (goto-char p)
  557. (insert-char ?\ n))
  558. (goto-char p)))
  559. ;; ^p d count+32 (should be ^p ^d but cretinous un*x won't send ^d chars!!!)
  560. (defun te-delete-char ()
  561. (let* ((p (point))
  562. (n (min (- (te-get-char) 32)
  563. (- (progn (end-of-line) (point)) p))))
  564. (if (<= n 0)
  565. nil
  566. (insert-char ?\ n)
  567. (goto-char p)
  568. (delete-char n))
  569. (goto-char p)))
  570. ;; disgusting unix-required shit
  571. ;; Are we living twenty years in the past yet?
  572. (defun te-losing-unix ()
  573. ;(what lossage)
  574. ;(message "fucking-unix: %d" char)
  575. )
  576. ;; ^i
  577. (defun te-losing-tab ()
  578. (let* ((p (point))
  579. (x (- p (progn (beginning-of-line) (point))))
  580. (l (min (- 8 (logand x 7))
  581. (progn (end-of-line) (- (point) p)))))
  582. (goto-char p)
  583. (delete-region p (+ p l))
  584. (insert-char ?\ l)))
  585. ;; Also:
  586. ;; ^m => beginning-of-line (for which it -should- be using ^p ^a, right?!!)
  587. ;; ^g => te-beep (for which it should use ^p ^g)
  588. ;; ^h => te-backward-char (for which it should use ^p ^b)
  589. (defun te-filter (process string)
  590. (let* ((obuf (current-buffer))
  591. (m meta-flag))
  592. ;; can't use save-excursion, as that preserves point, which we don't want
  593. (unwind-protect
  594. (progn
  595. (set-buffer (process-buffer process))
  596. (goto-char te-saved-point)
  597. (and (bufferp te-log-buffer)
  598. (if (null (buffer-name te-log-buffer))
  599. ;; killed
  600. (setq te-log-buffer nil)
  601. (set-buffer te-log-buffer)
  602. (goto-char (point-max))
  603. (insert string)
  604. (set-buffer (process-buffer process))))
  605. (setq te-pending-output (nconc te-pending-output (list string)))
  606. (te-update-pending-output-display)
  607. ;; this binding is needed because emacs looks at meta-flag when
  608. ;; the keystroke is read from the keyboard, not when it is about
  609. ;; to be fed into a keymap (or returned by read-char)
  610. ;; There still could be some screws, though.
  611. (let ((meta-flag m))
  612. (te-process-output (eq (current-buffer)
  613. (window-buffer (selected-window)))))
  614. (set-buffer (process-buffer process))
  615. (setq te-saved-point (point)))
  616. (set-buffer obuf))))
  617. ;; fucking unix has -such- braindamaged lack of tty control...
  618. (defun te-process-output (preemptable)
  619. ;;>> There seems no good reason to ever disallow preemption
  620. (setq preemptable t)
  621. (catch 'te-process-output
  622. (let ((buffer-read-only nil)
  623. start string (ostring nil) (matchpos nil) (char nil))
  624. (while (cdr te-pending-output)
  625. (setq ostring string
  626. start (car te-pending-output)
  627. string (car (cdr te-pending-output))
  628. char (aref string start))
  629. (if (eql (setq start (1+ start)) (length string))
  630. (progn (setq te-pending-output
  631. (cons 0 (cdr (cdr te-pending-output)))
  632. start 0
  633. string (car (cdr te-pending-output)))
  634. (te-update-pending-output-display))
  635. (setcar te-pending-output start))
  636. (if (and (> char ?\037) (< char ?\377))
  637. (cond ((eolp)
  638. ;; unread char
  639. (if (eql start 0)
  640. (setq te-pending-output
  641. (cons 0 (cons (make-string 1 char)
  642. (cdr te-pending-output))))
  643. (setcar te-pending-output (1- start)))
  644. (te-newline))
  645. ((null string)
  646. (delete-char 1) (insert char)
  647. (te-redisplay-if-necessary 1))
  648. (t
  649. (let ((end (or (and (eq ostring string) matchpos)
  650. (setq matchpos (string-match
  651. "[\000-\037\177-\377]"
  652. string start))
  653. (length string))))
  654. (delete-char 1) (insert char)
  655. (setq char (point)) (end-of-line)
  656. (setq end (min end (+ start (- (point) char))))
  657. (goto-char char)
  658. (if (eql end matchpos) (setq matchpos nil))
  659. (delete-region (point) (+ (point) (- end start)))
  660. (insert (if (and (eql start 0)
  661. (eql end (length string)))
  662. string
  663. (substring string start end)))
  664. (if (eql end (length string))
  665. (setq te-pending-output
  666. (cons 0 (cdr (cdr te-pending-output))))
  667. (setcar te-pending-output end))
  668. (te-redisplay-if-necessary (1+ (- end start))))))
  669. ;; I suppose if I split the guts of this out into a separate
  670. ;; function we could trivially emulate different terminals
  671. ;; Who cares in any case? (Apart from losers without supdup)
  672. (funcall
  673. (if (eql char ?\^p)
  674. (or (cdr (assq (te-get-char)
  675. '((?= . te-move-to-position)
  676. (?c . te-clear-rest-of-line)
  677. (?C . te-clear-rest-of-screen)
  678. (?\C-o . te-insert-lines)
  679. (?\C-k . te-delete-lines)
  680. ;; not necessary, but help sometimes.
  681. (?\C-a . te-beginning-of-line)
  682. (?\C-b . te-backward-char)
  683. ;; should be C-d, but bloody useless
  684. ;; cretin-designed-and-implemented un*x
  685. ;; pty's won't send \004 through!
  686. ;; Can you believe this?
  687. (?d . te-delete-char)
  688. (?_ . te-insert-spaces)
  689. ;; random
  690. (?\C-g . te-beep)
  691. (?\C-l . te-clear-screen)
  692. )))
  693. 'te-losing-unix)
  694. (or (cdr (assq char
  695. '((?\n . te-newline)
  696. (?\177 . te-delete)
  697. ;; Did I ask to be sent these characters?
  698. ;; I don't remember doing so, either.
  699. ;; (Perhaps some operating system or
  700. ;; other is completely incompetent...)
  701. (?\r . te-beginning-of-line) ;fuck me harder
  702. (?\^g . te-beep) ;again and again!
  703. (?\b . te-backward-char) ;wa12id!!
  704. (?\t . te-losing-tab)))) ;(spiked)
  705. 'te-losing-unix))) ;That feels better
  706. (te-redisplay-if-necessary 1))
  707. (and preemptable
  708. (input-pending-p)
  709. ;; preemptable output! Oh my!!
  710. (throw 'te-process-output t)))))
  711. ;; We must update window-point in every window displaying our buffer
  712. (let* ((s (selected-window))
  713. (w s))
  714. (while (not (eq s (setq w (next-window w))))
  715. (if (eq (window-buffer w) (current-buffer))
  716. (set-window-point w (point))))))
  717. (defun te-get-char ()
  718. (if (cdr te-pending-output)
  719. (let ((start (car te-pending-output))
  720. (string (car (cdr te-pending-output))))
  721. (prog1 (aref string start)
  722. (if (eql (setq start (1+ start)) (length string))
  723. (setq te-pending-output (cons 0 (cdr (cdr te-pending-output))))
  724. (setcar te-pending-output start))))
  725. (catch 'char
  726. (let ((filter (process-filter te-process)))
  727. (unwind-protect
  728. (progn
  729. (set-process-filter te-process
  730. '(lambda (p s)
  731. (or (eql (length s) 1)
  732. (setq te-pending-output (list 1 s)))
  733. (throw 'char (aref s 0))))
  734. (accept-process-output te-process))
  735. (set-process-filter te-process filter))))))
  736. (defun te-redisplay-if-necessary (length)
  737. (and (<= (setq te-redisplay-count (- te-redisplay-count length)) 0)
  738. (eq (current-buffer) (window-buffer (selected-window)))
  739. (waiting-for-user-input-p)
  740. (progn (te-update-pending-output-display)
  741. (sit-for 0)
  742. (setq te-redisplay-count terminal-redisplay-interval))))
  743. (defun te-update-pending-output-display ()
  744. (if (null (cdr te-pending-output))
  745. (setq te-pending-output-info "")
  746. (let ((length (te-pending-output-length)))
  747. (if (< length 1500)
  748. (setq te-pending-output-info "")
  749. (setq te-pending-output-info (format "(%dK chars output pending) "
  750. (/ (+ length 512) 1024))))))
  751. ;; update mode line
  752. (set-buffer-modified-p (buffer-modified-p)))
  753. (defun te-sentinel (process message)
  754. (cond ((eq (process-status process) 'run))
  755. ((null (buffer-name (process-buffer process)))) ;deleted
  756. (t (let ((b (current-buffer)))
  757. (save-excursion
  758. (set-buffer (process-buffer process))
  759. (setq buffer-read-only nil)
  760. (fundamental-mode)
  761. (goto-char (point-max))
  762. (insert "\n*******\n" message "*******"))
  763. (if (and (eq b (process-buffer process))
  764. (waiting-for-user-input-p))
  765. (progn (goto-char (point-max))
  766. (recenter -1)))))))
  767. (defun terminal-emulator (buffer program args)
  768. "Under a display-terminal emulator in BUFFER, run PROGRAM on arguments ARGS.
  769. BUFFER's contents are made an image of the display generated by that program,
  770. and any input typed when BUFFER is the current Emacs buffer is sent to that
  771. program an keyboard input.
  772. Interactively, BUFFER defaults to \"*terminal*\" and PROGRAM and ARGS
  773. are parsed from an input-string using `sh' (which means that the ~user
  774. filename-naming convention doesn't work. Isn't un*x wonderful?)
  775. The emulated display terminal will have height the same as that of the
  776. the selected window, and width one less than that window's.
  777. To switch buffers and leave the emulator, or to give commands
  778. to the emulator itself (as opposed to the program running under it),
  779. type Control-^. The following character is an emulator command.
  780. Type Control-^ twice to send it to the subprogram.
  781. This escape character may be changed using the variable `terminal-escape-char'.
  782. Meta characters may not currently be sent through the terminal emulator.
  783. Here is a list of some of the variables which control the behaviour
  784. of the emulator -- see their documentation for more information:
  785. terminal-escape-char, terminal-scrolling, terminal-more-processing,
  786. terminal-redisplay-interval.
  787. This function calls the value of terminal-mode-hook if that exists
  788. and is non-nil after the terminal buffer has been set up and the
  789. subprocess started.
  790. Presently with `termcap' only; if somebody sends us code to make this
  791. work with `terminfo' we will try to use it."
  792. (interactive
  793. (cons (save-excursion
  794. (set-buffer (get-buffer-create "*terminal*"))
  795. (buffer-name (if (or (not (boundp 'te-process))
  796. (null te-process)
  797. (not (eq (process-status te-process)
  798. 'run)))
  799. (current-buffer)
  800. (generate-new-buffer "*terminal*"))))
  801. (let ((s (read-string
  802. (format "Run program in emulator: (default %s) "
  803. (getenv "SHELL")))))
  804. (if (equal s "")
  805. (list (getenv "SHELL") ())
  806. (te-parse-program-and-args s)))))
  807. (switch-to-buffer buffer)
  808. (terminal-mode)
  809. (setq te-width (- (window-width (selected-window)) 2)
  810. te-height (1- (window-height (selected-window))))
  811. (setq mode-line-buffer-identification
  812. (list (format "Emacs terminal %dx%d: %%b " te-width te-height)
  813. 'te-pending-output-info))
  814. (let ((buffer-read-only nil))
  815. (te-clear-screen))
  816. (let (process)
  817. (while (setq process (get-buffer-process (current-buffer)))
  818. (if (y-or-n-p (format "Kill process %s? " (process-name process)))
  819. (delete-process process)
  820. (error "Process %s not killed" (process-name process)))))
  821. (condition-case err
  822. (let ((termcap (format "emacs-virtual:co#%d:li#%d:cm=^p=%%+ %%+ :cr=^p^a:le=^p^b:nw=^j:ce=^pc:cd=^pC:cl=^p^l:IC=^p_%%+ :DC=^pd%%+ :AL=^p^o%%+ :DL=^p^k%%+ :ic=^p_!:dc=^pd!:al=^p^o!:dl=^p^k!:bl=^p^g"
  823. te-width te-height)))
  824. (if (fboundp 'start-subprocess)
  825. ;; this winning function would do everything, except that
  826. ;; rms doesn't want it.
  827. (setq te-process (start-subprocess "terminal-emulator"
  828. program args
  829. 'channel-type 'terminal
  830. 'filter 'te-filter
  831. 'buffer (current-buffer)
  832. 'sentinel 'te-sentinel
  833. 'modify-environment
  834. (list (cons "TERM" "emacs-virtual")
  835. (cons "TERMCAP" termcap))))
  836. ;; so instead we resort to this... I want to throw up.
  837. (setq te-process (start-process "terminal-emulator" (current-buffer)
  838. "sh" "-c"
  839. ;; Yuck!!! Start a shell to set some terminal
  840. ;; control characteristics. Then start the
  841. ;; "env" program to setup the terminal type
  842. ;; Then finally start the program we wanted.
  843. (format "stty -nl echo; exec %s TERM=emacs-virtual %s %s"
  844. (te-quote-arg-for-sh
  845. (concat exec-directory "env"))
  846. (te-quote-arg-for-sh
  847. (concat "TERMCAP=" termcap))
  848. (mapconcat 'te-quote-arg-for-sh
  849. (cons program args) " "))))
  850. (set-process-filter te-process 'te-filter)
  851. (set-process-sentinel te-process 'te-sentinel)))
  852. (error (fundamental-mode)
  853. (signal (car err) (cdr err))))
  854. ;; sigh
  855. (if (default-value 'meta-flag)
  856. (progn (message
  857. "Note: Meta key disabled due to maybe-eventually-reparable braindamage")
  858. (sit-for 1)))
  859. (message "Entering emacs terminal-emulator... Type %s %s for help"
  860. (single-key-description terminal-escape-char)
  861. (mapconcat 'single-key-description
  862. (where-is-internal 'te-escape-help
  863. terminal-escape-map
  864. t)
  865. " "))
  866. (setq inhibit-quit t) ;sport death
  867. (use-local-map terminal-map)
  868. (run-hooks 'terminal-mode-hook))
  869. (defun te-parse-program-and-args (s)
  870. (cond ((string-match "\\`\\([a-zA-Z0-9-+=_.@/:]+[ \t]*\\)+\\'" s)
  871. (let ((l ()) (p 0))
  872. (while p
  873. (setq l (cons (if (string-match
  874. "\\([a-zA-Z0-9-+=_.@/:]+\\)\\([ \t]+\\)*"
  875. s p)
  876. (prog1 (substring s p (match-end 1))
  877. (setq p (match-end 0))
  878. (if (eql p (length s)) (setq p nil)))
  879. (prog1 (substring s p)
  880. (setq p nil)))
  881. l)))
  882. (setq l (nreverse l))
  883. (list (car l) (cdr l))))
  884. ((and (string-match "[ \t]" s) (not (file-exists-p s)))
  885. (list "sh" (list "-c" (concat "exec " s))))
  886. (t (list s ()))))
  887. (put 'terminal-mode 'mode-class 'special)
  888. ;; This is only separated out from function terminal-emulator
  889. ;; to keep the latter a little more managable.
  890. (defun terminal-mode ()
  891. "Set up variables for use f the terminal-emualtor.
  892. One should not call this -- it is an internal function
  893. of the terminal-emulator"
  894. (kill-all-local-variables)
  895. (buffer-flush-undo (current-buffer))
  896. (setq major-mode 'terminal-mode)
  897. (setq mode-name "terminal")
  898. ; (make-local-variable 'Helper-return-blurb)
  899. ; (setq Helper-return-blurb "return to terminal simulator")
  900. (setq mode-line-process '(": %s"))
  901. (setq buffer-read-only t)
  902. (make-local-variable 'terminal-escape-char)
  903. (setq terminal-escape-char (default-value 'terminal-escape-char))
  904. (make-local-variable 'terminal-scrolling)
  905. (setq terminal-scrolling (default-value 'terminal-scrolling))
  906. (make-local-variable 'terminal-more-processing)
  907. (setq terminal-more-processing (default-value 'terminal-more-processing))
  908. (make-local-variable 'terminal-redisplay-interval)
  909. (setq terminal-redisplay-interval (default-value 'terminal-redisplay-interval))
  910. (make-local-variable 'te-width)
  911. (make-local-variable 'te-height)
  912. (make-local-variable 'te-process)
  913. (make-local-variable 'te-pending-output)
  914. (setq te-pending-output (list 0))
  915. (make-local-variable 'te-saved-point)
  916. (setq te-saved-point (point-min))
  917. (make-local-variable 'te-pending-output-info) ;for the mode line
  918. (setq te-pending-output-info "")
  919. (make-local-variable 'inhibit-quit)
  920. ;(setq inhibit-quit t)
  921. (make-local-variable 'te-log-buffer)
  922. (setq te-log-buffer nil)
  923. (make-local-variable 'te-more-count)
  924. (setq te-more-count -1)
  925. (make-local-variable 'te-redisplay-count)
  926. (setq te-redisplay-count terminal-redisplay-interval)
  927. ;;>> Nothing can be done about this without decruftifying
  928. ;;>> emacs keymaps.
  929. (make-local-variable 'meta-flag) ;sigh
  930. (setq meta-flag nil)
  931. ;(use-local-map terminal-mode-map)
  932. ;; terminal-mode-hook is called above in function terminal-emulator
  933. )
  934. ;;;; what a complete loss
  935. (defun te-quote-arg-for-sh (fuckme)
  936. (cond ((string-match "\\`[a-zA-Z0-9-+=_.@/:]+\\'"
  937. fuckme)
  938. fuckme)
  939. ((not (string-match "[$]" fuckme))
  940. ;; "[\"\\]" are special to sh and the lisp reader in the same way
  941. (prin1-to-string fuckme))
  942. (t
  943. (let ((harder "")
  944. (cretin 0)
  945. (stupid 0))
  946. (while (cond ((>= cretin (length fuckme))
  947. nil)
  948. ;; this is the set of chars magic with "..." in `sh'
  949. ((setq stupid (string-match "[\"\\$]"
  950. fuckme cretin))
  951. t)
  952. (t (setq harder (concat harder
  953. (substring fuckme cretin)))
  954. nil))
  955. (setq harder (concat harder (substring fuckme cretin stupid)
  956. ;; can't use ?\\, since bloody idiotic
  957. ;; `concat' does prin1-to-string on fixna.
  958. ;; Can you believe this????
  959. "\\"
  960. ;; can't use aref for the same reason
  961. (substring fuckme
  962. stupid
  963. (1+ stupid)))
  964. cretin (1+ stupid)))
  965. (concat "\"" harder "\"")))))
  966. ;; local variables:
  967. ;; eval: (put 'te-install-escape-command 'lisp-indent-hook 'defun)
  968. ;; end: