ob-sed.el 3.6 KB

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