Backport remaining changes from commit 7e09ef0
[org-mode.git] / lisp / ob-ebnf.el
blob8c98d305d3e8b9e86b00a69b7154c535973f819c
1 ;;; ob-ebnf.el --- org-babel functions for ebnf evaluation
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: Michael Gauland
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 1.00
10 ;;; License:
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;;; Org-Babel support for using ebnf2ps to generate encapsulated postscript
30 ;;; railroad diagrams. It recogises these arguments:
31 ;;;
32 ;;; :file is required; it must include the extension '.eps.' All the rules
33 ;;; in the block will be drawn in the same file. This is done by
34 ;;; inserting a '[<file>' comment at the start of the block (see the
35 ;;; documentation for ebnf-eps-buffer for more information).
36 ;;;
37 ;;; :style specifies a value in ebnf-style-database. This provides the
38 ;;; ability to customise the output. The style can also specify the
39 ;;; grammar syntax (by setting ebnf-syntax); note that only ebnf,
40 ;;; iso-ebnf, and yacc are supported by this file.
42 ;;; Requirements:
44 ;;; Code:
45 (require 'ob)
46 (require 'ebnf2ps)
48 ;; optionally declare default header arguments for this language
49 (defvar org-babel-default-header-args:ebnf '((:style . nil)))
51 ;; Use ebnf-eps-buffer to produce an encapsulated postscript file.
53 (defun org-babel-execute:ebnf (body params)
54 "Execute a block of Ebnf code with org-babel. This function is
55 called by `org-babel-execute-src-block'"
56 (save-excursion
57 (let* ((dest-file (cdr (assoc :file params)))
58 (dest-dir (file-name-directory dest-file))
59 (dest-root (file-name-sans-extension
60 (file-name-nondirectory dest-file)))
61 (dest-ext (file-name-extension dest-file))
62 (style (cdr (assoc :style params)))
63 (current-dir default-directory)
64 (result nil))
65 (with-temp-buffer
66 (when style (ebnf-push-style style))
67 (let ((comment-format
68 (cond ((string= ebnf-syntax 'yacc) "/*%s*/")
69 ((string= ebnf-syntax 'ebnf) ";%s")
70 ((string= ebnf-syntax 'iso-ebnf) "(*%s*)")
71 (t (setq result
72 (format "EBNF error: format %s not supported."
73 ebnf-syntax))))))
74 (setq ebnf-eps-prefix dest-dir)
75 (insert (format comment-format (format "[%s" dest-root)))
76 (newline)
77 (insert body)
78 (newline)
79 (insert (format comment-format (format "]%s" dest-root)))
80 (ebnf-eps-buffer)
81 (when style (ebnf-pop-style))))
82 result)))
84 (provide 'ob-ebnf)
85 ;;; ob-ebnf.el ends here