ob-awk.el 4.2 KB

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