summaryrefslogtreecommitdiff
path: root/geiser-guile.el
blob: 275e194137dfeaaa8ce37517231df0fa626facf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
;;; geiser-guile.el --- Guile's implementation of the geiser protocols  -*- lexical-binding: t; -*-

;; Copyright (C) 2009-2021 Jose Antonio Ortega Ruiz
;; Start date: Sun Mar 08, 2009 23:03

;; Author: Jose Antonio Ortega Ruiz (jao@gnu.org)
;; Maintainer: Jose Antonio Ortega Ruiz (jao@gnu.org)
;; Keywords: languages, guile, scheme, geiser
;; Homepage: https://gitlab.com/emacs-geiser/guile
;; Package-Requires: ((emacs "24.4") (geiser "0.18"))
;; SPDX-License-Identifier: BSD-3-Clause
;; Version: 0.18

;; This file is NOT part of GNU Emacs.

;;; Commentary:

;; This package extends the `geiser' core package to support GNU
;; Guile.


;;; Code:

(require 'geiser-connection)
(require 'geiser-syntax)
(require 'geiser-custom)
(require 'geiser-repl)
(require 'geiser-impl)
(require 'geiser-base)
(require 'geiser-eval)
(require 'geiser-edit)
(require 'geiser-log)
(require 'geiser)

(require 'compile)
(require 'info-look)

(eval-when-compile (require 'cl-lib))


;;; Customization:

(defgroup geiser-guile nil
  "Customization for Geiser's Guile flavour."
  :group 'geiser)

(geiser-custom--defcustom geiser-guile-binary
    (cond ((eq system-type 'windows-nt) "guile.exe")
          ((eq system-type 'darwin) "guile")
          (t "guile"))
  "Name to use to call the Guile executable when starting a REPL."
  :type '(choice string (repeat string))
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-load-path nil
  "A list of paths to be added to Guile's load path when it's started.
The paths are added to both %`load-path' and %load-compiled path,
and only if they are not already present.  This variable is a
good candidate for an entry in your project's .dir-locals.el."
  :type '(repeat file)
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-init-file "~/.guile-geiser"
  "Initialization file with user code for the Guile REPL.
If all you want is to load ~/.guile, set
`geiser-guile-load-init-file-p' instead."
  :type 'string
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-load-init-file-p nil
  "Whether to load ~/.guile when starting Guile.
Note that, due to peculiarities in the way Guile loads its init
file, using `geiser-guile-init-file' is not equivalent to setting
this variable to t."
  :type 'boolean
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-debug-show-bt-p nil
  "Whether to automatically show a full backtrace when entering the debugger.
If nil, only the last frame is shown."
  :type 'boolean
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-jump-on-debug-p nil
  "Whether to automatically jump to error when entering the debugger.
If t, Geiser will use `next-error' to jump to the error's location."
  :type 'boolean
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-show-debug-help-p t
  "Whether to show brief help in the echo area when entering the debugger."
  :type 'boolean
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-warning-level 'medium
  "Verbosity of the warnings reported by Guile.

You can either choose one of the predefined warning sets, or
provide a list of symbols identifying the ones you want.  Possible
choices are arity-mismatch, unbound-variable, unused-variable and
unused-toplevel.  Unrecognised symbols are ignored.

The predefined levels are:

  - Medium: arity-mismatch, unbound-variable, format
  - High: arity-mismatch, unbound-variable, unused-variable, format
  - None: no warnings

Changes to the value of this variable will automatically take
effect on new REPLs.  For existing ones, use the command
\\[geiser-guile-update-warning-level]."
  :type '(choice (const :tag "Medium (arity and unbound vars)" medium)
                 (const :tag "High (also unused vars)" high)
                 (const :tag "No warnings" none)
                 (repeat :tag "Custom" symbol))
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-extra-keywords nil
  "Extra keywords highlighted in Guile scheme buffers."
  :type '(repeat string)
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-case-sensitive-p t
  "Non-nil means keyword highlighting is case-sensitive."
  :type 'boolean
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-manual-lookup-other-window-p nil
  "Non-nil means pop up the Info buffer in another window."
  :type 'boolean
  :group 'geiser-guile)

(geiser-custom--defcustom geiser-guile-manual-lookup-nodes
    '("Guile" "guile-2.0")
  "List of info nodes that, when present, are used for manual lookups."
  :type '(repeat string)
  :group 'geiser-guile)


;;; REPL support:

(defun geiser-guile--binary ()
  "Return the name of the Guile binary to execute."
  (if (listp geiser-guile-binary)
      (car geiser-guile-binary)
    geiser-guile-binary))

(defvar geiser-guile-scheme-dir
  (expand-file-name "src" (file-name-directory load-file-name))
  "Directory where the Guile scheme geiser modules are installed.")

(defun geiser-guile--parameters ()
  "Return a list with all parameters needed to start Guile.
This function uses `geiser-guile-init-file' if it exists."
  (let ((init-file (and (stringp geiser-guile-init-file)
                        (expand-file-name geiser-guile-init-file)))
        (q-flags (and (not geiser-guile-load-init-file-p) '("-q"))))
    `(,@(and (listp geiser-guile-binary) (cdr geiser-guile-binary))
      ,@q-flags "-L" ,geiser-guile-scheme-dir
      ,@(apply 'append (mapcar (lambda (p) (list "-L" p))
                               geiser-guile-load-path))
      ,@(and init-file (file-readable-p init-file) (list "-l" init-file)))))

;;(defconst geiser-guile--prompt-regexp "^[^() \n]+@([^)]*?)> ")
(defconst geiser-guile--prompt-regexp "[^@()]+@([^)]*?)> ")
(defconst geiser-guile--debugger-prompt-regexp
  "[^@()]+@([^)]*?) \\[[0-9]+\\]> ")


;;; Evaluation support:
(defsubst geiser-guile--linearize-args (args)
  "Concatenate the list ARGS."
  (mapconcat 'identity args " "))

(defun geiser-guile--geiser-procedure (proc &rest args)
  "Transform PROC in string for a scheme procedure using ARGS."
  (cl-case proc
    ((eval compile) (format ",geiser-eval %s %s%s"
                            (or (car args) "#f")
                            (geiser-guile--linearize-args (cdr args))
                            (if (cddr args) "" " ()")))
    ((load-file compile-file) (format ",geiser-load-file %s" (car args)))
    ((no-values) ",geiser-no-values")
    (t (format "ge:%s (%s)" proc (geiser-guile--linearize-args args)))))

(defconst geiser-guile--module-re
  "(define-module +\\(([^)]+)\\)")

(defconst geiser-guile--library-re
  "(library +\\(([^)]+)\\)")

(defun geiser-guile--get-module (&optional module)
  "Find current buffer's module using MODULE as a hint."
  (cond ((null module)
         (save-excursion
           (geiser-syntax--pop-to-top)
           (if (or (re-search-backward geiser-guile--module-re nil t)
                   (re-search-backward geiser-guile--library-re nil t)
                   (re-search-forward geiser-guile--module-re nil t)
                   (re-search-forward geiser-guile--library-re nil t))
               (geiser-guile--get-module (match-string-no-properties 1))
             :f)))
        ((listp module) module)
        ((stringp module)
         (condition-case nil
             (car (geiser-syntax--read-from-string module))
           (error :f)))
        (t :f)))

(defun geiser-guile--module-cmd (module fmt &optional def)
  "Use FMT to format a change to MODULE, with default DEF."
  (when module
    (let* ((module (geiser-guile--get-module module))
           (module (cond ((or (null module) (eq module :f)) def)
                         (t (format "%s" module)))))
      (and module (format fmt module)))))

(defun geiser-guile--import-command (module)
  "Format a REPL command to use MODULE."
  (geiser-guile--module-cmd module ",use %s"))

(defun geiser-guile--enter-command (module)
  "Format a REPL command to enter MODULE."
  (geiser-guile--module-cmd module ",m %s" "(guile-user)"))


(defun geiser-guile--exit-command ()
  "Format a REPL command to quit."
  ",q")

(defun geiser-guile--symbol-begin (module)
  "Find beginning of symbol in the context of MODULE."
  (if module
      (max (save-excursion (beginning-of-line) (point))
           (save-excursion (skip-syntax-backward "^(>") (1- (point))))
    (save-excursion (skip-syntax-backward "^'-()>") (point))))


;;; Error display

(defun geiser-guile--enter-debugger ()
  "Tell Geiser to interact with the debugger."
  (let ((bt-cmd (format ",geiser-newline\n,error-message\n,%s\n"
                        (if geiser-guile-debug-show-bt-p "bt" "fr"))))
    (compilation-forget-errors)
    (goto-char (point-max))
    (geiser-repl--prepare-send)
    (comint-send-string nil bt-cmd)
    (when geiser-guile-show-debug-help-p
      (message "Debug REPL. Enter ,q to quit, ,h for help."))
    (when geiser-guile-jump-on-debug-p
      (accept-process-output (get-buffer-process (current-buffer))
                             0.2 nil t)
      (ignore-errors (next-error)))
    t))

(defun geiser-guile--display-error (_module key msg)
  "Display error with given KEY and message MSG."
  (when (stringp msg)
    (save-excursion (insert msg))
    (geiser-edit--buttonize-files))
  (and (not key) (not (zerop (length msg))) msg))


;;; Trying to ascertain whether a buffer is Guile Scheme:

(defconst geiser-guile--guess-re
  (format "\\(%s\\|#! *.+\\(/\\| \\)guile\\( *\\\\\\)?\\)"
          geiser-guile--module-re))

(defun geiser-guile--guess ()
  "Ascertain whether we are in a Guile file."
  (save-excursion
    (goto-char (point-min))
    (re-search-forward geiser-guile--guess-re nil t)))


;;; Keywords and syntax

(defconst geiser-guile--builtin-keywords
  '("call-with-input-file"
    "call-with-input-string"
    "call-with-output-file"
    "call-with-output-string"
    "call-with-prompt"
    "call-with-trace"
    "define-accessor"
    "define-class"
    "define-enumeration"
    "define-inlinable"
    "define-syntax-parameter"
    "eval-when"
    "lambda*"
    "syntax-parameterize"
    "use-modules"
    "with-error-to-file"
    "with-error-to-port"
    "with-error-to-string"
    "with-fluid*"
    "with-fluids"
    "with-fluids*"
    "with-input-from-port"
    "with-input-from-string"
    "with-output-to-port"
    "with-output-to-string"))

(defun geiser-guile--keywords ()
  "Return Guile-specific scheme keywords."
  (append
   (geiser-syntax--simple-keywords geiser-guile-extra-keywords)
   (geiser-syntax--simple-keywords geiser-guile--builtin-keywords)
   `((,(rx "(" (group "define-once") eow (* space) (? (group (+ word))))
      (1 font-lock-keyword-face)
      (2 font-lock-variable-name-face nil t))
     ("(\\(define-module\\) +(\\([^)]+\\))"
      (1 font-lock-keyword-face)
      (2 font-lock-type-face nil t)))))

(geiser-syntax--scheme-indent
 (c-declare 0)
 (c-lambda 2)
 (call-with-input-string 1)
 (call-with-output-string 0)
 (call-with-prompt 1)
 (call-with-trace 0)
 (eval-when 1)
 (lambda* 1)
 (pmatch defun)
 (sigaction 1)
 (syntax-parameterize 1)
 (with-error-to-file 1)
 (with-error-to-port 1)
 (with-error-to-string 0)
 (with-fluid* 1)
 (with-fluids 1)
 (with-fluids* 1)
 (with-input-from-string 1)
 (with-method 1)
 (with-mutex 1)
 (with-output-to-string 0)
 (with-throw-handler 1))


;;; Compilation shell regexps

(defconst geiser-guile--path-rx "^In \\([^:\n ]+\\):\n")

(defconst geiser-guile--rel-path-rx "^In +\\([^/\n :]+\\):\n")

(defvar geiser-guile--file-cache (make-hash-table :test 'equal)
  "Internal cache.")

(defun geiser-guile--resolve-file (file)
  "Find the given FILE, if it's indeed a file."
  (when (and (stringp file)
             (not (member file '("socket" "stdin" "unknown file"))))
    (if (file-name-absolute-p file) file
      (or (gethash file geiser-guile--file-cache)
          (puthash file
                   (geiser-eval--send/result `(:eval (:ge find-file ,file)))
                   geiser-guile--file-cache)))))

(defun geiser-guile--resolve-file-x ()
  "Check if last match contain a resolvable file."
  (let ((f (geiser-guile--resolve-file (match-string-no-properties 1))))
    (and (stringp f) (list f))))


;;; REPL startup

(defconst geiser-guile-minimum-version "2.2")

(defun geiser-guile--version (binary)
  "Find Guile's version running BINARY."
  (car (process-lines binary "-c" "(display (version))")))

(defun geiser-guile-update-warning-level ()
  "Update the warning level used by the REPL.
The new level is set using the value of `geiser-guile-warning-level'."
  (interactive)
  (let ((code `(:eval (:ge set-warnings ',geiser-guile-warning-level)
                      (geiser evaluation))))
    (geiser-eval--send/result code)))

;;;###autoload
(defun connect-to-guile ()
  "Start a Guile REPL connected to a remote process.

Start the external Guile process with the flag --listen to make
it spawn a server thread."
  (interactive)
  (geiser-connect 'guile))

(defun geiser-guile--set-geiser-load-path ()
  "Set up scheme load path for REPL."
  (let* ((path geiser-guile-scheme-dir)
         (witness "geiser/emacs.scm")
         (code `(begin (if (not (%search-load-path ,witness))
                           (set! %load-path (cons ,path %load-path)))
                       'done)))
    (geiser-eval--send/wait code)))

(defun geiser-guile--startup (remote)
  "Startup function, for a remote connection if REMOTE is t."
  (set (make-local-variable 'compilation-error-regexp-alist)
       `((,geiser-guile--path-rx geiser-guile--resolve-file-x)
         ("^  +\\([0-9]+\\):\\([0-9]+\\)" nil 1 2)))
  (compilation-setup t)
  (font-lock-add-keywords nil `((,geiser-guile--path-rx
                                 1 compilation-error-face)))
  (let ((geiser-log-verbose-p t)
        (g-load-path (buffer-local-value 'geiser-guile-load-path
                                         (or geiser-repl--last-scm-buffer
                                             (current-buffer)))))
    (when remote (geiser-guile--set-geiser-load-path))
    (geiser-eval--send/wait ",use (geiser emacs)\n'done")
    (dolist (dir g-load-path)
      (let ((dir (expand-file-name dir)))
        (geiser-eval--send/wait `(:eval (:ge add-to-load-path ,dir)))))
    (geiser-guile-update-warning-level)))


;;; Manual lookup

(defun geiser-guile--info-spec (&optional nodes)
  "Return info specification for given NODES."
  (let* ((nrx "^[       ]+-+ [^:]+:[    ]*")
         (drx "\\b")
         (res (when (Info-find-file "r5rs" t)
                `(("(r5rs)Index" nil ,nrx ,drx)))))
    (dolist (node (or nodes geiser-guile-manual-lookup-nodes) res)
      (when (Info-find-file node t)
        (mapc (lambda (idx)
                (add-to-list 'res
                             (list (format "(%s)%s" node idx) nil nrx drx)))
              '("Variable Index" "Procedure Index" "R5RS Index"))))))


(info-lookup-add-help :topic 'symbol :mode 'geiser-guile-mode
                      :ignore-case nil
                      :regexp "[^()`',\"        \n]+"
                      :doc-spec (geiser-guile--info-spec))

(defun geiser-guile--manual-look-up (id _mod)
  "Look for ID in the Guile manuals."
  (let ((info-lookup-other-window-flag
         geiser-guile-manual-lookup-other-window-p))
    (info-lookup-symbol (symbol-name id) 'geiser-guile-mode))
  (when geiser-guile-manual-lookup-other-window-p
    (switch-to-buffer-other-window "*info*"))
  (search-forward (format "%s" id) nil t))


;;; Implementation definition:

(define-geiser-implementation guile
  (binary geiser-guile--binary)
  (arglist geiser-guile--parameters)
  (version-command geiser-guile--version)
  (minimum-version geiser-guile-minimum-version)
  (repl-startup geiser-guile--startup)
  (prompt-regexp geiser-guile--prompt-regexp)
  (debugger-prompt-regexp geiser-guile--debugger-prompt-regexp)
  (enter-debugger geiser-guile--enter-debugger)
  (marshall-procedure geiser-guile--geiser-procedure)
  (find-module geiser-guile--get-module)
  (enter-command geiser-guile--enter-command)
  (exit-command geiser-guile--exit-command)
  (import-command geiser-guile--import-command)
  (find-symbol-begin geiser-guile--symbol-begin)
  (display-error geiser-guile--display-error)
  (external-help geiser-guile--manual-look-up)
  (check-buffer geiser-guile--guess)
  (keywords geiser-guile--keywords)
  (case-sensitive geiser-guile-case-sensitive-p))

(geiser-implementation-extension 'guile "scm")

;;;###autoload
(geiser-activate-implementation 'guile)

;;;###autoload
(autoload 'run-guile "geiser-guile" "Start a Geiser Guile REPL." t)

;;;###autoload
(autoload 'switch-to-guile "geiser-guile"
  "Start a Geiser Guile REPL, or switch to a running one." t)

(provide 'geiser-guile)
;;; geiser-guile.el ends here