Merge branch 'maint'
[org-mode.git] / lisp / ob-ebnf.el
blob410570bc5d6f98212ce054df52e9074feda2f337
1 ;;; ob-ebnf.el --- Babel Functions for EBNF -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2013-2017 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 (assq :file params)))
58 (dest-dir (file-name-directory dest-file))
59 (dest-root (file-name-sans-extension
60 (file-name-nondirectory dest-file)))
61 (style (cdr (assq :style params)))
62 (result nil))
63 (with-temp-buffer
64 (when style (ebnf-push-style style))
65 (let ((comment-format
66 (cond ((string= ebnf-syntax 'yacc) "/*%s*/")
67 ((string= ebnf-syntax 'ebnf) ";%s")
68 ((string= ebnf-syntax 'iso-ebnf) "(*%s*)")
69 (t (setq result
70 (format "EBNF error: format %s not supported."
71 ebnf-syntax))))))
72 (setq ebnf-eps-prefix dest-dir)
73 (insert (format comment-format (format "[%s" dest-root)))
74 (newline)
75 (insert body)
76 (newline)
77 (insert (format comment-format (format "]%s" dest-root)))
78 (ebnf-eps-buffer)
79 (when style (ebnf-pop-style))))
80 result)))
82 (provide 'ob-ebnf)
83 ;;; ob-ebnf.el ends here