ob-awk.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. (require 'org-compat)
  28. (eval-when-compile (require 'cl))
  29. (declare-function org-babel-ref-resolve "ob-ref" (ref))
  30. (declare-function orgtbl-to-generic "org-table" (table params))
  31. (defvar org-babel-tangle-lang-exts)
  32. (add-to-list 'org-babel-tangle-lang-exts '("awk" . "awk"))
  33. (defvar org-babel-awk-command "awk"
  34. "Name of the awk executable command.")
  35. (defun org-babel-expand-body:awk (body params &optional processed-params)
  36. "Expand BODY according to PARAMS, return the expanded body."
  37. (dolist (pair (mapcar #'cdr (org-babel-get-header params :var)))
  38. (setf body (replace-regexp-in-string
  39. (regexp-quote (format "$%s" (car pair))) (cdr pair) body)))
  40. body)
  41. (defun org-babel-execute:awk (body params)
  42. "Execute a block of Awk code with org-babel. This function is
  43. called by `org-babel-execute-src-block'"
  44. (message "executing Awk source code block")
  45. (let* ((result-params (cdr (assoc :result-params params)))
  46. (cmd-line (cdr (assoc :cmd-line params)))
  47. (in-file (cdr (assoc :in-file params)))
  48. (full-body (org-babel-expand-body:awk body params))
  49. (code-file ((lambda (file) (with-temp-file file (insert full-body)) file)
  50. (org-babel-temp-file "awk-")))
  51. (stdin ((lambda (stdin)
  52. (when stdin
  53. (let ((tmp (org-babel-temp-file "awk-stdin-"))
  54. (res (org-babel-ref-resolve stdin)))
  55. (with-temp-file tmp
  56. (insert (org-babel-awk-var-to-awk res)))
  57. tmp)))
  58. (cdr (assoc :stdin params))))
  59. (cmd (mapconcat #'identity (remove nil (list org-babel-awk-command
  60. "-f" code-file
  61. cmd-line
  62. in-file))
  63. " ")))
  64. (org-babel-reassemble-table
  65. ((lambda (results)
  66. (when results
  67. (org-babel-result-cond result-params
  68. results
  69. (let ((tmp (org-babel-temp-file "awk-results-")))
  70. (with-temp-file tmp (insert results))
  71. (org-babel-import-elisp-from-file tmp)))))
  72. (cond
  73. (stdin (with-temp-buffer
  74. (call-process-shell-command cmd stdin (current-buffer))
  75. (buffer-string)))
  76. (t (org-babel-eval cmd ""))))
  77. (org-babel-pick-name
  78. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  79. (org-babel-pick-name
  80. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  81. (defun org-babel-awk-var-to-awk (var &optional sep)
  82. "Return a printed value of VAR suitable for parsing with awk."
  83. (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v)))))
  84. (cond
  85. ((and (listp var) (listp (car var)))
  86. (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var)))
  87. ((listp var)
  88. (mapconcat echo-var var "\n"))
  89. (t (funcall echo-var var)))))
  90. (defun org-babel-awk-table-or-string (results)
  91. "If the results look like a table, then convert them into an
  92. Emacs-lisp table, otherwise return the results as a string."
  93. (org-babel-script-escape results))
  94. (provide 'ob-awk)
  95. ;;; ob-awk.el ends here