ox-html: Fix center block class
[org-mode.git] / lisp / ob-sed.el
blob9e3db3725ee94248676a94403a5cc6babaecce90
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
7 ;; Version: 0.1.0
9 ;; This file is part of GNU Emacs.
11 ;;; License:
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)
16 ;; any later version.
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/>.
26 ;;; Commentary:
28 ;; Provides a way to evaluate sed scripts in Org mode.
30 ;;; Usage:
32 ;; Add to your Emacs config:
34 ;; (org-babel-do-load-languages
35 ;; 'org-babel-load-languages
36 ;; '((sed . t)))
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.
45 ;;; Code:
46 (require 'ob)
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
55 '((:cmd-line . :any)
56 (:in-file . :any))
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-")))
72 (with-temp-file file
73 (insert body)) file))
74 (stdin (let ((stdin (cdr (assq :stdin params))))
75 (when stdin
76 (let ((tmp (org-babel-temp-file "sed-stdin-"))
77 (res (org-babel-ref-resolve stdin)))
78 (with-temp-file tmp
79 (insert res))
80 tmp))))
81 (cmd (mapconcat #'identity
82 (remq nil
83 (list org-babel-sed-command
84 (format "--file=\"%s\"" code-file)
85 cmd-line
86 in-file))
87 " ")))
88 (org-babel-reassemble-table
89 (let ((results
90 (cond
91 (stdin (with-temp-buffer
92 (call-process-shell-command cmd stdin (current-buffer))
93 (buffer-string)))
94 (t (org-babel-eval cmd "")))))
95 (when results
96 (org-babel-result-cond result-params
97 results
98 (let ((tmp (org-babel-temp-file "sed-results-")))
99 (with-temp-file tmp (insert results))
100 (org-babel-import-elisp-from-file tmp)))))
101 (org-babel-pick-name
102 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
103 (org-babel-pick-name
104 (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
106 (provide 'ob-sed)
107 ;;; ob-sed.el ends here