ob-awk.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; ob-awk.el --- org-babel functions for awk evaluation
  2. ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Babel's awk can use special header argument:
  19. ;;
  20. ;; - :in-file takes a path to a file of data to be processed by awk
  21. ;;
  22. ;; - :stdin takes an Org-mode data or code block reference, the value
  23. ;; of which will be passed to the awk process through STDIN
  24. ;;; Code:
  25. (require 'ob)
  26. (require 'org-compat)
  27. (eval-when-compile (require 'cl))
  28. (declare-function org-babel-ref-resolve "ob-ref" (ref))
  29. (declare-function orgtbl-to-generic "org-table" (table params))
  30. (defvar org-babel-tangle-lang-exts)
  31. (add-to-list 'org-babel-tangle-lang-exts '("awk" . "awk"))
  32. (defvar org-babel-awk-command "awk"
  33. "Name of the awk executable command.")
  34. (defun org-babel-expand-body:awk (body params)
  35. "Expand BODY according to PARAMS, return the expanded body."
  36. (dolist (pair (mapcar #'cdr (org-babel-get-header params :var)))
  37. (setf body (replace-regexp-in-string
  38. (regexp-quote (format "$%s" (car pair))) (cdr pair) body)))
  39. body)
  40. (defun org-babel-execute:awk (body params)
  41. "Execute a block of Awk code with org-babel. This function is
  42. called by `org-babel-execute-src-block'"
  43. (message "executing Awk source code block")
  44. (let* ((result-params (cdr (assoc :result-params params)))
  45. (cmd-line (cdr (assoc :cmd-line params)))
  46. (in-file (cdr (assoc :in-file params)))
  47. (full-body (org-babel-expand-body:awk body params))
  48. (code-file (let ((file (org-babel-temp-file "awk-")))
  49. (with-temp-file file (insert full-body)) file))
  50. (stdin (let ((stdin (cdr (assoc :stdin params))))
  51. (when stdin
  52. (let ((tmp (org-babel-temp-file "awk-stdin-"))
  53. (res (org-babel-ref-resolve stdin)))
  54. (with-temp-file tmp
  55. (insert (org-babel-awk-var-to-awk res)))
  56. tmp))))
  57. (cmd (mapconcat #'identity (remove nil (list org-babel-awk-command
  58. "-f" code-file
  59. cmd-line
  60. in-file))
  61. " ")))
  62. (org-babel-reassemble-table
  63. (let ((results
  64. (cond
  65. (stdin (with-temp-buffer
  66. (call-process-shell-command cmd stdin (current-buffer))
  67. (buffer-string)))
  68. (t (org-babel-eval cmd "")))))
  69. (when results
  70. (org-babel-result-cond result-params
  71. results
  72. (let ((tmp (org-babel-temp-file "awk-results-")))
  73. (with-temp-file tmp (insert results))
  74. (org-babel-import-elisp-from-file tmp)))))
  75. (org-babel-pick-name
  76. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  77. (org-babel-pick-name
  78. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  79. (defun org-babel-awk-var-to-awk (var &optional sep)
  80. "Return a printed value of VAR suitable for parsing with awk."
  81. (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v)))))
  82. (cond
  83. ((and (listp var) (listp (car var)))
  84. (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var)))
  85. ((listp var)
  86. (mapconcat echo-var var "\n"))
  87. (t (funcall echo-var var)))))
  88. (defun org-babel-awk-table-or-string (results)
  89. "If the results look like a table, then convert them into an
  90. Emacs-lisp table, otherwise return the results as a string."
  91. (org-babel-script-escape results))
  92. (provide 'ob-awk)
  93. ;;; ob-awk.el ends here