ob-coq.el 2.7 KB

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