ob-sed.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;;; ob-sed.el --- Babel Functions for Sed Scripts -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015-2022 Free Software Foundation, Inc.
  3. ;; Author: Bjarte Johansen
  4. ;; Keywords: literate programming, reproducible research
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Provides a way to evaluate sed scripts in Org mode.
  18. ;;; Usage:
  19. ;; Add to your Emacs config:
  20. ;; (org-babel-do-load-languages
  21. ;; 'org-babel-load-languages
  22. ;; '((sed . t)))
  23. ;; In addition to the normal header arguments, ob-sed also provides
  24. ;; :cmd-line and :in-file. :cmd-line allows one to pass other flags to
  25. ;; the sed command like the "--in-place" flag which makes sed edit the
  26. ;; file pass to it instead of outputting to standard out or to a
  27. ;; different file. :in-file is a header arguments that allows one to
  28. ;; tell Org Babel which file the sed script to act on.
  29. ;;; Code:
  30. (require 'org-macs)
  31. (org-assert-version)
  32. (require 'ob)
  33. (defvar org-babel-sed-command "sed"
  34. "Name of the sed executable command.")
  35. (defvar org-babel-tangle-lang-exts)
  36. (add-to-list 'org-babel-tangle-lang-exts '("sed" . "sed"))
  37. (defconst org-babel-header-args:sed
  38. '((:cmd-line . :any)
  39. (:in-file . :any))
  40. "Sed specific header arguments.")
  41. (defvar org-babel-default-header-args:sed '()
  42. "Default arguments for evaluating a sed source block.")
  43. (defun org-babel-execute:sed (body params)
  44. "Execute a block of sed code with Org Babel.
  45. BODY is the source inside a sed source block and PARAMS is an
  46. association list over the source block configurations. This
  47. function is called by `org-babel-execute-src-block'."
  48. (message "executing sed source code block")
  49. (let* ((result-params (cdr (assq :result-params params)))
  50. (cmd-line (cdr (assq :cmd-line params)))
  51. (in-file (cdr (assq :in-file params)))
  52. (code-file (let ((file (org-babel-temp-file "sed-")))
  53. (with-temp-file file
  54. (insert body))
  55. file))
  56. (stdin (let ((stdin (cdr (assq :stdin params))))
  57. (when stdin
  58. (let ((tmp (org-babel-temp-file "sed-stdin-"))
  59. (res (org-babel-ref-resolve stdin)))
  60. (with-temp-file tmp
  61. (insert res))
  62. tmp))))
  63. (cmd (mapconcat #'identity
  64. (remq nil
  65. (list org-babel-sed-command
  66. (format "-f \"%s\"" code-file)
  67. cmd-line
  68. in-file))
  69. " ")))
  70. (org-babel-reassemble-table
  71. (let ((results
  72. (cond
  73. (stdin (with-temp-buffer
  74. (call-process-shell-command cmd stdin (current-buffer))
  75. (buffer-string)))
  76. (t (org-babel-eval cmd "")))))
  77. (when results
  78. (org-babel-result-cond result-params
  79. results
  80. (let ((tmp (org-babel-temp-file "sed-results-")))
  81. (with-temp-file tmp (insert results))
  82. (org-babel-import-elisp-from-file tmp)))))
  83. (org-babel-pick-name
  84. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  85. (org-babel-pick-name
  86. (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
  87. (provide 'ob-sed)
  88. ;;; ob-sed.el ends here