ob-awk.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ;;; ob-awk.el --- org-babel functions for awk evaluation
  2. ;; Copyright (C) 2011-2012 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 'ob-eval)
  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 &optional processed-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 ((lambda (file) (with-temp-file file (insert full-body)) file)
  49. (org-babel-temp-file "awk-")))
  50. (stdin ((lambda (stdin)
  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. (cdr (assoc :stdin params))))
  58. (cmd (mapconcat #'identity (remove nil (list org-babel-awk-command
  59. "-f" code-file
  60. cmd-line
  61. in-file))
  62. " ")))
  63. (org-babel-reassemble-table
  64. ((lambda (results)
  65. (when results
  66. (if (or (member "scalar" result-params)
  67. (member "verbatim" result-params)
  68. (member "output" result-params))
  69. results
  70. (let ((tmp (org-babel-temp-file "awk-results-")))
  71. (with-temp-file tmp (insert results))
  72. (org-babel-import-elisp-from-file tmp)))))
  73. (cond
  74. (stdin (with-temp-buffer
  75. (call-process-shell-command cmd stdin (current-buffer))
  76. (buffer-string)))
  77. (t (org-babel-eval cmd ""))))
  78. (org-babel-pick-name
  79. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  80. (org-babel-pick-name
  81. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  82. (defun org-babel-awk-var-to-awk (var &optional sep)
  83. "Return a printed value of VAR suitable for parsing with awk."
  84. (flet ((echo-var (v) (if (stringp v) v (format "%S" v))))
  85. (cond
  86. ((and (listp var) (listp (car var)))
  87. (orgtbl-to-generic var (list :sep (or sep "\t") :fmt #'echo-var)))
  88. ((listp var)
  89. (mapconcat #'echo-var var "\n"))
  90. (t (echo-var var)))))
  91. (defun org-babel-awk-table-or-string (results)
  92. "If the results look like a table, then convert them into an
  93. Emacs-lisp table, otherwise return the results as a string."
  94. (org-babel-script-escape results))
  95. (provide 'ob-awk)
  96. ;;; ob-awk.el ends here