首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从命令行执行org-babel文件?

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

Stack Overflow用户
提问于 2022-03-07 23:13:30
回答 1查看 196关注 0票数 1

这似乎很简单,但我在任何地方都找不到说明,所以我想在这里问一下。是否有一种方法可以在组织文件中执行整个缓冲区的代码块,并从命令行将它们全部保存在该组织文件中?

到目前为止,我已经尝试过这样的方法,但它不起作用(只是挂起):

代码语言:javascript
复制
emacsclient -a --eval '(find-file "index.org")(org-babel-execute-buffer)(save-buffer)'
EN

回答 1

Stack Overflow用户

发布于 2022-05-09 03:49:18

这可能会有帮助:

代码语言:javascript
复制
#!/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)))

这里有更多信息:

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

如果您专门搜索execute org files,也会有很多资源;我会发布链接,但我不知道这里是否欢迎这样做。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71388302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档