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