summaryrefslogtreecommitdiff
path: root/elisp/geiser-plt.el
blob: 52af7280f162b567894a224013f59afd8e67dc49 (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
;; geiser-plt.el -- geiser support for PLT scheme

;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the Modified BSD License. You should
;; have received a copy of the license along with this program. If
;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.

;; Start date: Sat Apr 25, 2009 21:13



(require 'geiser-edit)
(require 'geiser-doc)
(require 'geiser-eval)
(require 'geiser-syntax)
(require 'geiser-custom)
(require 'geiser-base)


;;; Customization:

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

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

(geiser-custom--defcustom geiser-plt-collects nil
  "A list of paths to be added to mzscheme's collection directories."
  :type '(repeat file)
  :group 'geiser-plt)

(geiser-custom--defcustom geiser-plt-init-file "~/.plt-geiser"
  "Initialization file with user code for the mzscheme REPL."
  :type 'string
  :group 'geiser-plt)



;;; REPL support:

(defun geiser-plt--binary ()
  (if (listp geiser-plt-binary) (car geiser-plt-binary) geiser-plt-binary))

(defun geiser-plt--parameters ()
  "Return a list with all parameters needed to start mzscheme.
This function uses `geiser-plt-init-file' if it exists."
  (let ((init-file (and (stringp geiser-plt-init-file)
                        (expand-file-name geiser-plt-init-file))))
    `("-i" "-q"
      "-S" ,(expand-file-name "plt/" geiser-scheme-dir)
      ,@(apply 'append (mapcar (lambda (p) (list "-S" p)) geiser-plt-collects))
      ,@(and (listp geiser-plt-binary) (cdr geiser-plt-binary))
      ,@(and init-file (file-readable-p init-file) (list "-f" init-file))
      "-f" ,(expand-file-name "plt/geiser.ss" geiser-scheme-dir))))

(defconst geiser-plt--prompt-regexp "^=?mzscheme@[^ ]*?> ")


;;; Evaluation support:

(defun geiser-plt--language ()
  (save-excursion
    (goto-char (point-min))
    (if (re-search-forward
         "^\\(?:#lang\\|(module +[^ ]+?\\) +\\([^ ]+?\\|([^)]+)\\) *$" nil t)
        (car (geiser-syntax--read-from-string (match-string-no-properties 1)))
      :f)))

(defun geiser-plt--geiser-procedure (proc)
  (if (memq proc '(eval compile))
      `((dynamic-require 'geiser 'geiser:eval) ',(geiser-plt--language))
    `(dynamic-require 'geiser ',(intern (format "geiser:%s" proc)))))

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

(defun geiser-plt--explicit-module ()
  (save-excursion
    (goto-char (point-min))
    (and (re-search-forward geiser-plt--module-re nil t)
         (ignore-errors
           (car (geiser-syntax--read-from-string
                 (match-string-no-properties 1)))))))

(defsubst geiser-plt--implicit-module ()
  (save-excursion
    (goto-char (point-min))
    (if (re-search-forward "^#lang " nil t)
        (buffer-file-name)
      :f)))

(defun geiser-plt--get-module (&optional module)
  (cond ((and (null module) (buffer-file-name)))
        ;; (geiser-plt--explicit-module)
        ((null module) (geiser-plt--implicit-module))
        ((symbolp module) module)
        ((and (stringp module) (file-name-absolute-p module)) module)
        ((stringp module) (intern module))
        (t nil)))

(defun geiser-plt--symbol-begin (module)
  (save-excursion (skip-syntax-backward "^-()>") (point)))

(defun geiser-plt--enter-command (module)
  (and (stringp module) (format "(enter! (file %S))" module)))

(defconst geiser-plt--binding-forms
  '(for for/list for/hash for/hasheq for/and for/or
    for/lists for/first for/last for/fold))

(defconst geiser-plt--binding-forms*
  '(for* for*/list for*/lists for*/hash for*/hasheq for*/and
    for*/or for*/first for*/last for*/fold))

;;; External help

(defsubst geiser-plt--get-help (symbol module)
  (geiser-eval--send/wait
   `(:eval (get-help ',symbol (:module ,module)) geiser/autodoc)))

(defun geiser-plt--external-help (id module)
  (message "Requesting help for '%s'..." id)
  (let ((out (geiser-eval--retort-output (geiser-plt--get-help id module))))
    (when (and out (string-match " but provided by:\n +\\(.+\\)\n" out))
      (geiser-plt--get-help symbol (match-string 1 out))))
  (minibuffer-message "%s done" (current-message))
  t)


;;; Error display

(defconst geiser-plt--file-rxs '("^\\([^:\n\"]+\\):\\([0-9]+\\):\\([0-9]+\\)"
                                 "path:\"?\\([^>\"\n]+\\)\"?>"
                                 "module: \"\\([^>\"\n]+\\)\""))

(defun geiser-plt--find-files (rx)
  (save-excursion
    (while (re-search-forward rx nil t)
      (geiser-edit--make-link (match-beginning 1)
                              (match-end 1)
                              (match-string 1)
                              (match-string 2)
                              (match-string 3)))))

(defun geiser-plt--display-error (module key msg)
  (when key
    (insert "Error: ")
    (geiser-doc--insert-button key nil 'plt)
    (newline 2))
  (when msg
    (let ((p (point)))
      (insert msg)
      (when key
        (let ((end (point)))
        (goto-char p)
        (mapc 'geiser-plt--find-files geiser-plt--file-rxs)
        (goto-char end)
        (newline)))))
  t)


;;; Trying to ascertain whether a buffer is mzscheme scheme:

(defun geiser-plt--guess ()
  (or (save-excursion
        (goto-char (point-min))
        (re-search-forward "#lang " nil t))
      (geiser-plt--explicit-module)))


;;; Implementation definition:

(define-geiser-implementation plt
  (unsupported-procedures '(callers callees generic-methods))
  (binary geiser-plt--binary)
  (arglist geiser-plt--parameters)
  (startup)
  (prompt-regexp geiser-plt--prompt-regexp)
  (marshall-procedure geiser-plt--geiser-procedure)
  (find-module geiser-plt--get-module)
  (enter-command geiser-plt--enter-command)
  (find-symbol-begin geiser-plt--symbol-begin)
  (display-error geiser-plt--display-error)
  (display-help geiser-plt--external-help)
  (check-buffer geiser-plt--guess)
  (binding-forms geiser-plt--binding-forms)
  (binding-forms* geiser-plt--binding-forms*))

(geiser-impl--add-to-alist 'regexp "\\.mzscheme\\.sl?s$" 'plt t)
(geiser-impl--add-to-alist 'regexp "\\.ss$" 'plt t)

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