Klimi's new dotfiles with stow.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

472 linhas
17 KiB

há 4 anos
  1. ;;; -*-Emacs-Lisp-*-
  2. ;;;%Header
  3. ;;; Bridge process filter, V1.0
  4. ;;; Copyright (C) 1991 Chris McConnell, ccm@cs.cmu.edu
  5. ;;;
  6. ;;; Send mail to ilisp@cons.org if you have problems.
  7. ;;;
  8. ;;; Send mail to majordomo@cons.org if you want to be on the
  9. ;;; ilisp mailing list.
  10. ;;; This file is part of GNU Emacs.
  11. ;;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;;; but WITHOUT ANY WARRANTY. No author or distributor
  13. ;;; accepts responsibility to anyone for the consequences of using it
  14. ;;; or for whether it serves any particular purpose or works at all,
  15. ;;; unless he says so in writing. Refer to the GNU Emacs General Public
  16. ;;; License for full details.
  17. ;;; Everyone is granted permission to copy, modify and redistribute
  18. ;;; GNU Emacs, but only under the conditions described in the
  19. ;;; GNU Emacs General Public License. A copy of this license is
  20. ;;; supposed to have been given to you along with GNU Emacs so you
  21. ;;; can know your rights and responsibilities. It should be in a
  22. ;;; file named COPYING. Among other things, the copyright notice
  23. ;;; and this notice must be preserved on all copies.
  24. ;;; Send any bugs or comments. Thanks to Todd Kaufmann for rewriting
  25. ;;; the process filter for continuous handlers.
  26. ;;; USAGE: M-x install-bridge will add a process output filter to the
  27. ;;; current buffer. Any output that the process does between
  28. ;;; bridge-start-regexp and bridge-end-regexp will be bundled up and
  29. ;;; passed to the first handler on bridge-handlers that matches the
  30. ;;; output using string-match. If bridge-prompt-regexp shows up
  31. ;;; before bridge-end-regexp, the bridge will be cancelled. If no
  32. ;;; handler matches the output, the first symbol in the output is
  33. ;;; assumed to be a buffer name and the rest of the output will be
  34. ;;; sent to that buffer's process. This can be used to communicate
  35. ;;; between processes or to set up two way interactions between Emacs
  36. ;;; and an inferior process.
  37. ;;; You can write handlers that process the output in special ways.
  38. ;;; See bridge-send-handler for the default handler. The command
  39. ;;; hand-bridge is useful for testing. Keep in mind that all
  40. ;;; variables are buffer local.
  41. ;;; YOUR .EMACS FILE:
  42. ;;;
  43. ;;; ;;; Set up load path to include bridge
  44. ;;; (setq load-path (cons "/bridge-directory/" load-path))
  45. ;;; (autoload 'install-bridge "bridge" "Install a process bridge." t)
  46. ;;; (setq bridge-hook
  47. ;;; '(lambda ()
  48. ;;; ;; Example options
  49. ;;; (setq bridge-source-insert nil) ;Don't insert in source buffer
  50. ;;; (setq bridge-destination-insert nil) ;Don't insert in dest buffer
  51. ;;; ;; Handle copy-it messages yourself
  52. ;;; (setq bridge-handlers
  53. ;;; '(("copy-it" . my-copy-handler)))))
  54. ;;; EXAMPLE:
  55. ;;; # This pipes stdin to the named buffer in a Unix shell
  56. ;;; alias devgnu '(echo -n "\!* "; cat -; echo -n "")'
  57. ;;;
  58. ;;; ls | devgnu *scratch*
  59. (eval-when-compile
  60. (require 'cl))
  61. ;;;%Parameters
  62. (defvar bridge-hook nil
  63. "Hook called when a bridge is installed by install-hook.")
  64. (defvar bridge-start-regexp ""
  65. "*Regular expression to match the start of a process bridge in
  66. process output. It should be followed by a buffer name, the data to
  67. be sent and a bridge-end-regexp.")
  68. (defvar bridge-end-regexp ""
  69. "*Regular expression to match the end of a process bridge in process
  70. output.")
  71. (defvar bridge-prompt-regexp nil
  72. "*Regular expression for detecting a prompt. If there is a
  73. comint-prompt-regexp, it will be initialized to that. A prompt before
  74. a bridge-end-regexp will stop the process bridge.")
  75. (defvar bridge-handlers nil
  76. "Alist of (regexp . handler) for handling process output delimited
  77. by bridge-start-regexp and bridge-end-regexp. The first entry on the
  78. list whose regexp matches the output will be called on the process and
  79. the delimited output.")
  80. (defvar bridge-source-insert t
  81. "*T to insert bridge input in the source buffer minus delimiters.")
  82. (defvar bridge-destination-insert t
  83. "*T for bridge-send-handler to insert bridge input into the
  84. destination buffer minus delimiters.")
  85. (defvar bridge-chunk-size 512
  86. "*Long inputs send to comint processes are broken up into chunks of
  87. this size. If your process is choking on big inputs, try lowering the
  88. value.")
  89. ;;;%Internal variables
  90. (defvar bridge-old-filter nil
  91. "Old filter for a bridged process buffer.")
  92. (defvar bridge-string nil
  93. "The current output in the process bridge.")
  94. (defvar bridge-in-progress nil
  95. "The current handler function, if any, that bridge passes strings on to,
  96. or nil if none.")
  97. (defvar bridge-leftovers nil
  98. "Because of chunking you might get an incomplete bridge signal - start but the end is in the next packet. Save the overhanging text here.")
  99. (defvar bridge-send-to-buffer nil
  100. "The buffer that the default bridge-handler (bridge-send-handler) is
  101. currently sending to, or nil if it hasn't started yet. Your handler
  102. function can use this variable also.")
  103. (defvar bridge-last-failure ()
  104. "Last thing that broke the bridge handler. First item is function call
  105. (eval'able); last item is error condition which resulted. This is provided
  106. to help handler-writers in their debugging.")
  107. (defvar bridge-insert-function nil
  108. "If non-nil use this instead of `bridge-insert'")
  109. ;;;%Utilities
  110. (defun bridge-insert (output &optional _dummy)
  111. "Insert process OUTPUT into the current buffer."
  112. (if bridge-insert-function
  113. (funcall bridge-insert-function output)
  114. (if output
  115. (let* ((buffer (current-buffer))
  116. (process (get-buffer-process buffer))
  117. (mark (process-mark process))
  118. (window (selected-window))
  119. (at-end nil))
  120. (if (eq (window-buffer window) buffer)
  121. (setq at-end (= (point) mark))
  122. (setq window (get-buffer-window buffer)))
  123. (save-excursion
  124. (goto-char mark)
  125. (insert output)
  126. (set-marker mark (point)))
  127. (if window
  128. (progn
  129. (if at-end (goto-char mark))
  130. (if (not (pos-visible-in-window-p (point) window))
  131. (let ((original (selected-window)))
  132. (save-excursion
  133. (select-window window)
  134. (recenter '(center))
  135. (select-window original))))))))))
  136. ;;;
  137. ;(defun bridge-send-string (process string)
  138. ; "Send PROCESS the contents of STRING as input.
  139. ;This is equivalent to process-send-string, except that long input strings
  140. ;are broken up into chunks of size comint-input-chunk-size. Processes
  141. ;are given a chance to output between chunks. This can help prevent processes
  142. ;from hanging when you send them long inputs on some OS's."
  143. ; (let* ((len (length string))
  144. ; (i (min len bridge-chunk-size)))
  145. ; (process-send-string process (substring string 0 i))
  146. ; (while (< i len)
  147. ; (let ((next-i (+ i bridge-chunk-size)))
  148. ; (accept-process-output)
  149. ; (process-send-string process (substring string i (min len next-i)))
  150. ; (setq i next-i)))))
  151. ;;;
  152. (defun bridge-call-handler (handler proc string)
  153. "Funcall HANDLER on PROC, STRING carefully. Error is caught if happens,
  154. and user is signaled. State is put in bridge-last-failure. Returns t if
  155. handler executed without error."
  156. (let ((inhibit-quit nil)
  157. (failed nil))
  158. (condition-case err
  159. (funcall handler proc string)
  160. (error
  161. (ding)
  162. (setq failed t)
  163. (message "bridge-handler \"%s\" failed %s (see bridge-last-failure)"
  164. handler err)
  165. (setq bridge-last-failure
  166. `((funcall ',handler ',proc ,string)
  167. "Caused: "
  168. ,err))))
  169. (not failed)))
  170. ;;;%Handlers
  171. (defun bridge-send-handler (process input)
  172. "Send PROCESS INPUT to the buffer name found at the start of the
  173. input. The input after the buffer name is sent to the buffer's
  174. process if it has one. If bridge-destination-insert is T, the input
  175. will be inserted into the buffer. If it does not have a process, it
  176. will be inserted at the end of the buffer."
  177. (if (null input)
  178. (setq bridge-send-to-buffer nil) ; end of bridge
  179. (let (buffer-and-start buffer-name dest to)
  180. ;; if this is first time, get the buffer out of the first line
  181. (cond ((not bridge-send-to-buffer)
  182. (setq buffer-and-start (read-from-string input)
  183. buffer-name (format "%s" (car (read-from-string input)))
  184. dest (get-buffer buffer-name)
  185. to (get-buffer-process dest)
  186. input (substring input (cdr buffer-and-start)))
  187. (setq bridge-send-to-buffer dest))
  188. (t
  189. (setq buffer-name bridge-send-to-buffer
  190. dest (get-buffer buffer-name)
  191. to (get-buffer-process dest)
  192. )))
  193. (if dest
  194. (let ((buffer (current-buffer)))
  195. (if bridge-destination-insert
  196. (unwind-protect
  197. (progn
  198. (set-buffer dest)
  199. (if to
  200. (bridge-insert process input)
  201. (goto-char (point-max))
  202. (insert input)))
  203. (set-buffer buffer)))
  204. (if to
  205. ;; (bridge-send-string to input)
  206. (process-send-string to input)
  207. ))
  208. (error "%s is not a buffer" buffer-name)))))
  209. ;;;%Filter
  210. (defun bridge-filter (process output)
  211. "Given PROCESS and some OUTPUT, check for the presence of
  212. bridge-start-regexp. Everything prior to this will be passed to the
  213. normal filter function or inserted in the buffer if it is nil. The
  214. output up to bridge-end-regexp will be sent to the first handler on
  215. bridge-handlers that matches the string. If no handlers match, the
  216. input will be sent to bridge-send-handler. If bridge-prompt-regexp is
  217. encountered before the bridge-end-regexp, the bridge will be cancelled."
  218. (let ((inhibit-quit t)
  219. (match-data (match-data))
  220. (buffer (current-buffer))
  221. (process-buffer (process-buffer process))
  222. (case-fold-search t)
  223. (start 0) (end 0)
  224. function
  225. b-start b-start-end b-end)
  226. (set-buffer process-buffer) ;; access locals
  227. ;; Handle bridge messages that straddle a packet by prepending
  228. ;; them to this packet.
  229. (when bridge-leftovers
  230. (setq output (concat bridge-leftovers output))
  231. (setq bridge-leftovers nil))
  232. (setq function bridge-in-progress)
  233. ;; How it works:
  234. ;;
  235. ;; start, end delimit the part of string we are interested in;
  236. ;; initially both 0; after an iteration we move them to next string.
  237. ;; b-start, b-end delimit part of string to bridge (possibly whole string);
  238. ;; this will be string between corresponding regexps.
  239. ;; There are two main cases when we come into loop:
  240. ;; bridge in progress
  241. ;;0 setq b-start = start
  242. ;;1 setq b-end (or end-pattern end)
  243. ;;4 process string
  244. ;;5 remove handler if end found
  245. ;; no bridge in progress
  246. ;;0 setq b-start if see start-pattern
  247. ;;1 setq b-end if bstart to (or end-pattern end)
  248. ;;2 send (substring start b-start) to normal place
  249. ;;3 find handler (in b-start, b-end) if not set
  250. ;;4 process string
  251. ;;5 remove handler if end found
  252. ;; equivalent sections have the same numbers here;
  253. ;; we fold them together in this code.
  254. (block bridge-filter
  255. (unwind-protect
  256. (while (< end (length output))
  257. ;;0 setq b-start if find
  258. (setq b-start
  259. (cond (bridge-in-progress
  260. (setq b-start-end start)
  261. start)
  262. ((string-match bridge-start-regexp output start)
  263. (setq b-start-end (match-end 0))
  264. (match-beginning 0))
  265. (t nil)))
  266. ;;1 setq b-end
  267. (setq b-end
  268. (if b-start
  269. (let ((end-seen (string-match bridge-end-regexp
  270. output b-start-end)))
  271. (if end-seen (setq end (match-end 0)))
  272. end-seen)))
  273. ;; Detect and save partial bridge messages
  274. (when (and b-start b-start-end (not b-end))
  275. (setq bridge-leftovers (substring output b-start))
  276. )
  277. (if (and b-start (not b-end))
  278. (setq end b-start)
  279. (if (not b-end)
  280. (setq end (length output))))
  281. ;;1.5 - if see prompt before end, remove current
  282. (if (and b-start b-end)
  283. (let ((prompt (string-match bridge-prompt-regexp
  284. output b-start-end)))
  285. (if (and prompt (<= (match-end 0) b-end))
  286. (setq b-start nil ; b-start-end start
  287. b-end start
  288. end (match-end 0)
  289. bridge-in-progress nil
  290. ))))
  291. ;;2 send (substring start b-start) to old filter, if any
  292. (when (not (equal start (or b-start end))) ; don't bother on empty string
  293. (let ((pass-on (substring output start (or b-start end))))
  294. (if bridge-old-filter
  295. (let ((old bridge-old-filter))
  296. (store-match-data match-data)
  297. (funcall old process pass-on)
  298. ;; if filter changed, re-install ourselves
  299. (let ((new (process-filter process)))
  300. (if (not (eq new 'bridge-filter))
  301. (progn (setq bridge-old-filter new)
  302. (set-process-filter process 'bridge-filter)))))
  303. (set-buffer process-buffer)
  304. (bridge-insert pass-on))))
  305. (if (and b-start-end (not b-end))
  306. (return-from bridge-filter t) ; when last bit has prematurely ending message, exit early.
  307. (progn
  308. ;;3 find handler (in b-start, b-end) if none current
  309. (if (and b-start (not bridge-in-progress))
  310. (let ((handlers bridge-handlers))
  311. (while (and handlers (not function))
  312. (let* ((handler (car handlers))
  313. (m (string-match (car handler) output b-start-end)))
  314. (if (and m (< m b-end))
  315. (setq function (cdr handler))
  316. (setq handlers (cdr handlers)))))
  317. ;; Set default handler if none
  318. (if (null function)
  319. (setq function 'bridge-send-handler))
  320. (setq bridge-in-progress function)))
  321. ;;4 process strin
  322. (if function
  323. (let ((ok t))
  324. (if (/= b-start-end b-end)
  325. (let ((send (substring output b-start-end b-end)))
  326. ;; also, insert the stuff in buffer between
  327. ;; iff bridge-source-insert.
  328. (if bridge-source-insert (bridge-insert send))
  329. ;; call handler on string
  330. (setq ok (bridge-call-handler function process send))))
  331. ;;5 remove handler if end found
  332. ;; if function removed then tell it that's all
  333. (if (or (not ok) (/= b-end end)) ;; saw end before end-of-string
  334. (progn
  335. (bridge-call-handler function process nil)
  336. ;; have to remove function too for next time around
  337. (setq function nil
  338. bridge-in-progress nil)
  339. ))
  340. ))
  341. ;; continue looping, in case there's more string
  342. (setq start end))
  343. ))
  344. ;; protected forms: restore buffer, match-data
  345. (set-buffer buffer)
  346. (store-match-data match-data)
  347. ))))
  348. ;;;%Interface
  349. (defun install-bridge ()
  350. "Set up a process bridge in the current buffer."
  351. (interactive)
  352. (if (not (get-buffer-process (current-buffer)))
  353. (error "%s does not have a process" (buffer-name (current-buffer)))
  354. (make-local-variable 'bridge-start-regexp)
  355. (make-local-variable 'bridge-end-regexp)
  356. (make-local-variable 'bridge-prompt-regexp)
  357. (make-local-variable 'bridge-handlers)
  358. (make-local-variable 'bridge-source-insert)
  359. (make-local-variable 'bridge-destination-insert)
  360. (make-local-variable 'bridge-chunk-size)
  361. (make-local-variable 'bridge-old-filter)
  362. (make-local-variable 'bridge-string)
  363. (make-local-variable 'bridge-in-progress)
  364. (make-local-variable 'bridge-send-to-buffer)
  365. (make-local-variable 'bridge-leftovers)
  366. (setq bridge-string nil bridge-in-progress nil
  367. bridge-send-to-buffer nil)
  368. (if (boundp 'comint-prompt-regexp)
  369. (setq bridge-prompt-regexp comint-prompt-regexp))
  370. (let ((process (get-buffer-process (current-buffer))))
  371. (if process
  372. (if (not (eq (process-filter process) 'bridge-filter))
  373. (progn
  374. (setq bridge-old-filter (process-filter process))
  375. (set-process-filter process 'bridge-filter)))
  376. (error "%s does not have a process"
  377. (buffer-name (current-buffer)))))
  378. (run-hooks 'bridge-hook)
  379. (message "Process bridge is installed")))
  380. ;;;
  381. (defun reset-bridge ()
  382. "Must be called from the process's buffer. Removes any active bridge."
  383. (interactive)
  384. ;; for when things get wedged
  385. (if bridge-in-progress
  386. (unwind-protect
  387. (funcall bridge-in-progress (get-buffer-process
  388. (current-buffer))
  389. nil)
  390. (setq bridge-in-progress nil))
  391. (message "No bridge in progress.")))
  392. ;;;
  393. (defun remove-bridge ()
  394. "Remove bridge from the current buffer."
  395. (interactive)
  396. (let ((process (get-buffer-process (current-buffer))))
  397. (if (or (not process) (not (eq (process-filter process) 'bridge-filter)))
  398. (error "%s has no bridge" (buffer-name (current-buffer)))
  399. ;; remove any bridge-in-progress
  400. (reset-bridge)
  401. (set-process-filter process bridge-old-filter)
  402. (funcall bridge-old-filter process bridge-string)
  403. (message "Process bridge is removed."))))
  404. ;;;% Utility for testing
  405. (defun hand-bridge (start end)
  406. "With point at bridge-start, sends bridge-start + string +
  407. bridge-end to bridge-filter. With prefix, use current region to send."
  408. (interactive "r")
  409. (let ((p0 (if current-prefix-arg (min start end)
  410. (if (looking-at bridge-start-regexp) (point)
  411. (error "Not looking at bridge-start-regexp"))))
  412. (p1 (if current-prefix-arg (max start end)
  413. (if (re-search-forward bridge-end-regexp nil t)
  414. (point) (error "Didn't see bridge-end-regexp")))))
  415. (bridge-filter (get-buffer-process (current-buffer))
  416. (buffer-substring-no-properties p0 p1))
  417. ))
  418. (provide 'bridge)