Documentation bit
[geiser.git] / elisp / geiser-guile.el
blobe4473d8fc727e87c29474364558b818aaf58aeb0
1 ;; geiser-guile.el -- guile's implementation of the geiser protocols
3 ;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the Modified BSD License. You should
7 ;; have received a copy of the license along with this program. If
8 ;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
10 ;; Start date: Sun Mar 08, 2009 23:03
14 (require 'geiser-connection)
15 (require 'geiser-syntax)
16 (require 'geiser-custom)
17 (require 'geiser-base)
18 (require 'geiser-eval)
19 (require 'geiser-edit)
20 (require 'geiser-log)
21 (require 'geiser)
23 (require 'compile)
24 (require 'info-look)
27 ;;; Customization:
29 (defgroup geiser-guile nil
30 "Customization for Geiser's Guile flavour."
31 :group 'geiser)
33 (geiser-custom--defcustom geiser-guile-binary
34 (cond ((eq system-type 'windows-nt) "guile.exe")
35 ((eq system-type 'darwin) "guile")
36 (t "guile"))
37 "Name to use to call the Guile executable when starting a REPL."
38 :type '(choice string (repeat string))
39 :group 'geiser-guile)
41 (geiser-custom--defcustom geiser-guile-load-path nil
42 "A list of paths to be added to Guile's load path when it's
43 started."
44 :type '(repeat file)
45 :group 'geiser-guile)
47 (geiser-custom--defcustom geiser-guile-init-file "~/.guile-geiser"
48 "Initialization file with user code for the Guile REPL."
49 :type 'string
50 :group 'geiser-guile)
52 (geiser-custom--defcustom geiser-guile-debug-show-bt-p nil
53 "Whether to autmatically show a full backtrace when entering the debugger.
54 If `nil', only the last frame is shown."
55 :type 'boolean
56 :group 'geiser-guile)
58 (geiser-custom--defcustom geiser-guile-jump-on-debug-p nil
59 "Whether to autmatically jump to error when entering the debugger.
60 If `t', Geiser will use `next-error' to jump to the error's location."
61 :type 'boolean
62 :group 'geiser-guile)
64 (geiser-custom--defcustom geiser-guile-show-debug-help-p t
65 "Whether to show brief help in the echo area when entering the debugger."
66 :type 'boolean
67 :group 'geiser-guile)
69 (geiser-custom--defcustom geiser-guile-warning-level 'medium
70 "Verbosity of the warnings reported by Guile.
72 You can either choose one of the predefined warning sets, or
73 provide a list of symbols identifying the ones you want. Possible
74 choices are arity-mismatch, unbound-variable, unused-variable and
75 unused-toplevel. Unrecognised symbols are ignored.
77 The predefined levels are:
79 - Medium: arity-mismatch, unbound-variable
80 - High: arity-mismatch, unbound-variable, unused-variable
81 - None: no warnings
83 Changes to the value of this variable will automatically take
84 effect on new REPLs. For existing ones, use the command
85 \\[geiser-guile-update-warning-level]."
86 :type '(choice (const :tag "Medium (arity and unbound vars)" medium)
87 (const :tag "High (also unused vars)" high)
88 (const :tag "No warnings" none)
89 (repeat :tag "Custom" symbol))
90 :group 'geiser-guile)
92 (geiser-custom--defcustom geiser-guile-extra-keywords nil
93 "Extra keywords highlighted in Guile scheme buffers."
94 :type '(repeat string)
95 :group 'geiser-guile)
97 (geiser-custom--defcustom geiser-guile-manual-lookup-other-window-p nil
98 "Non-nil means pop up the Info buffer in another window."
99 :type 'boolean
100 :group 'geiser-guile)
103 ;;; REPL support:
105 (defun geiser-guile--binary ()
106 (if (listp geiser-guile-binary)
107 (car geiser-guile-binary)
108 geiser-guile-binary))
110 (defun geiser-guile--parameters ()
111 "Return a list with all parameters needed to start Guile.
112 This function uses `geiser-guile-init-file' if it exists."
113 (let ((init-file (and (stringp geiser-guile-init-file)
114 (expand-file-name geiser-guile-init-file))))
115 `(,@(and (listp geiser-guile-binary) (cdr geiser-guile-binary))
116 "-q" "-L" ,(expand-file-name "guile/" geiser-scheme-dir)
117 ,@(apply 'append (mapcar (lambda (p) (list "-L" p)) geiser-guile-load-path))
118 ,@(and init-file (file-readable-p init-file) (list "-l" init-file)))))
120 ;;(defconst geiser-guile--prompt-regexp "^[^() \n]+@([^)]*?)> ")
121 (defconst geiser-guile--prompt-regexp "[^@()]+@([^)]*?)> ")
122 (defconst geiser-guile--debugger-prompt-regexp
123 "[^@()]+@([^)]*?) \\[[0-9]+\\]> ")
126 ;;; Evaluation support:
127 (defsubst geiser-guile--linearize-args (args)
128 (mapconcat 'identity args " "))
130 (defun geiser-guile--geiser-procedure (proc &rest args)
131 (case proc
132 ((eval compile) (format ",geiser-eval %s %s%s"
133 (or (car args) "#f")
134 (geiser-guile--linearize-args (cdr args))
135 (if (cddr args) "" " ()")))
136 ((load-file compile-file) (format ",geiser-load-file %s" (car args)))
137 ((no-values) ",geiser-no-values")
138 (t (format "ge:%s (%s)" proc (geiser-guile--linearize-args args)))))
140 (defconst geiser-guile--module-re
141 "(define-module +\\(([^)]+)\\)")
143 (defconst geiser-guile--library-re
144 "(library +\\(([^)]+)\\)")
146 (defun geiser-guile--get-module (&optional module)
147 (cond ((null module)
148 (save-excursion
149 (ignore-errors
150 (while (not (zerop (geiser-syntax--nesting-level)))
151 (backward-up-list)))
152 (if (or (re-search-backward geiser-guile--module-re nil t)
153 (looking-at geiser-guile--library-re))
154 (geiser-guile--get-module (match-string-no-properties 1))
155 :f)))
156 ((listp module) module)
157 ((stringp module)
158 (condition-case nil
159 (car (geiser-syntax--read-from-string module))
160 (error :f)))
161 (t :f)))
163 (defun geiser-guile--module-cmd (module fmt &optional def)
164 (when module
165 (let* ((module (geiser-guile--get-module module))
166 (module (cond ((or (null module) (eq module :f)) def)
167 (t (format "%s" module)))))
168 (and module (format fmt module)))))
170 (defun geiser-guile--import-command (module)
171 (geiser-guile--module-cmd module ",use %s"))
173 (defun geiser-guile--enter-command (module)
174 (geiser-guile--module-cmd module ",m %s" "(guile-user)"))
177 (defun geiser-guile--exit-command () ",q")
179 (defun geiser-guile--symbol-begin (module)
180 (if module
181 (max (save-excursion (beginning-of-line) (point))
182 (save-excursion (skip-syntax-backward "^(>") (1- (point))))
183 (save-excursion (skip-syntax-backward "^-()>") (point))))
186 ;;; Error display
188 (defun geiser-guile--enter-debugger ()
189 (let ((bt-cmd (format ",geiser-newline\n,error-message\n,%s\n"
190 (if geiser-guile-debug-show-bt-p "bt" "fr"))))
191 (compilation-forget-errors)
192 (goto-char (point-max))
193 (geiser-repl--prepare-send)
194 (comint-send-string nil bt-cmd)
195 (when geiser-guile-show-debug-help-p
196 (message "Debug REPL. Enter ,q to quit, ,h for help."))
197 (when geiser-guile-jump-on-debug-p
198 (accept-process-output (get-buffer-process (current-buffer))
199 0.2 nil t)
200 (ignore-errors (next-error)))))
202 (defun geiser-guile--display-error (module key msg)
203 (newline)
204 (when (stringp msg)
205 (save-excursion (insert msg))
206 (geiser-edit--buttonize-files))
207 (and (not key) msg (not (zerop (length msg)))))
210 ;;; Trying to ascertain whether a buffer is Guile Scheme:
212 (defconst geiser-guile--guess-re
213 (format "\\(%s\\|#! *.+\\(/\\| \\)guile\\( *\\\\\\)?\\)"
214 geiser-guile--module-re))
216 (defun geiser-guile--guess ()
217 (save-excursion
218 (goto-char (point-min))
219 (re-search-forward geiser-guile--guess-re nil t)))
222 ;;; Keywords
223 (defun geiser-guile--keywords ()
224 (when geiser-guile-extra-keywords
225 `((,(format "[[(]%s\\>" (regexp-opt geiser-guile-extra-keywords 1))
226 . 1))))
229 ;;; Compilation shell regexps
231 (defconst geiser-guile--path-rx "^In \\([^:\n ]+\\):\n")
233 (defconst geiser-guile--rel-path-rx "^In +\\([^/\n :]+\\):\n")
235 (defvar geiser-guile--file-cache (make-hash-table :test 'equal))
237 (defun geiser-guile--resolve-file (file)
238 (when (and (stringp file)
239 (not (member file '("socket" "stdin" "unknown file"))))
240 (if (file-name-absolute-p file) file
241 (or (gethash file geiser-guile--file-cache)
242 (puthash file
243 (geiser-eval--send/result `(:eval (:ge find-file ,file)))
244 geiser-guile--file-cache)))))
246 (defun geiser-guile--resolve-file-x ()
247 (let ((f (geiser-guile--resolve-file (match-string-no-properties 1))))
248 (and (stringp f) (list f))))
251 ;;; REPL startup
253 (defun geiser-guile-update-warning-level ()
254 "Update the warning level used by the REPL.
255 The new level is set using the value of `geiser-guile-warning-level'."
256 (interactive)
257 (let ((code `(:eval (:ge set-warnings ',geiser-guile-warning-level)
258 (geiser evaluation))))
259 (geiser-eval--send/result code)))
261 (defun connect-to-guile ()
262 "Start a Guile REPL connected to a remote process.
264 Start the external Guile process with the flag --listen to make
265 it spawn a server thread."
266 (interactive)
267 (geiser-connect 'guile))
269 (defun geiser-guile--set-load-path ()
270 (let* ((path (expand-file-name "guile/" geiser-scheme-dir))
271 (witness "geiser/emacs.scm")
272 (code `(begin (if (not (%search-load-path ,witness))
273 (set! %load-path (cons ,path %load-path)))
274 'done)))
275 (geiser-eval--send/wait code)))
277 (defun geiser-guile--startup (remote)
278 (set (make-local-variable 'compilation-error-regexp-alist)
279 `((,geiser-guile--path-rx geiser-guile--resolve-file-x)
280 ("^ +\\([0-9]+\\):\\([0-9]+\\)" nil 1 2)))
281 (compilation-setup t)
282 (font-lock-add-keywords nil
283 `((,geiser-guile--path-rx 1
284 compilation-error-face)))
285 (let ((geiser-log-verbose-p t))
286 (when remote (geiser-guile--set-load-path))
287 (geiser-eval--send/wait ",use (geiser emacs)\n'done")
288 (geiser-guile-update-warning-level)))
291 ;;; Manual lookup
292 (info-lookup-add-help :topic 'symbol :mode 'geiser-guile-mode
293 :ignore-case nil
294 :regexp "[^()`',\" \n]+"
295 :doc-spec
296 '(("(r5rs)Index" nil "^[ ]+-+ [^:]+:[ ]*" "\\b")
297 ("(Guile)R5RS Index" nil "^ - [^:]+: " "\\b")
298 ("(Guile)Procedure Index" nil "^ - [^:]+: " "\\b")
299 ("(Guile)Variable Index" nil "^ - [^:]+: " "\\b")))
301 (defun guile--manual-look-up (id mod)
302 (let ((info-lookup-other-window-flag
303 geiser-guile-manual-lookup-other-window-p))
304 (info-lookup-symbol id 'geiser-guile-mode))
305 (when geiser-guile-manual-lookup-other-window-p
306 (switch-to-buffer-other-window "*info*"))
307 (search-forward (format "%s" id) nil t))
311 ;;; Implementation definition:
313 (define-geiser-implementation guile
314 (binary geiser-guile--binary)
315 (arglist geiser-guile--parameters)
316 (repl-startup geiser-guile--startup)
317 (prompt-regexp geiser-guile--prompt-regexp)
318 (debugger-prompt-regexp geiser-guile--debugger-prompt-regexp)
319 (enter-debugger geiser-guile--enter-debugger)
320 (marshall-procedure geiser-guile--geiser-procedure)
321 (find-module geiser-guile--get-module)
322 (enter-command geiser-guile--enter-command)
323 (exit-command geiser-guile--exit-command)
324 (import-command geiser-guile--import-command)
325 (find-symbol-begin geiser-guile--symbol-begin)
326 (display-error geiser-guile--display-error)
327 (external-help guile--manual-look-up)
328 (check-buffer geiser-guile--guess)
329 (keywords geiser-guile--keywords))
331 (geiser-impl--add-to-alist 'regexp "\\.scm$" 'guile nil)
334 (provide 'geiser-guile)