org-indent.el: tiny docstring fix.
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-awk.el
blob3c2c23e264ea11a02be4b4e5073cb7d0973090ee
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.5
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 support relies on two special header argument one of
30 ;; which is required to pass data to the awk process.
31 ;;
32 ;; - :in-file takes a path to a file of data to be processed by awk
33 ;;
34 ;; - :stdin takes an Org-mode data or code block reference, the value
35 ;; of which will be passed to the awk process through STDIN
37 ;;; Code:
38 (require 'ob)
39 (require 'ob-eval)
40 (eval-when-compile (require 'cl))
42 (declare-function org-babel-ref-resolve "ob-ref" (ref))
43 (declare-function orgtbl-to-generic "org-table" (table params))
45 (defvar org-babel-tangle-lang-exts)
46 (add-to-list 'org-babel-tangle-lang-exts '("awk" . "awk"))
48 (defvar org-babel-awk-command "awk"
49 "Name of the awk executable command.")
51 (defun org-babel-expand-body:awk (body params &optional processed-params)
52 "Expand BODY according to PARAMS, return the expanded body."
53 (dolist (pair (mapcar #'cdr (org-babel-get-header params :var)))
54 (setf body (replace-regexp-in-string
55 (regexp-quote (concat "$" (car pair))) (cdr pair) body)))
56 body)
58 (defun org-babel-execute:awk (body params)
59 "Execute a block of Awk code with org-babel. This function is
60 called by `org-babel-execute-src-block'"
61 (message "executing Awk source code block")
62 (let* ((result-params (cdr (assoc :result-params params)))
63 (cmd-line (cdr (assoc :cmd-line params)))
64 (in-file (cdr (assoc :in-file params)))
65 (full-body (org-babel-expand-body:awk body params))
66 (code-file ((lambda (file) (with-temp-file file (insert full-body)) file)
67 (org-babel-temp-file "awk-")))
68 (stdin ((lambda (stdin)
69 (when stdin
70 (let ((tmp (org-babel-temp-file "awk-stdin-"))
71 (res (org-babel-ref-resolve stdin)))
72 (with-temp-file tmp
73 (insert (org-babel-awk-var-to-awk res)))
74 tmp)))
75 (cdr (assoc :stdin params))))
76 (cmd (mapconcat #'identity (remove nil (list org-babel-awk-command
77 "-f" code-file
78 cmd-line
79 in-file))
80 " ")))
81 (org-babel-reassemble-table
82 ((lambda (results)
83 (when results
84 (if (or (member "scalar" 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 (in-file (org-babel-eval cmd ""))
92 (stdin (with-temp-buffer
93 (call-process-shell-command cmd stdin (current-buffer))
94 (buffer-string)))
95 (t (error "ob-awk: must specify either :in-file or :stdin"))))
96 (org-babel-pick-name
97 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
98 (org-babel-pick-name
99 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
101 (defun org-babel-awk-var-to-awk (var &optional sep)
102 "Return a printed value of VAR suitable for parsing with awk."
103 (flet ((echo-var (v) (if (stringp v) v (format "%S" v))))
104 (cond
105 ((and (listp var) (listp (car var)))
106 (orgtbl-to-generic var (list :sep (or sep "\t") :fmt #'echo-var)))
107 ((listp var)
108 (mapconcat #'echo-var var "\n"))
109 (t (echo-var var)))))
111 (defun org-babel-awk-table-or-string (results)
112 "If the results look like a table, then convert them into an
113 Emacs-lisp table, otherwise return the results as a string."
114 (org-babel-script-escape results))
116 (provide 'ob-awk)
118 ;; arch-tag: 844e2c88-6aad-4018-868d-a2df6bcdf68f
120 ;;; ob-awk.el ends here