ob-awk.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; ob-awk.el --- Babel Functions for Awk -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Maintainer: Tyler Smith <tyler@plantarum.ca>
  5. ;; Keywords: literate programming, reproducible research
  6. ;; URL: https://orgmode.org
  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 <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Babel's awk can use special header argument:
  20. ;;
  21. ;; - :in-file takes a path to a file of data to be processed by awk
  22. ;;
  23. ;; - :stdin takes an Org data or code block reference, the value of
  24. ;; which will be passed to the awk process through STDIN
  25. ;;; Code:
  26. (require 'org-macs)
  27. (org-assert-version)
  28. (require 'ob)
  29. (require 'org-compat)
  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)
  37. "Expand BODY according to PARAMS, return the expanded body."
  38. body)
  39. (defun org-babel-execute:awk (body params)
  40. "Execute a block of Awk code with org-babel.
  41. This function is called by `org-babel-execute-src-block'."
  42. (message "executing Awk source code block")
  43. (let* ((result-params (cdr (assq :result-params params)))
  44. (cmd-line (cdr (assq :cmd-line params)))
  45. (in-file (cdr (assq :in-file params)))
  46. (full-body (org-babel-expand-body:awk body params))
  47. (code-file (let ((file (org-babel-temp-file "awk-")))
  48. (with-temp-file file (insert full-body)) file))
  49. (stdin (let ((stdin (cdr (assq :stdin params))))
  50. (when stdin
  51. (let ((tmp (org-babel-temp-file "awk-stdin-"))
  52. (res (org-babel-ref-resolve stdin)))
  53. (with-temp-file tmp
  54. (insert (org-babel-awk-var-to-awk res)))
  55. tmp))))
  56. (cmd (mapconcat #'identity
  57. (append
  58. (list org-babel-awk-command
  59. "-f" code-file cmd-line)
  60. (mapcar (lambda (pair)
  61. (format "-v %s='%s'"
  62. (car pair)
  63. (org-babel-awk-var-to-awk
  64. (cdr pair))))
  65. (org-babel--get-vars params))
  66. (list in-file))
  67. " ")))
  68. (org-babel-reassemble-table
  69. (let ((results
  70. (cond
  71. (stdin (with-temp-buffer
  72. (call-process-shell-command cmd stdin (current-buffer))
  73. (buffer-string)))
  74. (t (org-babel-eval cmd "")))))
  75. (when results
  76. (org-babel-result-cond result-params
  77. results
  78. (let ((tmp (org-babel-temp-file "awk-results-")))
  79. (with-temp-file tmp (insert results))
  80. (org-babel-import-elisp-from-file tmp)))))
  81. (org-babel-pick-name
  82. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  83. (org-babel-pick-name
  84. (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
  85. (defun org-babel-awk-var-to-awk (var &optional sep)
  86. "Return a printed value of VAR suitable for parsing with awk."
  87. (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v)))))
  88. (cond
  89. ((and (listp var) (listp (car var)))
  90. (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var)))
  91. ((listp var)
  92. (mapconcat echo-var var "\n"))
  93. (t (funcall echo-var var)))))
  94. (provide 'ob-awk)
  95. ;;; ob-awk.el ends here