Skip to content

Commit

Permalink
use stardict instead sdcv
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Bin committed Apr 14, 2023
1 parent 4947379 commit fce9fd9
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 482 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
projects
request/
.cache
.depend
Expand Down
5 changes: 3 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ But my =w3m= based utilities can open video/audio/image with the help of =w3m=.
**** js-beautify
- Beautify javascript code
- Install [[http://pip.readthedocs.org/en/stable/installing/][pip]] through OS package manager, then =pip install jsbeautifier=
**** sdcv (console version of StarDict)
- Required by =sdcv.el=
**** StarDict dictionaries
- Cli program is not needed
- Required by =stardict.el=
- Run =curl http://pkgs.fedoraproject.org/repo/pkgs/stardict-dic/stardict-dictd_www.dict.org_wn-2.4.2.tar.bz2/f164dcb24b1084e1cfa2b1cb63d590e6/stardict-dictd_www.dict.org_wn-2.4.2.tar.bz2 | tar jx -C ~/.stardict/dic= and =curl https://src.fedoraproject.org/repo/pkgs/stardict-dic/stardict-langdao-ec-gb-2.4.2.tar.bz2/41a71f5b3952709746dd7e52cf155b8b/stardict-langdao-ec-gb-2.4.2.tar.bz2 | tar jx -C ~/.stardict/dic= to install English dictionaries
**** [[https://github.com/BurntSushi/ripgrep][ripgrep]]
- Optionally used by =M-x counsel-etags-grep= to search text in files
Expand Down
1 change: 1 addition & 0 deletions init.el
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
(require-init 'init-essential)
;; tools nice to have
(require-init 'init-misc t)
(require-init 'init-dictionary t)
(require-init 'init-emms t)

(require-init 'init-emacs-w3m t)
Expand Down
3 changes: 0 additions & 3 deletions lisp/init-autoload.el
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
(autoload 'wucuo-start "wucuo" "" t nil)
(autoload 'markdown-mode "markdown-mode" "Mode for editing Markdown documents" t)
(autoload 'csv-mode "csv-mode" "Major mode for comma-separated value files." t)
(autoload 'sdcv-search-pointer "sdcv" "show word explanation in buffer" t)
(autoload 'sdcv-search-input+ "sdcv" "show word explanation in tooltip" t)
(autoload 'sdcv-search-input "sdcv" "show word explanation in tooltip" t)
(autoload 'elpamr-create-mirror-for-installed "elpa-mirror" "" t)
(autoload 'org2nikola-export-subtree "org2nikola" "" t)
(autoload 'org2nikola-rerender-published-posts "org2nikola" "" t)
Expand Down
74 changes: 74 additions & 0 deletions lisp/init-dictionary.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
;; -*- coding: utf-8; lexical-binding: t; -*-

(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))))
(read-string (format "Word (%s): " (or word ""))
nil nil
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))))

(provide 'init-dictionary)
;;; init-dictionary.el ends here
4 changes: 2 additions & 2 deletions lisp/init-evil.el
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,8 @@ If N > 0 and in js, only occurrences in current N lines are renamed."
"w" 'mybigword-big-words-in-current-window
"s" 'avy-goto-word-or-subword-1
"a" 'avy-goto-char-timer
"db" 'sdcv-search-input ; details
"dt" 'sdcv-search-input+ ; summary
"db" 'my-dict-complete-definition ; details
"dt" 'my-dict-simple-definition ; summary
"dd" 'my-lookup-dict-org
"mm" 'my-lookup-doc-in-man
"gg" 'my-w3m-generic-search
Expand Down
4 changes: 2 additions & 2 deletions lisp/init-hydra.el
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,8 @@ _s_ StackOverflow _d_ dict.org
_h_ Code
_m_ Man
"
("b" sdcv-search-input)
("t" sdcv-search-input+)
("b" my-dict-complete-definition)
("t" my-dict-simple-definition)
("d" my-lookup-dict-org)
("g" my-w3m-generic-search)
("f" my-w3m-search-financial-dictionary)
Expand Down
26 changes: 9 additions & 17 deletions lisp/init-misc.el
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,6 @@ In each rule, 1st item is default directory, 2nd item is the shell command.")
(if previous "prev" "next")))))))
;; }}

;; {{start dictionary lookup
(with-eval-after-load 'sdcv
;; English => Chinese
(setq sdcv-dictionary-simple-list '("朗道英汉字典5.0"))
;; WordNet English => English
(setq sdcv-dictionary-complete-list '("WordNet")))
;; }}

;; ANSI-escape coloring in compilation-mode
;; {{ http://stackoverflow.com/questions/13397737/ansi-coloring-in-compilation-mode
(ignore-errors
Expand Down Expand Up @@ -906,7 +898,7 @@ might be bad."
;; go to end of word to workaround `nov-mode' bug
(forward-word)
(forward-char -1)
(sdcv-search-input (thing-at-point 'word))))
(my-dict-complete-definition)))
(local-set-key (kbd ";") 'my-hydra-ebook/body)
(local-set-key (kbd "w") 'mybigword-big-words-in-current-window))
(add-hook 'nov-mode-hook 'nov-mode-hook-setup)
Expand Down Expand Up @@ -998,20 +990,20 @@ might be bad."
(setq eldoc-echo-area-use-multiline-p t))
;;}}

;; {{ use sdcv dictionary to find big word definition
(defvar my-sdcv-org-head-level 2)
(defun my-sdcv-format-bigword (word zipf)
"Format WORD and ZIPF using sdcv dictionary."
;; {{ use dictionary to find big word definition
(defvar my-dict-org-head-level 2)
(defun my-dict-format-bigword (word zipf)
"Format WORD and ZIPF."
(let* (rlt def)
(local-require 'sdcv)
(local-require 'stardict)
;; 2 level org format
(condition-case nil
(progn
(setq def (sdcv-search-with-dictionary word sdcv-dictionary-complete-list) )
(setq def (my-dict-search-detail my-dict-complete my-dict-complete-cache nil) )
(setq def (replace-regexp-in-string "^-->.*" "" def))
(setq def (replace-regexp-in-string "[\n\r][\n\r]+" "" def))
(setq rlt (format "%s %s (%s)\n%s\n"
(make-string my-sdcv-org-head-level ?*)
(make-string my-dict-org-head-level ?*)
word
zipf
def)))
Expand All @@ -1022,7 +1014,7 @@ might be bad."
"Look up big word definitions."
(interactive)
(local-require 'mybigword)
(let* ((mybigword-default-format-function 'my-sdcv-format-bigword))
(let* ((mybigword-default-format-function 'my-dict-format-bigword))
(mybigword-show-big-words-from-current-buffer)))
;; }}

Expand Down
Loading

0 comments on commit fce9fd9

Please sign in to comment.