Emacs:定义一个函数,用于加载定义函数本身的文件

时间:2012-03-30 16:37:21

标签: emacs

我在我的Emacs设置中进行了一些重构,并得出结论我想使用与默认文件不同的初始文件。所以基本上,在我的〜/ .emacs文件中,我有这个:

(load "/some/directory/init.el")

到目前为止,这一切都很好。但是,现在我想重新定义一个我用了很久的旧命令,它会打开我的init文件:

(defun conf ()
  "Open a buffer with the user init file."
  (interactive)
  (find-file user-init-file))

正如您所看到的,无论我做什么,都会打开〜/ .emacs。我希望它打开/some/directory/init.el,或者在conf命令本身被定义的任何地方。

我该怎么做?

3 个答案:

答案 0 :(得分:6)

您可以使用find-function

(defun conf ()
  "Open a buffer with the user init file."
  (interactive)
  (find-function this-command))

答案 1 :(得分:3)

你也可以使用一种偷偷摸摸的方式:

  

(defun conf()“blabla”(交互式)(find-file#$))

因为#$在C中有点像__FILE__:在读取文件时它被文件名替换。

答案 2 :(得分:0)

这对我有用。

;;; mymodule.el --- does my thing

(defvar mymodule--load-path nil  "load path of the module")

   ...

(defun mymodule-load-datafile () 
  "load a data file from the directory in which the .el resides"
  (let* ((dir-name (concat
                        (file-name-directory mymodule--load-path)))
         (data-file-name (concat dir-name "datafile.csv")))
    (if (file-exists-p  data-file-name) 
         ... )))

;; remember load time path
(setq mymodule--load-path load-file-name)

(provide 'mymodule)