ob-coq.el 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;; ob-coq.el --- Babel Functions for Coq -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2018 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. ;; http://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)