1 ;;; ob-sed.el --- Babel Functions for Sed Scripts -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
5 ;; Author: Bjarte Johansen
6 ;; Keywords: literate programming, reproducible research
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Provides a way to evaluate sed scripts in Org mode.
30 ;; Add to your Emacs config:
32 ;; (org-babel-do-load-languages
33 ;; 'org-babel-load-languages
36 ;; In addition to the normal header arguments, ob-sed also provides
37 ;; :cmd-line and :in-file. :cmd-line allows one to pass other flags to
38 ;; the sed command like the "--in-place" flag which makes sed edit the
39 ;; file pass to it instead of outputting to standard out or to a
40 ;; different file. :in-file is a header arguments that allows one to
41 ;; tell Org Babel which file the sed script to act on.
46 (defvar org-babel-sed-command
"sed"
47 "Name of the sed executable command.")
49 (defvar org-babel-tangle-lang-exts
)
50 (add-to-list 'org-babel-tangle-lang-exts
'("sed" .
"sed"))
52 (defconst org-babel-header-args
:sed
55 "Sed specific header arguments.")
57 (defvar org-babel-default-header-args
:sed
'()
58 "Default arguments for evaluating a sed source block.")
60 (defun org-babel-execute:sed
(body params
)
61 "Execute a block of sed code with Org Babel.
62 BODY is the source inside a sed source block and PARAMS is an
63 association list over the source block configurations. This
64 function is called by `org-babel-execute-src-block'."
65 (message "executing sed source code block")
66 (let* ((result-params (cdr (assq :result-params params
)))
67 (cmd-line (cdr (assq :cmd-line params
)))
68 (in-file (cdr (assq :in-file params
)))
69 (code-file (let ((file (org-babel-temp-file "sed-")))
72 (stdin (let ((stdin (cdr (assq :stdin params
))))
74 (let ((tmp (org-babel-temp-file "sed-stdin-"))
75 (res (org-babel-ref-resolve stdin
)))
79 (cmd (mapconcat #'identity
81 (list org-babel-sed-command
82 (format "--file=\"%s\"" code-file
)
86 (org-babel-reassemble-table
89 (stdin (with-temp-buffer
90 (call-process-shell-command cmd stdin
(current-buffer))
92 (t (org-babel-eval cmd
"")))))
94 (org-babel-result-cond result-params
96 (let ((tmp (org-babel-temp-file "sed-results-")))
97 (with-temp-file tmp
(insert results
))
98 (org-babel-import-elisp-from-file tmp
)))))
100 (cdr (assq :colname-names params
)) (cdr (assq :colnames params
)))
102 (cdr (assq :rowname-names params
)) (cdr (assq :rownames params
))))))
105 ;;; ob-sed.el ends here