summaryrefslogtreecommitdiff
path: root/geiser-chez.el
blob: 170570d70819785215619c3c7f92c0fb2452386b (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
;;; geiser-chez.el --- Chez and Geiser talk to each other  -*- lexical-binding: t; -*-

;; Author: Peter <craven@gmx.net>
;; Maintainer: Jose A Ortega Ruiz <jao@gnu.org>
;; Keywords: languages, chez, scheme, geiser
;; Homepage: https://gitlab.com/emacs-geiser/chez
;; Package-Requires: ((emacs "26.1") (geiser "0.19"))
;; SPDX-License-Identifier: BSD-3-Clause
;; Version: 0.17

;;; Commentary:

;; This package provides support for Chez scheme in geiser.

;;; Code:

(require 'geiser)

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

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

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

;;; Customization

(defgroup geiser-chez nil
  "Customization for Geiser's Chez Scheme flavour."
  :group 'geiser)

(geiser-custom--defcustom geiser-chez-binary
    "scheme"
  "Name to use to call the Chez Scheme executable when starting a REPL."
  :type '(choice string (repeat string)))

(geiser-custom--defcustom geiser-chez-init-file "~/.chez-geiser"
  "Initialization file with user code for the Chez REPL.

Do mind that this file is local to running process, so remote
process will use an init file at this location in the remote
host."
  :type 'string)

(geiser-custom--defcustom geiser-chez-extra-command-line-parameters '()
  "Additional parameters to supply to the Chez binary."
  :type '(repeat string))

(geiser-custom--defcustom geiser-chez-extra-keywords '()
  "Extra keywords highlighted in Chez Scheme buffers."
  :type '(repeat string))

(geiser-custom--defcustom geiser-chez-debug-on-exception nil
  "Whether to automatically enter the debugger when an evaluation throws."
  :type 'boolean)

(define-obsolete-variable-alias 'geiser-chez-debug-on-exception-p
  'geiser-chez-debug-on-exception "0.18")

(defconst geiser-chez-minimum-version "9.4")

;;; REPL support

(defun geiser-chez--binary ()
  "Return path to Chez scheme binary."
  (if (listp geiser-chez-binary)
      (car geiser-chez-binary)
    geiser-chez-binary))

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

(defun geiser-chez--init-files ()
  "Possibly remote init file(s), when they exist, as a list."
  (let* ((file (and (stringp geiser-chez-init-file)
                    (expand-file-name geiser-chez-init-file)))
         (file (and file (concat (file-remote-p default-directory) file))))
    (if (and file (file-exists-p file))
        (list file)
      (geiser-log--info "Init file not readable (%s)" file)
      nil)))

(defun geiser-chez--module-file (file)
  "Copy, if needed, the given scheme file to its remote destination.
Return its local name."
  (let ((local (expand-file-name (concat "geiser/" file) geiser-chez-scheme-dir)))
    (if (file-remote-p default-directory)
        (let* ((temporary-file-directory (temporary-file-directory))
               (temp-dir (make-temp-file "geiser" t))
               (remote (concat (file-name-as-directory temp-dir) "geiser.ss")))
          (with-temp-buffer
            (insert-file-contents local)
            (write-file remote))
          (file-local-name remote))
      local)))

(defun geiser-chez--module-files ()
  "List of (possibly copied to a tramped remote) scheme files used by chez."
  (mapcar #'geiser-chez--module-file '("geiser-data.ss" "geiser.ss")))

(defun geiser-chez--parameters ()
  "Return a list with all parameters needed to start Chez Scheme."
  (append (cons "--compile-imported-libraries" (geiser-chez--init-files))
          (geiser-chez--module-files)
          geiser-chez-extra-command-line-parameters))

(defconst geiser-chez--prompt-regexp "> ")
(defconst geiser-chez--debugger-prompt-regexp "debug> $\\|break> $\\|.+: $")

(defun geiser-chez--version (binary)
  "Use BINARY to find Chez scheme version."
  (car (process-lines binary "--version")))

(defun geiser-chez--startup (_remote)
  "Startup function."
  t)

;;; Evaluation support

(defsubst geiser-chez--geiser-eval (str)
  (format "(eval '%s (environment '(geiser)))" str))

(defconst geiser-chez--ge (geiser-chez--geiser-eval "geiser:ge:eval"))
(defconst geiser-chez--ev (geiser-chez--geiser-eval "geiser:eval"))
(defconst geiser-chez--load (geiser-chez--geiser-eval "geiser:load-file"))

(defun geiser-chez--geiser-procedure (proc &rest args)
  "Transform PROC in string for a scheme procedure using ARGS."
  (cl-case proc
    ((eval compile)
     (if (listp (cadr args))
         (format "(%s '%s '%s)" geiser-chez--ge (car args) (cadr args))
       (format "(%s '%s '%s)" geiser-chez--ev (car args) (cadr args))))
    ((load-file compile-file)
     (let ((lib (geiser-chez--current-library)))
       (format "(%s %s '%s)" geiser-chez--load (car args) (or lib "#f"))))
    (t (list (format "geiser:%s" proc) (mapconcat 'identity args " ")))))

(defun geiser-chez--current-library ()
  "Find current library."
  (save-excursion
    (geiser-syntax--pop-to-top)
    (when (looking-at "(library[ \n]+\\(([^)]+)\\)")
      (geiser-chez--get-module (match-string 1)))))

(defun geiser-chez--get-module (&optional module)
  "Find current module (libraries for Chez), or normalize MODULE."
  (cond ((null module) (or (geiser-chez--current-library) :f))
        ((listp module) module)
        ((and (stringp module)
              (ignore-errors (car (geiser-syntax--read-from-string module)))))
        (t :f)))

(defun geiser-chez--symbol-begin (module)
  "Return beginning of current symbol while in MODULE."
  (if module
      (max (save-excursion (beginning-of-line) (point))
           (save-excursion (skip-syntax-backward "^(>") (1- (point))))
    (save-excursion (skip-syntax-backward "^'-()>") (point))))

(defun geiser-chez--import-command (module)
  "Return string representing a sexp importing MODULE."
  (format "(import %s)" module))

(defun geiser-chez--exit-command ()
  "Return string representing a REPL exit sexp."
  "(exit 0)")


;;; Error display and debugger

(defun geiser-chez--enter-debugger ()
  "Tell Geiser to interact with the debugger."
  (when geiser-chez-debug-on-exception
    (geiser-repl-switch  nil 'chez)
    (compilation-forget-errors)
    (geiser-repl--send "(debug)")
    t))

(defun geiser-chez--display-error (_module key msg)
  "Display an error found during evaluation with the given KEY and message MSG."
  (when (and msg (listp msg))
    (save-excursion
      (insert (car msg))
      (insert "\n")
      (dolist (loc (reverse (cdr msg)))
        (let ((file (cdr (assoc "file" loc)))
              (line (or (cdr (assoc "line" loc)) ""))
              (col (or (cdr (assoc "column" loc)) (cdr (assoc "char" loc))))
              (name (cdr (assoc "name" loc))))
          (unless (string-prefix-p geiser-chez-scheme-dir file)
            (insert "\n" file (format ":%s:" line))
            (when col (insert (format "%s:" col)))
            (when name (insert (format " (%s)" name)))))))
    (geiser-edit--buttonize-files)
    t))

;;; Keywords and syntax

(defconst geiser-chez--builtin-keywords
  '("call-with-input-file"
    "call-with-output-file"
    "define-ftype"
    "define-structure"
    "exclusive-cond"
    "extend-syntax"
    "fluid-let"
    "fluid-let-syntax"
    "meta"
    "meta-cond"
    "module"
    "rec"
    "record-case"
    "trace-case-lambda"
    "trace-define"
    "trace-define-syntax"
    "trace-do"
    "trace-lambda"
    "trace-let"
    "with"
    "with-implicit"
    "with-input-from-file"
    "with-input-from-string"
    "with-interrupts-disabled"
    "with-mutex"
    "with-output-to-file"
    "with-output-to-string"))

(defun geiser-chez--keywords ()
  "Return list of Chez-specific keywords."
  (append
   (geiser-syntax--simple-keywords geiser-chez-extra-keywords)
   (geiser-syntax--simple-keywords geiser-chez--builtin-keywords)))

(geiser-syntax--scheme-indent
 (call-with-input-file 1)
 (call-with-output-file 1)
 (define-ftype 1)
 (struct 0)
 (union 0)
 (bits 0)
 (define-structure 1)
 (exclusive-cond 0)
 (extend-syntax 1)
 (fluid-let 1)
 (fluid-let-syntax 1)
 (meta 0)
 (meta-cond 0)
 (module 1)
 (rec 1)
 (record-case 1)
 (trace-case-lambda 1)
 (trace-define 1)
 (trace-define-syntax 1)
 (trace-do 2)
 (trace-lambda 2)
 (trace-let 2)
 (with 1)
 (with-implicit 1)
 (with-input-from-file 1)
 (with-input-from-string 1)
 (with-interrupts-disabled 0)
 (with-mutex 1)
 (with-output-to-file 1)
 (with-output-to-string 0))

;;; Implementation definition:

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

(geiser-implementation-extension 'chez "ss")

;;;###autoload
(geiser-implementation-extension 'chez "def")

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

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

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

;;; -
(provide 'geiser-chez)

;;; geiser-chez.el ends here