Emacs:.emacs 中延迟加载模式的最佳实践?

发布于 2024-11-27 23:16:04 字数 709 浏览 0 评论 0原文

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

此时我已经安装了大约 25 种不同的 Emacs 模式,并且启动变得很慢。例如,虽然准备好 clojure 模式很好,但我很少使用它,并且我想完全避免加载它,除非我打开扩展名为 .clj 的文件。这种“惰性需求”功能似乎是一般模式配置的正确方法。

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

而不是:

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

我有这个:

(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)

这似乎有效(我是一个 elisp 新手,所以欢迎评论!),但我对在网上找不到任何关于这个主题的文章感到不安。这是一个合理的做法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2024-12-04 23:16:04

您想要的工具称为自动加载clojure-mode 源文件 clojure-mode.el 包含评论如何安排:

;;     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))

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))
剩一世无双 2024-12-04 23:16:04

这是一种方式,

(provide 'my-slime)
(eval-after-load "slime"
  '(progn
     (setq slime-lisp-implementations
           '((sbcl ("/usr/bin/sbcl"))
             (clisp ("/usr/bin/clisp")))
           common-lisp-hyperspec-root "/home/sujoy/documents/hyperspec/")
     (slime-setup '(slime-asdf
                    slime-autodoc
                    slime-editing-commands
                    slime-fancy-inspector
                    slime-fontifying-fu
                    slime-fuzzy
                    slime-indentation
                    slime-mdot-fu
                    slime-package-fu
                    slime-references
                    slime-repl
                    slime-sbcl-exts
                    slime-scratch
                    slime-xref-browser))
     (slime-autodoc-mode)
     (setq slime-complete-symbol*-fancy t)
     (setq slime-complete-symbol-function 'slime-fuzzy-complete-symbol)
(add-hook 'lisp-mode-hook (lambda () (slime-mode t)))))

(require 'slime)

同时,

;; slime mode
(autoload 'slime "my-slime" "Slime mode." t)
(autoload 'slime-connect "my-slime" "Slime mode." t)

This is one way,

(provide 'my-slime)
(eval-after-load "slime"
  '(progn
     (setq slime-lisp-implementations
           '((sbcl ("/usr/bin/sbcl"))
             (clisp ("/usr/bin/clisp")))
           common-lisp-hyperspec-root "/home/sujoy/documents/hyperspec/")
     (slime-setup '(slime-asdf
                    slime-autodoc
                    slime-editing-commands
                    slime-fancy-inspector
                    slime-fontifying-fu
                    slime-fuzzy
                    slime-indentation
                    slime-mdot-fu
                    slime-package-fu
                    slime-references
                    slime-repl
                    slime-sbcl-exts
                    slime-scratch
                    slime-xref-browser))
     (slime-autodoc-mode)
     (setq slime-complete-symbol*-fancy t)
     (setq slime-complete-symbol-function 'slime-fuzzy-complete-symbol)
(add-hook 'lisp-mode-hook (lambda () (slime-mode t)))))

(require 'slime)

along with,

;; slime mode
(autoload 'slime "my-slime" "Slime mode." t)
(autoload 'slime-connect "my-slime" "Slime mode." t)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文