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
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/>.
29 ;; Babel's awk support relies on two special header argument one of
30 ;; which is required to pass data to the awk process.
32 ;; - :in-file takes a path to a file of data to be processed by awk
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
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 (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
)))
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)
69 (let ((tmp (org-babel-temp-file "awk-stdin-"))
70 (res (org-babel-ref-resolve stdin
)))
72 (insert (org-babel-awk-var-to-awk res
)))
74 (cdr (assoc :stdin params
))))
75 (cmd (mapconcat #'identity
(remove nil
(list org-babel-awk-command
80 (org-babel-reassemble-table
83 (if (or (member "scalar" result-params
)
84 (member "output" result-params
))
86 (let ((tmp (org-babel-temp-file "awk-results-")))
87 (with-temp-file tmp
(insert results
))
88 (org-babel-import-elisp-from-file tmp
)))))
90 (in-file (org-babel-eval cmd
""))
91 (stdin (with-temp-buffer
92 (call-process-shell-command cmd stdin
(current-buffer))
94 (t (error "ob-awk: must specify either :in-file or :stdin"))))
96 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
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
))))
104 ((and (listp var
) (listp (car var
)))
105 (orgtbl-to-generic var
(list :sep
(or sep
"\t") :fmt
#'echo-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
))
117 ;; arch-tag: 844e2c88-6aad-4018-868d-a2df6bcdf68f
119 ;;; ob-awk.el ends here