lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / ob-ebnf.el
blob5ed9319e56f09547e7286f32d66142e3388e754a
1 ;;; ob-ebnf.el --- Babel Functions for EBNF -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2013-2019 Free Software Foundation, Inc.
5 ;; Author: Michael Gauland
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: https://orgmode.org
8 ;; Version: 1.00
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Org-Babel support for using ebnf2ps to generate encapsulated postscript
28 ;;; railroad diagrams. It recognizes these arguments:
29 ;;;
30 ;;; :file is required; it must include the extension '.eps.' All the rules
31 ;;; in the block will be drawn in the same file. This is done by
32 ;;; inserting a '[<file>' comment at the start of the block (see the
33 ;;; documentation for ebnf-eps-buffer for more information).
34 ;;;
35 ;;; :style specifies a value in ebnf-style-database. This provides the
36 ;;; ability to customize the output. The style can also specify the
37 ;;; grammar syntax (by setting ebnf-syntax); note that only ebnf,
38 ;;; iso-ebnf, and yacc are supported by this file.
40 ;;; Requirements:
42 ;;; Code:
43 (require 'ob)
44 (require 'ebnf2ps)
46 ;; optionally declare default header arguments for this language
47 (defvar org-babel-default-header-args:ebnf '((:style . nil)))
49 ;; Use ebnf-eps-buffer to produce an encapsulated postscript file.
51 (defun org-babel-execute:ebnf (body params)
52 "Execute a block of Ebnf code with org-babel. This function is
53 called by `org-babel-execute-src-block'"
54 (save-excursion
55 (let* ((dest-file (cdr (assq :file params)))
56 (dest-dir (file-name-directory dest-file))
57 (dest-root (file-name-sans-extension
58 (file-name-nondirectory dest-file)))
59 (style (cdr (assq :style params)))
60 (result nil))
61 (with-temp-buffer
62 (when style (ebnf-push-style style))
63 (let ((comment-format
64 (cond ((string= ebnf-syntax 'yacc) "/*%s*/")
65 ((string= ebnf-syntax 'ebnf) ";%s")
66 ((string= ebnf-syntax 'iso-ebnf) "(*%s*)")
67 (t (setq result
68 (format "EBNF error: format %s not supported."
69 ebnf-syntax))))))
70 (setq ebnf-eps-prefix dest-dir)
71 (insert (format comment-format (format "[%s" dest-root)))
72 (newline)
73 (insert body)
74 (newline)
75 (insert (format comment-format (format "]%s" dest-root)))
76 (ebnf-eps-buffer)
77 (when style (ebnf-pop-style))))
78 result)))
80 (provide 'ob-ebnf)
81 ;;; ob-ebnf.el ends here