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