简体   繁体   English

如何从命令行执行 org-babel 文件?

[英]How can I execute an org-babel file from the command line?

This seems like it'd be straightforward, but I can't find instructions for this anywhere, so I thought I'd ask here.这看起来很简单,但我在任何地方都找不到这方面的说明,所以我想我会在这里问。 Is there a way to execute a whole buffer's worth of code blocks, in an org file, and save them all in that org file, headlessly, from the command line?有没有一种方法可以在 org 文件中执行整个缓冲区的代码块,然后从命令行无头地将它们全部保存在该 org 文件中?

Here's what I've tried so far, but it's not working (just hangs):到目前为止,这是我尝试过的方法,但它不起作用(只是挂起):

emacsclient -a --eval '(find-file "index.org")(org-babel-execute-buffer)(save-buffer)'

This might be of help:这可能有帮助:

#!/usr/bin/env sh
":"; exec emacs --quick --script "$0" -- "$@" # -*- mode: emacs-lisp; lexical-binding: t; -*-
(pop argv)

(require 'org-element)

(defun require-lang (lang)
    (defvar bootstrap-version)
    (let ((bootstrap-file
        (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
        (bootstrap-version 5))
    (unless (file-exists-p bootstrap-file)
        (with-current-buffer
            (url-retrieve-synchronously
            "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
            'silent 'inhibit-cookies)
        (goto-char (point-max))
        (eval-print-last-sexp)))
    (load bootstrap-file nil 'nomessage))
    (straight-use-package 'use-package)
    (setq straight-use-package-by-default t)
    (pcase (downcase lang)
        ((or "hy" "hylang") (use-package ob-hy :demand t :straight '(ob-hy :type git :host github :repo "allison-casey/ob-hy") :init (setq org-babel-hy-command "/usr/bin/env hy")))))

(defun message-advice (func &rest args) (interactive)
    (let* ((*message (apply #'format args)))
        (unless (or (string-prefix-p "executing" *message)
                    (string-prefix-p "Code block" *message))
            (apply func args))))
(advice-add #'message :around #'message-advice)

(defun org-babel-eval-error-notify-advice (exit-code stderr)
  "Open a buffer to display STDERR and a message with the value of EXIT-CODE."
  (let ((buf (get-buffer-create org-babel-error-buffer-name)))
    (with-current-buffer buf
      (goto-char (point-max))
      (save-excursion (insert stderr))
      (message (buffer-string)))
    (display-buffer buf))
  (message "Babel evaluation exited with code %S" exit-code))
(advice-add #'org-babel-eval-error-notify :override #'org-babel-eval-error-notify-advice)

(let ((org-confirm-babel-evaluate)
        (lang-list '()))
    (with-temp-buffer
        (while argv
            (let ((arg (pop argv)))
                (pcase arg
                    ((or "-l" "--languages")
                        (while (and (> (length argv) 1) (not (string-prefix-p "-" (car argv))))
                            (add-to-list 'lang-list (pop argv) t)))
                    (_ (setq file arg)))))
        (insert-file-contents file)
        (mapc 'require-lang lang-list)
(org-babel-execute-buffer)))

More information here:更多信息在这里:

https://github.com/sylvorg/settings/blob/main/README.org#org-interpreter https://github.com/sylvorg/settings/blob/main/README.org#org-interpreter

There are also a bunch of resources if you search specifically execute org files ;如果您专门搜索execute org files ,也有很多资源; I'd post the links, but I don't know if that would be welcome here.我会发布链接,但我不知道这是否会受到欢迎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM