Emacs 从零开始写配置: Difference between revisions

From 清冽之泉
Jump to navigation Jump to search
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 9: Line 9:
7 Emacs 完全启动
7 Emacs 完全启动
</pre>
</pre>
== load-path ==
设想的插件管理方式:自动追加插件目录,在插件出问题时可以全删干净,不影响早期有效配置。
# 新建一个目录,当自留地
# 把自留地加入 load-path,追加而不抢先
# 试用插件时,在自留地里,创建插件目录
# 插件目录也加入 load-path,追加而不抢先。一级,不递归
# provide 'name 及 require 'name
# 试用调整,满意就留,不满意就删该插件目录
<syntaxhighlight lang="elisp" line>
;; 个人自留地,根目录
(defvar qlzq-personal-dir (expand-file-name "qlzq-emacs-config/" user-emacs-directory))
;; 确保存在
(unless (file-directory-p qlzq-personal-dir)
  (make-directory qlzq-personal-dir t))
;; 把根目录加入 load-path
(add-to-list 'load-path qlzq-personal-dir t)
;; 把每个一级子目录加入 load-path(只加入含 .el/.elc 的目录)
(dolist (sub (directory-files qlzq-personal-dir t "^[^.]" t))
  (when (and (file-directory-p sub)
            (cl-some (lambda (f) (string-match-p "\\.el\\(c\\)?\\'" f))
                      (directory-files sub)))
    (add-to-list 'load-path sub t)))
</syntaxhighlight>


== 平台 ==
== 平台 ==
根据不同系统平同,自动加载配置。
<syntaxhighlight lang="elisp" line>
;;; init-qlzq-platforms.el --- detect platform and load per-platform file
(defvar my-platform nil
  "自定义变量.")


== load-path ==
(defun my-detect-platform ()
好的 load-path 方式为,自动扫描插件目录,在插件出问题时可以全删干净,不影响早期有效配置。
  "自定义函数."
  (cond
  ((eq system-type 'windows-nt) 'windows)
  ((eq system-type 'darwin) 'macos)
  ((and (eq system-type 'gnu/linux) (display-graphic-p)) 'linux-gui)
  ((and (eq system-type 'gnu/linux) (not (display-graphic-p))) 'linux-headless)
  (t 'unknown)))
 
(setq my-platform (or (getenv "EMACS_PLATFORM") ; allow override env var
                      (my-detect-platform)))
 
(let ((dir (expand-file-name "~/.emacs.d/qlzq-emacs-config/platforms")))
  (unless (file-exists-p dir)
    (make-directory dir t)))
 
;; 指定配置文件
(let* ((dir (expand-file-name "qlzq-emacs-config/platforms/" user-emacs-directory))
      (file (pcase my-platform
              ('windows  (expand-file-name "windows.el" dir))
              ('linux-gui (expand-file-name "debian-xfce.el" dir))
              ('linux-headless (expand-file-name "debian-server.el" dir))
              (_ (expand-file-name "default.el" dir)))))
  (when (file-exists-p file)
    (load-file file)))
 
;; Make this callable for frames (important for daemon mode)
(defun my-apply-platform-settings-to-frame (frame)
  "Apply frame-local settings when FRAME is created."
  (with-selected-frame frame
    ;; call a hook so each platform file can add handlers
    (run-hooks 'my-platform-frame-hook)))
 
(add-hook 'after-make-frame-functions #'my-apply-platform-settings-to-frame)
 
;; Also run for initial frame (or when starting in graphical)
(when (or (display-graphic-p) (daemonp))
  (my-apply-platform-settings-to-frame (selected-frame)))
 
(provide 'init-qlzq-platforms)
</syntaxhighlight>

Latest revision as of 15:07, 19 March 2026

据 ChatGPT 所说,Emacs 启动顺序为:

1 启动 C 核心
2 初始化 load-path
3 加载 site-start.el(系统级配置)
4 加载 early-init.el(用户早期配置)
5 初始化 package.el
6 加载 init.el 或 .emacs(用户配置)
7 Emacs 完全启动

load-path

设想的插件管理方式:自动追加插件目录,在插件出问题时可以全删干净,不影响早期有效配置。

  1. 新建一个目录,当自留地
  2. 把自留地加入 load-path,追加而不抢先
  3. 试用插件时,在自留地里,创建插件目录
  4. 插件目录也加入 load-path,追加而不抢先。一级,不递归
  5. provide 'name 及 require 'name
  6. 试用调整,满意就留,不满意就删该插件目录
;; 个人自留地,根目录
(defvar qlzq-personal-dir (expand-file-name "qlzq-emacs-config/" user-emacs-directory))

;; 确保存在
(unless (file-directory-p qlzq-personal-dir)
  (make-directory qlzq-personal-dir t))

;; 把根目录加入 load-path
(add-to-list 'load-path qlzq-personal-dir t)

;; 把每个一级子目录加入 load-path(只加入含 .el/.elc 的目录)
(dolist (sub (directory-files qlzq-personal-dir t "^[^.]" t))
  (when (and (file-directory-p sub)
             (cl-some (lambda (f) (string-match-p "\\.el\\(c\\)?\\'" f))
                      (directory-files sub)))
    (add-to-list 'load-path sub t)))

平台

根据不同系统平同,自动加载配置。

;;; init-qlzq-platforms.el --- detect platform and load per-platform file

(defvar my-platform nil
  "自定义变量.")

(defun my-detect-platform ()
  "自定义函数."
  (cond
   ((eq system-type 'windows-nt) 'windows)
   ((eq system-type 'darwin) 'macos)
   ((and (eq system-type 'gnu/linux) (display-graphic-p)) 'linux-gui)
   ((and (eq system-type 'gnu/linux) (not (display-graphic-p))) 'linux-headless)
   (t 'unknown)))

(setq my-platform (or (getenv "EMACS_PLATFORM") ; allow override env var
                      (my-detect-platform)))

(let ((dir (expand-file-name "~/.emacs.d/qlzq-emacs-config/platforms")))
  (unless (file-exists-p dir)
    (make-directory dir t)))

;; 指定配置文件
(let* ((dir (expand-file-name "qlzq-emacs-config/platforms/" user-emacs-directory))
       (file (pcase my-platform
               ('windows   (expand-file-name "windows.el" dir))
               ('linux-gui (expand-file-name "debian-xfce.el" dir))
               ('linux-headless (expand-file-name "debian-server.el" dir))
               (_ (expand-file-name "default.el" dir)))))
  (when (file-exists-p file)
    (load-file file)))

;; Make this callable for frames (important for daemon mode)
(defun my-apply-platform-settings-to-frame (frame)
  "Apply frame-local settings when FRAME is created."
  (with-selected-frame frame
    ;; call a hook so each platform file can add handlers
    (run-hooks 'my-platform-frame-hook)))

(add-hook 'after-make-frame-functions #'my-apply-platform-settings-to-frame)

;; Also run for initial frame (or when starting in graphical)
(when (or (display-graphic-p) (daemonp))
  (my-apply-platform-settings-to-frame (selected-frame)))

(provide 'init-qlzq-platforms)