2015-10-26 15:58:34 +00:00
|
|
|
;;; ob-sed.el --- Babel Functions for Sed Scripts -*- lexical-binding: t; -*-
|
2015-05-28 11:29:09 +00:00
|
|
|
|
2022-01-01 20:10:55 +00:00
|
|
|
;; Copyright (C) 2015-2022 Free Software Foundation, Inc.
|
2015-05-28 11:29:09 +00:00
|
|
|
|
|
|
|
;; Author: Bjarte Johansen
|
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2017-07-05 18:08:35 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2015-05-28 11:29:09 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2017-07-05 18:08:35 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2015-05-28 11:29:09 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2017-07-05 18:08:35 +00:00
|
|
|
|
2015-05-28 11:29:09 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2015-05-28 11:29:09 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Provides a way to evaluate sed scripts in Org mode.
|
|
|
|
|
|
|
|
;;; Usage:
|
|
|
|
|
|
|
|
;; Add to your Emacs config:
|
|
|
|
|
|
|
|
;; (org-babel-do-load-languages
|
|
|
|
;; 'org-babel-load-languages
|
|
|
|
;; '((sed . t)))
|
|
|
|
|
|
|
|
;; In addition to the normal header arguments, ob-sed also provides
|
|
|
|
;; :cmd-line and :in-file. :cmd-line allows one to pass other flags to
|
|
|
|
;; the sed command like the "--in-place" flag which makes sed edit the
|
|
|
|
;; file pass to it instead of outputting to standard out or to a
|
|
|
|
;; different file. :in-file is a header arguments that allows one to
|
|
|
|
;; tell Org Babel which file the sed script to act on.
|
|
|
|
|
|
|
|
;;; Code:
|
2022-08-04 13:53:05 +00:00
|
|
|
|
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
|
|
|
|
2015-05-28 11:29:09 +00:00
|
|
|
(require 'ob)
|
|
|
|
|
|
|
|
(defvar org-babel-sed-command "sed"
|
|
|
|
"Name of the sed executable command.")
|
|
|
|
|
|
|
|
(defvar org-babel-tangle-lang-exts)
|
|
|
|
(add-to-list 'org-babel-tangle-lang-exts '("sed" . "sed"))
|
|
|
|
|
|
|
|
(defconst org-babel-header-args:sed
|
|
|
|
'((:cmd-line . :any)
|
|
|
|
(:in-file . :any))
|
|
|
|
"Sed specific header arguments.")
|
|
|
|
|
|
|
|
(defvar org-babel-default-header-args:sed '()
|
|
|
|
"Default arguments for evaluating a sed source block.")
|
|
|
|
|
|
|
|
(defun org-babel-execute:sed (body params)
|
|
|
|
"Execute a block of sed code with Org Babel.
|
|
|
|
BODY is the source inside a sed source block and PARAMS is an
|
|
|
|
association list over the source block configurations. This
|
|
|
|
function is called by `org-babel-execute-src-block'."
|
|
|
|
(message "executing sed source code block")
|
|
|
|
(let* ((result-params (cdr (assq :result-params params)))
|
|
|
|
(cmd-line (cdr (assq :cmd-line params)))
|
|
|
|
(in-file (cdr (assq :in-file params)))
|
|
|
|
(code-file (let ((file (org-babel-temp-file "sed-")))
|
|
|
|
(with-temp-file file
|
2020-02-18 21:57:37 +00:00
|
|
|
(insert body))
|
|
|
|
file))
|
2015-05-28 11:29:09 +00:00
|
|
|
(stdin (let ((stdin (cdr (assq :stdin params))))
|
2021-09-29 07:22:47 +00:00
|
|
|
(when stdin
|
|
|
|
(let ((tmp (org-babel-temp-file "sed-stdin-"))
|
|
|
|
(res (org-babel-ref-resolve stdin)))
|
|
|
|
(with-temp-file tmp
|
|
|
|
(insert res))
|
|
|
|
tmp))))
|
2015-05-28 11:29:09 +00:00
|
|
|
(cmd (mapconcat #'identity
|
|
|
|
(remq nil
|
|
|
|
(list org-babel-sed-command
|
2018-04-04 11:19:32 +00:00
|
|
|
(format "-f \"%s\"" code-file)
|
2015-05-28 11:29:09 +00:00
|
|
|
cmd-line
|
|
|
|
in-file))
|
|
|
|
" ")))
|
|
|
|
(org-babel-reassemble-table
|
|
|
|
(let ((results
|
|
|
|
(cond
|
|
|
|
(stdin (with-temp-buffer
|
|
|
|
(call-process-shell-command cmd stdin (current-buffer))
|
|
|
|
(buffer-string)))
|
|
|
|
(t (org-babel-eval cmd "")))))
|
|
|
|
(when results
|
|
|
|
(org-babel-result-cond result-params
|
|
|
|
results
|
|
|
|
(let ((tmp (org-babel-temp-file "sed-results-")))
|
|
|
|
(with-temp-file tmp (insert results))
|
|
|
|
(org-babel-import-elisp-from-file tmp)))))
|
|
|
|
(org-babel-pick-name
|
|
|
|
(cdr (assq :colname-names params)) (cdr (assq :colnames params)))
|
|
|
|
(org-babel-pick-name
|
|
|
|
(cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
|
|
|
|
|
|
|
|
(provide 'ob-sed)
|
2020-02-18 21:57:37 +00:00
|
|
|
|
2015-05-28 11:29:09 +00:00
|
|
|
;;; ob-sed.el ends here
|