config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

ob-sed.el (3727B)


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