-
Notifications
You must be signed in to change notification settings - Fork 6
/
ob-prolog.el
263 lines (232 loc) · 9.51 KB
/
ob-prolog.el
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
;;; ob-prolog.el --- org-babel functions for prolog evaluation.
;; Copyright (C) Bjarte Johansen
;; Author: Bjarte Johansen
;; Keywords: literate programming, reproducible research
;; URL: https://github.com/ljos/ob-prolog
;; Version: 1.0.2
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Org-babel support for prolog.
;;
;; To activate ob-prolog add the following to your init.el file:
;;
;; (add-to-list 'load-path "/path/to/ob-prolog-dir")
;; (org-babel-do-load-languages
;; 'org-babel-load-languages
;; '((prolog . t)))
;;
;; It is unnecessary to add the directory to the load path if you
;; install using the package manager.
;;
;; In addition to the normal header arguments ob-prolog also supports
;; the :goal argument. :goal is the goal that prolog will run when
;; executing the source block. Prolog needs a goal to know what it is
;; going to execute.
;;
;;; Code:
(require 'ob)
(require 'ob-ref)
(require 'ob-comint)
(require 'ob-eval)
(require 'prolog)
(add-to-list 'org-babel-tangle-lang-exts '("prolog" . "pl"))
(defvar org-babel-prolog-command (or prolog-system "swipl")
"Name of the prolog executable command.")
(defconst org-babel-header-args:prolog
'((:goal . :any))
"Prolog-specific header arguments.")
(defvar org-babel-default-header-args:prolog
`((:goal . nil)))
(defun org-babel-prolog--elisp-to-pl (value)
"Convert the Emacs Lisp VALUE to equivalent Prolog."
(cond ((stringp value)
(format "'%s'"
(replace-regexp-in-string
"'" "\\'" value)))
((listp value)
(format "[%s]"
(mapconcat #'org-babel-prolog--elisp-to-pl
value
", ")))
(t (prin1-to-string value))))
(defun org-babel-prolog--variable-assignment (pair)
"Return a string of a recorda/2 assertion of (cdr PAIR) under (car PAIR).
The Emacs Lisp value of the car of PAIR is used as the Key argument to
recorda/2 without modification. The cdr of PAIR is converted to
equivalent Prolog before being provided as the Term argument to
recorda/2."
(format "recorda('%s', %s)"
(car pair)
(org-babel-prolog--elisp-to-pl (cdr pair))))
(defun org-babel-variable-assignments:prolog (params)
"Return the babel variable assignments in PARAMS.
PARAMS is a quasi-alist of header args, which may contain
multiple entries for the key `:var'. This function returns a
list of the cdr of all the `:var' entries."
(let (vars)
(dolist (param params vars)
(when (eq (car param) :var)
(setq vars (cons (org-babel-prolog--variable-assignment (cdr param))
vars))))
(when vars
(list (concat ":- " (mapconcat #'identity vars ", ") ".\n")))))
(defun org-babel-prolog--parse-goal (goal)
"Evaluate the inline Emacs Lisp in GOAL.
Example:
append(=(+ 2 3), =(quote a), B)
=> append(5, a, B)"
(when goal
(with-temp-buffer
(insert goal)
(while (search-backward "=" nil t)
(delete-char 1 t)
(forward-sexp)
(let ((value (eval (preceding-sexp))))
(kill-sexp -1)
(insert (format "%S" value))))
(buffer-string))))
(defun org-babel-execute:prolog (body params)
"Execute the Prolog in BODY according to the block's header PARAMS.
This function is called by `org-babel-execute-src-block.'"
(message "executing Prolog source code block")
(let* ((result-params (cdr (assq :result-params params)))
(session (cdr (assq :session params)))
(goal (org-babel-prolog--parse-goal
(cdr (assq :goal params))))
(vars (org-babel-variable-assignments:prolog params))
(full-body (org-babel-expand-body:generic body params vars))
(results (if (string= "none" session)
(org-babel-prolog-evaluate-external-process
goal full-body)
(org-babel-prolog-evaluate-session
session goal full-body))))
(unless (string= "" results)
(org-babel-reassemble-table
(org-babel-result-cond result-params
results
(let ((tmp (org-babel-temp-file "prolog-results-")))
(with-temp-file tmp (insert results))
(org-babel-import-elisp-from-file tmp)))
(org-babel-pick-name (cdr (assq :colname-names params))
(cdr (assq :colnames params)))
(org-babel-pick-name (cdr (assq :rowname-names params))
(cdr (assq :rownames params)))))))
(defun org-babel-load-session:prolog (session body params)
"Load BODY into SESSION."
(let* ((params (org-babel-process-params params))
(session (org-babel-prolog-initiate-session
(cdr (assq :session session)))))
(org-babel-prolog-initiate-session session)))
(defun org-babel-prolog-evaluate-external-process (goal body)
"Evaluate the GOAL given the BODY in an external Prolog process.
If no GOAL is given, the GOAL is replaced with HALT. This resulsts in
running just the body through the Prolog process."
(let* ((tmp-file (org-babel-temp-file "prolog-"))
(command (format "%s -q -l %s -t \"%s\""
org-babel-prolog-command
tmp-file
(replace-regexp-in-string
"\"" "\\\"" (or goal "halt")))))
(with-temp-file tmp-file
(insert (org-babel-chomp body)))
(org-babel-eval command "")))
(defun org-babel-prolog-evaluate-session (session goal body)
"In SESSION, evaluate GOAL given the BODY of the Prolog block.
Create SESSION if it does not already exist."
(let* ((session (org-babel-prolog-initiate-session session))
(body (split-string (org-babel-trim body) "\n")))
(org-babel-trim
(with-temp-buffer
(with-current-buffer session
(setq comint-prompt-regexp "^|: *"))
(org-babel-comint-input-command session "consult(user).\n")
(apply #'insert
(org-babel-comint-with-output (session "\n")
(setq comint-prompt-regexp (prolog-prompt-regexp))
(dolist (line body)
(insert line)
(comint-send-input nil t)
(accept-process-output
(get-buffer-process session)))
(comint-send-eof)))
(ansi-color-apply-on-region (point-min) (point-max))
(goto-char (point-max))
(if (save-excursion
(search-backward "ERROR: " nil t))
(progn
(save-excursion
(while (search-backward "|: " nil t)
(replace-match "" nil t)))
(search-backward "true." nil t)
(kill-whole-line)
(org-babel-eval-error-notify -1 (buffer-string))
(buffer-string))
(when goal
(kill-region (point-min) (point-max))
(apply #'insert
(org-babel-comint-with-output (session "")
(insert (concat goal ", !."))
(comint-send-input nil t))))
(ansi-color-apply-on-region (point-min) (point-max))
(if (not (save-excursion
(search-backward "ERROR: " nil t)))
(let ((delete-trailing-lines t))
(delete-trailing-whitespace (point-min))
(buffer-string))
(search-backward "?-" nil t)
(kill-whole-line)
(org-babel-eval-error-notify -1 (buffer-string))
(buffer-string)))))))
(defun org-babel-prolog--answer-correction (string)
"If STRING is Prolog's \"Correct to:\" prompt, send a refusal."
(when (string-match-p "Correct to: \".*\"\\?" string)
(insert "no")
(comint-send-input nil t)))
(defun org-babel-prolog--exit-debug (string)
"If STRING indicates an exception, continue Prolog execution in no debug mode."
(when (string-match-p "\\(.\\|\n\\)*Exception.* \\? $" string)
(insert "no debug")
(comint-send-input nil t)))
(defun org-babel-prolog-initiate-session (&optional session)
"Return SESSION with a current inferior-process-buffer.
Initialize SESSION if it has not already been initialized."
(unless (string= session "none")
(let ((session (get-buffer-create (or session "*prolog*"))))
(unless (comint-check-proc session)
(with-current-buffer session
(kill-region (point-min) (point-max))
(prolog-inferior-mode)
(setq prolog-program-name org-babel-prolog-command)
(apply #'make-comint-in-buffer
"prolog"
(current-buffer)
(prolog-program-name)
nil
(cons "-q" (prolog-program-switches)))
(add-hook 'comint-output-filter-functions
#'org-babel-prolog--answer-correction nil t)
(add-hook 'comint-output-filter-functions
#'org-babel-prolog--exit-debug nil t)
(while (progn
(goto-char comint-last-input-end)
(not (save-excursion
(re-search-forward comint-prompt-regexp nil t))))
(accept-process-output
(get-buffer-process session)))))
session)))
(provide 'ob-prolog)
;;; ob-prolog.el ends here