如何在 Spacemacs 启动后根据主模式变量提供自定义函数?

问题描述 投票:0回答:1

我最近开始编写一些 elisp 代码,主要是为了自定义我的 spacemacs -> 绝对初学者。尽管“如何定制...”有很多不同的来源,但我仍然对以下内容感到困惑。

情况是我想在 spacemacs 启动时准备好一个自定义函数,但该函数取决于一些主要模式变量。实现这一目标的最佳方法是什么,而无需先打开文件触发加载此主要模式?

具体例子: 我想以交互方式访问(完成)我的 org-agenda-files,所以我编写了这个函数

  (defun my/org-agenda-find-file(file-path)
    (interactive (list
                  (completing-read "Choose agenda file: " org-agenda-files)))
    (find-file file-path)
    )

# Binding 
 (spacemacs/set-leader-keys "oof" #'my/org-agenda-find-file)

并将其放入自定义的 my_org_config.el 中,该文件会加载到 .spacemacs/dotspaceamcs/user-conifg() 中。 到目前为止一切顺利,如果 org-mode 之前至少加载过一次,则可以按预期工作。

如果是新启动的 spacemacs,尝试使用我的函数,我收到错误: “org-agenda-files”不存在。所以看来“org-agenda-files”是在加载org-mode期间设置的。

一个可行的解决方案是在我的函数之前添加“require 'org”语句,但我不确定这是否是一个很好的处理方法。

(require 'org)
(defun my/org-agenda-find-file(file-path)
  (interactive (list
                (completing-read "Choose agenda file: " org-agenda-files)))
  (find-file file-path)
  )

是否有更好的方法来做到这一点,也许通过关键字或函数本身内的调用,或者这是 require 和 spacemacs 很好地结合在一起的情况之一:)?

如有任何帮助,我们将不胜感激!谢谢你。

elisp startup org-mode custom-function spacemacs
1个回答
0
投票

(require 'org)
置于顶层,就像您所做的那样,是确保在调用函数时加载 org 功能的一种方法。我认为这没有什么问题,只是即使你不使用该功能也会被加载,不必要地延迟启动。

您也可以将

(require 'org)
放入函数中,以防止在不需要时加载该功能,如 Emacs 手册中所示。

(defun my/org-agenda-find-file (file-path)
  (require 'org)
  ...)
© www.soinside.com 2019 - 2024. All rights reserved.