1 ;;; inferior-shen-mode --- an inferior-shen mode
3 ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 ;; Authors: Michael Ilseman, Eric Schulte <schulte.eric@gmail.com>
7 ;; Keywords: languages, shen, comint
8 ;; Description: A major mode for editing shen source code
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file defines an inferior Shen mode.
34 (defvar inferior-shen-filter-regexp "\\`\\s *\\(:\\(\\w\\|\\s_\\)\\)?\\s *\\'"
35 "*What not to save on inferior Shen's input history.
36 Input matching this regexp is not saved on the input history in Inferior Shen
37 mode. Default is whitespace followed by 0 or 1 single-letter colon-keyword
38 \(as in :a, :c, etc.)")
40 (defvar inferior-shen-mode-map
41 (let ((map (make-sparse-keymap)))
42 (set-keymap-parent map comint-mode-map)
43 ;; (set-keymap-parent inferior-shen-mode-map shen-mode-shared-map)
44 (define-key map "\C-x\C-e" 'shen-eval-last-sexp)
45 (define-key map "\C-c\C-l" 'shen-load-file)
46 (define-key map "\C-c\C-k" 'shen-compile-file)
47 (define-key map "\C-c\C-a" 'shen-show-arglist)
48 (define-key map "\C-c\C-d" 'shen-describe-sym)
49 (define-key map "\C-c\C-f" 'shen-show-function-documentation)
50 (define-key map "\C-c\C-v" 'shen-show-variable-documentation)
53 ;; These commands augment Shen mode, so you can process Shen code in
55 (define-key shen-mode-map "\M-\C-x" 'shen-eval-defun) ; Gnu convention
56 (define-key shen-mode-map "\C-x\C-e" 'shen-eval-last-sexp) ; Gnu convention
57 (define-key shen-mode-map "\C-c\C-e" 'shen-eval-defun)
58 (define-key shen-mode-map "\C-c\C-r" 'shen-eval-region)
59 (define-key shen-mode-map "\C-c\C-c" 'shen-compile-defun)
60 (define-key shen-mode-map "\C-c\C-z" 'switch-to-shen)
61 (define-key shen-mode-map "\C-c\C-l" 'shen-load-file)
62 (define-key shen-mode-map "\C-c\C-k" 'shen-compile-file) ; "kompile" file
63 (define-key shen-mode-map "\C-c\C-a" 'shen-show-arglist)
64 (define-key shen-mode-map "\C-c\C-d" 'shen-describe-sym)
65 (define-key shen-mode-map "\C-c\C-f" 'shen-show-function-documentation)
66 (define-key shen-mode-map "\C-c\C-v" 'shen-show-variable-documentation)
69 ;; This function exists for backwards compatibility.
70 ;; Previous versions of this package bound commands to C-c <letter>
71 ;; bindings, which is not allowed by the gnumacs standard.
73 ;; "This function binds many inferior-shen commands to C-c <letter> bindings,
74 ;;where they are more accessible. C-c <letter> bindings are reserved for the
75 ;;user, so these bindings are non-standard. If you want them, you should
76 ;;have this function called by the inferior-shen-load-hook:
77 ;; (setq inferior-shen-load-hook '(inferior-shen-install-letter-bindings))
78 ;;You can modify this function to install just the bindings you want."
79 (defun inferior-shen-install-letter-bindings ()
80 (define-key shen-mode-map "\C-ce" 'shen-eval-defun-and-go)
81 (define-key shen-mode-map "\C-cr" 'shen-eval-region-and-go)
82 (define-key shen-mode-map "\C-cc" 'shen-compile-defun-and-go)
83 (define-key shen-mode-map "\C-cz" 'switch-to-shen)
84 (define-key shen-mode-map "\C-cl" 'shen-load-file)
85 (define-key shen-mode-map "\C-ck" 'shen-compile-file)
86 (define-key shen-mode-map "\C-ca" 'shen-show-arglist)
87 (define-key shen-mode-map "\C-cd" 'shen-describe-sym)
88 (define-key shen-mode-map "\C-cf" 'shen-show-function-documentation)
89 (define-key shen-mode-map "\C-cv" 'shen-show-variable-documentation)
91 (define-key inferior-shen-mode-map "\C-cl" 'shen-load-file)
92 (define-key inferior-shen-mode-map "\C-ck" 'shen-compile-file)
93 (define-key inferior-shen-mode-map "\C-ca" 'shen-show-arglist)
94 (define-key inferior-shen-mode-map "\C-cd" 'shen-describe-sym)
95 (define-key inferior-shen-mode-map "\C-cf" 'shen-show-function-documentation)
96 (define-key inferior-shen-mode-map "\C-cv"
97 'shen-show-variable-documentation))
100 (defvar inferior-shen-program "shen"
101 "*Program name for invoking an inferior Shen with for Inferior Shen mode.")
103 (defvar inferior-shen-load-command "(load \"%s\")\n"
104 "*Format-string for building a Shen expression to load a file.
105 This format string should use `%s' to substitute a file name
106 and should result in a Shen expression that will command the inferior Shen
107 to load that file. The default works acceptably on most Shens.
108 The string \"(progn (load \\\"%s\\\" :verbose nil :print t) (values))\\n\"
109 produces cosmetically superior output for this application,
110 but it works only in Common Shen.")
112 (defvar inferior-shen-prompt "^[^> \n]*>+:? *"
113 "Regexp to recognise prompts in the Inferior Shen mode.
114 Defaults to \"^[^> \\n]*>+:? *\", which works pretty good for Lucid, kcl,
115 and franz. This variable is used to initialize `comint-prompt-regexp' in the
116 Inferior Shen buffer.
118 This variable is only used if the variable
119 `comint-use-prompt-regexp-instead-of-fields' is non-nil.
121 More precise choices:
122 Lucid Common Shen: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
123 franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
126 This is a fine thing to set in your .emacs file.")
128 (defvar inferior-shen-buffer nil "*The current inferior-shen process buffer.
130 MULTIPLE PROCESS SUPPORT
131 ===========================================================================
132 To run multiple Shen processes, you start the first up
133 with \\[inferior-shen]. It will be in a buffer named `*inferior-shen*'.
134 Rename this buffer with \\[rename-buffer]. You may now start up a new
135 process with another \\[inferior-shen]. It will be in a new buffer,
136 named `*inferior-shen*'. You can switch between the different process
137 buffers with \\[switch-to-buffer].
139 Commands that send text from source buffers to Shen processes --
140 like `shen-eval-defun' or `shen-show-arglist' -- have to choose a process
141 to send to, when you have more than one Shen process around. This
142 is determined by the global variable `inferior-shen-buffer'. Suppose you
143 have three inferior Shens running:
147 *inferior-shen* inferior-shen<3>
148 If you do a \\[shen-eval-defun] command on some Shen source code,
149 what process do you send it to?
151 - If you're in a process buffer (foo, bar, or *inferior-shen*),
152 you send it to that process.
153 - If you're in some other buffer (e.g., a source file), you
154 send it to the process attached to buffer `inferior-shen-buffer'.
155 This process selection is performed by function `inferior-shen-proc'.
157 Whenever \\[inferior-shen] fires up a new process, it resets
158 `inferior-shen-buffer' to be the new process's buffer. If you only run
159 one process, this does the right thing. If you run multiple
160 processes, you can change `inferior-shen-buffer' to another process
161 buffer with \\[set-variable].")
163 (defvar inferior-shen-mode-hook '()
164 "*Hook for customising Inferior Shen mode.")
166 (put 'inferior-shen-mode 'mode-class 'special)
168 (define-derived-mode inferior-shen-mode comint-mode "Inferior Shen"
169 "Major mode for interacting with an inferior Shen process.
170 Runs a Shen interpreter as a subprocess of Emacs, with Shen I/O through an
171 Emacs buffer. Variable `inferior-shen-program' controls which Shen interpreter
172 is run. Variables `inferior-shen-prompt', `inferior-shen-filter-regexp' and
173 `inferior-shen-load-command' can customize this mode for different Shen
176 For information on running multiple processes in multiple buffers, see
177 documentation for variable `inferior-shen-buffer'.
179 \\{inferior-shen-mode-map}
181 Customisation: Entry to this mode runs the hooks on `comint-mode-hook' and
182 `inferior-shen-mode-hook' (in that order).
184 You can send text to the inferior Shen process from other buffers containing
186 switch-to-shen switches the current buffer to the Shen process buffer.
187 shen-eval-defun sends the current defun to the Shen process.
188 shen-compile-defun compiles the current defun.
189 shen-eval-region sends the current region to the Shen process.
190 shen-compile-region compiles the current region.
192 Prefixing the shen-eval/compile-defun/region commands with
193 a \\[universal-argument] causes a switch to the Shen process buffer after sending
197 Return after the end of the process' output sends the text from the
198 end of process to point.
199 Return before the end of the process' output copies the sexp ending at point
200 to the end of the process' output, and sends it.
201 Delete converts tabs to spaces as it moves back.
202 Tab indents for Shen; with argument, shifts rest
203 of expression rigidly with the current line.
204 C-M-q does Tab on each line starting within following expression.
205 Paragraphs are separated only by blank lines. Semicolons start comments.
206 If you accidentally suspend your process, use \\[comint-continue-subjob]
208 (set (make-local-variable 'font-lock-defaults) '(shen-font-lock-keywords))
209 (setq comint-prompt-regexp inferior-shen-prompt)
210 (setq mode-line-process '(":%s"))
212 (setq comint-get-old-input (function shen-get-old-input))
213 (setq comint-input-filter (function shen-input-filter)))
215 (defun shen-get-old-input ()
216 "Return a string containing the sexp ending at point."
220 (buffer-substring (point) end))))
222 (defun shen-input-filter (str)
223 "t if STR does not match `inferior-shen-filter-regexp'."
224 (not (string-match inferior-shen-filter-regexp str)))
227 (defun inferior-shen (cmd)
228 "Run an inferior Shen process, input and output via buffer `*inferior-shen*'.
229 If there is a process already running in `*inferior-shen*', just switch
231 With argument, allows you to edit the command line (default is value
232 of `inferior-shen-program'). Runs the hooks from
233 `inferior-shen-mode-hook' (after the `comint-mode-hook' is run).
234 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
235 (interactive (list (if current-prefix-arg
236 (read-string "Run shen: " inferior-shen-program)
237 inferior-shen-program)))
238 (if (not (comint-check-proc "*inferior-shen*"))
239 (let ((cmdlist (split-string cmd)))
240 (set-buffer (apply (function make-comint)
241 "inferior-shen" (car cmdlist) nil (cdr cmdlist)))
242 (inferior-shen-mode)))
243 (setq inferior-shen-buffer "*inferior-shen*")
244 (pop-to-buffer "*inferior-shen*"))
245 ;;;###autoload (add-hook 'same-window-buffer-names "*inferior-shen*")
248 (defalias 'run-shen 'inferior-shen)
250 (defcustom shen-pre-eval-hook '()
251 "Hook to run on code before sending it to the inferior-shen-process.
252 Functions on this hook will be called with an active region
253 containing the shen source code about to be evaluated.")
255 (defun shen-remember-functions (start end)
256 "Add functions defined between START and END to `shen-functions'."
258 (let ((clean (lambda (text)
260 (set-text-properties 0 (length text) nil text) text))))
264 "^(define[ \t]+\\(.+\\)[\n\r]" ; function name
265 "\\([ \t]*\\\\\\*[ \t]*\\([^\000]+?\\)[ \t]*\\*\\\\\\)?" ; doc
266 "[\n\r]?[ \t]*\\({\\(.+\\)}\\)?"))) ; type
267 (while (re-search-forward re end t)
268 (let ((name (intern (match-string 1)))
269 (doc (funcall clean (match-string 3)))
270 (type (funcall clean (match-string 5))))
271 (add-to-list 'shen-functions (list name type doc))))))))
273 (add-hook 'shen-pre-eval-hook #'shen-remember-functions)
275 (defun check-balanced-parens (start end)
276 "Check if parentheses in the region are balanced."
277 (save-restriction (save-excursion
278 (let ((deactivate-mark nil))
280 (progn (narrow-to-region start end) (goto-char (point-min))
281 (while (/= 0 (- (point) (forward-list)))) t)
282 (scan-error (signal 'scan-error '("Parentheses not balanced."))))))))
284 (add-hook 'shen-pre-eval-hook
286 (condition-case err (check-balanced-parens start end)
287 (error (unless (y-or-n-p (format "%s Eval anyway ?"
288 (error-message-string err)))
289 (signal 'scan-error err))))))
291 (defun shen-eval-region (start end &optional and-go)
292 "Send the current region to the inferior Shen process.
293 Prefix argument means switch to the Shen buffer afterwards."
295 (let ((before-input (marker-position (process-mark (inferior-shen-proc))))
298 (run-hook-with-args 'shen-pre-eval-hook start end)
299 (comint-send-region (inferior-shen-proc) start end)
300 (comint-send-string (inferior-shen-proc) "\n")
301 (accept-process-output (inferior-shen-proc))
303 (with-current-buffer inferior-shen-buffer
304 (goto-char before-input)
305 (setq result (buffer-substring (point) (point-at-eol)))
306 (message "%s" result)
307 (goto-char (process-mark (inferior-shen-proc))))
308 (if and-go (switch-to-shen t))
311 (defun shen-eval-defun (&optional and-go)
312 "Send the current defun to the inferior Shen process.
313 Prefix argument means switch to the Shen buffer afterwards."
318 (skip-chars-backward " \t\n\r\f") ; Makes allegro happy
321 (setq result (shen-eval-region (point) end))))
322 (if and-go (switch-to-shen t))
325 (defun shen-eval-last-sexp (&optional and-go)
326 "Send the previous sexp to the inferior Shen process.
327 Prefix argument means switch to the Shen buffer afterwards."
329 (shen-eval-region (save-excursion (backward-sexp) (point)) (point) and-go))
331 ;;; Common Shen COMPILE sux.
332 (defun shen-compile-region (start end &optional and-go)
333 "Compile the current region in the inferior Shen process.
334 Prefix argument means switch to the Shen buffer afterwards."
338 (format "(funcall (compile nil `(lambda () (progn 'compile %s))))\n"
339 (buffer-substring start end)))
340 (if and-go (switch-to-shen t)))
342 (defun shen-compile-defun (&optional and-go)
343 "Compile the current defun in the inferior Shen process.
344 Prefix argument means switch to the Shen buffer afterwards."
348 (skip-chars-backward " \t\n\r\f") ; Makes allegro happy
351 (shen-compile-region (point) e)))
352 (if and-go (switch-to-shen t)))
354 (defun switch-to-shen (eob-p)
355 "Switch to the inferior Shen process buffer.
356 With argument, positions cursor at end of buffer."
358 (if (get-buffer-process inferior-shen-buffer)
360 ;; Be willing to use another frame
361 ;; that already has the window in it.
363 (get-buffer-window inferior-shen-buffer t))))
364 (pop-to-buffer inferior-shen-buffer))
365 (run-shen inferior-shen-program))
368 (goto-char (point-max))))
371 ;; Now that shen-compile/eval-defun/region takes an optional prefix arg,
372 ;; these commands are redundant. But they are kept around for the user
373 ;; to bind if he wishes, for backwards functionality, and because it's
374 ;; easier to type C-c e than C-u C-c C-e.
375 (defun shen-eval-region-and-go (start end)
376 "Send the current region to the inferior Shen, and switch to its buffer."
378 (shen-eval-region start end t))
380 (defun shen-eval-defun-and-go ()
381 "Send the current defun to the inferior Shen, and switch to its buffer."
385 (defun shen-compile-region-and-go (start end)
386 "Compile the current region in the inferior Shen, and switch to its buffer."
388 (shen-compile-region start end t))
390 (defun shen-compile-defun-and-go ()
391 "Compile the current defun in the inferior Shen, and switch to its buffer."
393 (shen-compile-defun t))
395 ;; A version of the form in H. Shevis' soar-mode.el package. Less robust.
396 ;; (defun shen-compile-sexp (start end)
397 ;; "Compile the s-expression bounded by START and END in the inferior shen.
398 ;; If the sexp isn't a DEFUN form, it is evaluated instead."
399 ;; (cond ((looking-at "(defun\\s +")
400 ;; (goto-char (match-end 0))
401 ;; (let ((name-start (point)))
403 ;; (process-send-string "inferior-shen"
404 ;; (format "(compile '%s #'(lambda "
405 ;; (buffer-substring name-start
407 ;; (let ((body-start (point)))
408 ;; (goto-char start) (forward-sexp 1) ; Can't use end-of-defun.
409 ;; (process-send-region "inferior-shen"
410 ;; (buffer-substring body-start (point))))
411 ;; (process-send-string "inferior-shen" ")\n"))
412 ;; (t (shen-eval-region start end)))))
414 ;; (defun shen-compile-region (start end)
415 ;; "Each s-expression in the current region is compiled (if a DEFUN)
416 ;; or evaluated (if not) in the inferior shen."
419 ;; (goto-char start) (end-of-defun) (beginning-of-defun) ; error check
420 ;; (if (< (point) start) (error "region begins in middle of defun"))
424 ;; (while (<= (point) end) ; Zip through
425 ;; (shen-compile-sexp s (point)) ; compiling up defun-sized chunks.
428 ;; (if (< s end) (shen-compile-sexp s end)))))
430 ;; End of HS-style code
433 (defvar shen-prev-l/c-dir/file nil
434 "Record last directory and file used in loading or compiling.
435 This holds a cons cell of the form `(DIRECTORY . FILE)'
436 describing the last `shen-load-file' or `shen-compile-file' command.")
438 (defvar shen-source-modes '(shen-mode)
439 "*Used to determine if a buffer contains Shen source code.
440 If it's loaded into a buffer that is in one of these major modes, it's
441 considered a Shen source file by `shen-load-file' and `shen-compile-file'.
442 Used by these commands to determine defaults.")
444 (defun shen-load-file (file-name)
445 "Load a Shen file into the inferior Shen process."
446 (interactive (comint-get-source "Load Shen file: " shen-prev-l/c-dir/file
447 shen-source-modes nil)) ; NIL because LOAD
448 ; doesn't need an exact name
449 (comint-check-source file-name) ; Check to see if buffer needs saved.
450 (setq shen-prev-l/c-dir/file (cons (file-name-directory file-name)
451 (file-name-nondirectory file-name)))
452 (comint-send-string (inferior-shen-proc)
453 (format inferior-shen-load-command file-name))
457 (defun shen-compile-file (file-name)
458 "Compile a Shen file in the inferior Shen process."
459 (interactive (comint-get-source "Compile Shen file: " shen-prev-l/c-dir/file
460 shen-source-modes nil)) ; NIL = don't need
462 (comint-check-source file-name) ; Check to see if buffer needs saved.
463 (setq shen-prev-l/c-dir/file (cons (file-name-directory file-name)
464 (file-name-nondirectory file-name)))
465 (comint-send-string (inferior-shen-proc) (concat "(compile-file \""
472 ;;; Documentation functions: function doc, var doc, arglist, and
474 ;;; ===========================================================================
479 (defvar shen-function-doc-command
481 (format t \"Documentation for ~a:~&~a\"
482 fn (documentation fn 'function))
484 "Command to query inferior Shen for a function's documentation.")
486 (defvar shen-var-doc-command
488 (format t \"Documentation for ~a:~&~a\"
489 v (documentation v 'variable))
491 "Command to query inferior Shen for a variable's documentation.")
493 (defvar shen-arglist-command
495 (format t \"Arglist for ~a: ~a\" fn (arglist fn))
497 "Command to query inferior Shen for a function's arglist.")
499 (defvar shen-describe-sym-command
501 "Command to query inferior Shen for a variable's documentation.")
504 ;;; Ancillary functions
505 ;;; ===================
507 (defun shen-symprompt (prompt default)
508 "Read a string from the user."
509 (list (let* ((prompt (if default
510 (format "%s (default %s): " prompt default)
511 (concat prompt ": ")))
512 (ans (read-string prompt)))
513 (if (zerop (length ans)) default ans))))
516 ;; Adapted from function-called-at-point in help.el.
517 (defun shen-fn-called-at-pt ()
518 "Return the name of the function called in the current call.
519 The value is nil if it can't find one."
523 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
526 (let ((obj (read (current-buffer))))
527 (and (symbolp obj) obj))))
531 ;; Adapted from variable-at-point in help.el.
532 (defun shen-var-at-pt ()
536 (skip-chars-forward "'")
537 (let ((obj (read (current-buffer))))
538 (and (symbolp obj) obj)))
542 ;;; Documentation functions: fn and var doc, arglist, and symbol describe.
543 ;;; ======================================================================
544 (defun shen-show-function-documentation (fn)
545 "Send a command to the inferior Shen to give documentation for function FN.
546 See variable `shen-function-doc-command'."
547 (interactive (shen-symprompt "Function doc" (shen-fn-called-at-pt)))
548 (comint-proc-query (inferior-shen-proc)
549 (format shen-function-doc-command fn)))
551 (defun shen-show-variable-documentation (var)
552 "Send a command to the inferior Shen to give documentation for function FN.
553 See variable `shen-var-doc-command'."
554 (interactive (shen-symprompt "Variable doc" (shen-var-at-pt)))
555 (comint-proc-query (inferior-shen-proc) (format shen-var-doc-command var)))
557 (defun shen-show-arglist (fn)
558 "Send a query to the inferior Shen for the arglist for function FN.
559 See variable `shen-arglist-command'."
560 (interactive (shen-symprompt "Arglist" (shen-fn-called-at-pt)))
561 (comint-proc-query (inferior-shen-proc) (format shen-arglist-command fn)))
563 (defun shen-describe-sym (sym)
564 "Send a command to the inferior Shen to describe symbol SYM.
565 See variable `shen-describe-sym-command'."
566 (interactive (shen-symprompt "Describe" (shen-var-at-pt)))
567 (comint-proc-query (inferior-shen-proc)
568 (format shen-describe-sym-command sym)))
571 ;; "Returns the current inferior Shen process.
572 ;; See variable `inferior-shen-buffer'."
573 (defun inferior-shen-proc ()
574 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-shen-mode)
576 inferior-shen-buffer))))
578 (error "No Shen subprocess; see variable `inferior-shen-buffer'"))))
581 ;;; Do the user's customisation...
582 ;;;===============================
583 (defvar inferior-shen-load-hook nil
584 "This hook is run when the library `inf-shen' is loaded.
585 This is a good place to put keybindings.")
587 (run-hooks 'inferior-shen-load-hook)
590 ;;; inf-shen.el ends here