1 ;;; inferior-shen-mode --- an inferior-shen mode
3 ;; Copyright (C) 2011 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 nil)
41 (unless inferior-shen-mode-map
42 (setq inferior-shen-mode-map (copy-keymap comint-mode-map))
43 ; (set-keymap-parent inferior-shen-mode-map shen-mode-shared-map)
44 (define-key inferior-shen-mode-map "\C-x\C-e" 'shen-eval-last-sexp)
45 (define-key inferior-shen-mode-map "\C-c\C-l" 'shen-load-file)
46 (define-key inferior-shen-mode-map "\C-c\C-k" 'shen-compile-file)
47 (define-key inferior-shen-mode-map "\C-c\C-a" 'shen-show-arglist)
48 (define-key inferior-shen-mode-map "\C-c\C-d" 'shen-describe-sym)
49 (define-key inferior-shen-mode-map "\C-c\C-f"
50 'shen-show-function-documentation)
51 (define-key inferior-shen-mode-map "\C-c\C-v"
52 'shen-show-variable-documentation))
54 ;;; These commands augment Shen mode, so you can process Shen code in
56 (define-key shen-mode-map "\M-\C-x" 'shen-eval-defun) ; Gnu convention
57 (define-key shen-mode-map "\C-x\C-e" 'shen-eval-last-sexp) ; Gnu convention
58 (define-key shen-mode-map "\C-c\C-e" 'shen-eval-defun)
59 (define-key shen-mode-map "\C-c\C-r" 'shen-eval-region)
60 (define-key shen-mode-map "\C-c\C-c" 'shen-compile-defun)
61 (define-key shen-mode-map "\C-c\C-z" 'switch-to-shen)
62 (define-key shen-mode-map "\C-c\C-l" 'shen-load-file)
63 (define-key shen-mode-map "\C-c\C-k" 'shen-compile-file) ; "kompile" file
64 (define-key shen-mode-map "\C-c\C-a" 'shen-show-arglist)
65 (define-key shen-mode-map "\C-c\C-d" 'shen-describe-sym)
66 (define-key shen-mode-map "\C-c\C-f" 'shen-show-function-documentation)
67 (define-key shen-mode-map "\C-c\C-v" 'shen-show-variable-documentation)
70 ;;; This function exists for backwards compatibility.
71 ;;; Previous versions of this package bound commands to C-c <letter>
72 ;;; bindings, which is not allowed by the gnumacs standard.
74 ;;; "This function binds many inferior-shen commands to C-c <letter> bindings,
75 ;;;where they are more accessible. C-c <letter> bindings are reserved for the
76 ;;;user, so these bindings are non-standard. If you want them, you should
77 ;;;have this function called by the inferior-shen-load-hook:
78 ;;; (setq inferior-shen-load-hook '(inferior-shen-install-letter-bindings))
79 ;;;You can modify this function to install just the bindings you want."
80 (defun inferior-shen-install-letter-bindings ()
81 (define-key shen-mode-map "\C-ce" 'shen-eval-defun-and-go)
82 (define-key shen-mode-map "\C-cr" 'shen-eval-region-and-go)
83 (define-key shen-mode-map "\C-cc" 'shen-compile-defun-and-go)
84 (define-key shen-mode-map "\C-cz" 'switch-to-shen)
85 (define-key shen-mode-map "\C-cl" 'shen-load-file)
86 (define-key shen-mode-map "\C-ck" 'shen-compile-file)
87 (define-key shen-mode-map "\C-ca" 'shen-show-arglist)
88 (define-key shen-mode-map "\C-cd" 'shen-describe-sym)
89 (define-key shen-mode-map "\C-cf" 'shen-show-function-documentation)
90 (define-key shen-mode-map "\C-cv" 'shen-show-variable-documentation)
92 (define-key inferior-shen-mode-map "\C-cl" 'shen-load-file)
93 (define-key inferior-shen-mode-map "\C-ck" 'shen-compile-file)
94 (define-key inferior-shen-mode-map "\C-ca" 'shen-show-arglist)
95 (define-key inferior-shen-mode-map "\C-cd" 'shen-describe-sym)
96 (define-key inferior-shen-mode-map "\C-cf" 'shen-show-function-documentation)
97 (define-key inferior-shen-mode-map "\C-cv"
98 'shen-show-variable-documentation))
102 (defvar inferior-shen-program "shen"
103 "*Program name for invoking an inferior Shen with for Inferior Shen mode.")
106 (defvar inferior-shen-load-command "(load \"%s\")\n"
107 "*Format-string for building a Shen expression to load a file.
108 This format string should use `%s' to substitute a file name
109 and should result in a Shen expression that will command the inferior Shen
110 to load that file. The default works acceptably on most Shens.
111 The string \"(progn (load \\\"%s\\\" :verbose nil :print t) (values))\\n\"
112 produces cosmetically superior output for this application,
113 but it works only in Common Shen.")
116 (defvar inferior-shen-prompt "^[^> \n]*>+:? *"
117 "Regexp to recognise prompts in the Inferior Shen mode.
118 Defaults to \"^[^> \\n]*>+:? *\", which works pretty good for Lucid, kcl,
119 and franz. This variable is used to initialize `comint-prompt-regexp' in the
120 Inferior Shen buffer.
122 This variable is only used if the variable
123 `comint-use-prompt-regexp-instead-of-fields' is non-nil.
125 More precise choices:
126 Lucid Common Shen: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
127 franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
130 This is a fine thing to set in your .emacs file.")
132 (defvar inferior-shen-buffer nil "*The current inferior-shen process buffer.
134 MULTIPLE PROCESS SUPPORT
135 ===========================================================================
136 To run multiple Shen processes, you start the first up
137 with \\[inferior-shen]. It will be in a buffer named `*inferior-shen*'.
138 Rename this buffer with \\[rename-buffer]. You may now start up a new
139 process with another \\[inferior-shen]. It will be in a new buffer,
140 named `*inferior-shen*'. You can switch between the different process
141 buffers with \\[switch-to-buffer].
143 Commands that send text from source buffers to Shen processes --
144 like `shen-eval-defun' or `shen-show-arglist' -- have to choose a process
145 to send to, when you have more than one Shen process around. This
146 is determined by the global variable `inferior-shen-buffer'. Suppose you
147 have three inferior Shens running:
151 *inferior-shen* inferior-shen<3>
152 If you do a \\[shen-eval-defun] command on some Shen source code,
153 what process do you send it to?
155 - If you're in a process buffer (foo, bar, or *inferior-shen*),
156 you send it to that process.
157 - If you're in some other buffer (e.g., a source file), you
158 send it to the process attached to buffer `inferior-shen-buffer'.
159 This process selection is performed by function `inferior-shen-proc'.
161 Whenever \\[inferior-shen] fires up a new process, it resets
162 `inferior-shen-buffer' to be the new process's buffer. If you only run
163 one process, this does the right thing. If you run multiple
164 processes, you can change `inferior-shen-buffer' to another process
165 buffer with \\[set-variable].")
168 (defvar inferior-shen-mode-hook '()
169 "*Hook for customising Inferior Shen mode.")
171 (put 'inferior-shen-mode 'mode-class 'special)
173 (defun inferior-shen-mode ()
174 "Major mode for interacting with an inferior Shen process.
175 Runs a Shen interpreter as a subprocess of Emacs, with Shen I/O through an
176 Emacs buffer. Variable `inferior-shen-program' controls which Shen interpreter
177 is run. Variables `inferior-shen-prompt', `inferior-shen-filter-regexp' and
178 `inferior-shen-load-command' can customize this mode for different Shen
181 For information on running multiple processes in multiple buffers, see
182 documentation for variable `inferior-shen-buffer'.
184 \\{inferior-shen-mode-map}
186 Customisation: Entry to this mode runs the hooks on `comint-mode-hook' and
187 `inferior-shen-mode-hook' (in that order).
189 You can send text to the inferior Shen process from other buffers containing
191 switch-to-shen switches the current buffer to the Shen process buffer.
192 shen-eval-defun sends the current defun to the Shen process.
193 shen-compile-defun compiles the current defun.
194 shen-eval-region sends the current region to the Shen process.
195 shen-compile-region compiles the current region.
197 Prefixing the shen-eval/compile-defun/region commands with
198 a \\[universal-argument] causes a switch to the Shen process buffer after sending
202 Return after the end of the process' output sends the text from the
203 end of process to point.
204 Return before the end of the process' output copies the sexp ending at point
205 to the end of the process' output, and sends it.
206 Delete converts tabs to spaces as it moves back.
207 Tab indents for Shen; with argument, shifts rest
208 of expression rigidly with the current line.
209 C-M-q does Tab on each line starting within following expression.
210 Paragraphs are separated only by blank lines. Semicolons start comments.
211 If you accidentally suspend your process, use \\[comint-continue-subjob]
215 (set (make-local-variable 'font-lock-defaults) '(shen-font-lock-keywords))
216 (setq comint-prompt-regexp inferior-shen-prompt)
217 (setq major-mode 'inferior-shen-mode)
218 (setq mode-name "Inferior Shen")
219 (setq mode-line-process '(":%s"))
221 (use-local-map inferior-shen-mode-map) ;c-c c-k for "kompile" file
222 (setq comint-get-old-input (function shen-get-old-input))
223 (setq comint-input-filter (function shen-input-filter))
224 (run-hooks 'inferior-shen-mode-hook))
226 (defun shen-get-old-input ()
227 "Return a string containing the sexp ending at point."
231 (buffer-substring (point) end))))
233 (defun shen-input-filter (str)
234 "t if STR does not match `inferior-shen-filter-regexp'."
235 (not (string-match inferior-shen-filter-regexp str)))
238 (defun inferior-shen (cmd)
239 "Run an inferior Shen process, input and output via buffer `*inferior-shen*'.
240 If there is a process already running in `*inferior-shen*', just switch
242 With argument, allows you to edit the command line (default is value
243 of `inferior-shen-program'). Runs the hooks from
244 `inferior-shen-mode-hook' (after the `comint-mode-hook' is run).
245 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
246 (interactive (list (if current-prefix-arg
247 (read-string "Run shen: " inferior-shen-program)
248 inferior-shen-program)))
249 (if (not (comint-check-proc "*inferior-shen*"))
250 (let ((cmdlist (split-string cmd)))
251 (set-buffer (apply (function make-comint)
252 "inferior-shen" (car cmdlist) nil (cdr cmdlist)))
253 (inferior-shen-mode)))
254 (setq inferior-shen-buffer "*inferior-shen*")
255 (pop-to-buffer "*inferior-shen*"))
256 ;;;###autoload (add-hook 'same-window-buffer-names "*inferior-shen*")
259 (defalias 'run-shen 'inferior-shen)
261 (defcustom shen-pre-eval-hook '()
262 "Hook to run on code before sending it to the inferior-shen-process.
263 Functions on this hook will be called with an active region
264 containing the shen source code about to be evaluated.")
266 (defun shen-remember-functions (start end)
267 "Add functions defined between START and END to `shen-functions'."
271 (set-text-properties 0 (length text) nil text) text)))
275 "^(define[ \t]+\\(.+\\)[\n\r]" ; function name
276 "\\([ \t]*\\\\\\*[ \t]*\\([^\000]+?\\)[ \t]*\\*\\\\\\)?" ; doc
277 "[\n\r]?[ \t]*\\({\\(.+\\)}\\)?"))) ; type
278 (while (re-search-forward re end t)
279 (let ((name (intern (match-string 1)))
280 (doc (clean (match-string 3)))
281 (type (clean (match-string 5))))
282 (add-to-list 'shen-functions (list name type doc))))))))
284 (add-hook 'shen-pre-eval-hook #'shen-remember-functions)
286 (defun check-balanced-parens (start end)
287 "Check if parentheses in the region are balanced."
288 (save-restriction (save-excursion
289 (let ((deactivate-mark nil))
291 (progn (narrow-to-region start end) (goto-char (point-min))
292 (while (/= 0 (- (point) (forward-list)))) t)
293 (scan-error (signal 'scan-error '("Parentheses not balanced."))))))))
295 (add-hook 'shen-pre-eval-hook
297 (condition-case err (check-balanced-parens start end)
298 (error (unless (y-or-n-p (format "%s Eval anyway ?"
299 (error-message-string err)))
300 (signal 'scan-error err))))))
302 (defun shen-eval-region (start end &optional and-go)
303 "Send the current region to the inferior Shen process.
304 Prefix argument means switch to the Shen buffer afterwards."
306 (let ((before-input (marker-position (process-mark (inferior-shen-proc))))
309 (run-hook-with-args 'shen-pre-eval-hook start end)
310 (comint-send-region (inferior-shen-proc) start end)
311 (comint-send-string (inferior-shen-proc) "\n")
312 (accept-process-output (inferior-shen-proc))
315 (set-buffer inferior-shen-buffer)
316 (goto-char before-input)
317 (setq result (buffer-substring (point) (point-at-eol)))
318 (message "%s" result)
319 (goto-char (process-mark (inferior-shen-proc))))
320 (if and-go (switch-to-shen t))
323 (defun shen-eval-defun (&optional and-go)
324 "Send the current defun to the inferior Shen process.
325 Prefix argument means switch to the Shen buffer afterwards."
330 (skip-chars-backward " \t\n\r\f") ; Makes allegro happy
333 (setq result (shen-eval-region (point) end))))
334 (if and-go (switch-to-shen t))
337 (defun shen-eval-last-sexp (&optional and-go)
338 "Send the previous sexp to the inferior Shen process.
339 Prefix argument means switch to the Shen buffer afterwards."
341 (shen-eval-region (save-excursion (backward-sexp) (point)) (point) and-go))
343 ;;; Common Shen COMPILE sux.
344 (defun shen-compile-region (start end &optional and-go)
345 "Compile the current region in the inferior Shen process.
346 Prefix argument means switch to the Shen buffer afterwards."
350 (format "(funcall (compile nil `(lambda () (progn 'compile %s))))\n"
351 (buffer-substring start end)))
352 (if and-go (switch-to-shen t)))
354 (defun shen-compile-defun (&optional and-go)
355 "Compile the current defun in the inferior Shen process.
356 Prefix argument means switch to the Shen buffer afterwards."
360 (skip-chars-backward " \t\n\r\f") ; Makes allegro happy
363 (shen-compile-region (point) e)))
364 (if and-go (switch-to-shen t)))
366 (defun switch-to-shen (eob-p)
367 "Switch to the inferior Shen process buffer.
368 With argument, positions cursor at end of buffer."
370 (if (get-buffer-process inferior-shen-buffer)
372 ;; Be willing to use another frame
373 ;; that already has the window in it.
375 (get-buffer-window inferior-shen-buffer t))))
376 (pop-to-buffer inferior-shen-buffer))
377 (run-shen inferior-shen-program))
380 (goto-char (point-max))))
383 ;;; Now that shen-compile/eval-defun/region takes an optional prefix arg,
384 ;;; these commands are redundant. But they are kept around for the user
385 ;;; to bind if he wishes, for backwards functionality, and because it's
386 ;;; easier to type C-c e than C-u C-c C-e.
387 (defun shen-eval-region-and-go (start end)
388 "Send the current region to the inferior Shen, and switch to its buffer."
390 (shen-eval-region start end t))
392 (defun shen-eval-defun-and-go ()
393 "Send the current defun to the inferior Shen, and switch to its buffer."
397 (defun shen-compile-region-and-go (start end)
398 "Compile the current region in the inferior Shen, and switch to its buffer."
400 (shen-compile-region start end t))
402 (defun shen-compile-defun-and-go ()
403 "Compile the current defun in the inferior Shen, and switch to its buffer."
405 (shen-compile-defun t))
407 ;;; A version of the form in H. Shevis' soar-mode.el package. Less robust.
408 ;;; (defun shen-compile-sexp (start end)
409 ;;; "Compile the s-expression bounded by START and END in the inferior shen.
410 ;;; If the sexp isn't a DEFUN form, it is evaluated instead."
411 ;;; (cond ((looking-at "(defun\\s +")
412 ;;; (goto-char (match-end 0))
413 ;;; (let ((name-start (point)))
415 ;;; (process-send-string "inferior-shen"
416 ;;; (format "(compile '%s #'(lambda "
417 ;;; (buffer-substring name-start
419 ;;; (let ((body-start (point)))
420 ;;; (goto-char start) (forward-sexp 1) ; Can't use end-of-defun.
421 ;;; (process-send-region "inferior-shen"
422 ;;; (buffer-substring body-start (point))))
423 ;;; (process-send-string "inferior-shen" ")\n"))
424 ;;; (t (shen-eval-region start end)))))
426 ;;; (defun shen-compile-region (start end)
427 ;;; "Each s-expression in the current region is compiled (if a DEFUN)
428 ;;; or evaluated (if not) in the inferior shen."
429 ;;; (interactive "r")
431 ;;; (goto-char start) (end-of-defun) (beginning-of-defun) ; error check
432 ;;; (if (< (point) start) (error "region begins in middle of defun"))
433 ;;; (goto-char start)
436 ;;; (while (<= (point) end) ; Zip through
437 ;;; (shen-compile-sexp s (point)) ; compiling up defun-sized chunks.
440 ;;; (if (< s end) (shen-compile-sexp s end)))))
442 ;;; End of HS-style code
445 (defvar shen-prev-l/c-dir/file nil
446 "Record last directory and file used in loading or compiling.
447 This holds a cons cell of the form `(DIRECTORY . FILE)'
448 describing the last `shen-load-file' or `shen-compile-file' command.")
450 (defvar shen-source-modes '(shen-mode)
451 "*Used to determine if a buffer contains Shen source code.
452 If it's loaded into a buffer that is in one of these major modes, it's
453 considered a Shen source file by `shen-load-file' and `shen-compile-file'.
454 Used by these commands to determine defaults.")
456 (defun shen-load-file (file-name)
457 "Load a Shen file into the inferior Shen process."
458 (interactive (comint-get-source "Load Shen file: " shen-prev-l/c-dir/file
459 shen-source-modes nil)) ; NIL because LOAD
460 ; doesn't need an exact name
461 (comint-check-source file-name) ; Check to see if buffer needs saved.
462 (setq shen-prev-l/c-dir/file (cons (file-name-directory file-name)
463 (file-name-nondirectory file-name)))
464 (comint-send-string (inferior-shen-proc)
465 (format inferior-shen-load-command file-name))
469 (defun shen-compile-file (file-name)
470 "Compile a Shen file in the inferior Shen process."
471 (interactive (comint-get-source "Compile Shen file: " shen-prev-l/c-dir/file
472 shen-source-modes nil)) ; NIL = don't need
474 (comint-check-source file-name) ; Check to see if buffer needs saved.
475 (setq shen-prev-l/c-dir/file (cons (file-name-directory file-name)
476 (file-name-nondirectory file-name)))
477 (comint-send-string (inferior-shen-proc) (concat "(compile-file \""
484 ;;; Documentation functions: function doc, var doc, arglist, and
486 ;;; ===========================================================================
491 (defvar shen-function-doc-command
493 (format t \"Documentation for ~a:~&~a\"
494 fn (documentation fn 'function))
496 "Command to query inferior Shen for a function's documentation.")
498 (defvar shen-var-doc-command
500 (format t \"Documentation for ~a:~&~a\"
501 v (documentation v 'variable))
503 "Command to query inferior Shen for a variable's documentation.")
505 (defvar shen-arglist-command
507 (format t \"Arglist for ~a: ~a\" fn (arglist fn))
509 "Command to query inferior Shen for a function's arglist.")
511 (defvar shen-describe-sym-command
513 "Command to query inferior Shen for a variable's documentation.")
516 ;;; Ancillary functions
517 ;;; ===================
519 ;;; Reads a string from the user.
520 (defun shen-symprompt (prompt default)
521 (list (let* ((prompt (if default
522 (format "%s (default %s): " prompt default)
523 (concat prompt ": ")))
524 (ans (read-string prompt)))
525 (if (zerop (length ans)) default ans))))
528 ;;; Adapted from function-called-at-point in help.el.
529 (defun shen-fn-called-at-pt ()
530 "Returns the name of the function called in the current call.
531 The value is nil if it can't find one."
535 (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
538 (let ((obj (read (current-buffer))))
539 (and (symbolp obj) obj))))
543 ;;; Adapted from variable-at-point in help.el.
544 (defun shen-var-at-pt ()
548 (skip-chars-forward "'")
549 (let ((obj (read (current-buffer))))
550 (and (symbolp obj) obj)))
554 ;;; Documentation functions: fn and var doc, arglist, and symbol describe.
555 ;;; ======================================================================
556 (defun shen-show-function-documentation (fn)
557 "Send a command to the inferior Shen to give documentation for function FN.
558 See variable `shen-function-doc-command'."
559 (interactive (shen-symprompt "Function doc" (shen-fn-called-at-pt)))
560 (comint-proc-query (inferior-shen-proc)
561 (format shen-function-doc-command fn)))
563 (defun shen-show-variable-documentation (var)
564 "Send a command to the inferior Shen to give documentation for function FN.
565 See variable `shen-var-doc-command'."
566 (interactive (shen-symprompt "Variable doc" (shen-var-at-pt)))
567 (comint-proc-query (inferior-shen-proc) (format shen-var-doc-command var)))
569 (defun shen-show-arglist (fn)
570 "Send a query to the inferior Shen for the arglist for function FN.
571 See variable `shen-arglist-command'."
572 (interactive (shen-symprompt "Arglist" (shen-fn-called-at-pt)))
573 (comint-proc-query (inferior-shen-proc) (format shen-arglist-command fn)))
575 (defun shen-describe-sym (sym)
576 "Send a command to the inferior Shen to describe symbol SYM.
577 See variable `shen-describe-sym-command'."
578 (interactive (shen-symprompt "Describe" (shen-var-at-pt)))
579 (comint-proc-query (inferior-shen-proc)
580 (format shen-describe-sym-command sym)))
583 ;; "Returns the current inferior Shen process.
584 ;; See variable `inferior-shen-buffer'."
585 (defun inferior-shen-proc ()
586 (let ((proc (get-buffer-process (if (eq major-mode 'inferior-shen-mode)
588 inferior-shen-buffer))))
590 (error "No Shen subprocess; see variable `inferior-shen-buffer'"))))
593 ;;; Do the user's customisation...
594 ;;;===============================
595 (defvar inferior-shen-load-hook nil
596 "This hook is run when the library `inf-shen' is loaded.
597 This is a good place to put keybindings.")
599 (run-hooks 'inferior-shen-load-hook)
602 ;;; inf-shen.el ends here