Backport commit 65c8c7c from Emacs
[org-mode.git] / lisp / ob-awk.el
blob3af8081bb93a3d6e2f684f99ab77f76990ee2d2a
1 ;;; ob-awk.el --- org-babel functions for awk evaluation
3 ;; Copyright (C) 2011-2016 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 <http://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-mode data or code block reference, the value
31 ;; of which will be passed to the awk process through STDIN
33 ;;; Code:
34 (require 'ob)
35 (require 'org-compat)
36 (eval-when-compile (require 'cl))
38 (declare-function org-babel-ref-resolve "ob-ref" (ref))
39 (declare-function orgtbl-to-generic "org-table"
40 (table params &optional backend))
42 (defvar org-babel-tangle-lang-exts)
43 (add-to-list 'org-babel-tangle-lang-exts '("awk" . "awk"))
45 (defvar org-babel-awk-command "awk"
46 "Name of the awk executable command.")
48 (defun org-babel-expand-body:awk (body params)
49 "Expand BODY according to PARAMS, return the expanded body."
50 body)
52 (defun org-babel-execute:awk (body params)
53 "Execute a block of Awk code with org-babel. This function is
54 called by `org-babel-execute-src-block'"
55 (message "executing Awk source code block")
56 (let* ((result-params (cdr (assoc :result-params params)))
57 (cmd-line (cdr (assoc :cmd-line params)))
58 (in-file (cdr (assoc :in-file params)))
59 (full-body (org-babel-expand-body:awk body params))
60 (code-file (let ((file (org-babel-temp-file "awk-")))
61 (with-temp-file file (insert full-body)) file))
62 (stdin (let ((stdin (cdr (assoc :stdin params))))
63 (when stdin
64 (let ((tmp (org-babel-temp-file "awk-stdin-"))
65 (res (org-babel-ref-resolve stdin)))
66 (with-temp-file tmp
67 (insert (org-babel-awk-var-to-awk res)))
68 tmp))))
69 (cmd (mapconcat #'identity
70 (append
71 (list org-babel-awk-command
72 "-f" code-file cmd-line)
73 (mapcar (lambda (pair)
74 (format "-v %s='%s'"
75 (cadr pair)
76 (org-babel-awk-var-to-awk
77 (cddr pair))))
78 (org-babel-get-header params :var))
79 (list in-file))
80 " ")))
81 (org-babel-reassemble-table
82 (let ((results
83 (cond
84 (stdin (with-temp-buffer
85 (call-process-shell-command cmd stdin (current-buffer))
86 (buffer-string)))
87 (t (org-babel-eval cmd "")))))
88 (when results
89 (org-babel-result-cond result-params
90 results
91 (let ((tmp (org-babel-temp-file "awk-results-")))
92 (with-temp-file tmp (insert results))
93 (org-babel-import-elisp-from-file tmp)))))
94 (org-babel-pick-name
95 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
96 (org-babel-pick-name
97 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
99 (defun org-babel-awk-var-to-awk (var &optional sep)
100 "Return a printed value of VAR suitable for parsing with awk."
101 (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v)))))
102 (cond
103 ((and (listp var) (listp (car var)))
104 (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var)))
105 ((listp var)
106 (mapconcat echo-var var "\n"))
107 (t (funcall echo-var var)))))
109 (provide 'ob-awk)
113 ;;; ob-awk.el ends here