From f8d92bae3251c172bfda2f570da2886112018e60 Mon Sep 17 00:00:00 2001 From: Cytrogen Date: Wed, 11 Mar 2026 19:24:17 -0400 Subject: [PATCH] =?UTF-8?q?feat(dictionary):=20=E6=B7=BB=E5=8A=A0=20sdcv?= =?UTF-8?q?=20=E7=A6=BB=E7=BA=BF=E8=AF=8D=E5=85=B8=E5=92=8C=20GoldenDict?= =?UTF-8?q?=20=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - sdcv(StarDict CLI):快速查词浮窗 + 详细查词 buffer - 自动扫描 ~/.stardict/dic/ 下的词典文件 - GoldenDict 外部程序集成:光标查词 + 手动输入查词 --- config/pkg-dictionary.el | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 config/pkg-dictionary.el diff --git a/config/pkg-dictionary.el b/config/pkg-dictionary.el new file mode 100644 index 0000000000000000000000000000000000000000..976f6428c04e8bc84249cf7b49690bca707dec18 --- /dev/null +++ b/config/pkg-dictionary.el @@ -0,0 +1,66 @@ +;;; pkg-dictionary.el --- Offline dictionary (sdcv + StarDict + GoldenDict) -*- lexical-binding: t -*- + +;;; Code: + +;; 依赖:posframe(浮动提示框) +(unless (package-installed-p 'posframe) + (package-refresh-contents) + (package-install 'posframe)) + +;; 自动检测 ~/.stardict/dic/ 下所有词典 +(defun my/sdcv-detect-dictionaries () + "扫描 `~/.stardict/dic/' 下所有 .ifo 文件,提取 bookname,返回词典名列表。" + (let ((dic-dir (expand-file-name "~/.stardict/dic")) + (names '())) + (when (file-directory-p dic-dir) + (dolist (ifo-file (directory-files-recursively dic-dir "\\.ifo$")) + (with-temp-buffer + (insert-file-contents ifo-file) + (when (re-search-forward "^bookname=\\(.+\\)$" nil t) + (push (match-string 1) names))))) + (nreverse names))) + +;; 加载 sdcv(从 site-lisp/sdcv/) +(let ((sdcv-path (expand-file-name "site-lisp/sdcv" user-emacs-directory))) + (if (file-directory-p sdcv-path) + (progn + (add-to-list 'load-path sdcv-path) + (require 'sdcv) + (setq sdcv-dictionary-data-dir (expand-file-name "~/.stardict/dic")) + (let ((dicts (my/sdcv-detect-dictionaries))) + (if dicts + (progn + (setq sdcv-dictionary-simple-list dicts) + (setq sdcv-dictionary-complete-list dicts)) + (setq sdcv-dictionary-simple-list '("简明英汉字典增强版")) + (setq sdcv-dictionary-complete-list '("简明英汉字典增强版")))) + (setq sdcv-tooltip-timeout 5)) + (message "pkg-dictionary: sdcv not found in site-lisp/sdcv, skipping. Run: git clone https://github.com/manateelazycat/sdcv.git ~/.emacs.d/site-lisp/sdcv"))) + +;; GoldenDict 集成 +(defun my/goldendict-search-at-point () + "取光标处单词,调用 GoldenDict 查询。" + (interactive) + (let ((word (thing-at-point 'word t))) + (if word + (start-process "goldendict" nil "goldendict" word) + (message "No word at point")))) + +(defun my/goldendict-search-input () + "手动输入单词,调用 GoldenDict 查询。" + (interactive) + (let ((word (read-string "GoldenDict: "))) + (unless (string-empty-p word) + (start-process "goldendict" nil "goldendict" word)))) + +;; 手动触发词典转换 +(defun my/convert-dictionaries () + "调用 convert-dictionaries.sh 转换 mdx 词典为 StarDict 格式。" + (interactive) + (let ((script (expand-file-name "~/.local/bin/convert-dictionaries.sh"))) + (if (file-executable-p script) + (async-shell-command script "*dict-convert*") + (message "Conversion script not found: %s" script)))) + +(provide 'pkg-dictionary) +;;; pkg-dictionary.el ends here