inf-lisp.el 26 KB

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