org-babel-haskell.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ;;; org-babel-haskell.el --- org-babel functions for haskell evaluation
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating haskell source code. This one will
  24. ;; be sort of tricky because haskell programs must be compiled before
  25. ;; they can be run, but haskell code can also be run through an
  26. ;; interactive interpreter.
  27. ;;
  28. ;; For now lets only allow evaluation using the haskell interpreter.
  29. ;;; Requirements:
  30. ;; - haskell-mode :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
  31. ;;
  32. ;; - inf-haskell :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
  33. ;;; Code:
  34. (require 'org-babel)
  35. (require 'haskell-mode)
  36. (require 'inf-haskell)
  37. (org-babel-add-interpreter "haskell")
  38. (add-to-list 'org-babel-tangle-langs '("haskell" "hs"))
  39. (defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"")
  40. (defun org-babel-execute:haskell (body params)
  41. "Execute a block of Haskell code with org-babel. This function
  42. is called by `org-babel-execute-src-block' with the following
  43. variables pre-set using `multiple-value-bind'.
  44. (session vars result-params result-type)"
  45. (message "executing haskell source code block")
  46. (let* ((full-body (concat
  47. (mapconcat
  48. (lambda (pair) (format "let %s = %s;" (car pair) (cdr pair)))
  49. vars "\n") "\n" body "\n"))
  50. (session (org-babel-prep-session:haskell session params))
  51. (raw (org-babel-comint-with-output session org-babel-haskell-eoe t
  52. (insert (org-babel-trim full-body))
  53. (comint-send-input nil t)
  54. (insert org-babel-haskell-eoe)
  55. (comint-send-input nil t)))
  56. (results (mapcar
  57. #'org-babel-haskell-read-string
  58. (cdr (member org-babel-haskell-eoe
  59. (reverse (mapcar #'org-babel-trim raw)))))))
  60. (case result-type
  61. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  62. (value (org-babel-haskell-table-or-string (car results))))))
  63. (defun org-babel-haskell-read-string (string)
  64. "Strip \\\"s from around haskell string"
  65. (if (string-match "\"\\([^\000]+\\)\"" string)
  66. (match-string 1 string)
  67. string))
  68. (defun org-babel-prep-session:haskell (session params)
  69. "Prepare SESSION according to the header arguments specified in PARAMS."
  70. (save-window-excursion (run-haskell) (current-buffer)))
  71. (defun org-babel-haskell-table-or-string (results)
  72. "If the results look like a table, then convert them into an
  73. Emacs-lisp table, otherwise return the results as a string."
  74. (org-babel-read
  75. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  76. (org-babel-read
  77. (replace-regexp-in-string
  78. "\\[" "(" (replace-regexp-in-string
  79. "\\]" ")" (replace-regexp-in-string
  80. "," " " (replace-regexp-in-string
  81. "'" "\"" results)))))
  82. results)))
  83. (provide 'org-babel-haskell)
  84. ;;; org-babel-haskell.el ends here