如何始终显示内联图像?

How to always show inline images?

我正在尝试使用内联图像(例如,通过 gnuplot 绘制数据),但遇到问题:默认情况下,图像总是插入为 links .我需要对 "force" emacs 进行一些按键操作以显示 实际图像 内联,而不仅仅是文件 link.

例如我从 gnuplot 代码开始:

#+BEGIN_SRC gnuplot :file plot.png
plot sin(x)
#+END_SRC

当我在此代码块上按 C-c C-c 时,它会 运行s,并将结果显示为 link 到图像文件:

#+RESULTS:
[[file:plot.png]]

等等(这些选项取自 Emacs org-display-inline-images and Inline images in org-mode 个问题)

但我不想按任何特殊的按钮:我希望默认情况下内联显示图像。我发现并尝试了以下变量:

但都没有得到我想要的行为。我很困惑 -- 我想要这么不自然的东西吗?

P.S。我在 MacOS X 上使用 GNU Emacs v26.1,组织模式 v9.1.9-65,如果重要的话

P.P.S。虽然这似乎是我的 emacs/orgmode 版本中的一个错误,而且我还没有报告它,但同时我发现了以下技巧:(add-hook 'org-babel-after-execute-hook 'org-display-inline-images 'append)(感谢 ob-ipython 作者)--它现在为我解决了问题。也许对其他人有用

我可以重现问题:

Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ /home/xyz/.emacs.d/elpa/org-plus-contrib-20190415/)

复制:

  1. 使用 emacs -Q 启动 Emacs 26.3。
  2. M-x load-library RET org RET
  3. 通过 M-x 添加 Gnuplotorg-babel-load-languages customize-option.
  4. 加载gnuplot.el
  5. 打开包含以下内容的 Org 文件,然后在源块上按 C-c C-c
#+STARTUP: inlineimages
Some text.

#+BEGIN_SRC gnuplot :file plot.png :results graphics
plot sin(x)
#+END_SRC

我有一个与您在问题中建议的类似的解决方案,但更加不同。

重新显示大型 Org 文档中的图像可能需要一些时间。所以我只在源块具有结果参数 graphics:

时才这样做
(require 'subr-x)
(defun org+-babel-after-execute ()
  "Redisplay inline images after executing source blocks with graphics results."
  (when-let ((info (org-babel-get-src-block-info t))
         (params (org-babel-process-params (nth 2 info)))
         (result-params (cdr (assq :result-params params)))
         ((member "graphics" result-params)))
    (org-display-inline-images)))

(add-hook 'org-babel-after-execute-hook #'org+-babel-after-execute)

@Tobias 是最佳答案。我调整了@Tobias 代码以进一步优化,方法是将重新显示绑定到当前子树并将 REFRESH 参数设置为 t 以仅在必要时重新显示。

  (require 'subr-x)
  (defun org+-babel-after-execute ()
    "Redisplay inline images in subtree if cursor in source block with :result graphics."

    (when (org-in-src-block-p)
      (let (beg end)
        (save-excursion
          (org-mark-subtree)
          (setq beg (point))
          (setq end (mark)))
        (when-let ((info (org-babel-get-src-block-info t))
                   (params (org-babel-process-params (nth 2 info)))
                   (result-params (cdr (assq :result-params params)))
                   ((member "graphics" result-params)))
          (org-display-inline-images nil t beg end)))))

  (add-hook 'org-babel-after-execute-hook #'org+-babel-after-execute)