emacs插件目录配置

本文介绍了如何配置emacs插件目录,包括在.emacs文件中加载插件目录,利用eval-after-load延迟加载以提高启动速度,以及解决使用package-install安装插件后管理混乱的问题。通过调整load subdirs,可以方便地加载指定目录下的所有文件,并讨论了js2-mode等需要编译的插件如何正确移动和加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

emacs插件目录配置

加载插件目录

在 .emacs 文件中加入如下代码,dir 为插件放置的目录

(add-to-list 'load-path dir)

插件加载完成之后再接着加入几项配置语句就完成了整个插件的安装和配置

eval-after-load

随着插件安装的增多,emacs每次启动都要加载.emacs中写入的全部插件,这样就会使emacs启动十分缓慢。
eval-after-load.el 详细使用参见emacser.com上关于这个的介绍
[http://emacser.com/eval-after-load.htm]
使用之后就会当要启用这个插件时才加载,这样会减少emacs启动时加载插件数

load subdirs

当我们为每一个实用的模式都配置了自己的相应代码时,每次都用load-path 来加载文件就会显得很繁琐,可以编写函数来将你指定的目录下的所有文件都加载进来,然后又有 eval-after-load.el 这个函数,可以避免过多的文件的加载

;;; my-subdirs --- load directory settings
;;; Commentary:

;; -*- Emacs-Lisp -*-

;; Time-stamp: <2015-03-10 19:38:56 Administrator>
;;; code:


(defun my-add-subdirs-to-load-path (dir)
  "把(as DIR)的所有子目录都加到`load-path'里面."
  (interactive)
  (let ((default-directory (concat dir "/")))
    (add-to-list 'load-path dir)
    (if (fboundp 'normal-top-level-add-subdirs-to-load-path)
        (normal-top-level-add-subdirs-to-load-path))))

;; Fix bug of `normal-top-level-add-subdirs-to-load-path'
;; which can not add directory which name end with ".elc?"
;; copy from emacs23 startup.el and modify it
(defun normal-top-level-add-subdirs-to-load-path ()
  "Add all subdirectories of current directory to `load-path'.
More precisely, this uses only the subdirectories whose names
start with letters or digits; it excludes any subdirectory named `RCS'
or `CVS', and any subdirectory that contains a file named `.nosearch'."
  (let (dirs
        attrs
        (pending (list default-directory)))
    ;; This loop does a breadth-first tree walk on DIR's subtree,
    ;; putting each subdir into DIRS as its contents are examined.
    (while pending
      (push (pop pending) dirs)
      (let* ((this-dir (car dirs))
             (contents (directory-files this-dir))
             (default-directory this-dir)
             (canonicalized (if (fboundp 'untranslated-canonical-name)
                                (untranslated-canonical-name this-dir))))
        ;; The Windows version doesn't report meaningful inode
        ;; numbers, so use the canonicalized absolute file name of the
        ;; directory instead.
        (setq attrs (or canonicalized
                        (nthcdr 10 (file-attributes this-dir))))
        (unless (member attrs normal-top-level-add-subdirs-inode-list)
          (push attrs normal-top-level-add-subdirs-inode-list)
          (dolist (file contents)
            ;; The lower-case variants of RCS and CVS are for DOS/Windows.
            (unless (member file '("." ".." "RCS" "CVS" "rcs" "cvs"))
              (when (and (string-match "\\`[[:alnum:]]" file)
                         ;; Avoid doing a `stat' when it isn't necessary
                         ;; because that can cause trouble when an NFS server
                         ;; is down.
                         (file-directory-p file))
                (let ((expanded (expand-file-name file)))
                  (unless (file-exists-p (expand-file-name ".nosearch"
                                                           expanded))
                    (setq pending (nconc pending (list expanded)))))))))))
    (if (equal window-system 'w32)
        (setq load-path (append (nreverse dirs) load-path))
      (normal-top-level-add-to-load-path (cdr (nreverse dirs))))))

(provide 'my-subdirs)
;;; my-subdirs.el ends here

然后在 .emacs 中只用调用这几个函数来加载自己定义的目录即可

;; path config
(defconst my-emacs-path "~/.emacs.d/" "My Emacs init path.")

(defconst my-emacs-lisps-path (concat my-emacs-path "lisps/") "Download lisps path.")

(defconst my-emacs-my-lisps-path (concat my-emacs-path "my-lisps/") "My lisps path.")

(defconst my-emmacs-templates-path (concat my-emacs-path "templates/") "Templates path.")

;; load config
(load (concat my-emacs-my-lisps-path "my-subdirs")) ; load my-subdirs.el to load all files in the root directory.

(my-add-subdirs-to-load-path my-emacs-lisps-path)

(my-add-subdirs-to-load-path my-emacs-my-lisps-path)

使用package-install带来的问题

现在emacs想要使用什么插件直接输命令 package-install RET package-make 就可以完成插件的安装了,但是这样这个插件会默认的安装到 ~/.emacs.d/elpa/ 目录下,这样打乱了我们对emacs插件的管理,一般的插件直接将下载的插件文件夹拷贝到 ~/.emacs.d/lisps/ 目录下即可,但是有的插件不能直接拷贝
比如说 js2-mode 这个插件,这个插件在安装的过程中不是简简单单的将文件下载下来就完了,在完成下载之后emacs会自动编译它。编译时是带有路径的,如果我们移动了这个文件的位置,emacs就会在加载这个插件时报错。
解决方法:将插件的目录拷贝到~/.emacs.d/lisps/ 目录下之后,删除 .elc 文件,然后在emacs中编译与 .elc 文件对应的 .el 文件

emacs编译命令:
byte-compile-file

编译时会提示编译生成文件的目录,默认回车就行了,这样就完成了要编译插件的移动和加载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值