ob-awk.el 3.8 KB

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