PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lisp/cmuscheme.el

https://bitbucket.org/zielmicha/emacs
Emacs Lisp | 524 lines | 363 code | 70 blank | 91 comment | 27 complexity | 77bf67375a634856385b8812d96c761d MD5 | raw file
  1. ;;; cmuscheme.el --- Scheme process in a buffer. Adapted from tea.el
  2. ;; Copyright (C) 1988, 1994, 1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Olin Shivers <olin.shivers@cs.cmu.edu>
  4. ;; Maintainer: FSF
  5. ;; Keywords: processes, lisp
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This is a customization of comint-mode (see comint.el)
  19. ;;
  20. ;; Written by Olin Shivers (olin.shivers@cs.cmu.edu). With bits and pieces
  21. ;; lifted from scheme.el, shell.el, clisp.el, newclisp.el, cobol.el, et al..
  22. ;; 8/88
  23. ;;
  24. ;; Please send me bug reports, bug fixes, and extensions, so that I can
  25. ;; merge them into the master source.
  26. ;;
  27. ;; The changelog is at the end of this file.
  28. ;;
  29. ;; NOTE: MIT Cscheme, when invoked with the -emacs flag, has a special user
  30. ;; interface that communicates process state back to the superior emacs by
  31. ;; outputting special control sequences. The gnumacs package, xscheme.el, has
  32. ;; lots and lots of special purpose code to read these control sequences, and
  33. ;; so is very tightly integrated with the cscheme process. The cscheme
  34. ;; interrupt handler and debugger read single character commands in cbreak
  35. ;; mode; when this happens, xscheme.el switches to special keymaps that bind
  36. ;; the single letter command keys to emacs functions that directly send the
  37. ;; character to the scheme process. Cmuscheme mode does *not* provide this
  38. ;; functionality. If you are a cscheme user, you may prefer to use the
  39. ;; xscheme.el/cscheme -emacs interaction.
  40. ;;
  41. ;; Here's a summary of the pros and cons, as I see them.
  42. ;; xscheme: Tightly integrated with inferior cscheme process! A few commands
  43. ;; not in cmuscheme. But. Integration is a bit of a hack. Input
  44. ;; history only keeps the immediately prior input. Bizarre
  45. ;; keybindings.
  46. ;;
  47. ;; cmuscheme: Not tightly integrated with inferior cscheme process. But.
  48. ;; Carefully integrated functionality with the entire suite of
  49. ;; comint-derived CMU process modes. Keybindings reminiscent of
  50. ;; Zwei and Hemlock. Good input history. A few commands not in
  51. ;; xscheme.
  52. ;;
  53. ;; It's a tradeoff. Pay your money; take your choice. If you use a Scheme
  54. ;; that isn't Cscheme, of course, there isn't a choice. Xscheme.el is *very*
  55. ;; Cscheme-specific; you must use cmuscheme.el. Interested parties are
  56. ;; invited to port xscheme functionality on top of comint mode...
  57. ;;; CHANGE LOG
  58. ;; ===========================================================================
  59. ;; 8/88 Olin
  60. ;; Created.
  61. ;;
  62. ;; 2/15/89 Olin
  63. ;; Removed -emacs flag from process invocation. It's only useful for
  64. ;; cscheme, and makes cscheme assume it's running under xscheme.el,
  65. ;; which messes things up royally. A bug.
  66. ;;
  67. ;; 5/22/90 Olin
  68. ;; - Upgraded to use comint-send-string and comint-send-region.
  69. ;; - run-scheme now offers to let you edit the command line if
  70. ;; you invoke it with a prefix-arg. M-x scheme is redundant, and
  71. ;; has been removed.
  72. ;; - Explicit references to process "scheme" have been replaced with
  73. ;; (scheme-proc). This allows better handling of multiple process bufs.
  74. ;; - Added scheme-send-last-sexp, bound to C-x C-e. A gnu convention.
  75. ;; - Have not added process query facility a la cmulisp.el's lisp-show-arglist
  76. ;; and friends, but interested hackers might find a useful application
  77. ;; of this facility.
  78. ;;
  79. ;; 3/12/90 Olin
  80. ;; - scheme-load-file and scheme-compile-file no longer switch-to-scheme.
  81. ;; Tale suggested this.
  82. ;;; Code:
  83. (require 'scheme)
  84. (require 'comint)
  85. (defgroup cmuscheme nil
  86. "Run a scheme process in a buffer."
  87. :group 'scheme)
  88. ;;; INFERIOR SCHEME MODE STUFF
  89. ;;;============================================================================
  90. (defcustom inferior-scheme-mode-hook nil
  91. "Hook for customizing inferior-scheme mode."
  92. :type 'hook
  93. :group 'cmuscheme)
  94. (defvar inferior-scheme-mode-map
  95. (let ((m (make-sparse-keymap)))
  96. (define-key m "\M-\C-x" 'scheme-send-definition) ;gnu convention
  97. (define-key m "\C-x\C-e" 'scheme-send-last-sexp)
  98. (define-key m "\C-c\C-l" 'scheme-load-file)
  99. (define-key m "\C-c\C-k" 'scheme-compile-file)
  100. (scheme-mode-commands m)
  101. m))
  102. ;; Install the process communication commands in the scheme-mode keymap.
  103. (define-key scheme-mode-map "\M-\C-x" 'scheme-send-definition);gnu convention
  104. (define-key scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp);gnu convention
  105. (define-key scheme-mode-map "\C-c\C-e" 'scheme-send-definition)
  106. (define-key scheme-mode-map "\C-c\M-e" 'scheme-send-definition-and-go)
  107. (define-key scheme-mode-map "\C-c\C-r" 'scheme-send-region)
  108. (define-key scheme-mode-map "\C-c\M-r" 'scheme-send-region-and-go)
  109. (define-key scheme-mode-map "\C-c\M-c" 'scheme-compile-definition)
  110. (define-key scheme-mode-map "\C-c\C-c" 'scheme-compile-definition-and-go)
  111. (define-key scheme-mode-map "\C-c\C-t" 'scheme-trace-procedure)
  112. (define-key scheme-mode-map "\C-c\C-x" 'scheme-expand-current-form)
  113. (define-key scheme-mode-map "\C-c\C-z" 'switch-to-scheme)
  114. (define-key scheme-mode-map "\C-c\C-l" 'scheme-load-file)
  115. (define-key scheme-mode-map "\C-c\C-k" 'scheme-compile-file) ;k for "kompile"
  116. (let ((map (lookup-key scheme-mode-map [menu-bar scheme])))
  117. (define-key map [separator-eval] '("--"))
  118. (define-key map [compile-file]
  119. '("Compile Scheme File" . scheme-compile-file))
  120. (define-key map [load-file]
  121. '("Load Scheme File" . scheme-load-file))
  122. (define-key map [switch]
  123. '("Switch to Scheme" . switch-to-scheme))
  124. (define-key map [com-def-go]
  125. '("Compile Definition & Go" . scheme-compile-definition-and-go))
  126. (define-key map [com-def]
  127. '("Compile Definition" . scheme-compile-definition))
  128. (define-key map [exp-form]
  129. '("Expand current form" . scheme-expand-current-form))
  130. (define-key map [trace-proc]
  131. '("Trace procedure" . scheme-trace-procedure))
  132. (define-key map [send-def-go]
  133. '("Evaluate Last Definition & Go" . scheme-send-definition-and-go))
  134. (define-key map [send-def]
  135. '("Evaluate Last Definition" . scheme-send-definition))
  136. (define-key map [send-region-go]
  137. '("Evaluate Region & Go" . scheme-send-region-and-go))
  138. (define-key map [send-region]
  139. '("Evaluate Region" . scheme-send-region))
  140. (define-key map [send-sexp]
  141. '("Evaluate Last S-expression" . scheme-send-last-sexp))
  142. )
  143. (defvar scheme-buffer)
  144. (define-derived-mode inferior-scheme-mode comint-mode "Inferior Scheme"
  145. "Major mode for interacting with an inferior Scheme process.
  146. The following commands are available:
  147. \\{inferior-scheme-mode-map}
  148. A Scheme process can be fired up with M-x run-scheme.
  149. Customization: Entry to this mode runs the hooks on comint-mode-hook and
  150. inferior-scheme-mode-hook (in that order).
  151. You can send text to the inferior Scheme process from other buffers containing
  152. Scheme source.
  153. switch-to-scheme switches the current buffer to the Scheme process buffer.
  154. scheme-send-definition sends the current definition to the Scheme process.
  155. scheme-compile-definition compiles the current definition.
  156. scheme-send-region sends the current region to the Scheme process.
  157. scheme-compile-region compiles the current region.
  158. scheme-send-definition-and-go, scheme-compile-definition-and-go,
  159. scheme-send-region-and-go, and scheme-compile-region-and-go
  160. switch to the Scheme process buffer after sending their text.
  161. For information on running multiple processes in multiple buffers, see
  162. documentation for variable scheme-buffer.
  163. Commands:
  164. Return after the end of the process' output sends the text from the
  165. end of process to point.
  166. Return before the end of the process' output copies the sexp ending at point
  167. to the end of the process' output, and sends it.
  168. Delete converts tabs to spaces as it moves back.
  169. Tab indents for Scheme; with argument, shifts rest
  170. of expression rigidly with the current line.
  171. C-M-q does Tab on each line starting within following expression.
  172. Paragraphs are separated only by blank lines. Semicolons start comments.
  173. If you accidentally suspend your process, use \\[comint-continue-subjob]
  174. to continue it."
  175. ;; Customize in inferior-scheme-mode-hook
  176. (setq comint-prompt-regexp "^[^>\n]*>+ *") ; OK for cscheme, oaklisp, T,...
  177. (scheme-mode-variables)
  178. (setq mode-line-process '(":%s"))
  179. (setq comint-input-filter (function scheme-input-filter))
  180. (setq comint-get-old-input (function scheme-get-old-input)))
  181. (defcustom inferior-scheme-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
  182. "Input matching this regexp are not saved on the history list.
  183. Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters."
  184. :type 'regexp
  185. :group 'cmuscheme)
  186. (defun scheme-input-filter (str)
  187. "Don't save anything matching `inferior-scheme-filter-regexp'."
  188. (not (string-match inferior-scheme-filter-regexp str)))
  189. (defun scheme-get-old-input ()
  190. "Snarf the sexp ending at point."
  191. (save-excursion
  192. (let ((end (point)))
  193. (backward-sexp)
  194. (buffer-substring (point) end))))
  195. ;;;###autoload
  196. (defun run-scheme (cmd)
  197. "Run an inferior Scheme process, input and output via buffer `*scheme*'.
  198. If there is a process already running in `*scheme*', switch to that buffer.
  199. With argument, allows you to edit the command line (default is value
  200. of `scheme-program-name').
  201. If the file `~/.emacs_SCHEMENAME' or `~/.emacs.d/init_SCHEMENAME.scm' exists,
  202. it is given as initial input.
  203. Note that this may lose due to a timing error if the Scheme processor
  204. discards input when it starts up.
  205. Runs the hook `inferior-scheme-mode-hook' \(after the `comint-mode-hook'
  206. is run).
  207. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  208. (interactive (list (if current-prefix-arg
  209. (read-string "Run Scheme: " scheme-program-name)
  210. scheme-program-name)))
  211. (if (not (comint-check-proc "*scheme*"))
  212. (let ((cmdlist (split-string-and-unquote cmd)))
  213. (set-buffer (apply 'make-comint "scheme" (car cmdlist)
  214. (scheme-start-file (car cmdlist)) (cdr cmdlist)))
  215. (inferior-scheme-mode)))
  216. (setq scheme-program-name cmd)
  217. (setq scheme-buffer "*scheme*")
  218. (pop-to-buffer-same-window "*scheme*"))
  219. (defun scheme-start-file (prog)
  220. "Return the name of the start file corresponding to PROG.
  221. Search in the directories \"~\" and \"~/.emacs.d\", in this
  222. order. Return nil if no start file found."
  223. (let* ((progname (file-name-nondirectory prog))
  224. (start-file (concat "~/.emacs_" progname))
  225. (alt-start-file (concat user-emacs-directory "init_" progname ".scm")))
  226. (if (file-exists-p start-file)
  227. start-file
  228. (and (file-exists-p alt-start-file) alt-start-file))))
  229. (defun scheme-send-region (start end)
  230. "Send the current region to the inferior Scheme process."
  231. (interactive "r")
  232. (comint-send-region (scheme-proc) start end)
  233. (comint-send-string (scheme-proc) "\n"))
  234. (defun scheme-send-definition ()
  235. "Send the current definition to the inferior Scheme process."
  236. (interactive)
  237. (save-excursion
  238. (end-of-defun)
  239. (let ((end (point)))
  240. (beginning-of-defun)
  241. (scheme-send-region (point) end))))
  242. (defun scheme-send-last-sexp ()
  243. "Send the previous sexp to the inferior Scheme process."
  244. (interactive)
  245. (scheme-send-region (save-excursion (backward-sexp) (point)) (point)))
  246. (defcustom scheme-compile-exp-command "(compile '%s)"
  247. "Template for issuing commands to compile arbitrary Scheme expressions."
  248. :type 'string
  249. :group 'cmuscheme)
  250. (defun scheme-compile-region (start end)
  251. "Compile the current region in the inferior Scheme process.
  252. \(A BEGIN is wrapped around the region: (BEGIN <region>))"
  253. (interactive "r")
  254. (comint-send-string (scheme-proc) (format scheme-compile-exp-command
  255. (format "(begin %s)"
  256. (buffer-substring start end))))
  257. (comint-send-string (scheme-proc) "\n"))
  258. (defun scheme-compile-definition ()
  259. "Compile the current definition in the inferior Scheme process."
  260. (interactive)
  261. (save-excursion
  262. (end-of-defun)
  263. (let ((end (point)))
  264. (beginning-of-defun)
  265. (scheme-compile-region (point) end))))
  266. (defcustom scheme-trace-command "(trace %s)"
  267. "Template for issuing commands to trace a Scheme procedure.
  268. Some Scheme implementations might require more elaborate commands here.
  269. For PLT-Scheme, e.g., one should use
  270. (setq scheme-trace-command \"(begin (require (lib \\\"trace.ss\\\")) (trace %s))\")
  271. For Scheme 48 and Scsh use \",trace %s\"."
  272. :type 'string
  273. :group 'cmuscheme)
  274. (defcustom scheme-untrace-command "(untrace %s)"
  275. "Template for switching off tracing of a Scheme procedure.
  276. Scheme 48 and Scsh users should set this variable to \",untrace %s\"."
  277. :type 'string
  278. :group 'cmuscheme)
  279. (defun scheme-trace-procedure (proc &optional untrace)
  280. "Trace procedure PROC in the inferior Scheme process.
  281. With a prefix argument switch off tracing of procedure PROC."
  282. (interactive
  283. (list (let ((current (symbol-at-point))
  284. (action (if current-prefix-arg "Untrace" "Trace")))
  285. (if current
  286. (read-string (format "%s procedure [%s]: " action current) nil nil (symbol-name current))
  287. (read-string (format "%s procedure: " action))))
  288. current-prefix-arg))
  289. (when (= (length proc) 0)
  290. (error "Invalid procedure name"))
  291. (comint-send-string (scheme-proc)
  292. (format
  293. (if untrace scheme-untrace-command scheme-trace-command)
  294. proc))
  295. (comint-send-string (scheme-proc) "\n"))
  296. (defcustom scheme-macro-expand-command "(expand %s)"
  297. "Template for macro-expanding a Scheme form.
  298. For Scheme 48 and Scsh use \",expand %s\"."
  299. :type 'string
  300. :group 'cmuscheme)
  301. (defun scheme-expand-current-form ()
  302. "Macro-expand the form at point in the inferior Scheme process."
  303. (interactive)
  304. (let ((current-form (scheme-form-at-point)))
  305. (if current-form
  306. (progn
  307. (comint-send-string (scheme-proc)
  308. (format
  309. scheme-macro-expand-command
  310. current-form))
  311. (comint-send-string (scheme-proc) "\n"))
  312. (error "Not at a form"))))
  313. (defun scheme-form-at-point ()
  314. (let ((next-sexp (thing-at-point 'sexp)))
  315. (if (and next-sexp (string-equal (substring next-sexp 0 1) "("))
  316. next-sexp
  317. (save-excursion
  318. (backward-up-list)
  319. (scheme-form-at-point)))))
  320. (defun switch-to-scheme (eob-p)
  321. "Switch to the scheme process buffer.
  322. With argument, position cursor at end of buffer."
  323. (interactive "P")
  324. (if (or (and scheme-buffer (get-buffer scheme-buffer))
  325. (scheme-interactively-start-process))
  326. (pop-to-buffer-same-window scheme-buffer)
  327. (error "No current process buffer. See variable `scheme-buffer'"))
  328. (when eob-p
  329. (push-mark)
  330. (goto-char (point-max))))
  331. (defun scheme-send-region-and-go (start end)
  332. "Send the current region to the inferior Scheme process.
  333. Then switch to the process buffer."
  334. (interactive "r")
  335. (scheme-send-region start end)
  336. (switch-to-scheme t))
  337. (defun scheme-send-definition-and-go ()
  338. "Send the current definition to the inferior Scheme.
  339. Then switch to the process buffer."
  340. (interactive)
  341. (scheme-send-definition)
  342. (switch-to-scheme t))
  343. (defun scheme-compile-definition-and-go ()
  344. "Compile the current definition in the inferior Scheme.
  345. Then switch to the process buffer."
  346. (interactive)
  347. (scheme-compile-definition)
  348. (switch-to-scheme t))
  349. (defun scheme-compile-region-and-go (start end)
  350. "Compile the current region in the inferior Scheme.
  351. Then switch to the process buffer."
  352. (interactive "r")
  353. (scheme-compile-region start end)
  354. (switch-to-scheme t))
  355. (defcustom scheme-source-modes '(scheme-mode)
  356. "Used to determine if a buffer contains Scheme source code.
  357. If it's loaded into a buffer that is in one of these major modes, it's
  358. considered a scheme source file by `scheme-load-file' and `scheme-compile-file'.
  359. Used by these commands to determine defaults."
  360. :type '(repeat function)
  361. :group 'cmuscheme)
  362. (defvar scheme-prev-l/c-dir/file nil
  363. "Caches the last (directory . file) pair.
  364. Caches the last pair used in the last `scheme-load-file' or
  365. `scheme-compile-file' command. Used for determining the default
  366. in the next one.")
  367. (defun scheme-load-file (file-name)
  368. "Load a Scheme file FILE-NAME into the inferior Scheme process."
  369. (interactive (comint-get-source "Load Scheme file: " scheme-prev-l/c-dir/file
  370. scheme-source-modes t)) ; t because `load'
  371. ; needs an exact name
  372. (comint-check-source file-name) ; Check to see if buffer needs saved.
  373. (setq scheme-prev-l/c-dir/file (cons (file-name-directory file-name)
  374. (file-name-nondirectory file-name)))
  375. (comint-send-string (scheme-proc) (concat "(load \""
  376. file-name
  377. "\"\)\n")))
  378. (defun scheme-compile-file (file-name)
  379. "Compile a Scheme file FILE-NAME in the inferior Scheme process."
  380. (interactive (comint-get-source "Compile Scheme file: "
  381. scheme-prev-l/c-dir/file
  382. scheme-source-modes
  383. nil)) ; nil because COMPILE doesn't
  384. ; need an exact name.
  385. (comint-check-source file-name) ; Check to see if buffer needs saved.
  386. (setq scheme-prev-l/c-dir/file (cons (file-name-directory file-name)
  387. (file-name-nondirectory file-name)))
  388. (comint-send-string (scheme-proc) (concat "(compile-file \""
  389. file-name
  390. "\"\)\n")))
  391. (defvar scheme-buffer nil "*The current scheme process buffer.
  392. MULTIPLE PROCESS SUPPORT
  393. ===========================================================================
  394. Cmuscheme.el supports, in a fairly simple fashion, running multiple Scheme
  395. processes. To run multiple Scheme processes, you start the first up with
  396. \\[run-scheme]. It will be in a buffer named *scheme*. Rename this buffer
  397. with \\[rename-buffer]. You may now start up a new process with another
  398. \\[run-scheme]. It will be in a new buffer, named *scheme*. You can
  399. switch between the different process buffers with \\[switch-to-buffer].
  400. Commands that send text from source buffers to Scheme processes --
  401. like `scheme-send-definition' or `scheme-compile-region' -- have to choose a
  402. process to send to, when you have more than one Scheme process around. This
  403. is determined by the global variable `scheme-buffer'. Suppose you
  404. have three inferior Schemes running:
  405. Buffer Process
  406. foo scheme
  407. bar scheme<2>
  408. *scheme* scheme<3>
  409. If you do a \\[scheme-send-definition-and-go] command on some Scheme source
  410. code, what process do you send it to?
  411. - If you're in a process buffer (foo, bar, or *scheme*),
  412. you send it to that process.
  413. - If you're in some other buffer (e.g., a source file), you
  414. send it to the process attached to buffer `scheme-buffer'.
  415. This process selection is performed by function `scheme-proc'.
  416. Whenever \\[run-scheme] fires up a new process, it resets `scheme-buffer'
  417. to be the new process's buffer. If you only run one process, this will
  418. do the right thing. If you run multiple processes, you can change
  419. `scheme-buffer' to another process buffer with \\[set-variable].
  420. More sophisticated approaches are, of course, possible. If you find yourself
  421. needing to switch back and forth between multiple processes frequently,
  422. you may wish to consider ilisp.el, a larger, more sophisticated package
  423. for running inferior Lisp and Scheme processes. The approach taken here is
  424. for a minimal, simple implementation. Feel free to extend it.")
  425. (defun scheme-proc ()
  426. "Return the current Scheme process, starting one if necessary.
  427. See variable `scheme-buffer'."
  428. (unless (and scheme-buffer
  429. (get-buffer scheme-buffer)
  430. (comint-check-proc scheme-buffer))
  431. (scheme-interactively-start-process))
  432. (or (scheme-get-process)
  433. (error "No current process. See variable `scheme-buffer'")))
  434. (defun scheme-get-process ()
  435. "Return the current Scheme process or nil if none is running."
  436. (get-buffer-process (if (eq major-mode 'inferior-scheme-mode)
  437. (current-buffer)
  438. scheme-buffer)))
  439. (defun scheme-interactively-start-process (&optional _cmd)
  440. "Start an inferior Scheme process. Return the process started.
  441. Since this command is run implicitly, always ask the user for the
  442. command to run."
  443. (save-window-excursion
  444. (run-scheme (read-string "Run Scheme: " scheme-program-name))))
  445. ;;; Do the user's customization...
  446. (defcustom cmuscheme-load-hook nil
  447. "This hook is run when cmuscheme is loaded in.
  448. This is a good place to put keybindings."
  449. :type 'hook
  450. :group 'cmuscheme)
  451. (run-hooks 'cmuscheme-load-hook)
  452. (provide 'cmuscheme)
  453. ;;; cmuscheme.el ends here