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
编译时会提示编译生成文件的目录,默认回车就行了,这样就完成了要编译插件的移动和加载