(provide (quote terminal))

(require (quote ehelp))

(defvar terminal-escape-char 30 "\
*All characters except for this are passed verbatim through the
terminal-emulator.  This character acts as a prefix for commands
to the emulator program itself.  Type this character twice to send
it through the emulator.  Type ? after typing it for a list of
possible commands.
This variable is local to each terminal-emulator buffer.")

(defvar terminal-scrolling t "\
*If non-nil, the terminal-emulator will `scroll' when output occurs
past the bottom of the screen.  If nil, output will `wrap' to the top
of the screen.
This variable is local to each terminal-emulator buffer.")

(defvar terminal-more-processing t "\
*If non-nil, do more-processing.
This variable is local to each terminal-emulator buffer.")

(defvar terminal-redisplay-interval 5000 "\
*Maximum number of characters which will be processed by the
terminal-emulator before a screen redisplay is forced.
Set this to a large value for greater throughput,
set it smaller for more frequent updates but overall slower

(defvar terminal-more-break-insertion "*** More break -- Press space to continue ***")

(defvar terminal-escape-map nil)

(defvar terminal-map nil)

(defvar terminal-more-break-map nil)

(if terminal-map nil (let ((map (make-keymap))) (fillarray map (quote te-pass-through)) (setq terminal-map map)))

(if terminal-escape-map nil (let ((map (make-keymap))) (fillarray map (quote undefined)) (let ((s "0")) (while (<= (aref s 0) 57) (define-key map s (quote digit-argument)) (aset s 0 (1+ (aref s 0))))) (define-key map "b" (quote switch-to-buffer)) (define-key map "o" (quote other-window)) (define-key map "e" (quote te-set-escape-char)) (define-key map "" (quote redraw-display)) (define-key map "" (quote te-flush-pending-output)) (define-key map "m" (quote te-toggle-more-processing)) (define-key map "x" (quote te-escape-extended-command)) (define-key map "?" (quote te-escape-help)) (define-key map (char-to-string help-char) (quote te-escape-help)) (setq terminal-escape-map map)))

(defvar te-escape-command-alist nil)

(if te-escape-command-alist nil (setq te-escape-command-alist (quote (("Set Escape Character" . te-set-escape-char) ("Refresh" . redraw-display) ("Record Output" . te-set-output-log) ("Photo" . te-set-output-log) ("Tofu" . te-tofu) ("Stuff Input" . te-stuff-string) ("Flush Pending Output" . te-flush-pending-output) ("Enable More Processing" . te-enable-more-processing) ("Disable More Processing" . te-disable-more-processing) ("Scroll at end of page" . te-do-scrolling) ("Wrap at end of page" . te-do-wrapping) ("Switch To Buffer" . switch-to-buffer) ("Other Window" . other-window) ("Kill Buffer" . kill-buffer) ("Help" . te-escape-help) ("Set Redisplay Interval" . te-set-redisplay-interval)))))

(if terminal-more-break-map nil (let ((map (make-keymap))) (fillarray map (quote te-more-break-unread)) (define-key map (char-to-string help-char) (quote te-more-break-help)) (define-key map " " (quote te-more-break-resume)) (define-key map "" (quote redraw-display)) (define-key map "" (quote te-more-break-flush-pending-output)) (define-key map "
" (quote te-more-break-advance-one-line)) (setq terminal-more-break-map map)))

(defun te-escape nil (interactive) (byte-code "Lj  ʎ!!\"!\"#!)!
 )O\"O!+" [s local global terminal-escape-map prefix-arg terminal-escape-char last-command-char nil current-local-map current-global-map ((byte-code "!	!" [global local use-global-map use-local-map] 3)) use-global-map use-local-map read-key-sequence format "Emacs Terminal escape> %d " prefix-numeric-value "Emacs Terminal escape> " message "" string= make-string 1 -259 te-pass-through lookup-key call-interactively] 14))

(defun te-escape-help nil "\
Provide help on commands available after terminal-escape-char is typed." (interactive) (byte-code "ˆ!	!!)" [char terminal-escape-char nil message "Terminal emulator escape help..." single-key-description with-electric-help (lambda nil (byte-code "%!!!	##!!+!\"1!\"o@A!?
!!)A2)Ƈ" [char terminal-escape-map t l te-escape-command-alist doc nil princ format "Terminal-emulator escape, invoked by \"%s\"
Type \"%s\" twice to send a single \"%s\" through.

Other chars following \"%s\" are interpreted as follows:
" substitute-command-keys "\\{terminal-escape-map}
" "
Subcommands of \"%s\" (%s)
" where-is-internal te-escape-extended-command fboundp sortcar copy-sequence string< sort (lambda (a b) (byte-code "@	@\"" [a b string<] 3)) documentation "Not documented" string-match "
" 0 match-beginning "  \"" "\":
     " write-char 10] 23))] 4))

(defun te-escape-extended-command nil (interactive) (byte-code "Ĉ$)ȍ)" [c completion-ignore-case t te-escape-command-alist nil completing-read "terminal command: " foo (byte-code "!
	*	@@!\"\"	@A!\"&	A)" [c l te-escape-command-alist downcase string= throw foo call-interactively] 8)] 5))

(defun te-escape-extended-command-unread nil (interactive) (byte-code "ˆ	 " [unread-command-char last-input-char nil te-escape-extended-command] 2))

(defun te-set-escape-char (c) "\
Change the terminal-emulator escape character." (interactive "cSet escape character to: ") (byte-code "È	
)" [o terminal-escape-char c nil message "\"%s\" is escape char" "\"%s\" is now escape; \"%s\" passes though" single-key-description] 6))

(defun te-stuff-string (string) "\
Read a string to send to through the terminal emulator
as though that string had been typed on the keyboard.

Very poor man's file transfer protocol." (interactive "sStuff string: ") (byte-code "ˆ	\"" [te-process string nil process-send-string] 3))

(defun te-set-output-log (name) "\
Record output from the terminal emulator in a buffer." (interactive (byte-code "p!\"#C" [te-log-buffer nil read-buffer "Record output in buffer: " format "%s output-log" buffer-name] 6)) (byte-code "
!q p! )
!!\"" [te-log-buffer nil name equal "" message "Output logging off." get-buffer get-buffer-create fundamental-mode buffer-flush-undo erase-buffer "Recording terminal emulator output into buffer \"%s\"" buffer-name] 12))

(defun te-tofu nil "\
Discontinue output log." (interactive) (byte-code "!" [nil te-set-output-log] 2))

(defun te-toggle (sym arg) (byte-code "	!?	#	UJ?#	W\"‚#L" [sym arg nil t numberp 1 0] 4))

(defun te-toggle-more-processing (arg) (interactive "p") (byte-code "È\"Ƃ!	ȉ" [arg terminal-more-processing te-more-count nil message te-toggle "More processing on" "More processing off" -1] 4))

(defun te-toggle-scrolling (arg) (interactive "p") (byte-code "\"ł!" [arg nil message te-toggle terminal-scrolling "Scroll at end of page" "Wrap at end of page"] 4))

(defun te-enable-more-processing nil "\
Enable ** MORE ** processing" (interactive) (byte-code "!" [t nil te-toggle-more-processing] 2))

(defun te-disable-more-processing nil "\
Disable ** MORE ** processing" (interactive) (byte-code "!" [nil te-toggle-more-processing] 2))

(defun te-do-scrolling nil "\
Scroll at end of page (yuck)" (interactive) (byte-code "!" [t nil te-toggle-scrolling] 2))

(defun te-do-wrapping nil "\
Wrap to top of window at end of page" (interactive) (byte-code "!" [nil te-toggle-scrolling] 2))

(defun te-set-redisplay-interval (arg) "\
Set the maximum interval (in output characters) between screen updates.
Set this number to large value for greater throughput,
set it smaller for more frequent updates (but overall slower performance." (interactive "NMax number of output chars between redisplay updates: ") (byte-code "È]ʼn" [arg terminal-redisplay-interval te-redisplay-count nil 1 0] 2))

(put (quote te-more-break-unread) (quote suppress-keymap) t)

(defun te-more-break-unread nil (interactive) (byte-code "Ĉ	U!#! #ˉ  )" [last-input-char terminal-escape-char te-more-count terminal-more-processing nil call-interactively te-escape message "Continuing from more break (\"%s\" typed, %d chars output pending...)" single-key-description te-pending-output-length 259259 te-more-break-unwind te-pass-through] 7))

(defun te-more-break-resume nil "\
Proceed past the **MORE** break,
allowing the next page of output to appear" (interactive) (byte-code "! " [nil message "Continuing from more break" te-more-break-unwind] 3))

(defun te-more-break-help nil "\
Provide help on commands available in a terminal-emulator **MORE** break" (interactive) (byte-code "!!!" [nil message "Terminal-emulator more break help..." sit-for 0 with-electric-help (lambda nil (byte-code "!#!#!!!!‡" [terminal-more-break-map t nil princ "Terminal-emulator more break.

" format "Type \"%s\" (te-more-break-resume)
" where-is-internal te-more-break-resume documentation substitute-command-keys "\\{terminal-more-break-map}
" "Any other key is passed through to the program
running under the terminal emulator and disables more processing until
all pending output has been dealt with."] 9))] 4))

(defun te-more-break-advance-one-line nil "\
Allow one more line of text to be output before doing another more break." (interactive) (byte-code "‰ " [te-more-count nil 1 te-more-break-unwind] 2))

(defun te-more-break-flush-pending-output nil "\
Discard any output which has been received by the terminal emulator but
not yet proceesed and then proceed from the more break." (interactive) (byte-code "  " [nil te-more-break-unwind te-flush-pending-output] 3))

(defun te-flush-pending-output nil "\
Discard any as-yet-unprocessed output which has been received by
the terminal emulator." (interactive) (byte-code "ˆA?!* 	\"	\"D !!)" [te-pending-output length nil message "(There is no output pending)" te-pending-output-length "Flushing %d chars of pending output" 0 format "
*** %d chars of pending output flushed ***
" te-update-pending-output-display te-process-output sit-for] 8))

(defun te-pass-through nil "\
Send the last character typed through the terminal-emulator
without any interpretation" (interactive) (byte-code "Ĉ	=!'
\"\"!" [last-input-char terminal-escape-char terminal-more-processing te-pending-output nil te-process t call-interactively te-escape te-set-more-count send-string make-string 1 te-process-output] 7))

(defun te-set-window-start nil (byte-code "p!!?@	`eZ
T\"Y$e\"@	d`Z
T\"#\"@*" [w h te-width t nil get-buffer-window window-height / set-window-start - * -1] 12))

(defun te-pending-output-length nil (byte-code "	@	A

A*" [length te-pending-output tem] 3))

(defun te-more-break nil (byte-code "!!`! !!
\"  !p=H!md	?d!``
\\\"	c)!!\"" [t te-more-old-point te-more-old-local-map terminal-more-break-map te-more-old-filter te-process te-more-old-mode-line-format mode-line-format mode-line-buffer-identification terminal-more-break-insertion te-width te-set-more-count make-local-variable current-local-map use-local-map process-filter "--   **MORE**  " "%-" set-process-filter (lambda (process string) (byte-code "!q	
C\") " [process te-pending-output string process-buffer nconc te-update-pending-output-display] 4)) te-update-pending-output-display window-buffer selected-window message "More break " forward-char 1 delete-region run-hooks terminal-more-break-hook sit-for 0 throw te-process-output] 20))

(defun te-more-break-unwind nil (byte-code "!	
 !m66!``G\\\"	\"blj
 ))!" [te-more-old-local-map te-process te-more-old-filter te-more-old-point mode-line-format te-more-old-mode-line-format buffer-read-only nil terminal-more-break-insertion te-width te-more-count t use-local-map set-process-filter set-buffer-modified-p buffer-modified-p forward-char 1 delete-region insert-char 32 259259 te-newline te-process-output] 10))

(defun te-set-more-count (newline) (byte-code "`eZ	T\"
TURSU*R\"W8ZVCZRVNǂOZ)" [line te-width newline te-height te-more-count t / 2 10 1] 5))

(defun te-newline nil "\
Move down a line, optionally do more processing, perhaps wrap/scroll,
move to start of new line, clear to end of line." (byte-code " ?	SW!	= m>ee\\\"eb;!dbcH!``\\\"\"  " [terminal-more-processing te-more-count t te-width terminal-scrolling end-of-line 0 te-set-more-count te-more-break delete-region delete-char 1 10 forward-char insert-char 32 beginning-of-line te-set-window-start] 11))

(defun te-down-vertically-or-scroll nil "\
Move down a line vertically, or scroll at bottom." (byte-code "i m'ee	\\\"eb!dbc	\" *!!) " [column te-width end-of-line delete-region delete-char 1 10 insert-char 32 beginning-of-line forward-line move-to-column te-set-window-start] 9))

(defun te-move-to-position nil (byte-code " Z Z	
T\"#b*ʉ" [y x te-width te-height nil te-more-count te-get-char 32 + * -1] 8))

(defun te-clear-rest-of-line nil (byte-code "` `Z``\\\"[\"))" [n end-of-line delete-region insert-char 32] 5))

(defun te-clear-rest-of-screen nil (byte-code "  m?\"! `Z`\"\")" [te-width te-clear-rest-of-line end-of-line forward-char 1 delete-region insert-char 32] 9))

(defun te-clear-screen nil (byte-code " 	WT
\"c)dSd\"ebʉ" [i te-height te-width te-more-count erase-buffer 0 insert-char 32 10 delete-region -1] 5))

(defun te-insert-lines nil (byte-code "n?	Q
`eZT\"# Z	^dT\"Zd\"`d=2c
	=Kc3+)ɉ" [nil line te-height te-width n i te-more-count - / -1 te-get-char 32 0 delete-region * 10 insert-char] 9))

(defun te-delete-lines nil (byte-code "n?	M
`eZT\"# Z	^``T\"\\d^\"db
	=Gc/)+ɉ" [nil line te-height te-width n i te-more-count - / -1 te-get-char 32 0 delete-region * insert-char 10] 9))

(defun te-beginning-of-line nil (byte-code " " [beginning-of-line] 2))

(defun te-backward-char nil (byte-code "n?!" [backward-char 1] 2))

(defun te-forward-char nil (byte-code "l?!" [forward-char 1] 2))

(defun te-delete nil (byte-code "n`S`\"c!" [nil delete-region 32 forward-char -1] 3))

(defun te-beep nil (byte-code " " [beep] 2))

(defun te-insert-spaces nil (byte-code "` Z `Z^	X‚$	[!b	\"b*" [p n nil te-get-char 32 end-of-line 0 delete-char insert-char] 6))

(defun te-delete-char nil (byte-code "` Z `Z^	X‚#	\"b	!b*" [p n nil te-get-char 32 end-of-line 0 insert-char delete-char] 5))

(defun te-losing-unix nil (byte-code "" [nil] 1))

(defun te-output-tab nil (byte-code "` `Z	\"Z `Z^
\\b+" [p x l beginning-of-line 8 logand 7 end-of-line] 6))

(defun te-filter (process string) (byte-code "p
Ɏ!qb
qdbc!qC\" 	p !=!)!q`)*" [obuf m meta-flag process te-saved-point te-log-buffer nil string te-pending-output ((byte-code "q" [obuf] 1)) process-buffer bufferp buffer-name nconc te-update-pending-output-display te-process-output window-buffer selected-window] 12))

(defun te-process-output (preemptable) (byte-code "ō 

*" [preemptable t s w te-process-output (byte-code "AP
HT
Wl|=p
\"ABBvS\" 
G	!
c` 	`
b	=``	Z\\\"=	

G=	AAB	\"	ZT!)=
=/ \"A,ނ8
L L\"
." [buffer-read-only nil string ostring start char matchpos te-pending-output t end preemptable 0 te-update-pending-output-display setcar 31 255 make-string 1 te-newline delete-char te-redisplay-if-necessary string-match "[--]" end-of-line delete-region funcall 16 assq te-get-char ((61 . te-move-to-position) (99 . te-clear-rest-of-line) (67 . te-clear-rest-of-screen) (15 . te-insert-lines) (11 . te-delete-lines) (1 . te-beginning-of-line) (2 . te-backward-char) (100 . te-delete-char) (95 . te-insert-spaces) (6 . te-forward-char) (7 . te-beep) (10 . te-down-vertically-or-scroll) (12 . te-clear-screen)) te-losing-unix ((10 . te-newline) (127 . te-delete) (13 . te-beginning-of-line) (7 . te-beep) (8 . te-backward-char) (9 . te-output-tab)) input-pending-p throw te-process-output] 23) selected-window next-window window-buffer set-window-point] 7))

(defun te-get-char nil (byte-code "A,@A@
	H	T
G=#AAB'	\"*/ƍ" [te-pending-output start string 0 setcar char (byte-code "	!Î	\"	!))" [filter te-process process-filter ((byte-code "	\"" [te-process filter set-process-filter] 3)) set-process-filter (lambda (p s) (byte-code "G=DH\"" [s te-pending-output 1 throw char 0] 4)) accept-process-output] 4)] 4))

(defun te-redisplay-if-necessary (length) (byte-code "	ZX\"p !=\" \" !
" [te-redisplay-count length terminal-redisplay-interval 0 window-buffer selected-window waiting-for-user-input-p te-update-pending-output-display sit-for] 7))

(defun te-update-pending-output-display nil (byte-code "A?É' 
\\\"\") !" [te-pending-output te-pending-output-info length "" te-pending-output-length 1500 format "(%dK chars output pending) " / 512 1024 set-buffer-modified-p buffer-modified-p] 6))

(defun te-sentinel (process message) (byte-code "!=D!!?Dp!qĉ db  
!=: Cdb!)" [process t b buffer-read-only nil message process-status run buffer-name process-buffer fundamental-mode delete-blank-lines delete-horizontal-space insert "
" "*******
" waiting-for-user-input-p recenter -1] 12))

(defvar te-stty-string "stty -nl new dec echo" "\
Command string (to be interpreted by \"sh\") which sets the modes
of the virtual terminal to be appropriate for interactive use.")

(defvar explicit-shell-file-name nil "\
*If non-nil, is file name to use for explicitly requested inferior shell.")

(defun terminal-emulator (buffer program args &optional width height) "\
Under a display-terminal emulator in BUFFER, run PROGRAM on arguments ARGS.
ARGS is a list of argument-strings.  Remaining arguments are WIDTH and HEIGHT.
BUFFER's contents are made an image of the display generated by that program,
and any input typed when BUFFER is the current Emacs buffer is sent to that
program an keyboard input.

Interactively, BUFFER defaults to \"*terminal*\" and PROGRAM and ARGS
are parsed from an input-string using your usual shell.
WIDTH and HEIGHT are determined from the size of the current window
-- WIDTH will be one less than the window's width, HEIGHT will be its height.

To switch buffers and leave the emulator, or to give commands
to the emulator itself (as opposed to the program running under it),
type Control-^.  The following character is an emulator command.
Type Control-^ twice to send it to the subprogram.
This escape character may be changed using the variable `terminal-escape-char'.

`Meta' characters may not currently be sent through the terminal emulator.

Here is a list of some of the variables which control the behaviour
of the emulator -- see their documentation for more information:
terminal-escape-char, terminal-scrolling, terminal-more-processing,

This function calls the value of terminal-mode-hook if that exists
and is non-nil after the terminal buffer has been set up and the
subprocess started.

Presently with `termcap' only; if somebody sends us code to make this
work with `terminfo' we will try to use it." (interactive (byte-code "!q!??!=? p#!!)
7!7!7	\"!\"L	DO!*!B" [te-process default-s explicit-shell-file-name s get-buffer-create "*terminal*" buffer-name boundp process-status run generate-new-buffer append getenv "ESHELL" "SHELL" "/bin/sh" read-string format "Run program in emulator: (default %s) " equal "" nil te-parse-program-and-args] 14)) (byte-code "ˈ!
? !Z?# !Z 
 )p!n!\"!c!j!\"G)又!!!
!###ω!!" [te-process default-s explicit-shell-file-name s buffer width height te-width te-height mode-line-buffer-identification buffer-read-only nil process terminal-escape-char terminal-escape-map t inhibit-quit terminal-map switch-to-buffer window-width selected-window 1 window-height terminal-mode format "Emacs terminal %dx%d: %%b  " te-pending-output-info te-clear-screen get-buffer-process y-or-n-p "Kill process %s? " process-name delete-process error "Process %s not killed" err (byte-code "	

B#%%\"\")" [termcap te-width te-height terminal-scrolling te-process program args te-stty-string exec-directory concat format "emacs-virtual:co#%d:li#%d:%s" "" "ns:" "cm=^p=%+ %+ :cr=^p^a:le=^p^b:nd=^p^f:" "nw=^j:ce=^pc:cd=^pC:cl=^p^l:bl=^p^g:" "IC=^p_%+ :DC=^pd%+ :AL=^p^o%+ :DL=^p^k%+ :" "LP:NF:" "ic=^p_!:dc=^pd!:al=^p^o!:dl=^p^k!:ho=^p=  :" "im=:ei=:dm=:ed=:mi:do=^p^j:nl=^p^j:bs:" fboundp start-subprocess "terminal-emulator" channel-type terminal filter te-filter buffer sentinel te-sentinel modify-environment "TERM" "emacs-virtual" "TERMCAP" start-process "/bin/sh" "-c" "%s; exec %s TERM=emacs-virtual %s %s" te-quote-arg-for-sh "env" "TERMCAP=" mapconcat " " set-process-filter set-process-sentinel] 20) ((error (byte-code " @A\"" [err fundamental-mode signal] 4))) default-value meta-flag message "Note:  Meta key disabled due to maybe-eventually-reparable braindamage" sit-for "Entering emacs terminal-emulator...  Type %s %s for help" single-key-description mapconcat where-is-internal te-escape-help " " use-local-map run-hooks terminal-mode-hook] 28))

(defun te-parse-program-and-args (s) (byte-code "\"QA#1!O!G=-‰9O‰	B	!	@	AD*l\"\\!?iPDDlD" [s l nil p shell-file-name t string-match "\\`\\([a-zA-Z0-9-+=_.@/:]+[ 	]*\\)+\\'" 0 "\\([a-zA-Z0-9-+=_.@/:]+\\)\\([ 	]+\\)*" match-end 1 nreverse "[ 	]" file-exists-p "-c" "exec "] 11))

(put (quote terminal-mode) (quote mode-class) (quote special))

(defun terminal-mode nil "\
Set up variables for use f the terminal-emualtor.
One should not call this -- it is an internal function
of the terminal-emulator" (byte-code " p!ԉՉ։ĉĉ!!!!!!!!	!!!!C
!e!݉!!Ή
!߉!	!Ή" [major-mode mode-name mode-line-process buffer-read-only t truncate-lines terminal-escape-char terminal-scrolling terminal-more-processing terminal-redisplay-interval te-pending-output te-saved-point te-pending-output-info te-log-buffer nil te-more-count te-redisplay-count meta-flag kill-all-local-variables buffer-flush-undo terminal-mode "terminal" (": %s") make-local-variable default-value te-width te-height te-process 0 "" inhibit-quit -1] 23))

(defun te-quote-arg-for-sh (fuckme) (byte-code "\"`\"?!`GY*łB#8B
Q+" [fuckme t harder cretin stupid nil string-match "\\`[a-zA-Z0-9-+=_.@/:]+\\'" "[$]" prin1-to-string "" 0 "[\"\\$]" "\\" "\""] 11))