Release 7.7
[org-mode.git] / lisp / ob-awk.el
blob368957c2f9b08e059f12afbc06733c4353938607
1 ;;; ob-awk.el --- org-babel functions for awk evaluation
3 ;; Copyright (C) 2011 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.7
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Commentary:
29 ;; Babel's awk can use special header argument:
30 ;;
31 ;; - :in-file takes a path to a file of data to be processed by awk
32 ;;
33 ;; - :stdin takes an Org-mode data or code block reference, the value
34 ;; of which will be passed to the awk process through STDIN
36 ;;; Code:
37 (require 'ob)
38 (require 'ob-eval)
39 (eval-when-compile (require 'cl))
41 (declare-function org-babel-ref-resolve "ob-ref" (ref))
42 (declare-function orgtbl-to-generic "org-table" (table params))
44 (defvar org-babel-tangle-lang-exts)
45 (add-to-list 'org-babel-tangle-lang-exts '("awk" . "awk"))
47 (defvar org-babel-awk-command "awk"
48 "Name of the awk executable command.")
50 (defun org-babel-expand-body:awk (body params &optional processed-params)
51 "Expand BODY according to PARAMS, return the expanded body."
52 (dolist (pair (mapcar #'cdr (org-babel-get-header params :var)))
53 (setf body (replace-regexp-in-string
54 (regexp-quote (concat "$" (car pair))) (cdr pair) body)))
55 body)
57 (defun org-babel-execute:awk (body params)
58 "Execute a block of Awk code with org-babel. This function is
59 called by `org-babel-execute-src-block'"
60 (message "executing Awk source code block")
61 (let* ((result-params (cdr (assoc :result-params params)))
62 (cmd-line (cdr (assoc :cmd-line params)))
63 (in-file (cdr (assoc :in-file params)))
64 (full-body (org-babel-expand-body:awk body params))
65 (code-file ((lambda (file) (with-temp-file file (insert full-body)) file)
66 (org-babel-temp-file "awk-")))
67 (stdin ((lambda (stdin)
68 (when stdin
69 (let ((tmp (org-babel-temp-file "awk-stdin-"))
70 (res (org-babel-ref-resolve stdin)))
71 (with-temp-file tmp
72 (insert (org-babel-awk-var-to-awk res)))
73 tmp)))
74 (cdr (assoc :stdin params))))
75 (cmd (mapconcat #'identity (remove nil (list org-babel-awk-command
76 "-f" code-file
77 cmd-line
78 in-file))
79 " ")))
80 (org-babel-reassemble-table
81 ((lambda (results)
82 (when results
83 (if (or (member "scalar" result-params)
84 (member "verbatim" result-params)
85 (member "output" result-params))
86 results
87 (let ((tmp (org-babel-temp-file "awk-results-")))
88 (with-temp-file tmp (insert results))
89 (org-babel-import-elisp-from-file tmp)))))
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 (org-babel-pick-name
96 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
97 (org-babel-pick-name
98 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
100 (defun org-babel-awk-var-to-awk (var &optional sep)
101 "Return a printed value of VAR suitable for parsing with awk."
102 (flet ((echo-var (v) (if (stringp v) v (format "%S" v))))
103 (cond
104 ((and (listp var) (listp (car var)))
105 (orgtbl-to-generic var (list :sep (or sep "\t") :fmt #'echo-var)))
106 ((listp var)
107 (mapconcat #'echo-var var "\n"))
108 (t (echo-var var)))))
110 (defun org-babel-awk-table-or-string (results)
111 "If the results look like a table, then convert them into an
112 Emacs-lisp table, otherwise return the results as a string."
113 (org-babel-script-escape results))
115 (provide 'ob-awk)
117 ;; arch-tag: 844e2c88-6aad-4018-868d-a2df6bcdf68f
119 ;;; ob-awk.el ends here