Commit | Line | Data |
---|---|---|
3ab2c837 BG |
1 | ;;; ob-awk.el --- org-babel functions for awk evaluation |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 2011-2013 Free Software Foundation, Inc. |
3ab2c837 BG |
4 | |
5 | ;; Author: Eric Schulte | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
3ab2c837 BG |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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. | |
15 | ||
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. | |
20 | ||
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/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
3ab2c837 | 26 | ;; Babel's awk can use special header argument: |
14e1337f | 27 | ;; |
3ab2c837 | 28 | ;; - :in-file takes a path to a file of data to be processed by awk |
14e1337f | 29 | ;; |
3ab2c837 BG |
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 | |
32 | ||
33 | ;;; Code: | |
34 | (require 'ob) | |
35 | (require 'ob-eval) | |
8223b1d2 | 36 | (require 'org-compat) |
3ab2c837 BG |
37 | (eval-when-compile (require 'cl)) |
38 | ||
39 | (declare-function org-babel-ref-resolve "ob-ref" (ref)) | |
40 | (declare-function orgtbl-to-generic "org-table" (table params)) | |
41 | ||
42 | (defvar org-babel-tangle-lang-exts) | |
43 | (add-to-list 'org-babel-tangle-lang-exts '("awk" . "awk")) | |
44 | ||
45 | (defvar org-babel-awk-command "awk" | |
46 | "Name of the awk executable command.") | |
47 | ||
48 | (defun org-babel-expand-body:awk (body params &optional processed-params) | |
49 | "Expand BODY according to PARAMS, return the expanded body." | |
50 | (dolist (pair (mapcar #'cdr (org-babel-get-header params :var))) | |
51 | (setf body (replace-regexp-in-string | |
e66ba1df | 52 | (regexp-quote (format "$%s" (car pair))) (cdr pair) body))) |
3ab2c837 BG |
53 | body) |
54 | ||
55 | (defun org-babel-execute:awk (body params) | |
56 | "Execute a block of Awk code with org-babel. This function is | |
57 | called by `org-babel-execute-src-block'" | |
58 | (message "executing Awk source code block") | |
59 | (let* ((result-params (cdr (assoc :result-params params))) | |
60 | (cmd-line (cdr (assoc :cmd-line params))) | |
61 | (in-file (cdr (assoc :in-file params))) | |
62 | (full-body (org-babel-expand-body:awk body params)) | |
63 | (code-file ((lambda (file) (with-temp-file file (insert full-body)) file) | |
64 | (org-babel-temp-file "awk-"))) | |
65 | (stdin ((lambda (stdin) | |
66 | (when stdin | |
67 | (let ((tmp (org-babel-temp-file "awk-stdin-")) | |
68 | (res (org-babel-ref-resolve stdin))) | |
69 | (with-temp-file tmp | |
70 | (insert (org-babel-awk-var-to-awk res))) | |
71 | tmp))) | |
72 | (cdr (assoc :stdin params)))) | |
73 | (cmd (mapconcat #'identity (remove nil (list org-babel-awk-command | |
74 | "-f" code-file | |
75 | cmd-line | |
76 | in-file)) | |
77 | " "))) | |
78 | (org-babel-reassemble-table | |
79 | ((lambda (results) | |
80 | (when results | |
81 | (if (or (member "scalar" result-params) | |
82 | (member "verbatim" result-params) | |
83 | (member "output" result-params)) | |
84 | results | |
85 | (let ((tmp (org-babel-temp-file "awk-results-"))) | |
86 | (with-temp-file tmp (insert results)) | |
87 | (org-babel-import-elisp-from-file tmp))))) | |
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 | (org-babel-pick-name | |
94 | (cdr (assoc :colname-names params)) (cdr (assoc :colnames params))) | |
95 | (org-babel-pick-name | |
96 | (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))) | |
97 | ||
98 | (defun org-babel-awk-var-to-awk (var &optional sep) | |
99 | "Return a printed value of VAR suitable for parsing with awk." | |
8223b1d2 | 100 | (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v))))) |
3ab2c837 BG |
101 | (cond |
102 | ((and (listp var) (listp (car var))) | |
8223b1d2 | 103 | (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var))) |
3ab2c837 | 104 | ((listp var) |
8223b1d2 BG |
105 | (mapconcat echo-var var "\n")) |
106 | (t (funcall echo-var var))))) | |
3ab2c837 BG |
107 | |
108 | (defun org-babel-awk-table-or-string (results) | |
109 | "If the results look like a table, then convert them into an | |
110 | Emacs-lisp table, otherwise return the results as a string." | |
111 | (org-babel-script-escape results)) | |
112 | ||
113 | (provide 'ob-awk) | |
114 | ||
5b409b39 | 115 | |
3ab2c837 BG |
116 | |
117 | ;;; ob-awk.el ends here |