解决:file local variables specified in -*- line

因为使用 Org 文件记笔记,Org 文件的图片附件管理是将图片放在以 Org 文件名称的子文件下。

所以 Org 文件中需要声明一个 file local variables:org-media-note-screenshot-image-dir 这个变量是和文件名相关的,这样使用 org-download-screenshot 就可以将截图存到org-media-note-screenshot-image-dir指定的子文件夹中。

文件名为“Tool tips on text in Emacs”Org 文件内容范例:

# -*- eval: (setq org-media-note-screenshot-image-dir (concat default-directory "./static/Tool tips on text in Emacs/")); -*-
#+TITLE: Tool tips on text in Emacs


这是正文。
这是正文。

可以看到第一行声明了这个 file local variable。

问题来了:

  1. 这行声明必须关闭 Org buffer,并重新打开该文件,手动输入 y(es) 确认该 file local variable 安全。如下图:

  1. 确认后,emacs 会将该 file local variable 写入 custom.el,如下:
(custom-set-variables
'(safe-local-variable-values
   '((eval setq org-media-note-screenshot-image-dir
           (concat default-directory "./static/Tool tips on text in Emacs/")))))

  1. 所以我想要简化该过程,尝试每次新创建 Org 文件时运行类似如下代码:
(add-to-list 'safe-local-variable-values `(eval (setq org-media-note-screenshot-image-dir (concat default-directory "./static/Tool tips on text in Emacs/"))))

但是该 file local variable 没有被写入 custom.el 还是需要像 1 中描述,手动确认。

所以该如何解决这个问题?

  1. 如何使得创建一个全新的 Org 文件时,将
(eval setq org-media-note-screenshot-image-dir
           (concat default-directory "./static/全新的 Org 文件/")

写入 custom.el?

  1. 或者有没有什么方法跳过 emacs 需要强制确认 file local variable 是否安全?

搜索到相关的链接,但是不知道如何使用,改造。

Mark a local variable safe for any value

换个角度思考如何保存图片文件。如果你要找某个图片,但是你想不起来他跟哪个笔记有关,你怎么找。

我目前是将所有图片保存到一个统一的目录。

这种方法不好的在于文件分享。

因为历史原因,很多不同图片名称是有重名的,以前图省事用数字命令,所以现在无法采用你建议的方法。

你不要在 safe-local-variable-values 把路径写死不就行了吗?

(add-to-list 'safe-local-variable-values
             '(eval . (setq org-media-note-screenshot-image-dir
                            (file-name-as-directory
                             (format "./static/%s" (file-name-base buffer-file-name))))))

Org local variable:

# Local Variables:
# eval: (setq org-media-note-screenshot-image-dir (file-name-as-directory (format "./static/%s" (file-name-base buffer-file-name))))
# End:

我自己会把它写成一个函数。emacs-grandview/early-init.el at master · alexluigit/emacs-grandview · GitHub

1 个赞

找到自己想要的函数了 customize-push-and-save 可以将所需的代码加入 custom.el :

 (add-to-list 'local-variable-list `(eval . (setq org-media-note-screenshot-image-dir (concat default-directory ,(format "%s" relative-img-dir)))))
 (customize-push-and-save 'safe-local-variable-values local-variable-list)

谢谢你的建议,我看了你的代码也才发现自己原来的写错了。

主题好看,modeline 是什么呀

Spacemcas 自带的主题:spacemacs-light。

1 个赞