ob-coq.el 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;; ob-coq.el --- Babel Functions for Coq -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://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 <http://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 org-babel-coq-buffer "*coq*"
  28. "Buffer in which to evaluate coq code blocks.")
  29. (defvar org-babel-coq-eoe "org-babel-coq-eoe")
  30. (defun org-babel-coq-clean-prompt (string)
  31. (if (string-match "^[^[:space:]]+ < " string)
  32. (substring string 0 (match-beginning 0))
  33. string))
  34. (defun org-babel-execute:coq (body params)
  35. (let ((full-body (org-babel-expand-body:generic body params))
  36. (session (org-babel-coq-initiate-session))
  37. (pt (lambda ()
  38. (marker-position
  39. (process-mark (get-buffer-process (current-buffer)))))))
  40. (org-babel-coq-clean-prompt
  41. (org-babel-comint-in-buffer session
  42. (let ((start (funcall pt)))
  43. (with-temp-buffer
  44. (insert full-body)
  45. (comint-send-region (coq-proc) (point-min) (point-max))
  46. (comint-send-string (coq-proc)
  47. (if (string= (buffer-substring (- (point-max) 1) (point-max)) ".")
  48. "\n"
  49. ".\n")))
  50. (while (equal start (funcall pt)) (sleep-for 0.1))
  51. (buffer-substring start (funcall pt)))))))
  52. (defun org-babel-coq-initiate-session ()
  53. "Initiate a coq session.
  54. If there is not a current inferior-process-buffer in SESSION then
  55. create one. Return the initialized session."
  56. (unless (fboundp 'run-coq)
  57. (error "`run-coq' not defined, load coq-inferior.el"))
  58. (save-window-excursion (run-coq "coqtop"))
  59. (sit-for 0.1)
  60. (get-buffer org-babel-coq-buffer))
  61. (provide 'ob-coq)