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 (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
)))
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)
70 (let ((tmp (org-babel-temp-file "awk-stdin-"))
71 (res (org-babel-ref-resolve stdin
)))
73 (insert (org-babel-awk-var-to-awk res
)))
75 (cdr (assoc :stdin params
))))
76 (cmd (mapconcat #'identity
(remove nil
(list org-babel-awk-command
81 (org-babel-reassemble-table
84 (if (or (member "scalar" result-params
)
85 (member "verbatim" result-params
)
86 (member "output" result-params
))
88 (let ((tmp (org-babel-temp-file "awk-results-")))
89 (with-temp-file tmp
(insert results
))
90 (org-babel-import-elisp-from-file tmp
)))))
92 (in-file (org-babel-eval cmd
""))
93 (stdin (with-temp-buffer
94 (call-process-shell-command cmd stdin
(current-buffer))
96 (t (error "ob-awk: must specify either :in-file or :stdin"))))
98 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
100 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
))))))
102 (defun org-babel-awk-var-to-awk (var &optional sep
)
103 "Return a printed value of VAR suitable for parsing with awk."
104 (flet ((echo-var (v) (if (stringp v
) v
(format "%S" v
))))
106 ((and (listp var
) (listp (car var
)))
107 (orgtbl-to-generic var
(list :sep
(or sep
"\t") :fmt
#'echo-var
)))
109 (mapconcat #'echo-var var
"\n"))
110 (t (echo-var var
)))))
112 (defun org-babel-awk-table-or-string (results)
113 "If the results look like a table, then convert them into an
114 Emacs-lisp table, otherwise return the results as a string."
115 (org-babel-script-escape results
))
119 ;; arch-tag: 844e2c88-6aad-4018-868d-a2df6bcdf68f
121 ;;; ob-awk.el ends here