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
)
26 (eval-when-compile (require 'cl
))
31 (defgroup geiser-guile nil
32 "Customization for Geiser's Guile flavour."
35 (geiser-custom--defcustom geiser-guile-binary
36 (cond ((eq system-type
'windows-nt
) "guile.exe")
37 ((eq system-type
'darwin
) "guile")
39 "Name to use to call the Guile executable when starting a REPL."
40 :type
'(choice string
(repeat string
))
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
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."
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
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."
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."
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."
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
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."
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."
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
)
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
)
157 ((eval compile
) (format ",geiser-eval %s %s%s"
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
)
175 (while (not (zerop (geiser-syntax--nesting-level)))
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))
182 ((listp module
) module
)
185 (car (geiser-syntax--read-from-string module
))
189 (defun geiser-guile--module-cmd (module fmt
&optional def
)
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)
207 (max (save-excursion (beginning-of-line) (point))
208 (save-excursion (skip-syntax-backward "^(>") (1- (point))))
209 (save-excursion (skip-syntax-backward "^-()>") (point))))
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))
226 (ignore-errors (next-error)))))
228 (defun geiser-guile--display-error (module key 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 ()
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))
255 (geiser-syntax--scheme-indent
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
)
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
))))
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'."
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."
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
)))
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)))
332 (defun geiser-guile--info-spec (&optional nodes
)
333 (let* ((nrx "^[ ]+-+ [^:]+:[ ]*")
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
)
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
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
)