在写layer时遇到的问题

我在写layer时想使用use的包中的一个函数。但是这个函数没有加上魔法注释~~就是没有加上autoload这样的注释,那么我应该怎么才能调用的这个包中的方法呢?(ps qq我也可以 加我时就说在emacs bbs上看到的 谢谢各位 1042181618)

(with-eval-after-load "some-package"
   (some-func))
1 个赞

可以手写 autoload:

(autoload 'org-mac-grab-link "org-mac-link" nil t)

其中 org-mac-grab-linkorg-mac-link.el[c] 中被定义,最后的 t 指定交互命令。

另外,如果使用 use-package 的话,:commands:bind 等自带 autoload 效果:

(use-package osx-dictionary
  :if *is-mac*
  :load-path "~/src/osx-dictionary.el"
  :bind ("C-c d" . osx-dictionary-search-at-point))

然后 osx-dictionary-search-at-point 自动会被 autoload

1 个赞

你好我使用这个方法后~~ 原来 调用了这个方法的 方法就提示 找不到这个方法为什么??

这里不绑定 bind不能用吗??我想在func。el中使用这个方法。想用它封装的某个方法。再在pacake。el中绑定快捷键。 额 就是在youdao-dictionary中的两个方法-region-or-word 和format-result 问下怎么办~

使用 autoload 或及时地 require 应该都可行:

autoload

(autoload 'youdao-dictionary--region-or-word "youdao-dictionary")
(autoload 'youdao-dictionary--format-result "youdao-dictionary")

(defun her-youdao-dict ()
  (interactive)
  (let ((word (youdao-dictionary--region-or-word)))
    (when word
      (youdao-dictionary--format-result word))))

require

(defun his-youdao-dict ()
  (interactive)
  (require 'youdao-dictionary)
  (let ((word (youdao-dictionary--region-or-word)))
    (when word
      (youdao-dictionary--format-result word))))
  1. (require 'youdao-dictionary) 放在函数体内,只有当函数被真正调用时才执行,eval 这个 (defun his-youdao-dict () ...) 本身不会调用;
  2. require 调用多次没有关系,如果 youdao-dictionary 已经被加载过了,它不会进行任何操作。
1 个赞

按照spacemacs官方对layer的说明,Keybindings要写在keybindings.el里,调用顺序是 package.el → funcs.el → config.el → Keybindings.el , 所以你在 package.el 里调用 funcs.el 里定义的函数肯定是不行的。 不过从现有的layer来看,也有很多人只在 package.el 里用 use-package 来配置。 use-package 很强大,init, config, Keybindings 都能做,你可以看它的 GitHub repo 来学习。

1 个赞

多谢 高人指点~

;;;;define function to save the word to a note

;;自动加载类
(autoload 'youdao-dictionary--region-or-word "youdao-dictionary")
(autoload 'youdao-dictionary--format-result "youdao-dictionary")

;;单词存放位置 保证以后的 单词本中没有重复?
(defconst note-uri "../.note/words")
;;单词本位置 应该可以改成可以定制的
(defconst note "../note.org")

;;todo 将word 存入单词本
(defun learnEn//add-word-to-note (d)
  )

;;定义模
(define-derived-mode learnEn-mode org-mode "learn-En"
  "Major mode for learnEn.
\\{learnEn-mode-map}"
  (read-only-mode 1)
  (define-key learnEn-mode-map "q" 'quit-window))

;;
(defun learnEn//add-word-at-ponit ()
  (interactive)
  (let ((word (youdao-dictionary--region-or-word)))
    (if word
        (with-current-buffer (get-buffer-create "*Learn-En*")
          (let ((inhibit-read-only t))
            (erase-buffer)
            (learnEn-mode)
            (insert (youdao-dictionary--format-result word))
            (goto-char (point-min)))
          (switch-to-buffer-other-window "*Learn-En*"))
      (message "Nothing to look up"))))

不好意思 帮忙再看一下 我这里绑定 的keymap 为何会无效呢?? 我现在 自定义的mode下绑定几个快捷键 但是 没有效果这里是为什么? 我是仿照 包中的写法 ~ 发现包中打开youdao-dictionary buffer后退出快捷键也是不好使的 ~~ 这是为什么呢?求解~~ 全部代码~在我git上https://github.com/zzhgithub/learnEn

这里应该写成绝对路径,或者用 expand-file-name 把相对路径扩展开来。因为运行时的工作路径是任意的。

不清楚什么意思,按 q 难道不执行 quit-window 吗?我这边没遇到问题。

恩~~ 是的 按q没有执行 quit-window 是不是在spacemacs环境的原因呢?

不清楚,没用过 Spacemacs,可能是,也可能是你自己的定制造成的。