Merge branch 'maint'
[org-mode.git] / lisp / ob-awk.el
blobe2eec9bf7f096da709930c7fc11bc9c59b961e36
1 ;;; ob-awk.el --- Babel Functions for Awk -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2017 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
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 ;; Babel's awk can use special header argument:
28 ;; - :in-file takes a path to a file of data to be processed by awk
30 ;; - :stdin takes an Org data or code block reference, the value of
31 ;; which will be passed to the awk process through STDIN
33 ;;; Code:
34 (require 'ob)
35 (require 'org-compat)
37 (declare-function org-babel-ref-resolve "ob-ref" (ref))
38 (declare-function orgtbl-to-generic "org-table" (table params))
40 (defvar org-babel-tangle-lang-exts)
41 (add-to-list 'org-babel-tangle-lang-exts '("awk" . "awk"))
43 (defvar org-babel-awk-command "awk"
44 "Name of the awk executable command.")
46 (defun org-babel-expand-body:awk (body _params)
47 "Expand BODY according to PARAMS, return the expanded body."
48 body)
50 (defun org-babel-execute:awk (body params)
51 "Execute a block of Awk code with org-babel. This function is
52 called by `org-babel-execute-src-block'"
53 (message "executing Awk source code block")
54 (let* ((result-params (cdr (assq :result-params params)))
55 (cmd-line (cdr (assq :cmd-line params)))
56 (in-file (cdr (assq :in-file params)))
57 (full-body (org-babel-expand-body:awk body params))
58 (code-file (let ((file (org-babel-temp-file "awk-")))
59 (with-temp-file file (insert full-body)) file))
60 (stdin (let ((stdin (cdr (assq :stdin params))))
61 (when stdin
62 (let ((tmp (org-babel-temp-file "awk-stdin-"))
63 (res (org-babel-ref-resolve stdin)))
64 (with-temp-file tmp
65 (insert (org-babel-awk-var-to-awk res)))
66 tmp))))
67 (cmd (mapconcat #'identity
68 (append
69 (list org-babel-awk-command
70 "-f" code-file cmd-line)
71 (mapcar (lambda (pair)
72 (format "-v %s='%s'"
73 (car pair)
74 (org-babel-awk-var-to-awk
75 (cdr pair))))
76 (org-babel--get-vars params))
77 (list in-file))
78 " ")))
79 (org-babel-reassemble-table
80 (let ((results
81 (cond
82 (stdin (with-temp-buffer
83 (call-process-shell-command cmd stdin (current-buffer))
84 (buffer-string)))
85 (t (org-babel-eval cmd "")))))
86 (when results
87 (org-babel-result-cond result-params
88 results
89 (let ((tmp (org-babel-temp-file "awk-results-")))
90 (with-temp-file tmp (insert results))
91 (org-babel-import-elisp-from-file tmp)))))
92 (org-babel-pick-name
93 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
94 (org-babel-pick-name
95 (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
97 (defun org-babel-awk-var-to-awk (var &optional sep)
98 "Return a printed value of VAR suitable for parsing with awk."
99 (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v)))))
100 (cond
101 ((and (listp var) (listp (car var)))
102 (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var)))
103 ((listp var)
104 (mapconcat echo-var var "\n"))
105 (t (funcall echo-var var)))))
107 (provide 'ob-awk)
111 ;;; ob-awk.el ends here