如何获取init.el加载的路径?

时间:2014-02-02 07:30:40

标签: emacs

我希望为emacs创建一个自定义配置以用于Erlang工作,我想将我的自定义EDTS repo称为在加载init.el的目录下。现在我有这个:

(add-to-list 'load-path "~/.emacs-edts/edts/")

但我宁愿不对它进行硬编码并通过变量引用它。

连连呢?

3 个答案:

答案 0 :(得分:3)

严格来说答案是(file-name-directory user-init-file),而是看 C-h v user-emacs-directory

答案 1 :(得分:1)

我的init.el中有以下代码段:

(setq my-init-dir
    (file-name-directory
        (or load-file-name (buffer-file-name))))

这样做有一个好处,即init.el是否在你的emacs.d目录中。

答案 2 :(得分:0)

我的init文件中有以下内容:

(defun my-file-name-basename (s)
  "The directory name, without the final part.

For example:
    (my-file-name-basename \"alpha/beta/gamma\") => \"alpha/beta\""
  (substring (file-name-directory s) 0 -1))


;; Note: Normally, it's not possible to find out the file a specific
;; function is defined in. However, it's possible to save the file
;; name at the time this file was loaded.
(defvar my-load-file-name load-file-name
  "The file name of this file.")


(defun my-start-directory (&optional path)
  "The root directory that contains this module.
When PATH is specified, return the start directory concatenated with PATH.
Otherwise return the directory with a trailing slash."
  ;; Note: Try to figure out where we are, so that we can add the
  ;; subdirectories. `load-file-name' only works when the file is
  ;; loaded. Picking up the file from the symbol works when this is
  ;; evaluated later.
  (let ((file-name (or my-load-file-name
                       (symbol-file 'my-start-directory)
                       ;; Default value. (This is used, for example,
                       ;; when using `eval-buffer' or `eval-region'.)
                       "~/emacs")))
    (let ((start (concat (my-file-name-basename
                          (my-file-name-basename file-name))
                         "/")))
      (if path
          (concat start path)
        start))))

除了找出包含上述代码的文件所在的位置(不必是init文件)之外,它还提供了一种基于它创建路径的便捷方法。例如:

(setq custom-file (my-start-directory "init/custom.el"))
相关问题