Emacs:.emacs 中延迟加载模式的最佳实践? [英] Emacs: Best-practice for lazy loading modes in .emacs?

查看:24
本文介绍了Emacs:.emacs 中延迟加载模式的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到相关文件扩展名时,是否有关于延迟加载模式的最佳实践?

此时我安装了大约 25 种不同的 Emacs 模式,并且启动变得缓慢.例如,尽管准备好 clojure-mode 很好,但我很少使用它,而且我想完全避免加载它,除非我打开一个扩展名为 .clj 的文件.这种懒惰的要求"功能似乎是一般模式配置的正确方法..

我在网上找不到任何东西,所以我自己尝试了一下.

代替:

(需要 'clojure-mode)(需要 'tpl 模式)

我有这个:

(defun lazy-require (ext 模式)(添加钩子'查找文件钩子`(拉姆达()(当(和(字符串缓冲文件名)(字符串匹配 (concat "\." ,ext "\'") 缓冲区文件名))(需要(引用,模式))(,模式)))))(懒惰需要大豆"'大豆模式​​)(lazy-require "tpl" 'tpl-mode)

这似乎有效(我是 elisp 新手,因此欢迎发表评论!),但我对在网上找不到有关此主题的任何文章感到不安.这是一个合理的方法吗?

解决方案

你想要的设施叫做 自动加载.clojure-mode 源文件 clojure-mode.el 包括一个评论如何安排:

<预>;;将这些行添加到您的 .emacs 中:;;(autoload 'clojure-mode "clojure-mode" "Clojure 的主要模式" t);;(添加到列表 'auto-mode-alist '("\.clj$" .clojure-mode))

Is there a best practice around lazily loading modes when encountering a relevant file extension?

At this point I have roughly 25 different Emacs modes installed, and startup has become slow. For example, although it's great to have clojure-mode at the ready, I rarely use it, and I want to avoid loading it at all unless I open a file with extension .clj. Such a "lazy require" functionality seems like the right way do mode configuration in general..

I found nothing online, so I've taken a crack at it myself.

Instead of:

(require 'clojure-mode)
(require 'tpl-mode) 

I have this:

(defun lazy-require (ext mode)
  (add-hook
   'find-file-hook
   `(lambda ()
      (when (and (stringp buffer-file-name)
                 (string-match (concat "\." ,ext "\'") buffer-file-name))
        (require (quote ,mode))
        (,mode)))))

(lazy-require "soy" 'soy-mode)
(lazy-require "tpl" 'tpl-mode)

This seems to work (I'm an elisp newbie so comments are welcome!), but I'm unnerved about finding nothing written about this topic online. Is this a reasonable approach?

解决方案

The facility you want is called autoloading. The clojure-mode source file, clojure-mode.el, includes a comment for how to arrange this:

;;     Add these lines to your .emacs:
;;       (autoload 'clojure-mode "clojure-mode" "A major mode for Clojure" t)
;;       (add-to-list 'auto-mode-alist '("\.clj$" . clojure-mode))

这篇关于Emacs:.emacs 中延迟加载模式的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆