ob-org.el (2956B)
1 ;;; ob-org.el --- Babel Functions for Org Code Blocks -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2010-2024 Free Software Foundation, Inc. 4 5 ;; Author: Eric Schulte 6 ;; Keywords: literate programming, reproducible research 7 ;; URL: https://orgmode.org 8 9 ;; This file is part of GNU Emacs. 10 11 ;; GNU Emacs is free software: you can redistribute it and/or modify 12 ;; it under the terms of the GNU General Public License as published by 13 ;; the Free Software Foundation, either version 3 of the License, or 14 ;; (at your option) any later version. 15 16 ;; GNU Emacs is distributed in the hope that it will be useful, 17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 ;; GNU General Public License for more details. 20 21 ;; You should have received a copy of the GNU General Public License 22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 23 24 ;;; Commentary: 25 26 ;; This is the simplest of code blocks, where upon evaluation the 27 ;; contents of the code block are returned in a raw result. 28 29 ;;; Code: 30 31 (require 'org-macs) 32 (org-assert-version) 33 34 (require 'ob) 35 36 (declare-function org-export-string-as "ox" 37 (string backend &optional body-only ext-plist)) 38 39 (defvar org-babel-default-header-args:org 40 '((:results . "raw silent") (:exports . "code")) 41 "Default arguments for evaluating an org source block.") 42 43 (defvar org-babel-org-default-header 44 "#+TITLE: default empty header\n" 45 "Default header inserted during export of org blocks.") 46 47 (defun org-babel-expand-body:org (body params) 48 "Expand Org BODY according to PARAMS. 49 $VAR instances are replaced by VAR values defined in PARAMS." 50 (dolist (var (org-babel--get-vars params)) 51 (setq body (replace-regexp-in-string 52 (regexp-quote (format "$%s" (car var))) 53 (format "%s" (cdr var)) 54 body 'fixedcase 'literal))) 55 56 (let ((prologue (cdr (assq :prologue params))) 57 (epilogue (cdr (assq :epilogue params)))) 58 (concat 59 (and prologue (concat prologue "\n")) 60 body 61 (and epilogue (concat "\n" epilogue "\n"))))) 62 63 (defun org-babel-execute:org (body params) 64 "Execute a Org BODY according to PARAMS. 65 The BODY is returned expanded as is or exported, if PARAMS define 66 latex/html/ascii result type. 67 This function is called by `org-babel-execute-src-block'." 68 (let ((result-params (split-string (or (cdr (assq :results params)) ""))) 69 (body (org-babel-expand-body:org 70 (replace-regexp-in-string "^," "" body) params))) 71 (cond 72 ((member "latex" result-params) 73 (org-export-string-as (concat "#+Title: \n" body) 'latex t)) 74 ((member "html" result-params) (org-export-string-as body 'html t)) 75 ((member "ascii" result-params) (org-export-string-as body 'ascii t)) 76 (t body)))) 77 78 (defun org-babel-prep-session:org (_session _params) 79 "Return an error because org does not support sessions." 80 (error "Org does not support sessions")) 81 82 (provide 'ob-org) 83 84 ;;; ob-org.el ends here