ob-awk.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. ;; Homepage: 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 'ob)
  27. (require 'org-compat)
  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.
  39. This function is called by `org-babel-execute-src-block'."
  40. (message "executing Awk source code block")
  41. (let* ((result-params (cdr (assq :result-params params)))
  42. (cmd-line (cdr (assq :cmd-line params)))
  43. (in-file (cdr (assq :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 (assq :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. (car pair)
  61. (org-babel-awk-var-to-awk
  62. (cdr pair))))
  63. (org-babel--get-vars params))
  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 (assq :colname-names params)) (cdr (assq :colnames params)))
  81. (org-babel-pick-name
  82. (cdr (assq :rowname-names params)) (cdr (assq :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