123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655 |
- (require 'comint)
- (require 'lisp-mode)
- (defgroup inferior-lisp nil
- "Run an outside Lisp in an Emacs buffer."
- :group 'lisp
- :version "22.1")
- (defcustom inferior-lisp-filter-regexp
- (purecopy "\\`\\s *\\(:\\(\\w\\|\\s_\\)\\)?\\s *\\'")
- "*What not to save on inferior Lisp's input history.
- Input matching this regexp is not saved on the input history in Inferior Lisp
- mode. Default is whitespace followed by 0 or 1 single-letter colon-keyword
- \(as in :a, :c, etc.)"
- :type 'regexp
- :group 'inferior-lisp)
- (defvar inferior-lisp-mode-map
- (let ((map (copy-keymap comint-mode-map)))
- (set-keymap-parent map lisp-mode-shared-map)
- (define-key map "\C-x\C-e" 'lisp-eval-last-sexp)
- (define-key map "\C-c\C-l" 'lisp-load-file)
- (define-key map "\C-c\C-k" 'lisp-compile-file)
- (define-key map "\C-c\C-a" 'lisp-show-arglist)
- (define-key map "\C-c\C-d" 'lisp-describe-sym)
- (define-key map "\C-c\C-f" 'lisp-show-function-documentation)
- (define-key map "\C-c\C-v" 'lisp-show-variable-documentation)
- map))
- (define-key lisp-mode-map "\M-\C-x" 'lisp-eval-defun)
- (define-key lisp-mode-map "\C-x\C-e" 'lisp-eval-last-sexp)
- (define-key lisp-mode-map "\C-c\C-e" 'lisp-eval-defun)
- (define-key lisp-mode-map "\C-c\C-r" 'lisp-eval-region)
- (define-key lisp-mode-map "\C-c\C-c" 'lisp-compile-defun)
- (define-key lisp-mode-map "\C-c\C-z" 'switch-to-lisp)
- (define-key lisp-mode-map "\C-c\C-l" 'lisp-load-file)
- (define-key lisp-mode-map "\C-c\C-k" 'lisp-compile-file)
- (define-key lisp-mode-map "\C-c\C-a" 'lisp-show-arglist)
- (define-key lisp-mode-map "\C-c\C-d" 'lisp-describe-sym)
- (define-key lisp-mode-map "\C-c\C-f" 'lisp-show-function-documentation)
- (define-key lisp-mode-map "\C-c\C-v" 'lisp-show-variable-documentation)
- (defun inferior-lisp-install-letter-bindings ()
- (define-key lisp-mode-map "\C-ce" 'lisp-eval-defun-and-go)
- (define-key lisp-mode-map "\C-cr" 'lisp-eval-region-and-go)
- (define-key lisp-mode-map "\C-cc" 'lisp-compile-defun-and-go)
- (define-key lisp-mode-map "\C-cz" 'switch-to-lisp)
- (define-key lisp-mode-map "\C-cl" 'lisp-load-file)
- (define-key lisp-mode-map "\C-ck" 'lisp-compile-file)
- (define-key lisp-mode-map "\C-ca" 'lisp-show-arglist)
- (define-key lisp-mode-map "\C-cd" 'lisp-describe-sym)
- (define-key lisp-mode-map "\C-cf" 'lisp-show-function-documentation)
- (define-key lisp-mode-map "\C-cv" 'lisp-show-variable-documentation)
- (define-key inferior-lisp-mode-map "\C-cl" 'lisp-load-file)
- (define-key inferior-lisp-mode-map "\C-ck" 'lisp-compile-file)
- (define-key inferior-lisp-mode-map "\C-ca" 'lisp-show-arglist)
- (define-key inferior-lisp-mode-map "\C-cd" 'lisp-describe-sym)
- (define-key inferior-lisp-mode-map "\C-cf" 'lisp-show-function-documentation)
- (define-key inferior-lisp-mode-map "\C-cv"
- 'lisp-show-variable-documentation))
- (defcustom inferior-lisp-program (purecopy "lisp")
- "*Program name for invoking an inferior Lisp in Inferior Lisp mode."
- :type 'string
- :group 'inferior-lisp)
- (defcustom inferior-lisp-load-command (purecopy "(load \"%s\")\n")
- "*Format-string for building a Lisp expression to load a file.
- This format string should use `%s' to substitute a file name
- and should result in a Lisp expression that will command the inferior Lisp
- to load that file. The default works acceptably on most Lisps.
- The string \"(progn (load \\\"%s\\\" :verbose nil :print t) (values))\\n\"
- produces cosmetically superior output for this application,
- but it works only in Common Lisp."
- :type 'string
- :group 'inferior-lisp)
- (defcustom inferior-lisp-prompt (purecopy "^[^> \n]*>+:? *")
- "Regexp to recognize prompts in the Inferior Lisp mode.
- Defaults to \"^[^> \\n]*>+:? *\", which works pretty good for Lucid, kcl,
- and franz. This variable is used to initialize `comint-prompt-regexp' in the
- Inferior Lisp buffer.
- This variable is only used if the variable
- `comint-use-prompt-regexp' is non-nil.
- More precise choices:
- Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
- franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
- kcl: \"^>+ *\"
- This is a fine thing to set in your .emacs file or through Custom."
- :type 'regexp
- :group 'inferior-lisp)
- (defvar inferior-lisp-buffer nil "*The current inferior-lisp process buffer.
- MULTIPLE PROCESS SUPPORT
- ===========================================================================
- To run multiple Lisp processes, you start the first up
- with \\[inferior-lisp]. It will be in a buffer named `*inferior-lisp*'.
- Rename this buffer with \\[rename-buffer]. You may now start up a new
- process with another \\[inferior-lisp]. It will be in a new buffer,
- named `*inferior-lisp*'. You can switch between the different process
- buffers with \\[switch-to-buffer].
- Commands that send text from source buffers to Lisp processes --
- like `lisp-eval-defun' or `lisp-show-arglist' -- have to choose a process
- to send to, when you have more than one Lisp process around. This
- is determined by the global variable `inferior-lisp-buffer'. Suppose you
- have three inferior Lisps running:
- Buffer Process
- foo inferior-lisp
- bar inferior-lisp<2>
- *inferior-lisp* inferior-lisp<3>
- If you do a \\[lisp-eval-defun] command on some Lisp source code,
- what process do you send it to?
- - If you're in a process buffer (foo, bar, or *inferior-lisp*),
- you send it to that process.
- - If you're in some other buffer (e.g., a source file), you
- send it to the process attached to buffer `inferior-lisp-buffer'.
- This process selection is performed by function `inferior-lisp-proc'.
- Whenever \\[inferior-lisp] fires up a new process, it resets
- `inferior-lisp-buffer' to be the new process's buffer. If you only run
- one process, this does the right thing. If you run multiple
- processes, you can change `inferior-lisp-buffer' to another process
- buffer with \\[set-variable].")
- (defvar inferior-lisp-mode-hook '()
- "*Hook for customizing Inferior Lisp mode.")
- (put 'inferior-lisp-mode 'mode-class 'special)
- (define-derived-mode inferior-lisp-mode comint-mode "Inferior Lisp"
- "Major mode for interacting with an inferior Lisp process.
- Runs a Lisp interpreter as a subprocess of Emacs, with Lisp I/O through an
- Emacs buffer. Variable `inferior-lisp-program' controls which Lisp interpreter
- is run. Variables `inferior-lisp-prompt', `inferior-lisp-filter-regexp' and
- `inferior-lisp-load-command' can customize this mode for different Lisp
- interpreters.
- For information on running multiple processes in multiple buffers, see
- documentation for variable `inferior-lisp-buffer'.
- \\{inferior-lisp-mode-map}
- Customization: Entry to this mode runs the hooks on `comint-mode-hook' and
- `inferior-lisp-mode-hook' (in that order).
- You can send text to the inferior Lisp process from other buffers containing
- Lisp source.
- `switch-to-lisp' switches the current buffer to the Lisp process buffer.
- `lisp-eval-defun' sends the current defun to the Lisp process.
- `lisp-compile-defun' compiles the current defun.
- `lisp-eval-region' sends the current region to the Lisp process.
- `lisp-compile-region' compiles the current region.
- Prefixing the lisp-eval/compile-defun/region commands with
- a \\[universal-argument] causes a switch to the Lisp process buffer after sending
- the text.
- Commands:\\<inferior-lisp-mode-map>
- \\[comint-send-input] after the end of the process' output sends the text from the
- end of process to point.
- \\[comint-send-input] before the end of the process' output copies the sexp ending at point
- to the end of the process' output, and sends it.
- \\[comint-copy-old-input] copies the sexp ending at point to the end of the process' output,
- allowing you to edit it before sending it.
- If `comint-use-prompt-regexp' is nil (the default), \\[comint-insert-input] on old input
- copies the entire old input to the end of the process' output, allowing
- you to edit it before sending it. When not used on old input, or if
- `comint-use-prompt-regexp' is non-nil, \\[comint-insert-input] behaves according to
- its global binding.
- \\[backward-delete-char-untabify] converts tabs to spaces as it moves back.
- \\[lisp-indent-line] indents for Lisp; with argument, shifts rest
- of expression rigidly with the current line.
- \\[indent-sexp] does \\[lisp-indent-line] on each line starting within following expression.
- Paragraphs are separated only by blank lines. Semicolons start comments.
- If you accidentally suspend your process, use \\[comint-continue-subjob]
- to continue it."
- (setq comint-prompt-regexp inferior-lisp-prompt)
- (setq mode-line-process '(":%s"))
- (lisp-mode-variables t)
- (setq comint-get-old-input (function lisp-get-old-input))
- (setq comint-input-filter (function lisp-input-filter)))
- (defun lisp-get-old-input ()
- "Return a string containing the sexp ending at point."
- (save-excursion
- (let ((end (point)))
- (backward-sexp)
- (buffer-substring (point) end))))
- (defun lisp-input-filter (str)
- "t if STR does not match `inferior-lisp-filter-regexp'."
- (not (string-match inferior-lisp-filter-regexp str)))
- (defun inferior-lisp (cmd)
- "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'.
- If there is a process already running in `*inferior-lisp*', just switch
- to that buffer.
- With argument, allows you to edit the command line (default is value
- of `inferior-lisp-program'). Runs the hooks from
- `inferior-lisp-mode-hook' (after the `comint-mode-hook' is run).
- \(Type \\[describe-mode] in the process buffer for a list of commands.)"
- (interactive (list (if current-prefix-arg
- (read-string "Run lisp: " inferior-lisp-program)
- inferior-lisp-program)))
- (if (not (comint-check-proc "*inferior-lisp*"))
- (let ((cmdlist (split-string cmd)))
- (set-buffer (apply (function make-comint)
- "inferior-lisp" (car cmdlist) nil (cdr cmdlist)))
- (inferior-lisp-mode)))
- (setq inferior-lisp-buffer "*inferior-lisp*")
- (pop-to-buffer-same-window "*inferior-lisp*"))
- (defalias 'run-lisp 'inferior-lisp)
- (defun lisp-eval-region (start end &optional and-go)
- "Send the current region to the inferior Lisp process.
- Prefix argument means switch to the Lisp buffer afterwards."
- (interactive "r\nP")
- (comint-send-region (inferior-lisp-proc) start end)
- (comint-send-string (inferior-lisp-proc) "\n")
- (if and-go (switch-to-lisp t)))
- (defun lisp-compile-string (string)
- "Send the string to the inferior Lisp process to be compiled and executed."
- (comint-send-string
- (inferior-lisp-proc)
- (format "(funcall (compile nil (lambda () %s)))\n" string)))
- (defun lisp-eval-string (string)
- "Send the string to the inferior Lisp process to be executed."
- (comint-send-string (inferior-lisp-proc) (concat string "\n")))
- (defun lisp-do-defun (do-string do-region)
- "Send the current defun to the inferior Lisp process.
- The actually processing is done by `do-string' and `do-region'
- which determine whether the code is compiled before evaluation.
- DEFVAR forms reset the variables to the init values."
- (save-excursion
- (end-of-defun)
- (skip-chars-backward " \t\n\r\f")
- (let ((end (point)) (case-fold-search t))
- (beginning-of-defun)
- (if (looking-at "(defvar")
- (funcall do-string
-
- (concat "(defparameter "
- (buffer-substring-no-properties (+ (point) 7) end)
- "\n"))
- (funcall do-region (point) end)))))
- (defun lisp-eval-defun (&optional and-go)
- "Send the current defun to the inferior Lisp process.
- DEFVAR forms reset the variables to the init values.
- Prefix argument means switch to the Lisp buffer afterwards."
- (interactive "P")
- (lisp-do-defun 'lisp-eval-string 'lisp-eval-region)
- (if and-go (switch-to-lisp t)))
- (defun lisp-eval-last-sexp (&optional and-go)
- "Send the previous sexp to the inferior Lisp process.
- Prefix argument means switch to the Lisp buffer afterwards."
- (interactive "P")
- (lisp-eval-region (save-excursion (backward-sexp) (point)) (point) and-go))
- (defun lisp-compile-region (start end &optional and-go)
- "Compile the current region in the inferior Lisp process.
- Prefix argument means switch to the Lisp buffer afterwards."
- (interactive "r\nP")
- (lisp-compile-string (buffer-substring-no-properties start end))
- (if and-go (switch-to-lisp t)))
- (defun lisp-compile-defun (&optional and-go)
- "Compile the current defun in the inferior Lisp process.
- DEFVAR forms reset the variables to the init values.
- Prefix argument means switch to the Lisp buffer afterwards."
- (interactive "P")
- (lisp-do-defun 'lisp-compile-string 'lisp-compile-region)
- (if and-go (switch-to-lisp t)))
- (defun switch-to-lisp (eob-p)
- "Switch to the inferior Lisp process buffer.
- With argument, positions cursor at end of buffer."
- (interactive "P")
- (if (get-buffer-process inferior-lisp-buffer)
- (let ((pop-up-frames
-
-
- (or pop-up-frames
- (get-buffer-window inferior-lisp-buffer t))))
- (pop-to-buffer inferior-lisp-buffer))
- (run-lisp inferior-lisp-program))
- (when eob-p
- (push-mark)
- (goto-char (point-max))))
- (defun lisp-eval-region-and-go (start end)
- "Send the current region to the inferior Lisp, and switch to its buffer."
- (interactive "r")
- (lisp-eval-region start end t))
- (defun lisp-eval-defun-and-go ()
- "Send the current defun to the inferior Lisp, and switch to its buffer."
- (interactive)
- (lisp-eval-defun t))
- (defun lisp-compile-region-and-go (start end)
- "Compile the current region in the inferior Lisp, and switch to its buffer."
- (interactive "r")
- (lisp-compile-region start end t))
- (defun lisp-compile-defun-and-go ()
- "Compile the current defun in the inferior Lisp, and switch to its buffer."
- (interactive)
- (lisp-compile-defun t))
- (defvar lisp-prev-l/c-dir/file nil
- "Record last directory and file used in loading or compiling.
- This holds a cons cell of the form `(DIRECTORY . FILE)'
- describing the last `lisp-load-file' or `lisp-compile-file' command.")
- (defcustom lisp-source-modes '(lisp-mode)
- "*Used to determine if a buffer contains Lisp source code.
- If it's loaded into a buffer that is in one of these major modes, it's
- considered a Lisp source file by `lisp-load-file' and `lisp-compile-file'.
- Used by these commands to determine defaults."
- :type '(repeat symbol)
- :group 'inferior-lisp)
- (defun lisp-load-file (file-name)
- "Load a Lisp file into the inferior Lisp process."
- (interactive (comint-get-source "Load Lisp file: " lisp-prev-l/c-dir/file
- lisp-source-modes nil))
-
- (comint-check-source file-name)
- (setq lisp-prev-l/c-dir/file (cons (file-name-directory file-name)
- (file-name-nondirectory file-name)))
- (comint-send-string (inferior-lisp-proc)
- (format inferior-lisp-load-command file-name))
- (switch-to-lisp t))
- (defun lisp-compile-file (file-name)
- "Compile a Lisp file in the inferior Lisp process."
- (interactive (comint-get-source "Compile Lisp file: " lisp-prev-l/c-dir/file
- lisp-source-modes nil))
-
- (comint-check-source file-name)
- (setq lisp-prev-l/c-dir/file (cons (file-name-directory file-name)
- (file-name-nondirectory file-name)))
- (comint-send-string (inferior-lisp-proc) (concat "(compile-file \""
- file-name
- "\"\)\n"))
- (switch-to-lisp t))
- (defvar lisp-function-doc-command
- "(let ((fn '%s))
- (format t \"Documentation for ~a:~&~a\"
- fn (documentation fn 'function))
- (values))\n"
- "Command to query inferior Lisp for a function's documentation.")
- (defvar lisp-var-doc-command
- "(let ((v '%s))
- (format t \"Documentation for ~a:~&~a\"
- v (documentation v 'variable))
- (values))\n"
- "Command to query inferior Lisp for a variable's documentation.")
- (defvar lisp-arglist-command
- "(let ((fn '%s))
- (format t \"Arglist for ~a: ~a\" fn (arglist fn))
- (values))\n"
- "Command to query inferior Lisp for a function's arglist.")
- (defvar lisp-describe-sym-command
- "(describe '%s)\n"
- "Command to query inferior Lisp for a variable's documentation.")
- (defun lisp-symprompt (prompt default)
- (list (let* ((prompt (if default
- (format "%s (default %s): " prompt default)
- (concat prompt ": ")))
- (ans (read-string prompt)))
- (if (zerop (length ans)) default ans))))
- (defun lisp-fn-called-at-pt ()
- "Returns the name of the function called in the current call.
- The value is nil if it can't find one."
- (condition-case nil
- (save-excursion
- (save-restriction
- (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
- (backward-up-list 1)
- (forward-char 1)
- (let ((obj (read (current-buffer))))
- (and (symbolp obj) obj))))
- (error nil)))
- (defun lisp-var-at-pt ()
- (condition-case ()
- (save-excursion
- (forward-sexp -1)
- (skip-chars-forward "'")
- (let ((obj (read (current-buffer))))
- (and (symbolp obj) obj)))
- (error nil)))
- (defun lisp-show-function-documentation (fn)
- "Send a command to the inferior Lisp to give documentation for function FN.
- See variable `lisp-function-doc-command'."
- (interactive (lisp-symprompt "Function doc" (lisp-fn-called-at-pt)))
- (comint-proc-query (inferior-lisp-proc)
- (format lisp-function-doc-command fn)))
- (defun lisp-show-variable-documentation (var)
- "Send a command to the inferior Lisp to give documentation for function FN.
- See variable `lisp-var-doc-command'."
- (interactive (lisp-symprompt "Variable doc" (lisp-var-at-pt)))
- (comint-proc-query (inferior-lisp-proc) (format lisp-var-doc-command var)))
- (defun lisp-show-arglist (fn)
- "Send a query to the inferior Lisp for the arglist for function FN.
- See variable `lisp-arglist-command'."
- (interactive (lisp-symprompt "Arglist" (lisp-fn-called-at-pt)))
- (comint-proc-query (inferior-lisp-proc) (format lisp-arglist-command fn)))
- (defun lisp-describe-sym (sym)
- "Send a command to the inferior Lisp to describe symbol SYM.
- See variable `lisp-describe-sym-command'."
- (interactive (lisp-symprompt "Describe" (lisp-var-at-pt)))
- (comint-proc-query (inferior-lisp-proc)
- (format lisp-describe-sym-command sym)))
- (defun inferior-lisp-proc ()
- (let ((proc (get-buffer-process (if (derived-mode-p 'inferior-lisp-mode)
- (current-buffer)
- inferior-lisp-buffer))))
- (or proc
- (error "No Lisp subprocess; see variable `inferior-lisp-buffer'"))))
- (defvar inferior-lisp-load-hook nil
- "This hook is run when the library `inf-lisp' is loaded.")
- (run-hooks 'inferior-lisp-load-hook)
- (provide 'inf-lisp)
|