comint.el 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. ;;; -*-Emacs-Lisp-*- General command interpreter in a window stuff
  2. ;;; Copyright Olin Shivers (1988).
  3. ;;; Please imagine a long, tedious, legalistic 5-page gnu-style copyright
  4. ;;; notice appearing here to the effect that you may use this code any
  5. ;;; way you like, as long as you don't charge money for it, remove this
  6. ;;; notice, or hold me liable for its results.
  7. ;;; The changelog is at the end of this file.
  8. ;;; Please send me bug reports, bug fixes, and extensions, so that I can
  9. ;;; merge them into the master source.
  10. ;;; - Olin Shivers (shivers@cs.cmu.edu)
  11. ;;; This hopefully generalises shell mode, lisp mode, tea mode, soar mode,...
  12. ;;; This file defines a general command-interpreter-in-a-buffer package
  13. ;;; (comint mode). The idea is that you can build specific process-in-a-buffer
  14. ;;; modes on top of comint mode -- e.g., lisp, shell, scheme, T, soar, ....
  15. ;;; This way, all these specific packages share a common base functionality,
  16. ;;; and a common set of bindings, which makes them easier to use (and
  17. ;;; saves code, implementation time, etc., etc.).
  18. ;;; Several packages are already defined using comint mode:
  19. ;;; - cmushell.el defines a shell-in-a-buffer mode.
  20. ;;; - cmulisp.el defines a simple lisp-in-a-buffer mode.
  21. ;;; Cmushell and cmulisp mode are similar to, and intended to replace,
  22. ;;; their counterparts in the standard gnu emacs release (in shell.el).
  23. ;;; These replacements are more featureful, robust, and uniform than the
  24. ;;; released versions. The key bindings in lisp mode are also more compatible
  25. ;;; with the bindings of Hemlock and Zwei (the Lisp Machine emacs).
  26. ;;;
  27. ;;; - The file cmuscheme.el defines a scheme-in-a-buffer mode.
  28. ;;; - The file tea.el tunes scheme and inferior-scheme modes for T.
  29. ;;; - The file soar.el tunes lisp and inferior-lisp modes for Soar.
  30. ;;; - cmutex.el defines tex and latex modes that invoke tex, latex, bibtex,
  31. ;;; previewers, and printers from within emacs.
  32. ;;; - background.el allows csh-like job control inside emacs.
  33. ;;; It is pretty easy to make new derived modes for other processes.
  34. ;;; For documentation on the functionality provided by comint mode, and
  35. ;;; the hooks available for customising it, see the comments below.
  36. ;;; For further information on the standard derived modes (shell,
  37. ;;; inferior-lisp, inferior-scheme, ...), see the relevant source files.
  38. ;;; For hints on converting existing process modes (e.g., tex-mode,
  39. ;;; background, dbx, gdb, kermit, prolog, telnet) to use comint-mode
  40. ;;; instead of shell-mode, see the notes at the end of this file.
  41. (provide 'comint)
  42. (defconst comint-version "2.02")
  43. ;;; Brief Command Documentation:
  44. ;;;============================================================================
  45. ;;; Comint Mode Commands: (common to all derived modes, like cmushell & cmulisp
  46. ;;; mode)
  47. ;;;
  48. ;;; m-p comint-previous-input Cycle backwards in input history
  49. ;;; m-n comint-next-input Cycle forwards
  50. ;;; m-s comint-previous-similar-input Previous similar input
  51. ;;; c-c r comint-previous-input-matching Search backwards in input history
  52. ;;; return comint-send-input
  53. ;;; c-a comint-bol Beginning of line; skip prompt.
  54. ;;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff.
  55. ;;; c-c c-u comint-kill-input ^u
  56. ;;; c-c c-w backward-kill-word ^w
  57. ;;; c-c c-c comint-interrupt-subjob ^c
  58. ;;; c-c c-z comint-stop-subjob ^z
  59. ;;; c-c c-\ comint-quit-subjob ^\
  60. ;;; c-c c-o comint-kill-output Delete last batch of process output
  61. ;;; c-c c-r comint-show-output Show last batch of process output
  62. ;;;
  63. ;;; Not bound by default in comint-mode
  64. ;;; send-invisible Read a line w/o echo, and send to proc
  65. ;;; (These are bound in shell-mode)
  66. ;;; comint-dynamic-complete Complete filename at point.
  67. ;;; comint-dynamic-list-completions List completions in help buffer.
  68. ;;; comint-replace-by-expanded-filename Expand and complete filename at point;
  69. ;;; replace with expanded/completed name.
  70. ;;; comint-kill-subjob No mercy.
  71. ;;; comint-continue-subjob Send CONT signal to buffer's process
  72. ;;; group. Useful if you accidentally
  73. ;;; suspend your process (with C-c C-z).
  74. ;;;
  75. ;;; Bound for RMS -- I prefer the input history stuff, but you might like 'em.
  76. ;;; m-P comint-msearch-input Search backwards for prompt
  77. ;;; m-N comint-psearch-input Search forwards for prompt
  78. ;;; C-cR comint-msearch-input-matching Search backwards for prompt & string
  79. ;;; comint-mode-hook is the comint mode hook. Basically for your keybindings.
  80. ;;; comint-load-hook is run after loading in this package.
  81. ;;; Buffer Local Variables:
  82. ;;;============================================================================
  83. ;;; Comint mode buffer local variables:
  84. ;;; comint-prompt-regexp - string comint-bol uses to match prompt.
  85. ;;; comint-last-input-end - marker For comint-kill-output command
  86. ;;; input-ring-size - integer For the input history
  87. ;;; input-ring - ring mechanism
  88. ;;; input-ring-index - marker ...
  89. ;;; comint-last-input-match - string ...
  90. ;;; comint-get-old-input - function Hooks for specific
  91. ;;; comint-input-sentinel - function process-in-a-buffer
  92. ;;; comint-input-filter - function modes.
  93. ;;; comint-input-send - function
  94. ;;; comint-eol-on-send - boolean
  95. (defvar comint-prompt-regexp "^"
  96. "Regexp to recognise prompts in the inferior process.
  97. Defaults to \"^\", the null string at BOL.
  98. Good choices:
  99. Canonical Lisp: \"^[^> ]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp)
  100. Lucid Common Lisp: \"^\\(>\\|\\(->\\)+\\) *\"
  101. franz: \"^\\(->\\|<[0-9]*>:\\) *\"
  102. kcl: \"^>+ *\"
  103. shell: \"^[^#$%>]*[#$%>] *\"
  104. T: \"^>+ *\"
  105. This is a good thing to set in mode hooks.")
  106. (defvar input-ring-size 30
  107. "Size of input history ring.")
  108. ;;; Here are the per-interpreter hooks.
  109. (defvar comint-get-old-input (function comint-get-old-input-default)
  110. "Function that submits old text in comint mode.
  111. This function is called when return is typed while the point is in old text.
  112. It returns the text to be submitted as process input. The default is
  113. comint-get-old-input-default, which grabs the current line, and strips off
  114. leading text matching comint-prompt-regexp")
  115. (defvar comint-input-sentinel (function ignore)
  116. "Called on each input submitted to comint mode process by comint-send-input.
  117. Thus it can, for instance, track cd/pushd/popd commands issued to the csh.")
  118. (defvar comint-input-filter
  119. (function (lambda (str) (not (string-match "\\`\\s *\\'" str))))
  120. "Predicate for filtering additions to input history.
  121. Only inputs answering true to this function are saved on the input
  122. history list. Default is to save anything that isn't all whitespace")
  123. (defvar comint-input-sender (function comint-simple-send)
  124. "Function to actually send to PROCESS the STRING submitted by user.
  125. Usually this is just 'comint-simple-send, but if your mode needs to
  126. massage the input string, this is your hook. This is called from
  127. the user command comint-send-input. comint-simple-send just sends
  128. the string plus a newline.")
  129. (defvar comint-eol-on-send 'T
  130. "If non-nil, then jump to the end of the line before sending input to process.
  131. See COMINT-SEND-INPUT")
  132. (defvar comint-mode-hook '()
  133. "Called upon entry into comint-mode
  134. This is run before the process is cranked up.")
  135. (defvar comint-exec-hook '()
  136. "Called each time a process is exec'd by comint-exec.
  137. This is called after the process is cranked up. It is useful for things that
  138. must be done each time a process is executed in a comint-mode buffer (e.g.,
  139. (process-kill-without-query)). In contrast, the comint-mode-hook is only
  140. executed once when the buffer is created.")
  141. (defvar comint-mode-map nil)
  142. (defun comint-mode ()
  143. "Major mode for interacting with an inferior interpreter.
  144. Interpreter name is same as buffer name, sans the asterisks.
  145. Return at end of buffer sends line as input.
  146. Return not at end copies rest of line to end and sends it.
  147. Setting mode variable comint-eol-on-send means jump to the end of the line
  148. before submitting new input.
  149. This mode is typically customised to create inferior-lisp-mode,
  150. shell-mode, etc.. This can be done by setting the hooks
  151. comint-input-sentinel, comint-input-filter, comint-input-sender and
  152. comint-get-old-input to appropriate functions, and the variable
  153. comint-prompt-regexp to the appropriate regular expression.
  154. An input history is maintained of size input-ring-size, and
  155. can be accessed with the commands comint-next-input [\\[comint-next-input]] and
  156. comint-previous-input [\\[comint-previous-input]]. Commands not keybound by
  157. default are send-invisible, comint-dynamic-complete, and
  158. comint-list-dynamic-completions.
  159. If you accidentally suspend your process, use \\[comint-continue-subjob]
  160. to continue it.
  161. \\{comint-mode-map}
  162. Entry to this mode runs the hooks on comint-mode-hook"
  163. (interactive)
  164. (let ((old-ring (and (assq 'input-ring (buffer-local-variables))
  165. (boundp 'input-ring)
  166. input-ring))
  167. (old-ptyp comint-ptyp)) ; preserve across local var kill. gross.
  168. ; (kill-all-local-variables) ; Removed 1/15/90 Olin
  169. (setq major-mode 'comint-mode)
  170. (setq mode-name "Comint")
  171. (setq mode-line-process '(": %s"))
  172. (use-local-map comint-mode-map)
  173. (make-local-variable 'comint-last-input-end)
  174. (setq comint-last-input-end (make-marker))
  175. (make-local-variable 'comint-last-input-match)
  176. (setq comint-last-input-match "")
  177. (make-local-variable 'comint-prompt-regexp) ; Don't set; default
  178. (make-local-variable 'input-ring-size) ; ...to global val.
  179. (make-local-variable 'input-ring)
  180. (make-local-variable 'input-ring-index)
  181. (setq input-ring-index 0)
  182. (make-local-variable 'comint-get-old-input)
  183. (make-local-variable 'comint-input-sentinel)
  184. (make-local-variable 'comint-input-filter)
  185. (make-local-variable 'comint-input-sender)
  186. (make-local-variable 'comint-eol-on-send)
  187. (make-local-variable 'comint-ptyp)
  188. (setq comint-ptyp old-ptyp)
  189. (make-local-variable 'comint-exec-hook)
  190. (run-hooks 'comint-mode-hook)
  191. ;Do this after the hook so the user can mung INPUT-RING-SIZE w/his hook.
  192. ;The test is so we don't lose history if we run comint-mode twice in
  193. ;a buffer.
  194. (setq input-ring (if (ring-p old-ring) old-ring
  195. (make-ring input-ring-size)))))
  196. ;;; The old-ptyp stuff above is because we have to preserve the value of
  197. ;;; comint-ptyp across calls to comint-mode, in spite of the
  198. ;;; kill-all-local-variables that it does. Blech. Hopefully, this will all
  199. ;;; go away when a later release fixes the signalling bug.
  200. ;;; (Later: I removed the kill-all-local-variables, but have left this
  201. ;;; other code in place just in case I reverse myself.)
  202. (if comint-mode-map
  203. nil
  204. (setq comint-mode-map (make-sparse-keymap))
  205. (define-key comint-mode-map "\ep" 'comint-previous-input)
  206. (define-key comint-mode-map "\en" 'comint-next-input)
  207. (define-key comint-mode-map "\es" 'comint-previous-similar-input)
  208. (define-key comint-mode-map "\C-m" 'comint-send-input)
  209. (define-key comint-mode-map "\C-d" 'comint-delchar-or-maybe-eof)
  210. (define-key comint-mode-map "\C-a" 'comint-bol)
  211. (define-key comint-mode-map "\C-c\C-u" 'comint-kill-input)
  212. (define-key comint-mode-map "\C-c\C-w" 'backward-kill-word)
  213. (define-key comint-mode-map "\C-c\C-c" 'comint-interrupt-subjob)
  214. (define-key comint-mode-map "\C-c\C-z" 'comint-stop-subjob)
  215. (define-key comint-mode-map "\C-c\C-\\" 'comint-quit-subjob)
  216. (define-key comint-mode-map "\C-c\C-o" 'comint-kill-output)
  217. (define-key comint-mode-map "\C-cr" 'comint-previous-input-matching)
  218. (define-key comint-mode-map "\C-c\C-r" 'comint-show-output)
  219. ;;; prompt-search commands commented out 3/90 -Olin
  220. ; (define-key comint-mode-map "\eP" 'comint-msearch-input)
  221. ; (define-key comint-mode-map "\eN" 'comint-psearch-input)
  222. ; (define-key comint-mode-map "\C-cR" 'comint-msearch-input-matching)
  223. )
  224. ;;; This function is used to make a full copy of the comint mode map,
  225. ;;; so that client modes won't interfere with each other. This function
  226. ;;; isn't necessary in emacs 18.5x, but we keep it around for 18.4x versions.
  227. (defun full-copy-sparse-keymap (km)
  228. "Recursively copy the sparse keymap KM"
  229. (cond ((consp km)
  230. (cons (full-copy-sparse-keymap (car km))
  231. (full-copy-sparse-keymap (cdr km))))
  232. (t km)))
  233. (defun comint-check-proc (buffer)
  234. "True if there is a process associated w/buffer BUFFER, and
  235. it is alive (status RUN or STOP). BUFFER can be either a buffer or the
  236. name of one"
  237. (let ((proc (get-buffer-process buffer)))
  238. (and proc (memq (process-status proc) '(run stop)))))
  239. ;;; Note that this guy, unlike shell.el's make-shell, barfs if you pass it ()
  240. ;;; for the second argument (program).
  241. (defun make-comint (name program &optional startfile &rest switches)
  242. (let ((buffer (get-buffer-create (concat "*" name "*"))))
  243. ;; If no process, or nuked process, crank up a new one and put buffer in
  244. ;; comint mode. Otherwise, leave buffer and existing process alone.
  245. (cond ((not (comint-check-proc buffer))
  246. (save-excursion
  247. (set-buffer buffer)
  248. (comint-mode)) ; Install local vars, mode, keymap, ...
  249. (comint-exec buffer name program startfile switches)))
  250. buffer))
  251. (defvar comint-ptyp t
  252. "True if communications via pty; false if by pipe. Buffer local.
  253. This is to work around a bug in emacs process signalling.")
  254. (defun comint-exec (buffer name command startfile switches)
  255. "Fires up a process in buffer for comint modes.
  256. Blasts any old process running in the buffer. Doesn't set the buffer mode.
  257. You can use this to cheaply run a series of processes in the same comint
  258. buffer. The hook comint-exec-hook is run after each exec."
  259. (save-excursion
  260. (set-buffer buffer)
  261. (let ((proc (get-buffer-process buffer))) ; Blast any old process.
  262. (if proc (delete-process proc)))
  263. ;; Crank up a new process
  264. (let ((proc (comint-exec-1 name buffer command switches)))
  265. (make-local-variable 'comint-ptyp)
  266. (setq comint-ptyp process-connection-type) ; T if pty, NIL if pipe.
  267. ;; Jump to the end, and set the process mark.
  268. (goto-char (point-max))
  269. (set-marker (process-mark proc) (point))
  270. ;; Feed it the startfile.
  271. (cond (startfile
  272. ;;This is guaranteed to wait long enough
  273. ;;but has bad results if the comint does not prompt at all
  274. ;; (while (= size (buffer-size))
  275. ;; (sleep-for 1))
  276. ;;I hope 1 second is enough!
  277. (sleep-for 1)
  278. (goto-char (point-max))
  279. (insert-file-contents startfile)
  280. (setq startfile (buffer-substring (point) (point-max)))
  281. (delete-region (point) (point-max))
  282. (comint-send-string proc startfile)))
  283. (run-hooks 'comint-exec-hook)
  284. buffer)))
  285. ;;; This auxiliary function cranks up the process for comint-exec in
  286. ;;; the appropriate environment. It is twice as long as it should be
  287. ;;; because emacs has two distinct mechanisms for manipulating the
  288. ;;; process environment, selected at compile time with the
  289. ;;; MAINTAIN-ENVIRONMENT #define. In one case, process-environment
  290. ;;; is bound; in the other it isn't.
  291. (defun comint-exec-1 (name buffer command switches)
  292. (if (boundp 'process-environment) ; Not a completely reliable test.
  293. (let ((process-environment
  294. (comint-update-env process-environment
  295. (list (format "TERMCAP=emacs:co#%d:tc=unknown"
  296. (screen-width))
  297. "TERM=emacs"
  298. "EMACS=t"))))
  299. (apply 'start-process name buffer command switches))
  300. (let ((tcapv (getenv "TERMCAP"))
  301. (termv (getenv "TERM"))
  302. (emv (getenv "EMACS")))
  303. (unwind-protect
  304. (progn (setenv "TERMCAP" (format "emacs:co#%d:tc=unknown"
  305. (screen-width)))
  306. (setenv "TERM" "emacs")
  307. (setenv "EMACS" "t")
  308. (apply 'start-process name buffer command switches))
  309. (setenv "TERMCAP" tcapv)
  310. (setenv "TERM" termv)
  311. (setenv "EMACS" emv)))))
  312. ;; This is just (append new old-env) that compresses out shadowed entries.
  313. ;; It's also pretty ugly, mostly due to elisp's horrible iteration structures.
  314. (defun comint-update-env (old-env new)
  315. (let ((ans (reverse new))
  316. (vars (mapcar (function (lambda (vv)
  317. (and (string-match "^[^=]*=" vv)
  318. (substring vv 0 (match-end 0)))))
  319. new)))
  320. (while old-env
  321. (let* ((vv (car old-env)) ; vv is var=value
  322. (var (and (string-match "^[^=]*=" vv)
  323. (substring vv 0 (match-end 0)))))
  324. (setq old-env (cdr old-env))
  325. (cond ((not (and var (comint-mem var vars)))
  326. (if var (setq var (cons var vars)))
  327. (setq ans (cons vv ans))))))
  328. (nreverse ans)))
  329. ;;; This should be in emacs, but it isn't.
  330. (defun comint-mem (item list &optional elt=)
  331. "Test to see if ITEM is equal to an item in LIST.
  332. Option comparison function ELT= defaults to equal."
  333. (let ((elt= (or elt= (function equal)))
  334. (done nil))
  335. (while (and list (not done))
  336. (if (funcall elt= item (car list))
  337. (setq done list)
  338. (setq list (cdr list))))
  339. done))
  340. ;;; Ring Code
  341. ;;;============================================================================
  342. ;;; This code defines a ring data structure. A ring is a
  343. ;;; (hd-index tl-index . vector)
  344. ;;; list. You can insert to, remove from, and rotate a ring. When the ring
  345. ;;; fills up, insertions cause the oldest elts to be quietly dropped.
  346. ;;;
  347. ;;; HEAD = index of the newest item on the ring.
  348. ;;; TAIL = index of the oldest item on the ring.
  349. ;;;
  350. ;;; These functions are used by the input history mechanism, but they can
  351. ;;; be used for other purposes as well.
  352. (defun ring-p (x)
  353. "T if X is a ring; NIL otherwise."
  354. (and (consp x) (integerp (car x))
  355. (consp (cdr x)) (integerp (car (cdr x)))
  356. (vectorp (cdr (cdr x)))))
  357. (defun make-ring (size)
  358. "Make a ring that can contain SIZE elts"
  359. (cons 1 (cons 0 (make-vector (+ size 1) nil))))
  360. (defun ring-plus1 (index veclen)
  361. "INDEX+1, with wraparound"
  362. (let ((new-index (+ index 1)))
  363. (if (= new-index veclen) 0 new-index)))
  364. (defun ring-minus1 (index veclen)
  365. "INDEX-1, with wraparound"
  366. (- (if (= 0 index) veclen index) 1))
  367. (defun ring-length (ring)
  368. "Number of elts in the ring."
  369. (let ((hd (car ring)) (tl (car (cdr ring))) (siz (length (cdr (cdr ring)))))
  370. (let ((len (if (<= hd tl) (+ 1 (- tl hd)) (+ 1 tl (- siz hd)))))
  371. (if (= len siz) 0 len))))
  372. (defun ring-empty-p (ring)
  373. (= 0 (ring-length ring)))
  374. (defun ring-insert (ring item)
  375. "Insert a new item onto the ring. If the ring is full, dump the oldest
  376. item to make room."
  377. (let* ((vec (cdr (cdr ring))) (len (length vec))
  378. (new-hd (ring-minus1 (car ring) len)))
  379. (setcar ring new-hd)
  380. (aset vec new-hd item)
  381. (if (ring-empty-p ring) ;overflow -- dump one off the tail.
  382. (setcar (cdr ring) (ring-minus1 (car (cdr ring)) len)))))
  383. (defun ring-remove (ring)
  384. "Remove the oldest item retained on the ring."
  385. (if (ring-empty-p ring) (error "Ring empty")
  386. (let ((tl (car (cdr ring))) (vec (cdr (cdr ring))))
  387. (set-car (cdr ring) (ring-minus1 tl (length vec)))
  388. (aref vec tl))))
  389. ;;; This isn't actually used in this package. I just threw it in in case
  390. ;;; someone else wanted it. If you want rotating-ring behavior on your history
  391. ;;; retrieval (analagous to kill ring behavior), this function is what you
  392. ;;; need. I should write the yank-input and yank-pop-input-or-kill to go with
  393. ;;; this, and not bind it to a key by default, so it would be available to
  394. ;;; people who want to bind it to a key. But who would want it? Blech.
  395. (defun ring-rotate (ring n)
  396. (if (not (= n 0))
  397. (if (ring-empty-p ring) ;Is this the right error check?
  398. (error "ring empty")
  399. (let ((hd (car ring)) (tl (car (cdr ring))) (vec (cdr (cdr ring))))
  400. (let ((len (length vec)))
  401. (while (> n 0)
  402. (setq tl (ring-plus1 tl len))
  403. (aset ring tl (aref ring hd))
  404. (setq hd (ring-plus1 hd len))
  405. (setq n (- n 1)))
  406. (while (< n 0)
  407. (setq hd (ring-minus1 hd len))
  408. (aset vec hd (aref vec tl))
  409. (setq tl (ring-minus1 tl len))
  410. (setq n (- n 1))))
  411. (set-car ring hd)
  412. (set-car (cdr ring) tl)))))
  413. (defun comint-mod (n m)
  414. "Returns N mod M. M is positive. Answer is guaranteed to be non-negative,
  415. and less than m."
  416. (let ((n (% n m)))
  417. (if (>= n 0) n
  418. (+ n
  419. (if (>= m 0) m (- m)))))) ; (abs m)
  420. (defun ring-ref (ring index)
  421. (let ((numelts (ring-length ring)))
  422. (if (= numelts 0) (error "indexed empty ring")
  423. (let* ((hd (car ring)) (tl (car (cdr ring))) (vec (cdr (cdr ring)))
  424. (index (comint-mod index numelts))
  425. (vec-index (comint-mod (+ index hd)
  426. (length vec))))
  427. (aref vec vec-index)))))
  428. ;;; Input history retrieval commands
  429. ;;; M-p -- previous input M-n -- next input
  430. ;;; C-c r -- previous input matching
  431. ;;; ===========================================================================
  432. (defun comint-previous-input (arg)
  433. "Cycle backwards through input history."
  434. (interactive "*p")
  435. (let ((len (ring-length input-ring)))
  436. (cond ((<= len 0)
  437. (message "Empty input ring")
  438. (ding))
  439. ((not (comint-after-pmark-p))
  440. (message "Not after process mark")
  441. (ding))
  442. (t
  443. (cond ((eq last-command 'comint-previous-input)
  444. (delete-region (mark) (point)))
  445. ((eq last-command 'comint-previous-similar-input)
  446. (delete-region
  447. (process-mark (get-buffer-process (current-buffer)))
  448. (point)))
  449. (t
  450. (setq input-ring-index
  451. (if (> arg 0) -1
  452. (if (< arg 0) 1 0)))
  453. (push-mark (point))))
  454. (setq input-ring-index (comint-mod (+ input-ring-index arg) len))
  455. (message "%d" (1+ input-ring-index))
  456. (insert (ring-ref input-ring input-ring-index))
  457. (setq this-command 'comint-previous-input)))))
  458. (defun comint-next-input (arg)
  459. "Cycle forwards through input history."
  460. (interactive "*p")
  461. (comint-previous-input (- arg)))
  462. (defvar comint-last-input-match ""
  463. "Last string searched for by comint input history search, for defaulting.
  464. Buffer local variable.")
  465. (defun comint-previous-input-matching (str)
  466. "Searches backwards through input history for substring match."
  467. (interactive (let* ((last-command last-command) ; preserve around r-f-m
  468. (s (read-from-minibuffer
  469. (format "Command substring (default %s): "
  470. comint-last-input-match))))
  471. (list (if (string= s "") comint-last-input-match s))))
  472. ; (interactive "sCommand substring: ")
  473. (setq comint-last-input-match str) ; update default
  474. (if (not (eq last-command 'comint-previous-input))
  475. (setq input-ring-index -1))
  476. (let ((str (regexp-quote str))
  477. (len (ring-length input-ring))
  478. (n (+ input-ring-index 1)))
  479. (while (and (< n len) (not (string-match str (ring-ref input-ring n))))
  480. (setq n (+ n 1)))
  481. (cond ((< n len)
  482. (comint-previous-input (- n input-ring-index)))
  483. (t (if (eq last-command 'comint-previous-input)
  484. (setq this-command 'comint-previous-input))
  485. (message "Not found.")
  486. (ding)))))
  487. ;;; These next three commands are alternatives to the input history commands
  488. ;;; -- comint-next-input, comint-previous-input and
  489. ;;; comint-previous-input-matching. They search through the process buffer
  490. ;;; text looking for occurrences of the prompt. Bound to M-P, M-N, and C-c R
  491. ;;; (uppercase P, N, and R) for now. Try'em out. Go with what you like...
  492. ;;; comint-msearch-input-matching prompts for a string, not a regexp.
  493. ;;; This could be considered to be the wrong thing. I decided to keep it
  494. ;;; simple, and not make the user worry about regexps. This, of course,
  495. ;;; limits functionality.
  496. ;;; These commands were deemed non-winning and have been commented out.
  497. ;;; Feel free to re-enable them if you like. -Olin 3/91
  498. ;(defun comint-psearch-input ()
  499. ; "Search forwards for next occurrence of prompt and skip to end of line.
  500. ;\(prompt is anything matching regexp comint-prompt-regexp)"
  501. ; (interactive)
  502. ; (if (re-search-forward comint-prompt-regexp (point-max) t)
  503. ; (end-of-line)
  504. ; (error "No occurrence of prompt found")))
  505. ;
  506. ;(defun comint-msearch-input ()
  507. ; "Search backwards for previous occurrence of prompt and skip to end of line.
  508. ;Search starts from beginning of current line."
  509. ; (interactive)
  510. ; (let ((p (save-excursion
  511. ; (beginning-of-line)
  512. ; (cond ((re-search-backward comint-prompt-regexp (point-min) t)
  513. ; (end-of-line)
  514. ; (point))
  515. ; (t nil)))))
  516. ; (if p (goto-char p)
  517. ; (error "No occurrence of prompt found"))))
  518. ;
  519. ;(defun comint-msearch-input-matching (str)
  520. ; "Search backwards for occurrence of prompt followed by STRING.
  521. ;STRING is prompted for, and is NOT a regular expression."
  522. ; (interactive (let ((s (read-from-minibuffer
  523. ; (format "Command (default %s): "
  524. ; comint-last-input-match))))
  525. ; (list (if (string= s "") comint-last-input-match s))))
  526. ;; (interactive "sCommand: ")
  527. ; (setq comint-last-input-match str) ; update default
  528. ; (let* ((r (concat comint-prompt-regexp (regexp-quote str)))
  529. ; (p (save-excursion
  530. ; (beginning-of-line)
  531. ; (cond ((re-search-backward r (point-min) t)
  532. ; (end-of-line)
  533. ; (point))
  534. ; (t nil)))))
  535. ; (if p (goto-char p)
  536. ; (error "No match"))))
  537. ;;;
  538. ;;; Similar input -- contributed by ccm and highly winning.
  539. ;;;
  540. ;;; Reenter input, removing back to the last insert point if it exists.
  541. ;;;
  542. (defvar comint-last-similar-string ""
  543. "The string last used in a similar string search.")
  544. (defun comint-previous-similar-input (arg)
  545. "Reenters the last input that matches the string typed so far. If repeated
  546. successively older inputs are reentered. If arg is 1, it will go back
  547. in the history, if -1 it will go forward."
  548. (interactive "p")
  549. (if (not (comint-after-pmark-p))
  550. (error "Not after process mark"))
  551. (if (not (eq last-command 'comint-previous-similar-input))
  552. (setq input-ring-index -1
  553. comint-last-similar-string
  554. (buffer-substring
  555. (process-mark (get-buffer-process (current-buffer)))
  556. (point))))
  557. (let* ((size (length comint-last-similar-string))
  558. (len (ring-length input-ring))
  559. (n (+ input-ring-index arg))
  560. entry)
  561. (while (and (< n len)
  562. (or (< (length (setq entry (ring-ref input-ring n))) size)
  563. (not (equal comint-last-similar-string
  564. (substring entry 0 size)))))
  565. (setq n (+ n arg)))
  566. (cond ((< n len)
  567. (setq input-ring-index n)
  568. (if (eq last-command 'comint-previous-similar-input)
  569. (delete-region (mark) (point)) ; repeat
  570. (push-mark (point))) ; 1st time
  571. (insert (substring entry size)))
  572. (t (message "Not found.") (ding) (sit-for 1)))
  573. (message "%d" (1+ input-ring-index))))
  574. (defun comint-send-input ()
  575. "Send input to process. After the process output mark, sends all text
  576. from the process mark to point as input to the process. Before the process
  577. output mark, calls value of variable comint-get-old-input to retrieve old
  578. input, copies it to the process mark, and sends it. A terminal newline is
  579. also inserted into the buffer and sent to the process. In either case, value
  580. of variable comint-input-sentinel is called on the input before sending it.
  581. The input is entered into the input history ring, if the value of variable
  582. comint-input-filter returns non-nil when called on the input.
  583. If variable comint-eol-on-send is non-nil, then point is moved to the end of
  584. line before sending the input.
  585. comint-get-old-input, comint-input-sentinel, and comint-input-filter are chosen
  586. according to the command interpreter running in the buffer. E.g.,
  587. If the interpreter is the csh,
  588. comint-get-old-input is the default: take the current line, discard any
  589. initial string matching regexp comint-prompt-regexp.
  590. comint-input-sentinel monitors input for \"cd\", \"pushd\", and \"popd\"
  591. commands. When it sees one, it cd's the buffer.
  592. comint-input-filter is the default: returns T if the input isn't all white
  593. space.
  594. If the comint is Lucid Common Lisp,
  595. comint-get-old-input snarfs the sexp ending at point.
  596. comint-input-sentinel does nothing.
  597. comint-input-filter returns NIL if the input matches input-filter-regexp,
  598. which matches (1) all whitespace (2) :a, :c, etc.
  599. Similarly for Soar, Scheme, etc.."
  600. (interactive)
  601. ;; Note that the input string does not include its terminal newline.
  602. (let ((proc (get-buffer-process (current-buffer))))
  603. (if (not proc) (error "Current buffer has no process")
  604. (let* ((pmark (process-mark proc))
  605. (pmark-val (marker-position pmark))
  606. (input (if (>= (point) pmark-val)
  607. (progn (if comint-eol-on-send (end-of-line))
  608. (buffer-substring pmark (point)))
  609. (let ((copy (funcall comint-get-old-input)))
  610. (goto-char pmark)
  611. (insert copy)
  612. copy))))
  613. (insert ?\n)
  614. (if (funcall comint-input-filter input) (ring-insert input-ring input))
  615. (funcall comint-input-sentinel input)
  616. (funcall comint-input-sender proc input)
  617. (set-marker (process-mark proc) (point))
  618. (set-marker comint-last-input-end (point))))))
  619. (defun comint-get-old-input-default ()
  620. "Default for comint-get-old-input: take the current line, and discard
  621. any initial text matching comint-prompt-regexp."
  622. (save-excursion
  623. (beginning-of-line)
  624. (comint-skip-prompt)
  625. (let ((beg (point)))
  626. (end-of-line)
  627. (buffer-substring beg (point)))))
  628. (defun comint-skip-prompt ()
  629. "Skip past the text matching regexp comint-prompt-regexp.
  630. If this takes us past the end of the current line, don't skip at all."
  631. (let ((eol (save-excursion (end-of-line) (point))))
  632. (if (and (looking-at comint-prompt-regexp)
  633. (<= (match-end 0) eol))
  634. (goto-char (match-end 0)))))
  635. (defun comint-after-pmark-p ()
  636. "Is point after the process output marker?"
  637. ;; Since output could come into the buffer after we looked at the point
  638. ;; but before we looked at the process marker's value, we explicitly
  639. ;; serialise. This is just because I don't know whether or not emacs
  640. ;; services input during execution of lisp commands.
  641. (let ((proc-pos (marker-position
  642. (process-mark (get-buffer-process (current-buffer))))))
  643. (<= proc-pos (point))))
  644. (defun comint-simple-send (proc string)
  645. "Default function for sending to PROC input STRING.
  646. This just sends STRING plus a newline. To override this,
  647. set the hook COMINT-INPUT-SENDER."
  648. (comint-send-string proc string)
  649. (comint-send-string proc "\n"))
  650. (defun comint-bol (arg)
  651. "Goes to the beginning of line, then skips past the prompt, if any.
  652. If a prefix argument is given (\\[universal-argument]), then no prompt skip
  653. -- go straight to column 0.
  654. The prompt skip is done by skipping text matching the regular expression
  655. comint-prompt-regexp, a buffer local variable.
  656. If you don't like this command, reset c-a to beginning-of-line
  657. in your hook, comint-mode-hook."
  658. (interactive "P")
  659. (beginning-of-line)
  660. (if (null arg) (comint-skip-prompt)))
  661. ;;; These two functions are for entering text you don't want echoed or
  662. ;;; saved -- typically passwords to ftp, telnet, or somesuch.
  663. ;;; Just enter m-x send-invisible and type in your line.
  664. (defun comint-read-noecho (prompt)
  665. "Prompt the user with argument PROMPT. Read a single line of text
  666. without echoing, and return it. Note that the keystrokes comprising
  667. the text can still be recovered (temporarily) with \\[view-lossage]. This
  668. may be a security bug for some applications."
  669. (let ((echo-keystrokes 0)
  670. (answ "")
  671. tem)
  672. (if (and (stringp prompt) (not (string= (message prompt) "")))
  673. (message prompt))
  674. (while (not(or (= (setq tem (read-char)) ?\^m)
  675. (= tem ?\n)))
  676. (setq answ (concat answ (char-to-string tem))))
  677. (message "")
  678. answ))
  679. (defun send-invisible (str)
  680. "Read a string without echoing, and send it to the process running
  681. in the current buffer. A new-line is additionally sent. String is not
  682. saved on comint input history list.
  683. Security bug: your string can still be temporarily recovered with
  684. \\[view-lossage]."
  685. ; (interactive (list (comint-read-noecho "Enter non-echoed text")))
  686. (interactive "P") ; Defeat snooping via C-x esc
  687. (let ((proc (get-buffer-process (current-buffer))))
  688. (if (not proc) (error "Current buffer has no process")
  689. (comint-send-string proc
  690. (if (stringp str) str
  691. (comint-read-noecho "Enter non-echoed text")))
  692. (comint-send-string proc "\n"))))
  693. ;;; Low-level process communication
  694. (defvar comint-input-chunk-size 512
  695. "*Long inputs send to comint processes are broken up into chunks of this size.
  696. If your process is choking on big inputs, try lowering the value.")
  697. (defun comint-send-string (proc str)
  698. "Send PROCESS the contents of STRING as input.
  699. This is equivalent to process-send-string, except that long input strings
  700. are broken up into chunks of size comint-input-chunk-size. Processes
  701. are given a chance to output between chunks. This can help prevent processes
  702. from hanging when you send them long inputs on some OS's."
  703. (let* ((len (length str))
  704. (i (min len comint-input-chunk-size)))
  705. (process-send-string proc (substring str 0 i))
  706. (while (< i len)
  707. (let ((next-i (+ i comint-input-chunk-size)))
  708. (accept-process-output)
  709. (process-send-string proc (substring str i (min len next-i)))
  710. (setq i next-i)))))
  711. (defun comint-send-region (proc start end)
  712. "Sends to PROC the region delimited by START and END.
  713. This is a replacement for process-send-region that tries to keep
  714. your process from hanging on long inputs. See comint-send-string."
  715. (comint-send-string proc (buffer-substring start end)))
  716. ;;; Random input hackage
  717. (defun comint-kill-output ()
  718. "Kill all output from interpreter since last input."
  719. (interactive)
  720. (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
  721. (kill-region comint-last-input-end pmark)
  722. (goto-char pmark)
  723. (insert "*** output flushed ***\n")
  724. (set-marker pmark (point))))
  725. (defun comint-show-output ()
  726. "Display start of this batch of interpreter output at top of window.
  727. Also put cursor there."
  728. (interactive)
  729. (goto-char comint-last-input-end)
  730. (backward-char)
  731. (beginning-of-line)
  732. (set-window-start (selected-window) (point))
  733. (end-of-line))
  734. (defun comint-interrupt-subjob ()
  735. "Interrupt the current subjob."
  736. (interactive)
  737. (interrupt-process nil comint-ptyp))
  738. (defun comint-kill-subjob ()
  739. "Send kill signal to the current subjob."
  740. (interactive)
  741. (kill-process nil comint-ptyp))
  742. (defun comint-quit-subjob ()
  743. "Send quit signal to the current subjob."
  744. (interactive)
  745. (quit-process nil comint-ptyp))
  746. (defun comint-stop-subjob ()
  747. "Stop the current subjob.
  748. WARNING: if there is no current subjob, you can end up suspending
  749. the top-level process running in the buffer. If you accidentally do
  750. this, use \\[comint-continue-subjob] to resume the process. (This
  751. is not a problem with most shells, since they ignore this signal.)"
  752. (interactive)
  753. (stop-process nil comint-ptyp))
  754. (defun comint-continue-subjob ()
  755. "Send CONT signal to process buffer's process group.
  756. Useful if you accidentally suspend the top-level process."
  757. (interactive)
  758. (continue-process nil comint-ptyp))
  759. (defun comint-kill-input ()
  760. "Kill all text from last stuff output by interpreter to point."
  761. (interactive)
  762. (let* ((pmark (process-mark (get-buffer-process (current-buffer))))
  763. (p-pos (marker-position pmark)))
  764. (if (> (point) p-pos)
  765. (kill-region pmark (point)))))
  766. (defun comint-delchar-or-maybe-eof (arg)
  767. "Delete ARG characters forward, or send an EOF to process if at end of buffer."
  768. (interactive "p")
  769. (if (eobp)
  770. (process-send-eof)
  771. (delete-char arg)))
  772. ;;; Support for source-file processing commands.
  773. ;;;============================================================================
  774. ;;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have
  775. ;;; commands that process files of source text (e.g. loading or compiling
  776. ;;; files). So the corresponding process-in-a-buffer modes have commands
  777. ;;; for doing this (e.g., lisp-load-file). The functions below are useful
  778. ;;; for defining these commands.
  779. ;;;
  780. ;;; Alas, these guys don't do exactly the right thing for Lisp, Scheme
  781. ;;; and Soar, in that they don't know anything about file extensions.
  782. ;;; So the compile/load interface gets the wrong default occasionally.
  783. ;;; The load-file/compile-file default mechanism could be smarter -- it
  784. ;;; doesn't know about the relationship between filename extensions and
  785. ;;; whether the file is source or executable. If you compile foo.lisp
  786. ;;; with compile-file, then the next load-file should use foo.bin for
  787. ;;; the default, not foo.lisp. This is tricky to do right, particularly
  788. ;;; because the extension for executable files varies so much (.o, .bin,
  789. ;;; .lbin, .mo, .vo, .ao, ...).
  790. ;;; COMINT-SOURCE-DEFAULT -- determines defaults for source-file processing
  791. ;;; commands.
  792. ;;;
  793. ;;; COMINT-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you
  794. ;;; want to save the buffer before issuing any process requests to the command
  795. ;;; interpreter.
  796. ;;;
  797. ;;; COMINT-GET-SOURCE -- used by the source-file processing commands to prompt
  798. ;;; for the file to process.
  799. ;;; (COMINT-SOURCE-DEFAULT previous-dir/file source-modes)
  800. ;;;============================================================================
  801. ;;; This function computes the defaults for the load-file and compile-file
  802. ;;; commands for tea, soar, cmulisp, and cmuscheme modes.
  803. ;;;
  804. ;;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last
  805. ;;; source-file processing command. NIL if there hasn't been one yet.
  806. ;;; - SOURCE-MODES is a list used to determine what buffers contain source
  807. ;;; files: if the major mode of the buffer is in SOURCE-MODES, it's source.
  808. ;;; Typically, (lisp-mode) or (scheme-mode).
  809. ;;;
  810. ;;; If the command is given while the cursor is inside a string, *and*
  811. ;;; the string is an existing filename, *and* the filename is not a directory,
  812. ;;; then the string is taken as default. This allows you to just position
  813. ;;; your cursor over a string that's a filename and have it taken as default.
  814. ;;;
  815. ;;; If the command is given in a file buffer whose major mode is in
  816. ;;; SOURCE-MODES, then the the filename is the default file, and the
  817. ;;; file's directory is the default directory.
  818. ;;;
  819. ;;; If the buffer isn't a source file buffer (e.g., it's the process buffer),
  820. ;;; then the default directory & file are what was used in the last source-file
  821. ;;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time
  822. ;;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory
  823. ;;; is the cwd, with no default file. (\"no default file\" = nil)
  824. ;;;
  825. ;;; SOURCE-REGEXP is typically going to be something like (tea-mode)
  826. ;;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode)
  827. ;;; for Soar programs, etc.
  828. ;;;
  829. ;;; The function returns a pair: (default-directory . default-file).
  830. (defun comint-source-default (previous-dir/file source-modes)
  831. (cond ((and buffer-file-name (memq major-mode source-modes))
  832. (cons (file-name-directory buffer-file-name)
  833. (file-name-nondirectory buffer-file-name)))
  834. (previous-dir/file)
  835. (t
  836. (cons default-directory nil))))
  837. ;;; (COMINT-CHECK-SOURCE fname)
  838. ;;;============================================================================
  839. ;;; Prior to loading or compiling (or otherwise processing) a file (in the CMU
  840. ;;; process-in-a-buffer modes), this function can be called on the filename.
  841. ;;; If the file is loaded into a buffer, and the buffer is modified, the user
  842. ;;; is queried to see if he wants to save the buffer before proceeding with
  843. ;;; the load or compile.
  844. (defun comint-check-source (fname)
  845. (let ((buff (get-file-buffer fname)))
  846. (if (and buff
  847. (buffer-modified-p buff)
  848. (y-or-n-p (format "Save buffer %s first? "
  849. (buffer-name buff))))
  850. ;; save BUFF.
  851. (let ((old-buffer (current-buffer)))
  852. (set-buffer buff)
  853. (save-buffer)
  854. (set-buffer old-buffer)))))
  855. ;;; (COMINT-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p)
  856. ;;;============================================================================
  857. ;;; COMINT-GET-SOURCE is used to prompt for filenames in command-interpreter
  858. ;;; commands that process source files (like loading or compiling a file).
  859. ;;; It prompts for the filename, provides a default, if there is one,
  860. ;;; and returns the result filename.
  861. ;;;
  862. ;;; See COMINT-SOURCE-DEFAULT for more on determining defaults.
  863. ;;;
  864. ;;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair
  865. ;;; from the last source processing command. SOURCE-MODES is a list of major
  866. ;;; modes used to determine what file buffers contain source files. (These
  867. ;;; two arguments are used for determining defaults). If MUSTMATCH-P is true,
  868. ;;; then the filename reader will only accept a file that exists.
  869. ;;;
  870. ;;; A typical use:
  871. ;;; (interactive (comint-get-source "Compile file: " prev-lisp-dir/file
  872. ;;; '(lisp-mode) t))
  873. ;;; This is pretty stupid about strings. It decides we're in a string
  874. ;;; if there's a quote on both sides of point on the current line.
  875. (defun comint-extract-string ()
  876. "Returns string around point that starts the current line or nil."
  877. (save-excursion
  878. (let* ((point (point))
  879. (bol (progn (beginning-of-line) (point)))
  880. (eol (progn (end-of-line) (point)))
  881. (start (progn (goto-char point)
  882. (and (search-backward "\"" bol t)
  883. (1+ (point)))))
  884. (end (progn (goto-char point)
  885. (and (search-forward "\"" eol t)
  886. (1- (point))))))
  887. (and start end
  888. (buffer-substring start end)))))
  889. (defun comint-get-source (prompt prev-dir/file source-modes mustmatch-p)
  890. (let* ((def (comint-source-default prev-dir/file source-modes))
  891. (stringfile (comint-extract-string))
  892. (sfile-p (and stringfile
  893. (condition-case ()
  894. (file-exists-p stringfile)
  895. (error nil))
  896. (not (file-directory-p stringfile))))
  897. (defdir (if sfile-p (file-name-directory stringfile)
  898. (car def)))
  899. (deffile (if sfile-p (file-name-nondirectory stringfile)
  900. (cdr def)))
  901. (ans (read-file-name (if deffile (format "%s(default %s) "
  902. prompt deffile)
  903. prompt)
  904. defdir
  905. (concat defdir deffile)
  906. mustmatch-p)))
  907. (list (expand-file-name (substitute-in-file-name ans)))))
  908. ;;; I am somewhat divided on this string-default feature. It seems
  909. ;;; to violate the principle-of-least-astonishment, in that it makes
  910. ;;; the default harder to predict, so you actually have to look and see
  911. ;;; what the default really is before choosing it. This can trip you up.
  912. ;;; On the other hand, it can be useful, I guess. I would appreciate feedback
  913. ;;; on this.
  914. ;;; -Olin
  915. ;;; Simple process query facility.
  916. ;;; ===========================================================================
  917. ;;; This function is for commands that want to send a query to the process
  918. ;;; and show the response to the user. For example, a command to get the
  919. ;;; arglist for a Common Lisp function might send a "(arglist 'foo)" query
  920. ;;; to an inferior Common Lisp process.
  921. ;;;
  922. ;;; This simple facility just sends strings to the inferior process and pops
  923. ;;; up a window for the process buffer so you can see what the process
  924. ;;; responds with. We don't do anything fancy like try to intercept what the
  925. ;;; process responds with and put it in a pop-up window or on the message
  926. ;;; line. We just display the buffer. Low tech. Simple. Works good.
  927. ;;; Send to the inferior process PROC the string STR. Pop-up but do not select
  928. ;;; a window for the inferior process so that its response can be seen.
  929. (defun comint-proc-query (proc str)
  930. (let* ((proc-buf (process-buffer proc))
  931. (proc-mark (process-mark proc)))
  932. (display-buffer proc-buf)
  933. (set-buffer proc-buf) ; but it's not the selected *window*
  934. (let ((proc-win (get-buffer-window proc-buf))
  935. (proc-pt (marker-position proc-mark)))
  936. (comint-send-string proc str) ; send the query
  937. (accept-process-output proc) ; wait for some output
  938. ;; Try to position the proc window so you can see the answer.
  939. ;; This is bogus code. If you delete the (sit-for 0), it breaks.
  940. ;; I don't know why. Wizards invited to improve it.
  941. (if (not (pos-visible-in-window-p proc-pt proc-win))
  942. (let ((opoint (window-point proc-win)))
  943. (set-window-point proc-win proc-mark) (sit-for 0)
  944. (if (not (pos-visible-in-window-p opoint proc-win))
  945. (push-mark opoint)
  946. (set-window-point proc-win opoint)))))))
  947. ;;; Filename completion in a buffer
  948. ;;; ===========================================================================
  949. ;;; Useful completion functions, courtesy of the Ergo group.
  950. ;;; M-<Tab> will complete the filename at the cursor as much as possible
  951. ;;; M-? will display a list of completions in the help buffer.
  952. ;;; Three commands:
  953. ;;; comint-dynamic-complete Complete filename at point.
  954. ;;; comint-dynamic-list-completions List completions in help buffer.
  955. ;;; comint-replace-by-expanded-filename Expand and complete filename at point;
  956. ;;; replace with expanded/completed name.
  957. ;;; These are not installed in the comint-mode keymap. But they are
  958. ;;; available for people who want them. Shell-mode installs them:
  959. ;;; (define-key cmushell-mode-map "\M-\t" 'comint-dynamic-complete)
  960. ;;; (define-key cmushell-mode-map "\M-?" 'comint-dynamic-list-completions)))
  961. ;;;
  962. ;;; Commands like this are fine things to put in load hooks if you
  963. ;;; want them present in specific modes. Example:
  964. ;;; (setq cmushell-load-hook
  965. ;;; '((lambda () (define-key lisp-mode-map "\M-\t"
  966. ;;; 'comint-replace-by-expanded-filename))))
  967. ;;;
  968. (defun comint-match-partial-pathname ()
  969. "Returns the filename at point or causes an error."
  970. (save-excursion
  971. (if (re-search-backward "[^~/A-Za-z0-9---_.$#,=]" nil 'move)
  972. (forward-char 1))
  973. ;; Anchor the search forwards.
  974. (if (not (looking-at "[~/A-Za-z0-9---_.$#,=]")) (error ""))
  975. (re-search-forward "[~/A-Za-z0-9---_.$#,=]+")
  976. (substitute-in-file-name
  977. (buffer-substring (match-beginning 0) (match-end 0)))))
  978. (defun comint-replace-by-expanded-filename ()
  979. "Replace the filename at point with an expanded, canonicalised, and
  980. completed replacement.
  981. \"Expanded\" means environment variables (e.g., $HOME) and ~'s are
  982. replaced with the corresponding directories. \"Canonicalised\" means ..
  983. and \. are removed, and the filename is made absolute instead of relative.
  984. See functions expand-file-name and substitute-in-file-name. See also
  985. comint-dynamic-complete."
  986. (interactive)
  987. (let* ((pathname (comint-match-partial-pathname))
  988. (pathdir (file-name-directory pathname))
  989. (pathnondir (file-name-nondirectory pathname))
  990. (completion (file-name-completion pathnondir
  991. (or pathdir default-directory))))
  992. (cond ((null completion)
  993. (message "No completions of %s." pathname)
  994. (ding))
  995. ((eql completion t)
  996. (message "Unique completion."))
  997. (t ; this means a string was returned.
  998. (delete-region (match-beginning 0) (match-end 0))
  999. (insert (expand-file-name (concat pathdir completion)))))))
  1000. (defun comint-dynamic-complete ()
  1001. "Dynamically complete the filename at point.
  1002. This function is similar to comint-replace-by-expanded-filename, except
  1003. that it won't change parts of the filename already entered in the buffer;
  1004. it just adds completion characters to the end of the filename."
  1005. (interactive)
  1006. (let* ((pathname (comint-match-partial-pathname))
  1007. (pathdir (file-name-directory pathname))
  1008. (pathnondir (file-name-nondirectory pathname))
  1009. (completion (file-name-completion pathnondir
  1010. (or pathdir default-directory))))
  1011. (cond ((null completion)
  1012. (message "No completions of %s." pathname)
  1013. (ding))
  1014. ((eql completion t)
  1015. (message "Unique completion."))
  1016. (t ; this means a string was returned.
  1017. (goto-char (match-end 0))
  1018. (insert (substring completion (length pathnondir)))))))
  1019. (defun comint-dynamic-list-completions ()
  1020. "List in help buffer all possible completions of the filename at point."
  1021. (interactive)
  1022. (let* ((pathname (comint-match-partial-pathname))
  1023. (pathdir (file-name-directory pathname))
  1024. (pathnondir (file-name-nondirectory pathname))
  1025. (completions
  1026. (file-name-all-completions pathnondir
  1027. (or pathdir default-directory))))
  1028. (cond ((null completions)
  1029. (message "No completions of %s." pathname)
  1030. (ding))
  1031. (t
  1032. (let ((conf (current-window-configuration)))
  1033. (with-output-to-temp-buffer "*Help*"
  1034. (display-completion-list completions))
  1035. (sit-for 0)
  1036. (message "Hit space to flush.")
  1037. (let ((ch (read-char)))
  1038. (if (= ch ?\ )
  1039. (set-window-configuration conf)
  1040. (setq unread-command-char ch))))))))
  1041. ; Ergo bindings
  1042. ; (global-set-key "\M-\t" 'comint-replace-by-expanded-filename)
  1043. ; (global-set-key "\M-?" 'comint-dynamic-list-completions)
  1044. ; (define-key shell-mode-map "\M-\t" 'comint-dynamic-complete)
  1045. ;;; Converting process modes to use comint mode
  1046. ;;; ===========================================================================
  1047. ;;; Several gnu packages (tex-mode, background, dbx, gdb, kermit, prolog,
  1048. ;;; telnet are some) use the shell package as clients. Most of them would
  1049. ;;; be better off using the comint package, but they predate it.
  1050. ;;;
  1051. ;;; Altering these packages to use comint mode should greatly
  1052. ;;; improve their functionality, and is fairly easy.
  1053. ;;;
  1054. ;;; Renaming variables
  1055. ;;; Most of the work is renaming variables and functions. These are the common
  1056. ;;; ones:
  1057. ;;; Local variables:
  1058. ;;; last-input-end comint-last-input-end
  1059. ;;; last-input-start <unnecessary>
  1060. ;;; shell-prompt-pattern comint-prompt-regexp
  1061. ;;; shell-set-directory-error-hook <no equivalent>
  1062. ;;; Miscellaneous:
  1063. ;;; shell-set-directory <unnecessary>
  1064. ;;; shell-mode-map comint-mode-map
  1065. ;;; Commands:
  1066. ;;; shell-send-input comint-send-input
  1067. ;;; shell-send-eof comint-delchar-or-maybe-eof
  1068. ;;; kill-shell-input comint-kill-input
  1069. ;;; interrupt-shell-subjob comint-interrupt-subjob
  1070. ;;; stop-shell-subjob comint-stop-subjob
  1071. ;;; quit-shell-subjob comint-quit-subjob
  1072. ;;; kill-shell-subjob comint-kill-subjob
  1073. ;;; kill-output-from-shell comint-kill-output
  1074. ;;; show-output-from-shell comint-show-output
  1075. ;;; copy-last-shell-input Use comint-previous-input/comint-next-input
  1076. ;;;
  1077. ;;; LAST-INPUT-START is no longer necessary because inputs are stored on the
  1078. ;;; input history ring. SHELL-SET-DIRECTORY is gone, its functionality taken
  1079. ;;; over by SHELL-DIRECTORY-TRACKER, the shell mode's comint-input-sentinel.
  1080. ;;; Comint mode does not provide functionality equivalent to
  1081. ;;; shell-set-directory-error-hook; it is gone.
  1082. ;;;
  1083. ;;; If you are implementing some process-in-a-buffer mode, called foo-mode, do
  1084. ;;; *not* create the comint-mode local variables in your foo-mode function.
  1085. ;;; This is not modular. Instead, call comint-mode, and let *it* create the
  1086. ;;; necessary comint-specific local variables. Then create the
  1087. ;;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to
  1088. ;;; be foo-mode-map, and its mode to be foo-mode. Set the comint-mode hooks
  1089. ;;; (comint-prompt-regexp, comint-input-filter, comint-input-sentinel,
  1090. ;;; comint-get-old-input) that need to be different from the defaults. Call
  1091. ;;; foo-mode-hook, and you're done. Don't run the comint-mode hook yourself;
  1092. ;;; comint-mode will take care of it. The following example, from cmushell.el,
  1093. ;;; is typical:
  1094. ;;;
  1095. ;;; (defun shell-mode ()
  1096. ;;; (interactive)
  1097. ;;; (comint-mode)
  1098. ;;; (setq comint-prompt-regexp shell-prompt-pattern)
  1099. ;;; (setq major-mode 'shell-mode)
  1100. ;;; (setq mode-name "Shell")
  1101. ;;; (cond ((not shell-mode-map)
  1102. ;;; (setq shell-mode-map (full-copy-sparse-keymap comint-mode-map))
  1103. ;;; (define-key shell-mode-map "\M-\t" 'comint-dynamic-complete)
  1104. ;;; (define-key shell-mode-map "\M-?"
  1105. ;;; 'comint-dynamic-list-completions)))
  1106. ;;; (use-local-map shell-mode-map)
  1107. ;;; (make-local-variable 'shell-directory-stack)
  1108. ;;; (setq shell-directory-stack nil)
  1109. ;;; (setq comint-input-sentinel 'shell-directory-tracker)
  1110. ;;; (run-hooks 'shell-mode-hook))
  1111. ;;;
  1112. ;;;
  1113. ;;; Note that make-comint is different from make-shell in that it
  1114. ;;; doesn't have a default program argument. If you give make-shell
  1115. ;;; a program name of NIL, it cleverly chooses one of explicit-shell-name,
  1116. ;;; $ESHELL, $SHELL, or /bin/sh. If you give make-comint a program argument
  1117. ;;; of NIL, it barfs. Adjust your code accordingly...
  1118. ;;;
  1119. ;;; Do the user's customisation...
  1120. (defvar comint-load-hook nil
  1121. "This hook is run when comint is loaded in.
  1122. This is a good place to put keybindings.")
  1123. (run-hooks 'comint-load-hook)
  1124. ;;; Change log:
  1125. ;;; 9/12/89
  1126. ;;; - Souped up the filename expansion procedures.
  1127. ;;; Doc strings are much clearer and more detailed.
  1128. ;;; Fixed a bug where doing a filename completion when the point
  1129. ;;; was in the middle of the filename instead of at the end would lose.
  1130. ;;;
  1131. ;;; 2/17/90
  1132. ;;; - Souped up the command history stuff so that text inserted
  1133. ;;; by comint-previous-input-matching is removed by following
  1134. ;;; command history recalls. comint-next/previous-input-matching
  1135. ;;; is now much more smoothly integrated w/the command history stuff.
  1136. ;;; - Added comint-eol-on-send flag and comint-input-sender hook.
  1137. ;;; Comint-input-sender based on code contributed by Jeff Peck
  1138. ;;; (peck@sun.com).
  1139. ;;;
  1140. ;;; 3/13/90 ccm@cmu.cs.edu
  1141. ;;; - Added comint-previous-similar-input for looking up similar inputs.
  1142. ;;; - Added comint-send-and-get-output to allow snarfing input from
  1143. ;;; buffer.
  1144. ;;; - Added the ability to pick up a source file by positioning over
  1145. ;;; a string in comint-get-source.
  1146. ;;; - Added add-hook to make it a little easier for the user to use
  1147. ;;; multiple hooks.
  1148. ;;;
  1149. ;;; 5/22/90 shivers
  1150. ;;; - Moved Chris' multiplexed ipc stuff to comint-ipc.el.
  1151. ;;; - Altered Chris' comint-get-source string feature. The string
  1152. ;;; is only offered as a default if it names an existing file.
  1153. ;;; - Changed comint-exec to directly crank up the process, instead
  1154. ;;; of calling the env program. This made background.el happy.
  1155. ;;; - Added new buffer-local var comint-ptyp. The problem is that
  1156. ;;; the signalling functions don't work as advertised. If you are
  1157. ;;; communicating via pipes, the CURRENT-GROUP arg is supposed to
  1158. ;;; be ignored, but, unfortunately it seems to be the case that you
  1159. ;;; must pass a NIL for this arg in the pipe case. COMINT-PTYP
  1160. ;;; is a flag that tells whether the process is communicating
  1161. ;;; via pipes or a pty. The comint signalling functions use it
  1162. ;;; to determine the necessary CURRENT-GROUP arg value. The bug
  1163. ;;; has been reported to the Gnu folks.
  1164. ;;; - comint-dynamic-complete flushes the help window if you hit space
  1165. ;;; after you execute it.
  1166. ;;; - Added functions comint-send-string, comint-send-region and var
  1167. ;;; comint-input-chunk-size. comint-send-string tries to prevent processes
  1168. ;;; from hanging when you send them long strings by breaking them into
  1169. ;;; chunks and allowing process output between chunks. I got the idea from
  1170. ;;; Eero Simoncelli's Common Lisp package. Note that using
  1171. ;;; comint-send-string means that the process buffer's contents can change
  1172. ;;; during a call! If you depend on process output only happening between
  1173. ;;; toplevel commands, this could be a problem. In such a case, use
  1174. ;;; process-send-string instead. If this is a problem for people, I'd like
  1175. ;;; to hear about it.
  1176. ;;; - Added comint-proc-query as a simple mechanism for commands that
  1177. ;;; want to query an inferior process and display its response. For a
  1178. ;;; typical use, see lisp-show-arglist in cmulisp.el.
  1179. ;;; - Added constant comint-version, which is now "2.01".
  1180. ;;;
  1181. ;;; 6/14/90 shivers
  1182. ;;; - Had comint-update-env defined twice. Removed extra copy. Also
  1183. ;;; renamed mem to be comint-mem, for modularity. The duplication
  1184. ;;; was reported by Michael Meissner.
  1185. ;;; 6/16/90 shivers
  1186. ;;; - Emacs has two different mechanisms for maintaining the process
  1187. ;;; environment, determined at compile time by the MAINTAIN-ENVIRONMENT
  1188. ;;; #define. One uses the process-environment global variable, and
  1189. ;;; one uses a getenv/setenv interface. comint-exec assumed the
  1190. ;;; process-environment interface; it has been generalised (with
  1191. ;;; comint-exec-1) to handle both cases. Pretty bogus. We could,
  1192. ;;; of course, skip all this and just use the etc/env program to
  1193. ;;; handle the environment tweaking, but that obscures process
  1194. ;;; queries that other modules (like background.el) depend on. etc/env
  1195. ;;; is also fairly bogus. This bug, and some of the fix code was
  1196. ;;; reported by Dan Pierson.
  1197. ;;;
  1198. ;;; 9/5/90 shivers
  1199. ;;; - Changed make-variable-buffer-local's to make-local-variable's.
  1200. ;;; This leaves non-comint-mode buffers alone. Stephane Payrard
  1201. ;;; reported the sloppy useage.
  1202. ;;; - You can now go from comint-previous-similar-input to
  1203. ;;; comint-previous-input with no problem.
  1204. ;;;
  1205. ;;; 12/21/90 shivers
  1206. ;;; - Added a condition-case to comint-get-source. Bogus strings
  1207. ;;; beginning with ~ were making the file-exists-p barf.
  1208. ;;; - Added "=" to the set of chars recognised by file completion
  1209. ;;; as constituting a filename.
  1210. ;;;
  1211. ;;; 1/90 shivers
  1212. ;;; These changes comprise release 2.02:
  1213. ;;; - Removed the kill-all-local-variables in comint-mode. This
  1214. ;;; made it impossible for client modes to set things before calling
  1215. ;;; comint-mode. (In particular, it messed up ilisp.el) In general,
  1216. ;;; the client mode should be responsible for a k-a-l-v's.
  1217. ;;; - Fixed comint-match-partial-pathname so that it works in
  1218. ;;; more cases: if the filename begins at the start-of-buffer;
  1219. ;;; if point is on the first char of the filename. Just a question
  1220. ;;; of getting the tricky bits right.
  1221. ;;; - Added a hook, comint-exec-hook that is run each time a process
  1222. ;;; is cranked up. Useful for things like process-kill-without-query.
  1223. ;;;
  1224. ;;; These two were pointed out by tale:
  1225. ;;; - Improved the doc string in comint-send-input a little bit.
  1226. ;;; - Tweaked make-comint to check process status with comint-check-proc
  1227. ;;; instead of equivalent inline code.
  1228. ;;;
  1229. ;;; - Prompt-search history commands have been commented out. I never
  1230. ;;; liked them; I don't think anyone used them.
  1231. ;;; - Made comint-exec-hook a local var, as it should have been.
  1232. ;;; (This way, for instance, you can have cmushell procs kill-w/o-query,
  1233. ;;; but let Scheme procs be default.)