]> code.delx.au - gnu-emacs-elpa/blob - packages/ediprolog/ediprolog.el
Fix some quoting problems in doc strings
[gnu-emacs-elpa] / packages / ediprolog / ediprolog.el
1 ;;; ediprolog.el --- Emacs Does Interactive Prolog
2
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2012, 2013 Free Software Foundation, Inc.
4
5 ;; Author: Markus Triska <markus.triska@gmx.at>
6 ;; Keywords: languages, processes
7 ;; Version: 1.1
8
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
12 ;; any later version.
13
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; These definitions let you interact with SWI-Prolog in all buffers.
25 ;; You can consult Prolog programs and evaluate embedded queries.
26
27 ;; Installation
28 ;; ============
29 ;;
30 ;; Copy ediprolog.el to your load-path and add to your .emacs:
31 ;;
32 ;; (require 'ediprolog)
33 ;; (global-set-key [f10] 'ediprolog-dwim)
34 ;;
35 ;; Restart Emacs and customize ediprolog with
36 ;;
37 ;; M-x customize-group RET ediprolog RET
38 ;;
39
40 ;; Usage
41 ;; =====
42 ;;
43 ;; The central function is `ediprolog-dwim' (Do What I Mean), which is
44 ;; bound to F10 by the snippet above. Depending on the content at
45 ;; point, `ediprolog-dwim' does the "appropriate" thing: If point is
46 ;; on a query, F10 sends the query to a Prolog process, and you
47 ;; interact with the process in the current buffer as on a terminal.
48 ;; Queries start with "?-" or ":-", possibly preceded by "%" and
49 ;; whitespace. An example of a query is (without leading ";;"):
50 ;;
51 ;; %?- member(X, [a,b,c]).
52 ;;
53 ;; If you press F10 when point is on that query, you get:
54 ;;
55 ;; %?- member(X, [a,b,c]).
56 ;; %@ X = a ;
57 ;; %@ X = b ;
58 ;; %@ X = c ;
59 ;; %@ false.
60 ;;
61 ;; When waiting for output of the Prolog process, you can press C-g to
62 ;; unblock Emacs and continue with other work. To resume interaction
63 ;; with the Prolog process, use M-x ediprolog-toplevel RET.
64
65 ;; If you press F10 when point is NOT on a query, the buffer content
66 ;; is consulted in the Prolog process, and point is moved to the first
67 ;; error (if any).
68
69 ;; For convenience, the most recent interactions with the Prolog
70 ;; process are logged in the buffer "*ediprolog-history*".
71
72 ;; Use M-x ediprolog-localize RET to make any Prolog process started
73 ;; in the current buffer buffer-local. This way, you can run distinct
74 ;; processes simultaneously. Revert with M-x ediprolog-unlocalize RET.
75
76 ;; `ediprolog-dwim' with prefix arguments has special meanings:
77 ;;
78 ;; C-0 F10 kill Prolog process
79 ;; C-1 F10 always consult buffer (even when point is on a query)
80 ;; C-2 F10 always consult buffer, using a new process
81 ;; C-7 F10 equivalent to `ediprolog-toplevel'
82 ;; C-u F10 first consult buffer, then evaluate query (if any)
83 ;; C-u C-u F10 like C-u F10, with a new process
84
85 ;; Tested with SWI-Prolog 5.6.55 + Emacs 21.2, 22.3, 23.1 and 24.3
86
87 ;;; Code:
88
89 (defconst ediprolog-version "0.9z")
90
91 (defgroup ediprolog nil
92 "Transparent interaction with SWI-Prolog."
93 :group 'languages
94 :group 'processes)
95
96 (defcustom ediprolog-program
97 (or (executable-find "swipl") (executable-find "pl") "swipl")
98 "Program name of the Prolog executable."
99 :group 'ediprolog
100 :type 'string)
101
102 (defcustom ediprolog-program-switches nil
103 "List of switches passed to the Prolog process. Example:
104 (\"-G128M\" \"-O\")"
105 :group 'ediprolog
106 :type '(repeat string))
107
108 (defcustom ediprolog-prefix "%@ "
109 "String to prepend when inserting output from the Prolog
110 process into the buffer."
111 :group 'ediprolog
112 :type 'string)
113
114 (defcustom ediprolog-max-history 80000
115 "Maximal size of history buffers storing recent interactions, or
116 nil to never truncate the history."
117 :group 'ediprolog
118 :type 'sexp)
119
120 (defvar ediprolog-process nil "A Prolog process.")
121
122 (defvar ediprolog-temp-buffer nil
123 "Buffer that temporarily saves process output ")
124
125 (defvar ediprolog-seen-prompt nil
126 "Whether a prompt was (recently) emitted by the Prolog process.")
127
128 (defvar ediprolog-read-term nil
129 "Whether the Prolog process waits for the user to enter a term.")
130
131 (defvar ediprolog-indent-prefix ""
132 "Any whitespace occurring before the most recently executed query.")
133
134 (defvar ediprolog-temp-file nil
135 "File name of a temporary file used for consulting the buffer.")
136
137 (defvar ediprolog-prompt "?ediprolog- "
138 "Prompt used in the Prolog session. It must differ from the
139 default Prolog prompt.")
140
141 (defvar ediprolog-consult-buffer "*ediprolog-consult*"
142 "Buffer used to display consult output.")
143
144 (defvar ediprolog-consult-window nil
145 "Window used to show consult output.")
146
147 (defvar ediprolog-history-buffer nil
148 "Buffer that stores recent interactions.")
149
150 (defvar ediprolog-interrupted nil
151 "True iff waiting for the previous query was interrupted with C-g.")
152
153 (defmacro ediprolog-wait-for-prompt-after (&rest forms)
154 "Evaluate FORMS and wait for prompt."
155 `(progn
156 (setq ediprolog-seen-prompt nil)
157 (ediprolog-ensure-buffer "temp")
158 (with-current-buffer ediprolog-temp-buffer
159 (let (buffer-read-only)
160 (erase-buffer)))
161 ;; execute forms with default-directory etc. from invocation buffer
162 ,@forms
163 (while (not ediprolog-seen-prompt)
164 ;; Wait for output/sentinel and update consult window, if any.
165 ;; As `accept-process-output' does not run the sentinel in
166 ;; Emacs <= 23.1, we use `sit-for' to do both. However,
167 ;; `sit-for' returns immediately if keyboard input is
168 ;; available, so we must discard input.
169 (discard-input)
170 (sit-for 0.1))))
171
172 (defmacro ediprolog-remember-interruption (form)
173 "Set `ediprolog-interrupted' if evaluation of FORM was interrupted."
174 `(condition-case nil
175 ,form
176 (quit (setq ediprolog-interrupted t))))
177
178 ;; Only the sentinel can reliably detect if no more output follows -
179 ;; even if process-status is 'exit, further output can still follow.
180 (defun ediprolog-sentinel (proc str)
181 (when (buffer-live-p (process-buffer proc))
182 (with-current-buffer (process-buffer proc)
183 (let ((status (with-temp-buffer
184 (insert str)
185 (while (search-backward "\n" nil t)
186 (replace-match ""))
187 (buffer-string))))
188 (ediprolog-log
189 (format "%s: %s.\n"
190 (substring (current-time-string) 4 -5) status) "green" t))
191 (when (string-match "^\\(?:finished\n\\|exited abnormally\\|killed\n\\)"
192 str)
193 (setq ediprolog-seen-prompt t)))))
194
195 (defun ediprolog-ensure-buffer (name)
196 (let ((str (format "*ediprolog-%s*" name))
197 (var (intern (format "ediprolog-%s-buffer" name))))
198 (unless (buffer-live-p (symbol-value var))
199 (set var (generate-new-buffer str))
200 (with-current-buffer (symbol-value var)
201 (buffer-disable-undo)
202 (setq buffer-read-only t)))))
203
204 (defun ediprolog-log (str &optional col nl)
205 (ediprolog-ensure-buffer "history")
206 (with-current-buffer ediprolog-history-buffer
207 (let (buffer-read-only)
208 (goto-char (point-max))
209 (let ((s (format "%s%s" (if (and nl (not (bolp))) "\n" "") str)))
210 (insert (if col (propertize s 'face `(:background ,col)) s)))
211 (let ((size (- (point-max) (point-min))))
212 (when (and ediprolog-max-history
213 (> size ediprolog-max-history))
214 ;; delete older half of the (possibly narrowed) history
215 (delete-region (point-min) (+ (point-min) (/ size 2))))))))
216
217 (defun ediprolog-run-prolog ()
218 "Start a Prolog process."
219 (let ((args (cons ediprolog-program ediprolog-program-switches)))
220 (ediprolog-log (format "%s: starting: %S\n"
221 (substring (current-time-string) 4 -5) args)
222 "green" t)
223 (condition-case nil
224 (ediprolog-wait-for-prompt-after
225 (setq ediprolog-process
226 (apply #'start-process "ediprolog" (current-buffer) args))
227 (set-process-sentinel ediprolog-process 'ediprolog-sentinel)
228 (set-process-filter ediprolog-process
229 'ediprolog-wait-for-prompt-filter)
230 (ediprolog-send-string
231 (format "set_prolog_flag(color_term, false),\
232 '$set_prompt'('%s').\n" ediprolog-prompt)))
233 ((error quit)
234 (ediprolog-log "No prompt found." "red" t)
235 (error "No prompt from: %s" ediprolog-program)))))
236
237 (defun ediprolog-kill-prolog ()
238 "Kill the Prolog process and run the process sentinel."
239 (when (ediprolog-running)
240 (delete-process ediprolog-process)))
241
242 (defun ediprolog-show-consult-output (str)
243 (with-current-buffer (get-buffer-create ediprolog-consult-buffer)
244 (setq buffer-read-only t)
245 (let (buffer-read-only)
246 (erase-buffer)
247 (insert str)
248 (goto-char (point-min))
249 ;; remove normal consult status lines, which start with "%"
250 (while (re-search-forward "^[\t ]*%.*\n" nil t)
251 (delete-region (match-beginning 0) (match-end 0))))
252 (setq str (buffer-string)))
253 ;; show consult output in a separate window unless it is a prefix of
254 ;; success (i.e., consulted without errors), or still an incomplete
255 ;; line that starts with a comment character
256 (unless (or (string-match "^[\t ]*\\(?:%.*\\)?\\'" str)
257 (let ((success "true."))
258 (and (<= (length str) (length success))
259 (string= str (substring success 0 (length str))))))
260 (setq ediprolog-consult-window (display-buffer ediprolog-consult-buffer))
261 (set-window-dedicated-p ediprolog-consult-window t)
262 (fit-window-to-buffer ediprolog-consult-window (/ (frame-height) 2))))
263
264 (defun ediprolog-consult-filter (proc str)
265 "Filter used when consulting a file, showing consult output."
266 (with-current-buffer (ediprolog-temp-buffer proc)
267 (goto-char (point-max))
268 (let (buffer-read-only)
269 (insert str))
270 (with-current-buffer (process-buffer proc)
271 (ediprolog-log str))
272 (when (re-search-backward
273 (format "^%s" (regexp-quote ediprolog-prompt)) nil t)
274 (with-current-buffer (process-buffer proc)
275 (setq ediprolog-seen-prompt t)))
276 (skip-chars-backward "\n")
277 (ediprolog-show-consult-output (buffer-substring (point-min) (point)))))
278
279 (defun ediprolog-wait-for-prompt-filter (proc str)
280 "Filter that only waits until prompt appears."
281 (with-current-buffer (ediprolog-temp-buffer proc)
282 (goto-char (point-max))
283 (let (buffer-read-only)
284 (insert str))
285 (with-current-buffer (process-buffer proc)
286 (ediprolog-log str))
287 (when (re-search-backward
288 (format "^%s" (regexp-quote ediprolog-prompt)) nil t)
289 (with-current-buffer (process-buffer proc)
290 (setq ediprolog-seen-prompt t)))))
291
292 \f
293 ;;;###autoload
294 (defun ediprolog-dwim (&optional arg)
295 "Load current buffer into Prolog or post query (Do What I Mean).
296 If invoked on a line starting with `:-' or `?-', possibly
297 preceded by `%' and whitespace, call `ediprolog-interact' with
298 the query as argument. Otherwise, call `ediprolog-consult'.
299
300 With prefix argument 0, kill the Prolog process. With prefix 1,
301 equivalent to `ediprolog-consult'. With prefix 2, equivalent to
302 `ediprolog-consult' with a new Prolog process. With prefix 7,
303 equivalent to `ediprolog-toplevel'. With just C-u, first call
304 `ediprolog-consult' and then, if point is on a query, call
305 `ediprolog-interact' with it as argument. Analogously, C-u C-u
306 for `ediprolog-consult' with a new process. With other prefix
307 arguments, equivalent to `ediprolog-remove-interactions'."
308 (interactive "P")
309 (cond ((eq arg 0)
310 (unless (ediprolog-running)
311 (error "No Prolog process running"))
312 (ediprolog-kill-prolog)
313 (message "Prolog process killed."))
314 ((eq arg 1) (ediprolog-consult))
315 ((eq arg 2) (ediprolog-consult t))
316 ((eq arg 7)
317 (unless (ediprolog-more-solutions)
318 (error "No query in progress"))
319 (ediprolog-toplevel))
320 ((equal arg '(4)) (ediprolog-consult) (ediprolog-query))
321 ((equal arg '(16)) (ediprolog-consult t) (ediprolog-query))
322 ((null arg) (unless (ediprolog-query) (ediprolog-consult)))
323 (t (ediprolog-remove-interactions))))
324
325 (defun ediprolog-process-ready ()
326 "Error if the previous query is still in progress."
327 (when (and ediprolog-interrupted
328 (ediprolog-running)
329 (ediprolog-more-solutions))
330 (error "Previous query still in progress, see `ediprolog-toplevel'"))
331 (setq ediprolog-interrupted nil))
332
333 (defun ediprolog-query ()
334 "If point is on a query, send it to the process and start interaction."
335 (ediprolog-process-ready)
336 (when (and (not (and transient-mark-mode mark-active))
337 (save-excursion
338 (beginning-of-line)
339 (looking-at "\\([\t ]*\\)%*[\t ]*[:?]-")))
340 ;; whitespace preceding the query is the indentation level
341 (setq ediprolog-indent-prefix (match-string 1))
342 (let* ((from (goto-char (match-end 0)))
343 (to (if (re-search-forward "\\.[\t ]*\\(?:%.*\\)?$" nil t)
344 ;; omit trailing whitespace
345 (+ (point) (skip-chars-backward "\t "))
346 (error "Missing `.' at the end of this query")))
347 (query (buffer-substring-no-properties from to)))
348 (end-of-line)
349 (insert "\n" ediprolog-indent-prefix ediprolog-prefix)
350 (ediprolog-interact
351 (format "%s\n" (mapconcat #'identity
352 ;; `%' can precede each query line
353 (split-string query "\n[ \t%]*") " "))))
354 t))
355
356 ;;;###autoload
357 (defun ediprolog-interact (query)
358 "Send QUERY to Prolog process and interact as on a terminal.
359
360 You can use \\[keyboard-quit] to unblock Emacs in the case of
361 longer-running queries. When the query completes and the toplevel
362 asks for input, use \\[ediprolog-toplevel] to resume interaction
363 with the Prolog process."
364 (unless (ediprolog-running)
365 (ediprolog-run-prolog))
366 (set-marker (process-mark ediprolog-process) (point))
367 (set-process-buffer ediprolog-process (current-buffer))
368 (set-process-filter ediprolog-process 'ediprolog-interact-filter)
369 (ediprolog-ensure-buffer "temp")
370 (with-current-buffer ediprolog-temp-buffer
371 (let (buffer-read-only)
372 (erase-buffer)))
373 (setq ediprolog-seen-prompt nil
374 ediprolog-read-term nil)
375 (ediprolog-send-string query)
376 (ediprolog-toplevel))
377
378 (defun ediprolog-send-string (str)
379 "Send string to Prolog process and log it."
380 (ediprolog-log str "cyan")
381 (process-send-string ediprolog-process str))
382
383 (defun ediprolog-toplevel ()
384 "Start or resume Prolog toplevel interaction in the buffer.
385
386 You can use this function if you have previously quit (with
387 \\[keyboard-quit]) waiting for a longer-running query and now
388 want to resume interaction with the toplevel."
389 (interactive)
390 (when ediprolog-process
391 (select-window (display-buffer (process-buffer ediprolog-process))))
392 (ediprolog-remember-interruption
393 (while (ediprolog-more-solutions)
394 (let (str
395 char)
396 ;; poll for user input; meanwhile, process output can arrive
397 (while (and (ediprolog-more-solutions) (null str))
398 (goto-char (process-mark ediprolog-process))
399 (if ediprolog-read-term
400 (progn
401 (setq str (concat (read-string "Input: ") "\n"))
402 (ediprolog-insert-at-marker
403 str ediprolog-indent-prefix ediprolog-prefix)
404 (setq ediprolog-read-term nil))
405 (condition-case nil
406 (when (setq char (if (>= emacs-major-version 22)
407 (read-char nil nil 0.1)
408 (with-timeout (0.1 nil)
409 (read-char))))
410 ;; char-to-string might still yield an error (C-0 etc.)
411 (setq str (char-to-string char)))
412 (error
413 (message "Non-character key")
414 ;; non-character keys must not remain in the input
415 ;; buffer, lest `read-char' return immediately
416 (discard-input)))))
417 (when (ediprolog-more-solutions)
418 (if (eq char ?\C-c) ; char can be nil too
419 ;; sending C-c directly yields strange SWI buffering
420 (interrupt-process ediprolog-process)
421 (ediprolog-send-string str)))))))
422
423 ;;;###autoload
424 (defun ediprolog-remove-interactions ()
425 "Remove all lines starting with `ediprolog-prefix' from buffer.
426
427 In transient mark mode, the function operates on the region if it
428 is active."
429 (interactive)
430 (save-excursion
431 (save-restriction
432 (when (and transient-mark-mode mark-active)
433 (narrow-to-region (region-beginning) (region-end)))
434 (goto-char (point-min))
435 (flush-lines (concat "^[\t ]*" (regexp-quote ediprolog-prefix)))))
436 (message "Interactions removed."))
437
438 \f
439 ;;;###autoload
440 (defun ediprolog-consult (&optional new-process)
441 "Buffer is loaded into a Prolog process. If NEW-PROCESS is
442 non-nil, start a new process. Otherwise use the existing process,
443 if any. In case of errors, point is moved to the position of the
444 first error, and the mark is left at the previous position.
445
446 In transient mark mode, the function operates on the region if it
447 is active."
448 (interactive)
449 (when (string= (buffer-name) ediprolog-consult-buffer)
450 (error "Cannot consult the consult buffer"))
451 (when (window-live-p ediprolog-consult-window)
452 (condition-case nil
453 ;; deleting the window can still raise an error, if the window
454 ;; was the only window in the frame and the consult buffer was
455 ;; killed (and it thus displays a different buffer now)
456 (delete-window ediprolog-consult-window)
457 (error nil)))
458 (when (buffer-live-p ediprolog-consult-buffer)
459 (bury-buffer ediprolog-consult-buffer))
460 (when new-process
461 (ediprolog-kill-prolog))
462 (unless (ediprolog-running)
463 (ediprolog-run-prolog))
464 (ediprolog-process-ready)
465 (set-process-buffer ediprolog-process (current-buffer))
466 (unless ediprolog-temp-file
467 (setq ediprolog-temp-file (make-temp-file "ediprolog")))
468 (let ((start (if (and transient-mark-mode mark-active)
469 (region-beginning) (point-min)))
470 (end (if (and transient-mark-mode mark-active)
471 (region-end) (point-max))))
472 (write-region start end ediprolog-temp-file nil 'silent))
473 (set-process-filter ediprolog-process 'ediprolog-consult-filter)
474 (ediprolog-remember-interruption
475 (ediprolog-wait-for-prompt-after
476 (ediprolog-send-string (format "['%s'].\n" ediprolog-temp-file))))
477 (message "%s consulted." (if (and transient-mark-mode mark-active)
478 "Region" "Buffer"))
479 ;; go to line of the first error, if any
480 (let ((line (with-current-buffer ediprolog-temp-buffer
481 (when (save-excursion
482 (goto-char (point-min))
483 (re-search-forward "^ERROR.*?:\\([0-9]+\\)" nil t))
484 (string-to-number (match-string 1))))))
485 (when line
486 (if (and transient-mark-mode mark-active)
487 (when (fboundp 'line-number-at-pos)
488 (goto-line (+ (line-number-at-pos (region-beginning)) line -1)))
489 (goto-line line)))))
490
491 (defun ediprolog-running ()
492 "True iff `ediprolog-process' is a running process."
493 (and (processp ediprolog-process)
494 (eq (process-status ediprolog-process) 'run)))
495
496 (defun ediprolog-more-solutions ()
497 "True iff there could be more solutions from the process."
498 (not ediprolog-seen-prompt))
499
500 (defun ediprolog-interact-filter (proc string)
501 "Insert output from the process and update the state."
502 (when (and (buffer-live-p (ediprolog-temp-buffer proc))
503 (buffer-live-p (process-buffer proc)))
504 (let (str)
505 (with-current-buffer (ediprolog-temp-buffer proc)
506 (goto-char (point-max))
507 (let (buffer-read-only)
508 (insert string))
509 (with-current-buffer (process-buffer proc)
510 (ediprolog-log string))
511 ;; read a term from the user?
512 (when (re-search-backward "^|: $" nil t)
513 (with-current-buffer (process-buffer proc)
514 (setq ediprolog-read-term t))
515 (setq str (buffer-string))
516 (let (buffer-read-only)
517 (erase-buffer)))
518 ;; check for prompt
519 (goto-char (point-max))
520 (when (re-search-backward
521 (format "^%s" (regexp-quote ediprolog-prompt)) nil t)
522 (with-current-buffer (process-buffer proc)
523 (setq ediprolog-seen-prompt t))
524 ;; ignore further output due to accidental user input (C-j,
525 ;; C-m, etc.) while the query was running
526 (set-process-filter proc 'ediprolog-ignore-filter)
527 (skip-chars-backward "\n")
528 (setq str (buffer-substring (point-min) (point))))
529 (unless str
530 (goto-char (point-max))
531 ;; delay final line if it can still be completed to prompt
532 (let ((l (buffer-substring (line-beginning-position) (point))))
533 (when (and (<= (length l) (length ediprolog-prompt))
534 (string= l (substring ediprolog-prompt 0 (length l))))
535 (goto-char (line-beginning-position))))
536 ;; delay emitting newlines until we are sure no prompt
537 ;; follows; one or two newlines can precede a prompt
538 (let ((d (abs (skip-chars-backward "\n"))))
539 (when (> d 2)
540 (forward-char (- d 2))))
541 (setq str (buffer-substring (point-min) (point)))
542 (let (buffer-read-only)
543 (delete-region (point-min) (point))))
544 (when str
545 (with-temp-buffer
546 ;; precede each line with ediprolog prefices
547 (insert str)
548 (goto-char (point-min))
549 (while (search-forward "\n" nil t)
550 (replace-match
551 (format "\n%s%s" (with-current-buffer (process-buffer proc)
552 ediprolog-indent-prefix) ediprolog-prefix)))
553 (setq str (buffer-string)))
554 (with-current-buffer (process-buffer proc)
555 (let ((near (<= (abs (- (point) (process-mark proc))) 1)))
556 (ediprolog-insert-at-marker str)
557 (when near
558 ;; catch up with output if point was reasonably close
559 (goto-char (process-mark proc))))))))))
560
561
562 (defun ediprolog-insert-at-marker (&rest args)
563 "Insert strings ARGS at marker and update the marker."
564 (save-excursion
565 (goto-char (process-mark ediprolog-process))
566 (end-of-line)
567 (apply #'insert args)
568 (set-marker (process-mark ediprolog-process) (point))))
569
570 (defun ediprolog-ignore-filter (proc str)
571 "Log and then ignore all process output."
572 (with-current-buffer (process-buffer proc)
573 (ediprolog-log str "gray")))
574
575 (defun ediprolog-temp-buffer (proc)
576 (with-current-buffer (process-buffer proc)
577 ;; temp buffer can be buffer local
578 ediprolog-temp-buffer))
579
580 (defun ediprolog-map-variables (func)
581 "Call FUNC with all ediprolog variables that can become buffer-local."
582 (mapc func '(ediprolog-process
583 ediprolog-program
584 ediprolog-program-switches
585 ediprolog-temp-buffer
586 ediprolog-history-buffer
587 ediprolog-seen-prompt
588 ediprolog-interrupted
589 ediprolog-read-term
590 ediprolog-indent-prefix
591 ediprolog-temp-file)))
592
593 ;;;###autoload
594 (defun ediprolog-localize ()
595 "After `ediprolog-localize', any Prolog process started from
596 this buffer becomes buffer-local."
597 (interactive)
598 (unless (local-variable-p 'ediprolog-process)
599 (ediprolog-map-variables #'make-local-variable)
600 (setq ediprolog-temp-file nil
601 ediprolog-process nil
602 ediprolog-history-buffer nil
603 ediprolog-temp-buffer nil)))
604
605 (defun ediprolog-unlocalize ()
606 "Revert the effect of `ediprolog-localize'."
607 (interactive)
608 (when (local-variable-p 'ediprolog-process)
609 (ediprolog-kill-prolog)
610 (ediprolog-map-variables #'kill-local-variable)))
611
612 (provide 'ediprolog)
613
614 ;;; ediprolog.el ends here