ob-coq.el 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; ob-coq.el --- Babel Functions for Coq -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: https://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Rudimentary support for evaluating Coq code blocks. Currently only
  19. ;; session evaluation is supported. Requires both coq.el and
  20. ;; coq-inferior.el, both of which are distributed with Coq.
  21. ;;
  22. ;; https://coq.inria.fr/
  23. ;;; Code:
  24. (require 'ob)
  25. (declare-function run-coq "ext:coq-inferior.el" (cmd))
  26. (declare-function coq-proc "ext:coq-inferior.el" ())
  27. (defvar coq-program-name "coqtop"
  28. "Name of the coq toplevel to run.")
  29. (defvar org-babel-coq-buffer "*coq*"
  30. "Buffer in which to evaluate coq code blocks.")
  31. (defun org-babel-coq-clean-prompt (string)
  32. (if (string-match "^[^[:space:]]+ < " string)
  33. (substring string 0 (match-beginning 0))
  34. string))
  35. (defun org-babel-execute:coq (body params)
  36. (let ((full-body (org-babel-expand-body:generic body params))
  37. (session (org-babel-coq-initiate-session))
  38. (pt (lambda ()
  39. (marker-position
  40. (process-mark (get-buffer-process (current-buffer)))))))
  41. (org-babel-coq-clean-prompt
  42. (org-babel-comint-in-buffer session
  43. (let ((start (funcall pt)))
  44. (with-temp-buffer
  45. (insert full-body)
  46. (comint-send-region (coq-proc) (point-min) (point-max))
  47. (comint-send-string (coq-proc)
  48. (if (string= (buffer-substring (- (point-max) 1) (point-max)) ".")
  49. "\n"
  50. ".\n")))
  51. (while (equal start (funcall pt)) (sleep-for 0.1))
  52. (buffer-substring start (funcall pt)))))))
  53. (defun org-babel-coq-initiate-session ()
  54. "Initiate a coq session.
  55. If there is not a current inferior-process-buffer in SESSION then
  56. create one. Return the initialized session."
  57. (unless (fboundp 'run-coq)
  58. (error "`run-coq' not defined, load coq-inferior.el"))
  59. (save-window-excursion (run-coq coq-program-name))
  60. (sit-for 0.1)
  61. (get-buffer org-babel-coq-buffer))
  62. (provide 'ob-coq)
  63. ;;; ob-coq.el ends here