魔改了一个纯elisp的stardict插件...

我用file命令查看idx文件是zlib压缩文件,但是用gzip和pigz都解压失败,stardict3的文件标准我看不太懂,目前不知道该怎么搞。。。我怀疑是你使用的字典文件的idx文件有问题,换个字典试试?

我试了朗道汉英和新华字典都不行。直接把idx文件在emacs 里打开然后recode 到utf 8 汉字就会正确地显示出来,但是不知道为什么在elisp里就不行。。

原来只要在stardict-open-1里面用utf-8解码一下就可以了,只需要改一行代码:

(let (p word offset size)
            (re-search-forward "\\([^\x00]+?\\)\x00" nil t)
            (setq p (point))
-           (setq word (match-string 1))
+           (setq word (decode-coding-string (encode-coding-string (match-string 1) 'no-conversion) 'utf-8))
            (setq offset
                  (stardict-str2int
                   (buffer-substring-no-properties p
                                                   (+ p idx-offset-bytes))))
            (setq size
                  (stardict-str2int
                   (buffer-substring-no-properties (+ p idx-offset-bytes)
                                                   (+ p idx-offset-bytes 4))))

我用朗道汉英词典试过了没问题

2 个赞

优秀,太棒了,感谢哈!

兄弟,有没有人说过你很优秀

基于stardict写了个类sdcv.el的代码,

(defvar my-dict-buffer-name "*MYDICT*"
  "The buffer buffer of my dictionary lookup.")

;; English => Chinese
(defvar my-dict-simple
  '("~/.stardict/dic/stardict-langdao-ec-gb-2.4.2" "langdao-ec-gb"))
;; WordNet English => English
(defvar my-dict-complete
  '("~/.stardict/dic/stardict-dictd_www.dict.org_wn-2.4.2" "dictd_www.dict.org_wn"))

(defvar my-dict-simple-cache nil "Internal variable.")
(defvar my-dict-complete-cache nil "Internal variable.")

(defun my-dict-prompt-input ()
  "Prompt input for translate."
  (let* ((word (if mark-active
                   (buffer-substring-no-properties (region-beginning)
                                                   (region-end))
                 (thing-at-point 'word))))
    (setq word (read-string (format "Word (%s): " (or word ""))
                            nil nil
                            word))
    (if word (downcase word))))

(defun my-dict-quit-window ()
  "Quit window."
  (interactive)
  (quit-window t))

(defmacro my-dict-search-detail (dict cache)
  "Return word's definition with DICT, CACHE."
  `(let* ((word (my-dict-prompt-input)))
     (when word
       (unless (featurep 'stardict) (require 'stardict))
       (unless ,cache
         (setq ,cache
               (stardict-open (nth 0 ,dict) (nth 1 ,dict) t)))
       (stardict-lookup ,cache word))))

(defun my-dict-complete-definition ()
  "Show dictionary lookup in buffer."
  (interactive)
  (let* ((def (my-dict-search-detail my-dict-complete my-dict-complete-cache))
         buf
         win)
    (when def
      (setq buf (get-buffer-create my-dict-buffer-name))
        (with-current-buffer buf
          (setq buffer-read-only nil)
          (erase-buffer)
          (insert def)
          (goto-char (point-min))

          ;; quit easily
          (local-set-key (kbd "q") 'my-dict-quit-window)
          (when (and (boundp 'evil-mode) evil-mode)
            (evil-local-set-key 'normal "q" 'my-dict-quit-window)))
        (unless (eq (current-buffer) buf)
          (if (null (setq win (get-buffer-window buf)))
              (switch-to-buffer-other-window buf)
            (select-window win))))))

(defun my-dict-simple-definition ()
  "Show dictionary lookup in popup."
  (interactive)
  (let* ((def (my-dict-search-detail my-dict-simple my-dict-simple-cache)))
    (when def
      (unless (featurep 'popup) (require 'popup))
      (popup-tip def))))

5 个赞