Use faster module list
[geiser.git] / elisp / geiser-guile.el
blobc4f6293d7aa37431c5d5155e579b86ac33c3a30e
1 ;; geiser-guile.el -- guile's implementation of the geiser protocols
3 ;; Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 (geiser-syntax--pop-to-top)
175 (if (or (re-search-backward geiser-guile--module-re nil t)
176 (looking-at geiser-guile--library-re)
177 (re-search-forward geiser-guile--module-re nil t))
178 (geiser-guile--get-module (match-string-no-properties 1))
179 :f)))
180 ((listp module) module)
181 ((stringp module)
182 (condition-case nil
183 (car (geiser-syntax--read-from-string module))
184 (error :f)))
185 (t :f)))
187 (defun geiser-guile--module-cmd (module fmt &optional def)
188 (when module
189 (let* ((module (geiser-guile--get-module module))
190 (module (cond ((or (null module) (eq module :f)) def)
191 (t (format "%s" module)))))
192 (and module (format fmt module)))))
194 (defun geiser-guile--import-command (module)
195 (geiser-guile--module-cmd module ",use %s"))
197 (defun geiser-guile--enter-command (module)
198 (geiser-guile--module-cmd module ",m %s" "(guile-user)"))
201 (defun geiser-guile--exit-command () ",q")
203 (defun geiser-guile--symbol-begin (module)
204 (if module
205 (max (save-excursion (beginning-of-line) (point))
206 (save-excursion (skip-syntax-backward "^(>") (1- (point))))
207 (save-excursion (skip-syntax-backward "^'-()>") (point))))
210 ;;; Error display
212 (defun geiser-guile--enter-debugger ()
213 (let ((bt-cmd (format ",geiser-newline\n,error-message\n,%s\n"
214 (if geiser-guile-debug-show-bt-p "bt" "fr"))))
215 (compilation-forget-errors)
216 (goto-char (point-max))
217 (geiser-repl--prepare-send)
218 (comint-send-string nil bt-cmd)
219 (when geiser-guile-show-debug-help-p
220 (message "Debug REPL. Enter ,q to quit, ,h for help."))
221 (when geiser-guile-jump-on-debug-p
222 (accept-process-output (get-buffer-process (current-buffer))
223 0.2 nil t)
224 (ignore-errors (next-error)))))
226 (defun geiser-guile--display-error (module key msg)
227 (newline)
228 (when (stringp msg)
229 (save-excursion (insert msg))
230 (geiser-edit--buttonize-files))
231 (and (not key) msg (not (zerop (length msg)))))
234 ;;; Trying to ascertain whether a buffer is Guile Scheme:
236 (defconst geiser-guile--guess-re
237 (format "\\(%s\\|#! *.+\\(/\\| \\)guile\\( *\\\\\\)?\\)"
238 geiser-guile--module-re))
240 (defun geiser-guile--guess ()
241 (save-excursion
242 (goto-char (point-min))
243 (re-search-forward geiser-guile--guess-re nil t)))
246 ;;; Keywords and syntax
248 (defconst geiser-guile--builtin-keywords
249 '("call-with-input-file"
250 "call-with-input-string"
251 "call-with-output-file"
252 "call-with-output-string"
253 "call-with-prompt"
254 "call-with-trace"
255 "define-accessor"
256 "define-class"
257 "define-enumeration"
258 "define-inlinable"
259 "define-syntax-parameter"
260 "eval-when"
261 "lambda*"
262 "syntax-parameterize"
263 "use-modules"
264 "with-error-to-file"
265 "with-error-to-port"
266 "with-error-to-string"
267 "with-fluid*"
268 "with-fluids"
269 "with-fluids*"
270 "with-input-from-port"
271 "with-input-from-string"
272 "with-output-to-port"
273 "with-output-to-string"))
275 (defun geiser-guile--keywords ()
276 (append
277 (geiser-syntax--simple-keywords geiser-guile-extra-keywords)
278 (geiser-syntax--simple-keywords geiser-guile--builtin-keywords)
279 `((,(rx "(" (group "define-once") eow (* space) (? (group (+ word))))
280 (1 font-lock-keyword-face)
281 (2 font-lock-variable-name-face nil t))
282 ("(\\(define-module\\) +(\\([^)]+\\))"
283 (1 font-lock-keyword-face)
284 (2 font-lock-type-face nil t)))))
286 (geiser-syntax--scheme-indent
287 (c-declare 0)
288 (c-lambda 2)
289 (call-with-input-string 1)
290 (call-with-output-string 0)
291 (call-with-prompt 1)
292 (call-with-trace 0)
293 (eval-when 1)
294 (lambda* 1)
295 (pmatch defun)
296 (sigaction 1)
297 (syntax-parameterize 1)
298 (with-error-to-file 1)
299 (with-error-to-port 1)
300 (with-error-to-string 0)
301 (with-fluid* 1)
302 (with-fluids 1)
303 (with-fluids* 1)
304 (with-input-from-string 1)
305 (with-method 1)
306 (with-mutex 1)
307 (with-output-to-string 0)
308 (with-throw-handler 1))
311 ;;; Compilation shell regexps
313 (defconst geiser-guile--path-rx "^In \\([^:\n ]+\\):\n")
315 (defconst geiser-guile--rel-path-rx "^In +\\([^/\n :]+\\):\n")
317 (defvar geiser-guile--file-cache (make-hash-table :test 'equal))
319 (defun geiser-guile--resolve-file (file)
320 (when (and (stringp file)
321 (not (member file '("socket" "stdin" "unknown file"))))
322 (if (file-name-absolute-p file) file
323 (or (gethash file geiser-guile--file-cache)
324 (puthash file
325 (geiser-eval--send/result `(:eval (:ge find-file ,file)))
326 geiser-guile--file-cache)))))
328 (defun geiser-guile--resolve-file-x ()
329 (let ((f (geiser-guile--resolve-file (match-string-no-properties 1))))
330 (and (stringp f) (list f))))
333 ;;; REPL startup
335 (defconst geiser-guile-minimum-version "2.0")
337 (defun geiser-guile--version (binary)
338 (shell-command-to-string
339 (format "%s -c %s"
340 (shell-quote-argument binary)
341 (shell-quote-argument "(display (version))"))))
343 (defun geiser-guile-update-warning-level ()
344 "Update the warning level used by the REPL.
345 The new level is set using the value of `geiser-guile-warning-level'."
346 (interactive)
347 (let ((code `(:eval (:ge set-warnings ',geiser-guile-warning-level)
348 (geiser evaluation))))
349 (geiser-eval--send/result code)))
351 (defun connect-to-guile ()
352 "Start a Guile REPL connected to a remote process.
354 Start the external Guile process with the flag --listen to make
355 it spawn a server thread."
356 (interactive)
357 (geiser-connect 'guile))
359 (defun geiser-guile--set-geiser-load-path ()
360 (let* ((path (expand-file-name "guile/" geiser-scheme-dir))
361 (witness "geiser/emacs.scm")
362 (code `(begin (if (not (%search-load-path ,witness))
363 (set! %load-path (cons ,path %load-path)))
364 'done)))
365 (geiser-eval--send/wait code)))
367 (defun geiser-guile--startup (remote)
368 (set (make-local-variable 'compilation-error-regexp-alist)
369 `((,geiser-guile--path-rx geiser-guile--resolve-file-x)
370 ("^ +\\([0-9]+\\):\\([0-9]+\\)" nil 1 2)))
371 (compilation-setup t)
372 (font-lock-add-keywords nil `((,geiser-guile--path-rx
373 1 compilation-error-face)))
374 (let ((geiser-log-verbose-p t))
375 (when remote (geiser-guile--set-geiser-load-path))
376 (geiser-eval--send/wait ",use (geiser emacs)\n'done")
377 (dolist (dir geiser-guile-load-path)
378 (let ((dir (expand-file-name dir)))
379 (geiser-eval--send/wait `(:eval (:ge add-to-load-path ,dir)))))
380 (geiser-guile-update-warning-level)))
383 ;;; Manual lookup
385 (defun geiser-guile--info-spec (&optional nodes)
386 (let* ((nrx "^[ ]+-+ [^:]+:[ ]*")
387 (drx "\\b")
388 (res (when (Info-find-file "r5rs" t)
389 `(("(r5rs)Index" nil ,nrx ,drx)))))
390 (dolist (node (or nodes geiser-guile-manual-lookup-nodes) res)
391 (when (Info-find-file node t)
392 (mapc (lambda (idx)
393 (add-to-list 'res
394 (list (format "(%s)%s" node idx) nil nrx drx)))
395 '("Variable Index" "Procedure Index" "R5RS Index"))))))
398 (info-lookup-add-help :topic 'symbol :mode 'geiser-guile-mode
399 :ignore-case nil
400 :regexp "[^()`',\" \n]+"
401 :doc-spec (geiser-guile--info-spec))
403 (defun guile--manual-look-up (id mod)
404 (let ((info-lookup-other-window-flag
405 geiser-guile-manual-lookup-other-window-p))
406 (info-lookup-symbol id 'geiser-guile-mode))
407 (when geiser-guile-manual-lookup-other-window-p
408 (switch-to-buffer-other-window "*info*"))
409 (search-forward (format "%s" id) nil t))
413 ;;; Implementation definition:
415 (define-geiser-implementation guile
416 (binary geiser-guile--binary)
417 (arglist geiser-guile--parameters)
418 (version-command geiser-guile--version)
419 (minimum-version geiser-guile-minimum-version)
420 (repl-startup geiser-guile--startup)
421 (prompt-regexp geiser-guile--prompt-regexp)
422 (debugger-prompt-regexp geiser-guile--debugger-prompt-regexp)
423 (enter-debugger geiser-guile--enter-debugger)
424 (marshall-procedure geiser-guile--geiser-procedure)
425 (find-module geiser-guile--get-module)
426 (enter-command geiser-guile--enter-command)
427 (exit-command geiser-guile--exit-command)
428 (import-command geiser-guile--import-command)
429 (find-symbol-begin geiser-guile--symbol-begin)
430 (display-error geiser-guile--display-error)
431 (external-help guile--manual-look-up)
432 (check-buffer geiser-guile--guess)
433 (keywords geiser-guile--keywords)
434 (case-sensitive geiser-guile-case-sensitive-p))
436 (geiser-impl--add-to-alist 'regexp "\\.scm$" 'guile t)
439 (provide 'geiser-guile)