Add `dir' to EXTRA_DIST
[geiser.git] / elisp / geiser-guile.el
blobd51218555b48d1d1b4b5f58daba774e8717902b4
1 ;; geiser-guile.el -- guile's implementation of the geiser protocols
3 ;; Copyright (C) 2009, 2010, 2011, 2012, 2013 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)
26 (eval-when-compile (require 'cl))
29 ;;; Customization:
31 (defgroup geiser-guile nil
32 "Customization for Geiser's Guile flavour."
33 :group 'geiser)
35 (geiser-custom--defcustom geiser-guile-binary
36 (cond ((eq system-type 'windows-nt) "guile.exe")
37 ((eq system-type 'darwin) "guile")
38 (t "guile"))
39 "Name to use to call the Guile executable when starting a REPL."
40 :type '(choice string (repeat string))
41 :group 'geiser-guile)
43 (geiser-custom--defcustom geiser-guile-load-path nil
44 "A list of paths to be added to Guile's load path when it's
45 started."
46 :type '(repeat file)
47 :group 'geiser-guile)
49 (geiser-custom--defcustom geiser-guile-init-file "~/.guile-geiser"
50 "Initialization file with user code for the Guile REPL.
51 If all you want is to load ~/.guile, set
52 `geiser-guile-load-init-file-p' instead."
53 :type 'string
54 :group 'geiser-guile)
56 (geiser-custom--defcustom geiser-guile-load-init-file-p nil
57 "Whether to load ~/.guile when starting Guile.
58 Note that, due to peculiarities in the way Guile loads its init
59 file, using `geiser-guile-init-file' is not equivalent to setting
60 this variable to t."
61 :type 'boolean
62 :group 'geiser-guile)
64 (geiser-custom--defcustom geiser-guile-debug-show-bt-p nil
65 "Whether to autmatically show a full backtrace when entering the debugger.
66 If `nil', only the last frame is shown."
67 :type 'boolean
68 :group 'geiser-guile)
70 (geiser-custom--defcustom geiser-guile-jump-on-debug-p nil
71 "Whether to autmatically jump to error when entering the debugger.
72 If `t', Geiser will use `next-error' to jump to the error's location."
73 :type 'boolean
74 :group 'geiser-guile)
76 (geiser-custom--defcustom geiser-guile-show-debug-help-p t
77 "Whether to show brief help in the echo area when entering the debugger."
78 :type 'boolean
79 :group 'geiser-guile)
81 (geiser-custom--defcustom geiser-guile-warning-level 'medium
82 "Verbosity of the warnings reported by Guile.
84 You can either choose one of the predefined warning sets, or
85 provide a list of symbols identifying the ones you want. Possible
86 choices are arity-mismatch, unbound-variable, unused-variable and
87 unused-toplevel. Unrecognised symbols are ignored.
89 The predefined levels are:
91 - Medium: arity-mismatch, unbound-variable, format
92 - High: arity-mismatch, unbound-variable, unused-variable, format
93 - None: no warnings
95 Changes to the value of this variable will automatically take
96 effect on new REPLs. For existing ones, use the command
97 \\[geiser-guile-update-warning-level]."
98 :type '(choice (const :tag "Medium (arity and unbound vars)" medium)
99 (const :tag "High (also unused vars)" high)
100 (const :tag "No warnings" none)
101 (repeat :tag "Custom" symbol))
102 :group 'geiser-guile)
104 (geiser-custom--defcustom geiser-guile-extra-keywords nil
105 "Extra keywords highlighted in Guile scheme buffers."
106 :type '(repeat string)
107 :group 'geiser-guile)
109 (geiser-custom--defcustom geiser-guile-case-sensitive-p t
110 "Non-nil means keyword highlighting is case-sensitive."
111 :type 'boolean
112 :group 'geiser-guile)
114 (geiser-custom--defcustom geiser-guile-manual-lookup-other-window-p nil
115 "Non-nil means pop up the Info buffer in another window."
116 :type 'boolean
117 :group 'geiser-guile)
119 (geiser-custom--defcustom geiser-guile-manual-lookup-nodes
120 '("Guile" "guile-2.0")
121 "List of info nodes that, when present, are used for manual lookups"
122 :type '(repeat string)
123 :group 'geiser-guile)
126 ;;; REPL support:
128 (defun geiser-guile--binary ()
129 (if (listp geiser-guile-binary)
130 (car geiser-guile-binary)
131 geiser-guile-binary))
133 (defun geiser-guile--parameters ()
134 "Return a list with all parameters needed to start Guile.
135 This function uses `geiser-guile-init-file' if it exists."
136 (let ((init-file (and (stringp geiser-guile-init-file)
137 (expand-file-name geiser-guile-init-file)))
138 (q-flags (and (not geiser-guile-load-init-file-p) '("-q"))))
139 `(,@(and (listp geiser-guile-binary) (cdr geiser-guile-binary))
140 ,@q-flags "-L" ,(expand-file-name "guile/" geiser-scheme-dir)
141 ,@(apply 'append (mapcar (lambda (p) (list "-L" p))
142 geiser-guile-load-path))
143 ,@(and init-file (file-readable-p init-file) (list "-l" init-file)))))
145 ;;(defconst geiser-guile--prompt-regexp "^[^() \n]+@([^)]*?)> ")
146 (defconst geiser-guile--prompt-regexp "[^@()]+@([^)]*?)> ")
147 (defconst geiser-guile--debugger-prompt-regexp
148 "[^@()]+@([^)]*?) \\[[0-9]+\\]> ")
151 ;;; Evaluation support:
152 (defsubst geiser-guile--linearize-args (args)
153 (mapconcat 'identity args " "))
155 (defun geiser-guile--geiser-procedure (proc &rest args)
156 (case proc
157 ((eval compile) (format ",geiser-eval %s %s%s"
158 (or (car args) "#f")
159 (geiser-guile--linearize-args (cdr args))
160 (if (cddr args) "" " ()")))
161 ((load-file compile-file) (format ",geiser-load-file %s" (car args)))
162 ((no-values) ",geiser-no-values")
163 (t (format "ge:%s (%s)" proc (geiser-guile--linearize-args args)))))
165 (defconst geiser-guile--module-re
166 "(define-module +\\(([^)]+)\\)")
168 (defconst geiser-guile--library-re
169 "(library +\\(([^)]+)\\)")
171 (defun geiser-guile--get-module (&optional module)
172 (cond ((null module)
173 (save-excursion
174 (ignore-errors
175 (while (not (zerop (geiser-syntax--nesting-level)))
176 (backward-up-list)))
177 (if (or (re-search-backward geiser-guile--module-re nil t)
178 (looking-at geiser-guile--library-re)
179 (re-search-forward geiser-guile--module-re nil t))
180 (geiser-guile--get-module (match-string-no-properties 1))
181 :f)))
182 ((listp module) module)
183 ((stringp module)
184 (condition-case nil
185 (car (geiser-syntax--read-from-string module))
186 (error :f)))
187 (t :f)))
189 (defun geiser-guile--module-cmd (module fmt &optional def)
190 (when module
191 (let* ((module (geiser-guile--get-module module))
192 (module (cond ((or (null module) (eq module :f)) def)
193 (t (format "%s" module)))))
194 (and module (format fmt module)))))
196 (defun geiser-guile--import-command (module)
197 (geiser-guile--module-cmd module ",use %s"))
199 (defun geiser-guile--enter-command (module)
200 (geiser-guile--module-cmd module ",m %s" "(guile-user)"))
203 (defun geiser-guile--exit-command () ",q")
205 (defun geiser-guile--symbol-begin (module)
206 (if module
207 (max (save-excursion (beginning-of-line) (point))
208 (save-excursion (skip-syntax-backward "^(>") (1- (point))))
209 (save-excursion (skip-syntax-backward "^-()>") (point))))
212 ;;; Error display
214 (defun geiser-guile--enter-debugger ()
215 (let ((bt-cmd (format ",geiser-newline\n,error-message\n,%s\n"
216 (if geiser-guile-debug-show-bt-p "bt" "fr"))))
217 (compilation-forget-errors)
218 (goto-char (point-max))
219 (geiser-repl--prepare-send)
220 (comint-send-string nil bt-cmd)
221 (when geiser-guile-show-debug-help-p
222 (message "Debug REPL. Enter ,q to quit, ,h for help."))
223 (when geiser-guile-jump-on-debug-p
224 (accept-process-output (get-buffer-process (current-buffer))
225 0.2 nil t)
226 (ignore-errors (next-error)))))
228 (defun geiser-guile--display-error (module key msg)
229 (newline)
230 (when (stringp msg)
231 (save-excursion (insert msg))
232 (geiser-edit--buttonize-files))
233 (and (not key) msg (not (zerop (length msg)))))
236 ;;; Trying to ascertain whether a buffer is Guile Scheme:
238 (defconst geiser-guile--guess-re
239 (format "\\(%s\\|#! *.+\\(/\\| \\)guile\\( *\\\\\\)?\\)"
240 geiser-guile--module-re))
242 (defun geiser-guile--guess ()
243 (save-excursion
244 (goto-char (point-min))
245 (re-search-forward geiser-guile--guess-re nil t)))
248 ;;; Keywords and syntax
250 (defun geiser-guile--keywords ()
251 (when geiser-guile-extra-keywords
252 `((,(format "[[(]%s\\>" (regexp-opt geiser-guile-extra-keywords 1))
253 . 1))))
255 (geiser-syntax--scheme-indent
256 (c-declare 0)
257 (c-lambda 2)
258 (lambda* 1)
259 (pmatch defun)
260 (sigaction 1)
261 (with-fluid* 1)
262 (with-fluids 1)
263 (with-fluids* 1)
264 (with-method 1))
268 ;;; Compilation shell regexps
270 (defconst geiser-guile--path-rx "^In \\([^:\n ]+\\):\n")
272 (defconst geiser-guile--rel-path-rx "^In +\\([^/\n :]+\\):\n")
274 (defvar geiser-guile--file-cache (make-hash-table :test 'equal))
276 (defun geiser-guile--resolve-file (file)
277 (when (and (stringp file)
278 (not (member file '("socket" "stdin" "unknown file"))))
279 (if (file-name-absolute-p file) file
280 (or (gethash file geiser-guile--file-cache)
281 (puthash file
282 (geiser-eval--send/result `(:eval (:ge find-file ,file)))
283 geiser-guile--file-cache)))))
285 (defun geiser-guile--resolve-file-x ()
286 (let ((f (geiser-guile--resolve-file (match-string-no-properties 1))))
287 (and (stringp f) (list f))))
290 ;;; REPL startup
292 (defun geiser-guile-update-warning-level ()
293 "Update the warning level used by the REPL.
294 The new level is set using the value of `geiser-guile-warning-level'."
295 (interactive)
296 (let ((code `(:eval (:ge set-warnings ',geiser-guile-warning-level)
297 (geiser evaluation))))
298 (geiser-eval--send/result code)))
300 (defun connect-to-guile ()
301 "Start a Guile REPL connected to a remote process.
303 Start the external Guile process with the flag --listen to make
304 it spawn a server thread."
305 (interactive)
306 (geiser-connect 'guile))
308 (defun geiser-guile--set-load-path ()
309 (let* ((path (expand-file-name "guile/" geiser-scheme-dir))
310 (witness "geiser/emacs.scm")
311 (code `(begin (if (not (%search-load-path ,witness))
312 (set! %load-path (cons ,path %load-path)))
313 'done)))
314 (geiser-eval--send/wait code)))
316 (defun geiser-guile--startup (remote)
317 (set (make-local-variable 'compilation-error-regexp-alist)
318 `((,geiser-guile--path-rx geiser-guile--resolve-file-x)
319 ("^ +\\([0-9]+\\):\\([0-9]+\\)" nil 1 2)))
320 (compilation-setup t)
321 (font-lock-add-keywords nil
322 `((,geiser-guile--path-rx 1
323 compilation-error-face)))
324 (let ((geiser-log-verbose-p t))
325 (when remote (geiser-guile--set-load-path))
326 (geiser-eval--send/wait ",use (geiser emacs)\n'done")
327 (geiser-guile-update-warning-level)))
330 ;;; Manual lookup
332 (defun geiser-guile--info-spec (&optional nodes)
333 (let* ((nrx "^[ ]+-+ [^:]+:[ ]*")
334 (drx "\\b")
335 (res (when (Info-find-file "r5rs" t)
336 `(("(r5rs)Index" nil ,nrx ,drx)))))
337 (dolist (node (or nodes geiser-guile-manual-lookup-nodes) res)
338 (when (Info-find-file node t)
339 (mapc (lambda (idx)
340 (add-to-list 'res
341 (list (format "(%s)%s" node idx) nil nrx drx)))
342 '("Variable Index" "Procedure Index" "R5RS Index"))))))
345 (info-lookup-add-help :topic 'symbol :mode 'geiser-guile-mode
346 :ignore-case nil
347 :regexp "[^()`',\" \n]+"
348 :doc-spec (geiser-guile--info-spec))
350 (defun guile--manual-look-up (id mod)
351 (let ((info-lookup-other-window-flag
352 geiser-guile-manual-lookup-other-window-p))
353 (info-lookup-symbol id 'geiser-guile-mode))
354 (when geiser-guile-manual-lookup-other-window-p
355 (switch-to-buffer-other-window "*info*"))
356 (search-forward (format "%s" id) nil t))
360 ;;; Implementation definition:
362 (define-geiser-implementation guile
363 (binary geiser-guile--binary)
364 (arglist geiser-guile--parameters)
365 (repl-startup geiser-guile--startup)
366 (prompt-regexp geiser-guile--prompt-regexp)
367 (debugger-prompt-regexp geiser-guile--debugger-prompt-regexp)
368 (enter-debugger geiser-guile--enter-debugger)
369 (marshall-procedure geiser-guile--geiser-procedure)
370 (find-module geiser-guile--get-module)
371 (enter-command geiser-guile--enter-command)
372 (exit-command geiser-guile--exit-command)
373 (import-command geiser-guile--import-command)
374 (find-symbol-begin geiser-guile--symbol-begin)
375 (display-error geiser-guile--display-error)
376 (external-help guile--manual-look-up)
377 (check-buffer geiser-guile--guess)
378 (keywords geiser-guile--keywords)
379 (case-sensitive geiser-guile-case-sensitive-p))
381 (geiser-impl--add-to-alist 'regexp "\\.scm$" 'guile t)
384 (provide 'geiser-guile)