inf-lisp.el 27 KB

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