ob-awk.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ;;; ob-awk.el --- Babel Functions for Awk -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2015 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. body)
  37. (defun org-babel-execute:awk (body params)
  38. "Execute a block of Awk code with org-babel. This function is
  39. called by `org-babel-execute-src-block'"
  40. (message "executing Awk source code block")
  41. (let* ((result-params (cdr (assoc :result-params params)))
  42. (cmd-line (cdr (assoc :cmd-line params)))
  43. (in-file (cdr (assoc :in-file params)))
  44. (full-body (org-babel-expand-body:awk body params))
  45. (code-file (let ((file (org-babel-temp-file "awk-")))
  46. (with-temp-file file (insert full-body)) file))
  47. (stdin (let ((stdin (cdr (assoc :stdin params))))
  48. (when stdin
  49. (let ((tmp (org-babel-temp-file "awk-stdin-"))
  50. (res (org-babel-ref-resolve stdin)))
  51. (with-temp-file tmp
  52. (insert (org-babel-awk-var-to-awk res)))
  53. tmp))))
  54. (cmd (mapconcat #'identity
  55. (append
  56. (list org-babel-awk-command
  57. "-f" code-file cmd-line)
  58. (mapcar (lambda (pair)
  59. (format "-v %s='%s'"
  60. (cadr pair)
  61. (org-babel-awk-var-to-awk
  62. (cddr pair))))
  63. (org-babel-get-header params :var))
  64. (list in-file))
  65. " ")))
  66. (org-babel-reassemble-table
  67. (let ((results
  68. (cond
  69. (stdin (with-temp-buffer
  70. (call-process-shell-command cmd stdin (current-buffer))
  71. (buffer-string)))
  72. (t (org-babel-eval cmd "")))))
  73. (when results
  74. (org-babel-result-cond result-params
  75. results
  76. (let ((tmp (org-babel-temp-file "awk-results-")))
  77. (with-temp-file tmp (insert results))
  78. (org-babel-import-elisp-from-file tmp)))))
  79. (org-babel-pick-name
  80. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  81. (org-babel-pick-name
  82. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  83. (defun org-babel-awk-var-to-awk (var &optional sep)
  84. "Return a printed value of VAR suitable for parsing with awk."
  85. (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v)))))
  86. (cond
  87. ((and (listp var) (listp (car var)))
  88. (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var)))
  89. ((listp var)
  90. (mapconcat echo-var var "\n"))
  91. (t (funcall echo-var var)))))
  92. (provide 'ob-awk)
  93. ;;; ob-awk.el ends here