cmulisp.el 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. ;;; -*-Emacs-Lisp-*- cmulisp.el
  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. ;;; This replaces the standard inferior-lisp mode.
  8. ;;; Hacked from tea.el by Olin Shivers (shivers@cs.cmu.edu). 8/88
  9. ;;; Please send me bug reports, bug fixes, and extensions, so that I can
  10. ;;; merge them into the master source.
  11. ;;;
  12. ;;; Change log at end of file.
  13. ;;; This file defines a a lisp-in-a-buffer package (cmulisp mode) built on top
  14. ;;; of comint mode. Cmulisp mode is similar to, and intended to replace, its
  15. ;;; counterpart in the standard gnu emacs release. This replacements is more
  16. ;;; featureful, robust, and uniform than the released version. The key
  17. ;;; bindings are also more compatible with the bindings of Hemlock and Zwei
  18. ;;; (the Lisp Machine emacs).
  19. ;;; Since this mode is built on top of the general command-interpreter-in-
  20. ;;; a-buffer mode (comint mode), it shares a common base functionality,
  21. ;;; and a common set of bindings, with all modes derived from comint mode.
  22. ;;; This makes these modes easier to use.
  23. ;;; For documentation on the functionality provided by comint mode, and
  24. ;;; the hooks available for customising it, see the file comint.el.
  25. ;;; For further information on cmulisp mode, see the comments below.
  26. ;;; Needs fixin:
  27. ;;; The load-file/compile-file default mechanism could be smarter -- it
  28. ;;; doesn't know about the relationship between filename extensions and
  29. ;;; whether the file is source or executable. If you compile foo.lisp
  30. ;;; with compile-file, then the next load-file should use foo.bin for
  31. ;;; the default, not foo.lisp. This is tricky to do right, particularly
  32. ;;; because the extension for executable files varies so much (.o, .bin,
  33. ;;; .lbin, .mo, .vo, .ao, ...).
  34. ;;;
  35. ;;; It would be nice if cmulisp (and inferior scheme, T, ...) modes
  36. ;;; had a verbose minor mode wherein sending or compiling defuns, etc.
  37. ;;; would be reflected in the transcript with suitable comments, e.g.
  38. ;;; ";;; redefining fact". Several ways to do this. Which is right?
  39. ;;;
  40. ;;; When sending text from a source file to a subprocess, the process-mark can
  41. ;;; move off the window, so you can lose sight of the process interactions.
  42. ;;; Maybe I should ensure the process mark is in the window when I send
  43. ;;; text to the process? Switch selectable?
  44. (require 'comint)
  45. ;; YOUR .EMACS FILE
  46. ;;=============================================================================
  47. ;; Some suggestions for your .emacs file.
  48. ;;
  49. ;; ; If cmulisp lives in some non-standard directory, you must tell emacs
  50. ;; ; where to get it. This may or may not be necessary.
  51. ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
  52. ;;
  53. ;; ; Autoload cmulisp from file cmulisp.el
  54. ;; (autoload 'cmulisp "cmulisp"
  55. ;; "Run an inferior Lisp process."
  56. ;; t)
  57. ;;
  58. ;; ; Define C-c t to run my favorite command in cmulisp mode:
  59. ;; (setq cmulisp-load-hook
  60. ;; '((lambda ()
  61. ;; (define-key cmulisp-mode-map "\C-ct" 'favorite-cmd))))
  62. ;;; Brief Command Documentation:
  63. ;;;============================================================================
  64. ;;; Comint Mode Commands: (common to cmulisp and all comint-derived modes)
  65. ;;;
  66. ;;; m-p comint-previous-input Cycle backwards in input history
  67. ;;; m-n comint-next-input Cycle forwards
  68. ;;; m-c-r comint-previous-input-matching Search backwards in input history
  69. ;;; return comint-send-input
  70. ;;; c-a comint-bol Beginning of line; skip prompt.
  71. ;;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff.
  72. ;;; c-c c-u comint-kill-input ^u
  73. ;;; c-c c-w backward-kill-word ^w
  74. ;;; c-c c-c comint-interrupt-subjob ^c
  75. ;;; c-c c-z comint-stop-subjob ^z
  76. ;;; c-c c-\ comint-quit-subjob ^\
  77. ;;; c-c c-o comint-kill-output Delete last batch of process output
  78. ;;; c-c c-r comint-show-output Show last batch of process output
  79. ;;; send-invisible Read line w/o echo & send to proc
  80. ;;; comint-continue-subjob Useful if you accidentally suspend
  81. ;;; top-level job.
  82. ;;; comint-mode-hook is the comint mode hook.
  83. ;;; CMU Lisp Mode Commands:
  84. ;;; c-m-x lisp-send-defun This binding is a gnu convention.
  85. ;;; c-c c-l lisp-load-file Prompt for file name; tell Lisp to load it.
  86. ;;; c-c c-k lisp-compile-file Prompt for file name; tell Lisp to kompile it.
  87. ;;; Filename completion is available, of course.
  88. ;;;
  89. ;;; Additionally, these commands are added to the key bindings of Lisp mode:
  90. ;;; c-m-x lisp-eval-defun This binding is a gnu convention.
  91. ;;; c-c c-e lisp-eval-defun Send the current defun to Lisp process.
  92. ;;; c-x c-e lisp-eval-last-sexp Send the previous sexp to Lisp process.
  93. ;;; c-c c-r lisp-eval-region Send the current region to Lisp process.
  94. ;;; c-c c-c lisp-compile-defun Compile the current defun in Lisp process.
  95. ;;; c-c c-z switch-to-lisp Switch to the Lisp process buffer.
  96. ;;; c-c c-l lisp-load-file (See above. In a Lisp file buffer, default
  97. ;;; c-c c-k lisp-compile-file is to load/compile the current file.)
  98. ;;; c-c c-d lisp-describe-sym Query Lisp for a symbol's description.
  99. ;;; c-c c-a lisp-show-arglist Query Lisp for function's arglist.
  100. ;;; c-c c-f lisp-show-function-documentation Query Lisp for a function's doc.
  101. ;;; c-c c-v lisp-show-variable-documentation Query Lisp for a variable's doc.
  102. ;;; cmulisp Fires up the Lisp process.
  103. ;;; lisp-compile-region Compile all forms in the current region.
  104. ;;;
  105. ;;; CMU Lisp Mode Variables:
  106. ;;; cmulisp-filter-regexp Match this => don't get saved on input hist
  107. ;;; inferior-lisp-program Name of Lisp program run-lisp executes
  108. ;;; inferior-lisp-load-command Customises lisp-load-file
  109. ;;; cmulisp-mode-hook
  110. ;;; inferior-lisp-prompt Initialises comint-prompt-regexp.
  111. ;;; Backwards compatibility.
  112. ;;; lisp-source-modes Anything loaded into a buffer that's in
  113. ;;; one of these modes is considered Lisp
  114. ;;; source by lisp-load/compile-file.
  115. ;;; Read the rest of this file for more information.
  116. (defvar cmulisp-filter-regexp "\\`\\s *\\(:\\(\\w\\|\\s_\\)\\)?\\s *\\'"
  117. "*What not to save on inferior Lisp's input history
  118. Input matching this regexp is not saved on the input history in cmulisp
  119. mode. Default is whitespace followed by 0 or 1 single-letter :keyword
  120. (as in :a, :c, etc.)")
  121. (defvar cmulisp-mode-map nil)
  122. (cond ((not cmulisp-mode-map)
  123. (setq cmulisp-mode-map
  124. (copy-keymap comint-mode-map))
  125. (lisp-mode-commands cmulisp-mode-map)
  126. (define-key cmulisp-mode-map "\C-x\C-e" 'lisp-eval-last-sexp)
  127. (define-key cmulisp-mode-map "\C-c\C-l" 'lisp-load-file)
  128. (define-key cmulisp-mode-map "\C-c\C-k" 'lisp-compile-file)
  129. (define-key cmulisp-mode-map "\C-c\C-a" 'lisp-show-arglist)
  130. (define-key cmulisp-mode-map "\C-c\C-d" 'lisp-describe-sym)
  131. (define-key cmulisp-mode-map "\C-c\C-f" 'lisp-show-function-documentation)
  132. (define-key cmulisp-mode-map "\C-c\C-v" 'lisp-show-variable-documentation)))
  133. ;;; These commands augment Lisp mode, so you can process Lisp code in
  134. ;;; the source files.
  135. (define-key lisp-mode-map "\M-\C-x" 'lisp-eval-defun) ; Gnu convention
  136. (define-key lisp-mode-map "\C-x\C-e" 'lisp-eval-last-sexp) ; Gnu convention
  137. (define-key lisp-mode-map "\C-c\C-e" 'lisp-eval-defun)
  138. (define-key lisp-mode-map "\C-c\C-r" 'lisp-eval-region)
  139. (define-key lisp-mode-map "\C-c\C-c" 'lisp-compile-defun)
  140. (define-key lisp-mode-map "\C-c\C-z" 'switch-to-lisp)
  141. (define-key lisp-mode-map "\C-c\C-l" 'lisp-load-file)
  142. (define-key lisp-mode-map "\C-c\C-k" 'lisp-compile-file) ; "kompile" file
  143. (define-key lisp-mode-map "\C-c\C-a" 'lisp-show-arglist)
  144. (define-key lisp-mode-map "\C-c\C-d" 'lisp-describe-sym)
  145. (define-key lisp-mode-map "\C-c\C-f" 'lisp-show-function-documentation)
  146. (define-key lisp-mode-map "\C-c\C-v" 'lisp-show-variable-documentation)
  147. ;;; This function exists for backwards compatibility.
  148. ;;; Previous versions of this package bound commands to C-c <letter>
  149. ;;; bindings, which is not allowed by the gnumacs standard.
  150. (defun cmulisp-install-letter-bindings ()
  151. "This function binds many cmulisp commands to C-c <letter> bindings,
  152. where they are more accessible. C-c <letter> bindings are reserved for the
  153. user, so these bindings are non-standard. If you want them, you should
  154. have this function called by the cmulisp-load-hook:
  155. (setq cmulisp-load-hook '(cmulisp-install-letter-bindings))
  156. You can modify this function to install just the bindings you want."
  157. (define-key lisp-mode-map "\C-ce" 'lisp-eval-defun-and-go)
  158. (define-key lisp-mode-map "\C-cr" 'lisp-eval-region-and-go)
  159. (define-key lisp-mode-map "\C-cc" 'lisp-compile-defun-and-go)
  160. (define-key lisp-mode-map "\C-cz" 'switch-to-lisp)
  161. (define-key lisp-mode-map "\C-cl" 'lisp-load-file)
  162. (define-key lisp-mode-map "\C-ck" 'lisp-compile-file)
  163. (define-key lisp-mode-map "\C-ca" 'lisp-show-arglist)
  164. (define-key lisp-mode-map "\C-cd" 'lisp-describe-sym)
  165. (define-key lisp-mode-map "\C-cf" 'lisp-show-function-documentation)
  166. (define-key lisp-mode-map "\C-cv" 'lisp-show-variable-documentation)
  167. (define-key cmulisp-mode-map "\C-cl" 'lisp-load-file)
  168. (define-key cmulisp-mode-map "\C-ck" 'lisp-compile-file)
  169. (define-key cmulisp-mode-map "\C-ca" 'lisp-show-arglist)
  170. (define-key cmulisp-mode-map "\C-cd" 'lisp-describe-sym)
  171. (define-key cmulisp-mode-map "\C-cf" 'lisp-show-function-documentation)
  172. (define-key cmulisp-mode-map "\C-cv" 'lisp-show-variable-documentation))
  173. (defvar inferior-lisp-program "lisp"
  174. "*Program name for invoking an inferior Lisp with `cmulisp'.")
  175. (defvar inferior-lisp-load-command "(load \"%s\")\n"
  176. "*Format-string for building a Lisp expression to load a file.
  177. This format string should use %s to substitute a file name
  178. and should result in a Lisp expression that will command the inferior Lisp
  179. to load that file. The default works acceptably on most Lisps.
  180. The string \"(progn (load \\\"%s\\\" :verbose nil :print t) (values))\\\n\"
  181. produces cosmetically superior output for this application,
  182. but it works only in Common Lisp.")
  183. (defvar inferior-lisp-prompt "^[^> ]*>+:? *"
  184. "Regexp to recognise prompts in the inferior Lisp.
  185. Defaults to \"^[^> ]*>+:? *\", which works pretty good for Lucid, kcl,
  186. and franz. This variable is used to initialise comint-prompt-regexp in the
  187. cmulisp buffer.
  188. More precise choices:
  189. Lucid Common Lisp: \"^\\(>\\|\\(->\\)+\\) *\"
  190. franz: \"^\\(->\\|<[0-9]*>:\\) *\"
  191. kcl: \"^>+ *\"
  192. This is a fine thing to set in your .emacs file.")
  193. (defvar cmulisp-mode-hook '()
  194. "*Hook for customising cmulisp mode")
  195. (defun cmulisp-mode ()
  196. "Major mode for interacting with an inferior Lisp process.
  197. Runs a Lisp interpreter as a subprocess of Emacs, with Lisp I/O through an
  198. Emacs buffer. Variable inferior-lisp-program controls which Lisp interpreter
  199. is run. Variables inferior-lisp-prompt, cmulisp-filter-regexp and
  200. inferior-lisp-load-command can customize this mode for different Lisp
  201. interpreters.
  202. For information on running multiple processes in multiple buffers, see
  203. documentation for variable cmulisp-buffer.
  204. \\{cmulisp-mode-map}
  205. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  206. cmulisp-mode-hook (in that order).
  207. You can send text to the inferior Lisp process from other buffers containing
  208. Lisp source.
  209. switch-to-lisp switches the current buffer to the Lisp process buffer.
  210. lisp-eval-defun sends the current defun to the Lisp process.
  211. lisp-compile-defun compiles the current defun.
  212. lisp-eval-region sends the current region to the Lisp process.
  213. lisp-compile-region compiles the current region.
  214. Prefixing the lisp-eval/compile-defun/region commands with
  215. a \\[universal-argument] causes a switch to the Lisp process buffer after sending
  216. the text.
  217. Commands:
  218. Return after the end of the process' output sends the text from the
  219. end of process to point.
  220. Return before the end of the process' output copies the sexp ending at point
  221. to the end of the process' output, and sends it.
  222. Delete converts tabs to spaces as it moves back.
  223. Tab indents for Lisp; with argument, shifts rest
  224. of expression rigidly with the current line.
  225. C-M-q does Tab on each line starting within following expression.
  226. Paragraphs are separated only by blank lines. Semicolons start comments.
  227. If you accidentally suspend your process, use \\[comint-continue-subjob]
  228. to continue it."
  229. (interactive)
  230. (kill-all-local-variables)
  231. (comint-mode)
  232. (setq comint-prompt-regexp inferior-lisp-prompt)
  233. (setq major-mode 'cmulisp-mode)
  234. (setq mode-name "CMU Lisp")
  235. (setq mode-line-process '(": %s"))
  236. (if (string-match "^18.4" emacs-version) ; hack.
  237. (lisp-mode-variables) ; This is right for 18.49
  238. (lisp-mode-variables t)) ; This is right for 18.50
  239. (use-local-map cmulisp-mode-map) ;c-c c-k for "kompile" file
  240. (setq comint-get-old-input (function lisp-get-old-input))
  241. (setq comint-input-filter (function lisp-input-filter))
  242. (setq comint-input-sentinel 'ignore)
  243. (run-hooks 'cmulisp-mode-hook))
  244. (defun lisp-get-old-input ()
  245. "Snarf the sexp ending at point"
  246. (save-excursion
  247. (let ((end (point)))
  248. (backward-sexp)
  249. (buffer-substring (point) end))))
  250. (defun lisp-input-filter (str)
  251. "Don't save anything matching cmulisp-filter-regexp"
  252. (not (string-match cmulisp-filter-regexp str)))
  253. (defun cmulisp (&optional cmd)
  254. "Run an inferior Lisp process, input and output via buffer *cmulisp*.
  255. If there is a process already running in *cmulisp*, just switch to that buffer.
  256. With argument, allows you to edit the command line (default is value
  257. of inferior-lisp-program). Runs the hooks from cmulisp-mode-hook (after the
  258. comint-mode-hook is run).
  259. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  260. (interactive (list (and current-prefix-arg
  261. (read-string "Run lisp: " inferior-lisp-program))))
  262. (let ((cmd (or cmd inferior-lisp-program)))
  263. (if (not (comint-check-proc "*cmulisp*"))
  264. (let ((cmdlist (cmulisp-args-to-list cmd)))
  265. (set-buffer (apply (function make-comint) "cmulisp" (car cmdlist) nil
  266. (cdr cmdlist)))
  267. (cmulisp-mode))))
  268. (setq cmulisp-buffer "*cmulisp*")
  269. (switch-to-buffer "*cmulisp*"))
  270. ;;; Break a string up into a list of arguments.
  271. ;;; This will break if you have an argument with whitespace, as in
  272. ;;; string = "-ab +c -x 'you lose'".
  273. (defun cmulisp-args-to-list (string)
  274. (let ((where (string-match "[ \t]" string)))
  275. (cond ((null where) (list string))
  276. ((not (= where 0))
  277. (cons (substring string 0 where)
  278. (cmulisp-args-to-list (substring string (+ 1 where)
  279. (length string)))))
  280. (t (let ((pos (string-match "[^ \t]" string)))
  281. (if (null pos)
  282. nil
  283. (cmulisp-args-to-list (substring string pos
  284. (length string)))))))))
  285. (defun lisp-eval-region (start end &optional and-go)
  286. "Send the current region to the inferior Lisp process.
  287. Prefix argument means switch-to-lisp afterwards."
  288. (interactive "r\nP")
  289. (comint-send-region (cmulisp-proc) start end)
  290. (comint-send-string (cmulisp-proc) "\n")
  291. (if and-go (switch-to-lisp t)))
  292. (defun lisp-eval-defun (&optional and-go)
  293. "Send the current defun to the inferior Lisp process.
  294. Prefix argument means switch-to-lisp afterwards."
  295. (interactive "P")
  296. (save-excursion
  297. (end-of-defun)
  298. (skip-chars-backward " \t\n\r\f") ; Makes allegro happy
  299. (let ((end (point)))
  300. (beginning-of-defun)
  301. (lisp-eval-region (point) end)))
  302. (if and-go (switch-to-lisp t)))
  303. (defun lisp-eval-last-sexp (&optional and-go)
  304. "Send the previous sexp to the inferior Lisp process.
  305. Prefix argument means switch-to-lisp afterwards."
  306. (interactive "P")
  307. (lisp-eval-region (save-excursion (backward-sexp) (point)) (point) and-go))
  308. ;;; Common Lisp COMPILE sux.
  309. (defun lisp-compile-region (start end &optional and-go)
  310. "Compile the current region in the inferior Lisp process.
  311. Prefix argument means switch-to-lisp afterwards."
  312. (interactive "r\nP")
  313. (comint-send-string (cmulisp-proc)
  314. (format "(funcall (compile nil `(lambda () (progn 'compile %s))))\n"
  315. (buffer-substring start end)))
  316. (if and-go (switch-to-lisp t)))
  317. (defun lisp-compile-defun (&optional and-go)
  318. "Compile the current defun in the inferior Lisp process.
  319. Prefix argument means switch-to-lisp afterwards."
  320. (interactive "P")
  321. (save-excursion
  322. (end-of-defun)
  323. (skip-chars-backward " \t\n\r\f") ; Makes allegro happy
  324. (let ((e (point)))
  325. (beginning-of-defun)
  326. (lisp-compile-region (point) e)))
  327. (if and-go (switch-to-lisp t)))
  328. (defun switch-to-lisp (eob-p)
  329. "Switch to the inferior Lisp process buffer.
  330. With argument, positions cursor at end of buffer."
  331. (interactive "P")
  332. (if (get-buffer cmulisp-buffer)
  333. (pop-to-buffer cmulisp-buffer)
  334. (error "No current process buffer. See variable cmulisp-buffer."))
  335. (cond (eob-p
  336. (push-mark)
  337. (goto-char (point-max)))))
  338. ;;; Now that lisp-compile/eval-defun/region takes an optional prefix arg,
  339. ;;; these commands are redundant. But they are kept around for the user
  340. ;;; to bind if he wishes, for backwards functionality, and because it's
  341. ;;; easier to type C-c e than C-u C-c C-e.
  342. (defun lisp-eval-region-and-go (start end)
  343. "Send the current region to the inferior Lisp,
  344. and switch to the process buffer."
  345. (interactive "r")
  346. (lisp-eval-region start end t))
  347. (defun lisp-eval-defun-and-go ()
  348. "Send the current defun to the inferior Lisp,
  349. and switch to the process buffer."
  350. (interactive)
  351. (lisp-eval-defun t))
  352. (defun lisp-compile-region-and-go (start end)
  353. "Compile the current region in the inferior Lisp,
  354. and switch to the process buffer."
  355. (interactive "r")
  356. (lisp-compile-region start end t))
  357. (defun lisp-compile-defun-and-go ()
  358. "Compile the current defun in the inferior Lisp,
  359. and switch to the process buffer."
  360. (interactive)
  361. (lisp-compile-defun t))
  362. ;;; A version of the form in H. Shevis' soar-mode.el package. Less robust.
  363. ;(defun lisp-compile-sexp (start end)
  364. ; "Compile the s-expression bounded by START and END in the inferior lisp.
  365. ;If the sexp isn't a DEFUN form, it is evaluated instead."
  366. ; (cond ((looking-at "(defun\\s +")
  367. ; (goto-char (match-end 0))
  368. ; (let ((name-start (point)))
  369. ; (forward-sexp 1)
  370. ; (process-send-string "cmulisp" (format "(compile '%s #'(lambda "
  371. ; (buffer-substring name-start
  372. ; (point)))))
  373. ; (let ((body-start (point)))
  374. ; (goto-char start) (forward-sexp 1) ; Can't use end-of-defun.
  375. ; (process-send-region "cmulisp" (buffer-substring body-start (point))))
  376. ; (process-send-string "cmulisp" ")\n"))
  377. ; (t (lisp-eval-region start end)))))
  378. ;
  379. ;(defun lisp-compile-region (start end)
  380. ; "Each s-expression in the current region is compiled (if a DEFUN)
  381. ;or evaluated (if not) in the inferior lisp."
  382. ; (interactive "r")
  383. ; (save-excursion
  384. ; (goto-char start) (end-of-defun) (beginning-of-defun) ; error check
  385. ; (if (< (point) start) (error "region begins in middle of defun"))
  386. ; (goto-char start)
  387. ; (let ((s start))
  388. ; (end-of-defun)
  389. ; (while (<= (point) end) ; Zip through
  390. ; (lisp-compile-sexp s (point)) ; compiling up defun-sized chunks.
  391. ; (setq s (point))
  392. ; (end-of-defun))
  393. ; (if (< s end) (lisp-compile-sexp s end)))))
  394. ;;;
  395. ;;; End of HS-style code
  396. (defvar lisp-prev-l/c-dir/file nil
  397. "Saves the (directory . file) pair used in the last lisp-load-file or
  398. lisp-compile-file command. Used for determining the default in the
  399. next one.")
  400. (defvar lisp-source-modes '(lisp-mode)
  401. "*Used to determine if a buffer contains Lisp source code.
  402. If it's loaded into a buffer that is in one of these major modes, it's
  403. considered a Lisp source file by lisp-load-file and lisp-compile-file.
  404. Used by these commands to determine defaults.")
  405. (defun lisp-load-file (file-name)
  406. "Load a Lisp file into the inferior Lisp process."
  407. (interactive (comint-get-source "Load Lisp file: " lisp-prev-l/c-dir/file
  408. lisp-source-modes nil)) ; NIL because LOAD
  409. ; doesn't need an exact name
  410. (comint-check-source file-name) ; Check to see if buffer needs saved.
  411. (setq lisp-prev-l/c-dir/file (cons (file-name-directory file-name)
  412. (file-name-nondirectory file-name)))
  413. (comint-send-string (cmulisp-proc)
  414. (format inferior-lisp-load-command file-name))
  415. (switch-to-lisp t))
  416. (defun lisp-compile-file (file-name)
  417. "Compile a Lisp file in the inferior Lisp process."
  418. (interactive (comint-get-source "Compile Lisp file: " lisp-prev-l/c-dir/file
  419. lisp-source-modes nil)) ; NIL = don't need
  420. ; suffix .lisp
  421. (comint-check-source file-name) ; Check to see if buffer needs saved.
  422. (setq lisp-prev-l/c-dir/file (cons (file-name-directory file-name)
  423. (file-name-nondirectory file-name)))
  424. (comint-send-string (cmulisp-proc) (concat "(compile-file \""
  425. file-name
  426. "\"\)\n"))
  427. (switch-to-lisp t))
  428. ;;; Documentation functions: function doc, var doc, arglist, and
  429. ;;; describe symbol.
  430. ;;; ===========================================================================
  431. ;;; Command strings
  432. ;;; ===============
  433. (defvar lisp-function-doc-command
  434. "(let ((fn '%s))
  435. (format t \"Documentation for ~a:~&~a\"
  436. fn (documentation fn 'function))
  437. (values))\n"
  438. "Command to query inferior Lisp for a function's documentation.")
  439. (defvar lisp-var-doc-command
  440. "(let ((v '%s))
  441. (format t \"Documentation for ~a:~&~a\"
  442. v (documentation v 'variable))
  443. (values))\n"
  444. "Command to query inferior Lisp for a variable's documentation.")
  445. (defvar lisp-arglist-command
  446. "(let ((fn '%s))
  447. (format t \"Arglist for ~a: ~a\" fn (arglist fn))
  448. (values))\n"
  449. "Command to query inferior Lisp for a function's arglist.")
  450. (defvar lisp-describe-sym-command
  451. "(describe '%s)\n"
  452. "Command to query inferior Lisp for a variable's documentation.")
  453. ;;; Ancillary functions
  454. ;;; ===================
  455. ;;; Reads a string from the user.
  456. (defun lisp-symprompt (prompt default)
  457. (list (let* ((prompt (if default
  458. (format "%s (default %s): " prompt default)
  459. (concat prompt ": ")))
  460. (ans (read-string prompt)))
  461. (if (zerop (length ans)) default ans))))
  462. ;;; Adapted from function-called-at-point in help.el.
  463. (defun lisp-fn-called-at-pt ()
  464. "Returns the name of the function called in the current call.
  465. Nil if it can't find one."
  466. (condition-case nil
  467. (save-excursion
  468. (save-restriction
  469. (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
  470. (backward-up-list 1)
  471. (forward-char 1)
  472. (let ((obj (read (current-buffer))))
  473. (and (symbolp obj) obj))))
  474. (error nil)))
  475. ;;; Adapted from variable-at-point in help.el.
  476. (defun lisp-var-at-pt ()
  477. (condition-case ()
  478. (save-excursion
  479. (forward-sexp -1)
  480. (skip-chars-forward "'")
  481. (let ((obj (read (current-buffer))))
  482. (and (symbolp obj) obj)))
  483. (error nil)))
  484. ;;; Documentation functions: fn and var doc, arglist, and symbol describe.
  485. ;;; ======================================================================
  486. (defun lisp-show-function-documentation (fn)
  487. "Send a command to the inferior Lisp to give documentation for function FN.
  488. See variable lisp-function-doc-command."
  489. (interactive (lisp-symprompt "Function doc" (lisp-fn-called-at-pt)))
  490. (comint-proc-query (cmulisp-proc) (format lisp-function-doc-command fn)))
  491. (defun lisp-show-variable-documentation (var)
  492. "Send a command to the inferior Lisp to give documentation for function FN.
  493. See variable lisp-var-doc-command."
  494. (interactive (lisp-symprompt "Variable doc" (lisp-var-at-pt)))
  495. (comint-proc-query (cmulisp-proc) (format lisp-var-doc-command var)))
  496. (defun lisp-show-arglist (fn)
  497. "Sends an query to the inferior Lisp for the arglist for function FN.
  498. See variable lisp-arglist-command."
  499. (interactive (lisp-symprompt "Arglist" (lisp-fn-called-at-pt)))
  500. (comint-proc-query (cmulisp-proc) (format lisp-arglist-command fn)))
  501. (defun lisp-describe-sym (sym)
  502. "Send a command to the inferior Lisp to describe symbol SYM.
  503. See variable lisp-describe-sym-command."
  504. (interactive (lisp-symprompt "Describe" (lisp-var-at-pt)))
  505. (comint-proc-query (cmulisp-proc) (format lisp-describe-sym-command sym)))
  506. (defvar cmulisp-buffer nil "*The current cmulisp process buffer.
  507. MULTIPLE PROCESS SUPPORT
  508. ===========================================================================
  509. Cmulisp.el supports, in a fairly simple fashion, running multiple Lisp
  510. processes. To run multiple Lisp processes, you start the first up with
  511. \\[cmulisp]. It will be in a buffer named *cmulisp*. Rename this buffer
  512. with \\[rename-buffer]. You may now start up a new process with another
  513. \\[cmulisp]. It will be in a new buffer, named *cmulisp*. You can
  514. switch between the different process buffers with \\[switch-to-buffer].
  515. Commands that send text from source buffers to Lisp processes --
  516. like lisp-eval-defun or lisp-show-arglist -- have to choose a process
  517. to send to, when you have more than one Lisp process around. This
  518. is determined by the global variable cmulisp-buffer. Suppose you
  519. have three inferior lisps running:
  520. Buffer Process
  521. foo cmulisp
  522. bar cmulisp<2>
  523. *cmulisp* cmulisp<3>
  524. If you do a \\[lisp-eval-defun] command on some Lisp source code,
  525. what process do you send it to?
  526. - If you're in a process buffer (foo, bar, or *cmulisp*),
  527. you send it to that process.
  528. - If you're in some other buffer (e.g., a source file), you
  529. send it to the process attached to buffer cmulisp-buffer.
  530. This process selection is performed by function cmulisp-proc.
  531. Whenever \\[cmulisp] fires up a new process, it resets cmulisp-buffer
  532. to be the new process's buffer. If you only run one process, this will
  533. do the right thing. If you run multiple processes, you can change
  534. cmulisp-buffer to another process buffer with \\[set-variable].
  535. More sophisticated approaches are, of course, possible. If you find youself
  536. needing to switch back and forth between multiple processes frequently,
  537. you may wish to consider ilisp.el, a larger, more sophisticated package
  538. for running inferior Lisp processes. The approach taken here is for a
  539. minimal, simple implementation. Feel free to extend it.")
  540. (defun cmulisp-proc ()
  541. "Returns the current cmulisp process. See variable cmulisp-buffer."
  542. (let ((proc (get-buffer-process (if (eq major-mode 'inferior-lisp-mode)
  543. (current-buffer)
  544. cmulisp-buffer))))
  545. (or proc
  546. (error "No current process. See variable cmulisp-buffer"))))
  547. ;;; Do the user's customisation...
  548. ;;;===============================
  549. (defvar cmulisp-load-hook nil
  550. "This hook is run when cmulisp is loaded in.
  551. This is a good place to put keybindings.")
  552. (run-hooks 'cmulisp-load-hook)
  553. ;;; CHANGE LOG
  554. ;;; ===========================================================================
  555. ;;; 5/24/90 Olin
  556. ;;; - Split cmulisp and cmushell modes into separate files.
  557. ;;; Not only is this a good idea, it's apparently the way it'll be rel 19.
  558. ;;; - Upgraded process sends to use comint-send-string instead of
  559. ;;; process-send-string.
  560. ;;; - Explicit references to process "cmulisp" have been replaced with
  561. ;;; (cmulisp-proc). This allows better handling of multiple process bufs.
  562. ;;; - Added process query and var/function/symbol documentation
  563. ;;; commands. Based on code written by Douglas Roberts.
  564. ;;; - Added lisp-eval-last-sexp, bound to C-x C-e.
  565. ;;;
  566. ;;; 9/20/90 Olin
  567. ;;; Added a save-restriction to lisp-fn-called-at-pt. This bug and fix
  568. ;;; reported by Lennart Staflin.
  569. ;;;
  570. ;;; 3/12/90 Olin
  571. ;;; - lisp-load-file and lisp-compile-file no longer switch-to-lisp.
  572. ;;; Tale suggested this.
  573. ;;; - Reversed this decision 7/15/91. You need the visual feedback.
  574. ;;;
  575. ;;; 7/25/91 Olin
  576. ;;; Changed all keybindings of the form C-c <letter>. These are
  577. ;;; supposed to be reserved for the user to bind. This affected
  578. ;;; mainly the compile/eval-defun/region[-and-go] commands.
  579. ;;; This was painful, but necessary to adhere to the gnumacs standard.
  580. ;;; For some backwards compatibility, see the
  581. ;;; cmulisp-install-letter-bindings
  582. ;;; function.
  583. ;;;
  584. ;;; 8/2/91 Olin
  585. ;;; - The lisp-compile/eval-defun/region commands now take a prefix arg,
  586. ;;; which means switch-to-lisp after sending the text to the Lisp process.
  587. ;;; This obsoletes all the -and-go commands. The -and-go commands are
  588. ;;; kept around for historical reasons, and because the user can bind
  589. ;;; them to key sequences shorter than C-u C-c C-<letter>.
  590. ;;; - If M-x cmulisp is invoked with a prefix arg, it allows you to
  591. ;;; edit the command line.
  592. ;;;
  593. ;;; 11/91 Olin
  594. ;;; Added a (kill-all-local-variables) to cmulisp-mode, because comint-mode
  595. ;;; doesn't do it anymore.
  596. ;;;
  597. ;;; 12/91 Olin
  598. ;;; Changed a bogus recursion in cmulisp-args-to-list from
  599. ;;; tea-args-to-list to cmulisp-args-to-lisp. The perils of cut-and-paste
  600. ;;; programming. Bug reported by Miles Bader, who also suggested replacing
  601. ;;; the whole bogus parser with /bin/sh -c.
  602. (provide 'cmulisp)