1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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