ob-sed.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 'ob)
  31. (defvar org-babel-sed-command "sed"
  32. "Name of the sed executable command.")
  33. (defvar org-babel-tangle-lang-exts)
  34. (add-to-list 'org-babel-tangle-lang-exts '("sed" . "sed"))
  35. (defconst org-babel-header-args:sed
  36. '((:cmd-line . :any)
  37. (:in-file . :any))
  38. "Sed specific header arguments.")
  39. (defvar org-babel-default-header-args:sed '()
  40. "Default arguments for evaluating a sed source block.")
  41. (defun org-babel-execute:sed (body params)
  42. "Execute a block of sed code with Org Babel.
  43. BODY is the source inside a sed source block and PARAMS is an
  44. association list over the source block configurations. This
  45. function is called by `org-babel-execute-src-block'."
  46. (message "executing sed source code block")
  47. (let* ((result-params (cdr (assq :result-params params)))
  48. (cmd-line (cdr (assq :cmd-line params)))
  49. (in-file (cdr (assq :in-file params)))
  50. (code-file (let ((file (org-babel-temp-file "sed-")))
  51. (with-temp-file file
  52. (insert body))
  53. file))
  54. (stdin (let ((stdin (cdr (assq :stdin params))))
  55. (when stdin
  56. (let ((tmp (org-babel-temp-file "sed-stdin-"))
  57. (res (org-babel-ref-resolve stdin)))
  58. (with-temp-file tmp
  59. (insert res))
  60. tmp))))
  61. (cmd (mapconcat #'identity
  62. (remq nil
  63. (list org-babel-sed-command
  64. (format "-f \"%s\"" code-file)
  65. cmd-line
  66. 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 "sed-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. (provide 'ob-sed)
  86. ;;; ob-sed.el ends here