lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / ob-sed.el
blobbe4cff48a54dbaed260d3c5f870629673781ed3b
1 ;;; ob-sed.el --- Babel Functions for Sed Scripts -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2019 Free Software Foundation, Inc.
5 ;; Author: Bjarte Johansen
6 ;; Keywords: literate programming, reproducible research
7 ;; Version: 0.1.1
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 <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Provides a way to evaluate sed scripts in Org mode.
28 ;;; Usage:
30 ;; Add to your Emacs config:
32 ;; (org-babel-do-load-languages
33 ;; 'org-babel-load-languages
34 ;; '((sed . t)))
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.
43 ;;; Code:
44 (require 'ob)
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
53 '((:cmd-line . :any)
54 (:in-file . :any))
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-")))
70 (with-temp-file file
71 (insert body)) file))
72 (stdin (let ((stdin (cdr (assq :stdin params))))
73 (when stdin
74 (let ((tmp (org-babel-temp-file "sed-stdin-"))
75 (res (org-babel-ref-resolve stdin)))
76 (with-temp-file tmp
77 (insert res))
78 tmp))))
79 (cmd (mapconcat #'identity
80 (remq nil
81 (list org-babel-sed-command
82 (format "-f \"%s\"" code-file)
83 cmd-line
84 in-file))
85 " ")))
86 (org-babel-reassemble-table
87 (let ((results
88 (cond
89 (stdin (with-temp-buffer
90 (call-process-shell-command cmd stdin (current-buffer))
91 (buffer-string)))
92 (t (org-babel-eval cmd "")))))
93 (when results
94 (org-babel-result-cond result-params
95 results
96 (let ((tmp (org-babel-temp-file "sed-results-")))
97 (with-temp-file tmp (insert results))
98 (org-babel-import-elisp-from-file tmp)))))
99 (org-babel-pick-name
100 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
101 (org-babel-pick-name
102 (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
104 (provide 'ob-sed)
105 ;;; ob-sed.el ends here