ob-ebnf.el 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ;;; ob-ebnf.el --- org-babel functions for ebnf evaluation
  2. ;; Copyright (C) your name here
  3. ;; Author: Michael Gauland
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 1.00
  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 using ebnf2ps to generate encapsulated postscript
  24. ;;; railroad diagrams. It recogises these arguments:
  25. ;;;
  26. ;;; :file is required; it must include the extension '.eps.' All the rules
  27. ;;; in the block will be drawn in the same file. This is done by
  28. ;;; inserting a '[<file>' comment at the start of the block (see the
  29. ;;; documentation for ebnf-eps-buffer for more information).
  30. ;;;
  31. ;;; :style specifies a value in ebnf-style-database. This provides the
  32. ;;; ability to customise the output. The style can also specify the
  33. ;;; gramnmar syntax (by setting ebnf-syntax); note that only ebnf,
  34. ;;; iso-ebnf, and yacc are supported by this file.
  35. ;;; Requirements:
  36. ;;; Code:
  37. (require 'ob)
  38. (require 'ebnf2ps)
  39. ;; optionally declare default header arguments for this language
  40. (defvar org-babel-default-header-args:ebnf '((:style . nil)))
  41. ;; Use ebnf-eps-buffer to produce an encapsulated postscript file.
  42. ;;
  43. (defun org-babel-execute:ebnf (body params)
  44. "Execute a block of Ebnf code with org-babel. This function is
  45. called by `org-babel-execute-src-block'"
  46. (save-excursion
  47. (let* ((dest-file (cdr (assoc :file params)))
  48. (dest-dir (file-name-directory dest-file))
  49. (dest-root (file-name-sans-extension
  50. (file-name-nondirectory dest-file)))
  51. (dest-ext (file-name-extension dest-file))
  52. (style (cdr (assoc :style params)))
  53. (current-dir default-directory)
  54. (result nil))
  55. (with-temp-buffer
  56. (when style (ebnf-push-style style))
  57. (let
  58. ((comment-format
  59. (cond ((string= ebnf-syntax 'yacc) "/*%s*/")
  60. ((string= ebnf-syntax 'ebnf) ";%s")
  61. ((string= ebnf-syntax 'iso-ebnf) "(*%s*)")
  62. (t (setq result
  63. (format "EBNF error: format %s not supported."
  64. ebnf-syntax))))))
  65. (setq ebnf-eps-prefix dest-dir)
  66. (insert (format comment-format (format "[%s" dest-root)))
  67. (newline)
  68. (insert body)
  69. (newline)
  70. (insert (format comment-format (format "]%s" dest-root)))
  71. (ebnf-eps-buffer)
  72. (when style (ebnf-pop-style))))
  73. result
  74. )))
  75. (provide 'ob-ebnf)
  76. ;;; ob-ebnf.el ends here