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