Files
workspace/init.el
e2hang d8251f02a1 emacs
2026-01-09 11:53:59 +08:00

291 lines
11 KiB
EmacsLisp
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
;;; --- 1. 包管理与镜像源 ---
(require 'package)
(setq package-archives '(("gnu" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
("nongnu" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/nongnu/")
("melpa" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")))
(package-initialize)
;; 自动安装基础包
(unless package-archive-contents (package-refresh-contents))
(dolist (pkg '(use-package company orderless rust-mode cargo))
(unless (package-installed-p pkg) (package-install pkg)))
(with-eval-after-load 'ox
(require 'ox-md))
;; 记得下载doom-themes: M-x package-install doom-themes
;; 或者是 timu-macos t
;; 1. 先设置变体为浅色
;;(setq timu-macos-variant "light")
;; 2. 再加载主题
;;(use-package timu-macos-theme
;; :ensure t
;; :config
;; (load-theme 'timu-macos t))
;;(customize-set-variable 'timu-macos-flavour "light")
;;(load-theme 'modus-operandi-tinted t)
(load-theme 'doom-snazzy t)
;; 关闭工具栏 (带图标的那一栏)
(tool-bar-mode -1)
;; 如果你觉得菜单栏File, Edit...)也占地方,可以顺便关掉
;;(menu-bar-mode -1)
;; 如果你在图形界面下,滚动条也可以关掉
(scroll-bar-mode -1)
;; 性能优化:提升 LSP 通讯读取限制
(setq read-process-output-max (* 1024 1024))
(setq gc-cons-threshold (* 100 1024 1024))
;;; --- 2. 基础 UI 与编码设置 ---
(set-language-environment "UTF-8")
(setq inhibit-startup-screen t)
(global-display-line-numbers-mode t)
(electric-pair-mode t)
(set-face-attribute 'default nil :family "Consolas" :height 160)
(dolist (charset '(kana han symbol cjk-misc bopomofo))
(set-fontset-font (frame-parameter nil 'font)
charset (font-spec :family "Microsoft YaHei"))) ; 中文
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
;; 设置剪贴板编码为 utf-8
(set-selection-coding-system 'utf-8)
;; 更加全面的编码设置(推荐)
(set-language-environment "UTF-8")
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
(if (eq system-type 'windows-nt)
;; Windows 专用设置
(set-selection-coding-system 'utf-16le-dos)
;; Linux/macOS 专用设置
(set-selection-coding-system 'utf-8))
;; 备份设置
(setq backup-directory-alist `(("." . "~/.emacs.d/backups/")))
(setq vc-make-backup-files t)
;;; --- 3. Company 补全配置 ---
(setq company-minimum-prefix-length 2)
(setq company-idle-delay 0.1)
(setq company-show-quick-access t)
(add-hook 'after-init-hook 'global-company-mode)
(with-eval-after-load 'company
(define-key company-active-map (kbd "RET") nil)
(define-key company-active-map (kbd "<return>") nil)
(define-key company-active-map (kbd "TAB") #'company-complete-selection)
(define-key company-active-map [tab] #'company-complete-selection))
;;; --- 4. Eglot (LSP) 核心配置 ---
(use-package eglot
:ensure nil
:hook ((python-mode . eglot-ensure)
(c-mode . eglot-ensure)
(c++-mode . eglot-ensure)
(rust-mode . eglot-ensure)
(go-mode . eglot-ensure)
(java-mode . eglot-ensure))
:config
(setq eglot-autoshutdown t)
(setq eglot-format-on-save nil)
(setq flymake-no-changes-timeout 2.0)
;; 禁用不必要的能力,保持响应速度
(add-to-list 'eglot-ignored-server-capabilities :inlayHintProvider)
(add-to-list 'eglot-ignored-server-capabilities :documentOnTypeFormattingProvider)
;; 确保保存时不自动格式化
(add-hook 'eglot-managed-mode-hook
(lambda () (remove-hook 'before-save-hook 'eglot-format-buffer t))))
;;; --- 5. 编程语言专属设置 ---
;; ** Rust **
;;1 rust-analyzer: 这是 LSP 的大脑。
;;安装命令rustup component add rust-analyzer
;;2 clippy: 更好的静态检查。
;;安装命令rustup component add clippy
;;rustfmt: 虽然你关闭了自动格式化,但建议保留以备手动调用。
;;安装命令rustup component add rustfmt
(use-package rust-mode
:ensure t
:mode "\\.rs\\'"
:config
(setq rust-format-on-save nil)
(add-hook 'rust-mode-hook (lambda ()
(setq-local rust-indent-offset 4)))
;; 配置 rust-analyzer 细节
(with-eval-after-load 'eglot
(add-to-list 'eglot-server-programs
`(rust-mode . ("rust-analyzer" :initializationOptions
(:checkOnSave t;
:check (:command "clippy")
:procMacro (:enable t)
:cargo (:loadOutDirsFromCheck t)))))))
(use-package cargo
:ensure t
:hook (rust-mode . cargo-minor-mode))
;; ** Python **
(use-package python
:ensure nil
:config
(setq python-indent-offset 4))
;; ** C/C++ **
(defun my-c-mode-setup ()
(c-set-style "k&r")
(setq c-basic-offset 4)
(c-set-offset 'substatement-open 0)
(c-set-offset 'case-label '+))
(add-hook 'c-mode-hook 'my-c-mode-setup)
(add-hook 'c++-mode-hook 'my-c-mode-setup)
;; ** Java **
(with-eval-after-load 'eglot
(add-to-list 'eglot-server-programs
`(java-mode . ("java"
"-jar" "d:/JavajDK/jdt-language-server-1.9.0-202203031534/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar"
"-configuration" "D:/JavaJDK/jdt-language-server-1.9.0-202203031534/config_win/"
"-data" "~/.emacs.d/cache/jdtls-workspace"))))
;;; --- 6. Org-mode ---
;;; --- 6. Org-mode ---
(with-eval-after-load 'org
(add-hook 'org-mode-hook #'visual-line-mode)
(setq org-startup-with-latex-preview t
org-startup-indented t
org-src-fontify-natively t
org-confirm-babel-evaluate nil)
(org-babel-do-load-languages 'org-babel-load-languages '((C . t) (shell . t) (python . t))))
(with-eval-after-load 'org
;; 1. 开启视觉换行 (视觉上自动折行,不改变文件内容)
(add-hook 'org-mode-hook #'visual-line-mode)
;; 2. 彻底禁用自动填充 (防止按空格时插入硬换行符)
(add-hook 'org-mode-hook (lambda () (auto-fill-mode -1)))
;; 3. 额外保险:即使全局开启了 auto-fill在 org 里也关掉它
(setq-local auto-fill-function nil)
)
;; 允许编辑隐藏区域,它会自动展开而不是报错拦截
(setq org-fold-catch-invisible-edits 'show-and-error)
;; 或者更彻底一点,允许直接修改
(setq org-fold-catch-invisible-edits 'smart)
;;; --- 7. 搜索与便捷功能 ---
(use-package orderless
:ensure t
:custom
(completion-styles '(orderless basic))
(completion-category-overrides '((file (styles basic partial-completion)))))
(use-package projectile
:ensure t
:init (projectile-mode +1)
:bind-keymap ("C-c p" . projectile-command-map))
(use-package vertico
:ensure t
:init (vertico-mode))
(use-package treemacs
:ensure t
:bind ("C-c t" . treemacs-select-window)) ; 按 C-c t 快速跳到文件树
(with-eval-after-load 'treemacs
(treemacs-follow-mode t) ; Tree 自动跟随当前 buffer
(treemacs-filewatch-mode t)) ; 硬盘文件变动时 Tree 自动刷新
(setq treemacs-python-executable "python")
(with-eval-after-load 'treemacs
(setq treemacs-deferred-git-apply t ; 异步处理 Git 状态
treemacs-file-event-delay 2000 ; 延长刷新延迟
treemacs-read-string-input 'from-minibuffer ; 优化输入方式
treemacs-tag-follow-delay 1.5 ; 减慢跟随速度,减少计算
))
(use-package consult
:ensure t
:bind (("C-s" . consult-line) ; 替代默认搜索,带实时预览
("C-x b" . consult-buffer) ; 增强版 buffer 切换
("M-y" . consult-yank-pop) ; 历史粘贴板
;; 修改这里:不要直接 bind "C-c p s"
;; 而是把命令绑定到 projectile 的 map 里
:map projectile-command-map
("s" . consult-ripgrep))) ; 这样你按 C-c p s 就能触发全局搜索了
(use-package marginalia
:ensure t
:init (marginalia-mode))
(global-set-key (kbd "<f5>") (lambda () (interactive) (find-file user-init-file)))
(defun my-toggle-lsp-and-flymake ()
"一键切换智能模式与自由模式"
(interactive)
(if (bound-and-true-p eglot--managed-mode)
(progn
(flymake-mode -1) (company-mode -1)
(when (eglot-current-server) (eglot-shutdown (eglot-current-server)))
(message ">>> [自由模式] LSP & Company 已关闭"))
(progn
(eglot-ensure) (flymake-mode 1) (company-mode 1)
(message ">>> [智能模式] 服务已开启"))))
(global-set-key (kbd "C-<return>") 'my-toggle-lsp-and-flymake)
(use-package which-key
:ensure t
:init (which-key-mode))
(use-package avy
:ensure t
:bind ("C-c s" . avy-goto-char-timer))
;; --- 自动生成配置 (保持在末尾) ---
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(custom-safe-themes
'("f64189544da6f16bab285747d04a92bd57c7e7813d8c24c30f382f087d460a33"
"7771c8496c10162220af0ca7b7e61459cb42d18c35ce272a63461c0fc1336015"
"d97ac0baa0b67be4f7523795621ea5096939a47e8b46378f79e78846e0e4ad3d"
"d481904809c509641a1a1f1b1eb80b94c58c210145effc2631c1a7f2e4a2fdf4"
"720838034f1dd3b3da66f6bd4d053ee67c93a747b219d1c546c41c4e425daf93"
"b7a09eb77a1e9b98cafba8ef1bd58871f91958538f6671b22976ea38c2580755"
"0f1341c0096825b1e5d8f2ed90996025a0d013a0978677956a9e61408fcd2c77"
"f1e8339b04aef8f145dd4782d03499d9d716fdc0361319411ac2efc603249326"
"15cdf123689e3d76ca08c1bcd9ef6d85f37df994d577686b7593c6114a8fa165"
"4594d6b9753691142f02e67b8eb0fda7d12f6cc9f1299a49b819312d6addad1d"
"a9eeab09d61fef94084a95f82557e147d9630fbbb82a837f971f83e66e21e5ad"
"8d3ef5ff6273f2a552152c7febc40eabca26bae05bd12bc85062e2dc224cde9a"
"b5fd9c7429d52190235f2383e47d340d7ff769f141cd8f9e7a4629a81abc6b19"
"8c7e832be864674c220f9a9361c851917a93f921fedb7717b1b5ece47690c098"
"e8ceeba381ba723b59a9abc4961f41583112fc7dc0e886d9fc36fa1dc37b4079"
"42a6583a45e0f413e3197907aa5acca3293ef33b4d3b388f54fa44435a494739"
"9b9d7a851a8e26f294e778e02c8df25c8a3b15170e6f9fd6965ac5f2544ef2a9"
"e4a702e262c3e3501dfe25091621fe12cd63c7845221687e36a79e17cf3a67e0"
"3613617b9953c22fe46ef2b593a2e5bc79ef3cc88770602e7e569bbd71de113b"
"aec7b55f2a13307a55517fdf08438863d694550565dee23181d2ebd973ebd6b8"
"7c3d62a64bafb2cc95cd2de70f7e4446de85e40098ad314ba2291fc07501b70c"
"0325a6b5eea7e5febae709dab35ec8648908af12cf2d2b569bedc8da0a3a81c1"
default))
'(package-selected-packages
'(cargo company consult doom-themes marginalia orderless projectile
rust-mode treemacs vertico)))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)