ob-sed.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ;;; ob-sed.el --- Babel Functions for Sed Scripts -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015-2018 Free Software Foundation, Inc.
  3. ;; Author: Bjarte Johansen
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Version: 0.1.0
  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. ;; Provides a way to evaluate sed scripts in Org mode.
  19. ;;; Usage:
  20. ;; Add to your Emacs config:
  21. ;; (org-babel-do-load-languages
  22. ;; 'org-babel-load-languages
  23. ;; '((sed . t)))
  24. ;; In addition to the normal header arguments, ob-sed also provides
  25. ;; :cmd-line and :in-file. :cmd-line allows one to pass other flags to
  26. ;; the sed command like the "--in-place" flag which makes sed edit the
  27. ;; file pass to it instead of outputting to standard out or to a
  28. ;; different file. :in-file is a header arguments that allows one to
  29. ;; tell Org Babel which file the sed script to act on.
  30. ;;; Code:
  31. (require 'ob)
  32. (defvar org-babel-sed-command "sed"
  33. "Name of the sed executable command.")
  34. (defvar org-babel-tangle-lang-exts)
  35. (add-to-list 'org-babel-tangle-lang-exts '("sed" . "sed"))
  36. (defconst org-babel-header-args:sed
  37. '((:cmd-line . :any)
  38. (:in-file . :any))
  39. "Sed specific header arguments.")
  40. (defvar org-babel-default-header-args:sed '()
  41. "Default arguments for evaluating a sed source block.")
  42. (defun org-babel-execute:sed (body params)
  43. "Execute a block of sed code with Org Babel.
  44. BODY is the source inside a sed source block and PARAMS is an
  45. association list over the source block configurations. This
  46. function is called by `org-babel-execute-src-block'."
  47. (message "executing sed source code block")
  48. (let* ((result-params (cdr (assq :result-params params)))
  49. (cmd-line (cdr (assq :cmd-line params)))
  50. (in-file (cdr (assq :in-file params)))
  51. (code-file (let ((file (org-babel-temp-file "sed-")))
  52. (with-temp-file file
  53. (insert body)) 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 "--file=\"%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