Org模式 - 禁用Elisp代码执行确认对话框

5

我有以下代码来在用户点击链接时调用elisp函数"myfun":

#+BEGIN_SRC elisp :results output raw
  (defun myfun(filepath lineno)
    (if (not (get-buffer bufname))
        (get-buffer-create bufname)
      )
    (switch-to-buffer-other-window bufname)
    (global-linum-mode)
    (erase-buffer)
    (insert-file-contents (format "%s" filepath))
    (goto-char (point-min))
    (forward-line lineno)
    (setq start (point))
    (forward-line -1)
    (setq end (point))
    (let ((x (make-overlay start end)))
      (overlay-put x 'face '(:background "#fef0f1")))
    )

  (defun createSampleFile(file-name count)
    (let ((c 0))
      (with-temp-buffer
        (while (< c (* 2 count))
          (setq c (1+ c))
          (insert (format "line %d\n" c))
          (write-file filename)
          ))))

  (let ((c 0)(filename nil))
    (while (< c 4)
      (setq c (1+ c))
      (setq filename (format "./test%d.txt" c))
      (createSampleFile filename c)
      (princ (format "[[elisp:(myfun '%s' %d)][%s]]\n" filename c filename))
      ))
#+END_SRC

#+RESULTS:
[[elisp:(myfun './test1.txt' 1)][./test1.txt]]
[[elisp:(myfun './test2.txt' 2)][./test2.txt]]
[[elisp:(myfun './test3.txt' 3)][./test3.txt]]
[[elisp:(myfun './test4.txt' 4)][./test4.txt]]

但是当我点击链接时,总是会弹出以下提示警告对话框:

enter image description here

我们能否禁用该对话框?

1个回答

9
在init.el文件中添加以下行:
(setq org-confirm-elisp-link-function nil)

但是请注意变量文档中的警告。执行任意代码(无论是在elisp还是任何其他语言中)都具有风险。 - NickD
也许将其仅添加到org文件中,而不要将其添加到init.el中会更好。 - lucky1928
是的 - 至少,您可以查看代码并确保它不包含隐藏的炸弹,然后将其标记为上面。这可以通过file variable完成。而且您不会给任何文件执行任何代码的全面权限。 - NickD
@Nick 你认为 org-confirm-babel-evaluate 和这个情况一样吗?我通常在 init.el 中将其设置为 nill。我认为 babel 代码需要用户运行它,那么当打开一个文件时它会自动执行吗? - lucky1928
1
正如NickD所提到的,更安全的替代方法是将以下内容添加为文件的第一行:# -*- mode: Org; org-confirm-elisp-link-function: nil; -*- - Mark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接