diff -r dd8b8f805163 -r 59bfd8195afa contrib/gadict.el --- a/contrib/gadict.el Fri Dec 09 00:12:01 2016 +0200 +++ b/contrib/gadict.el Sun Dec 11 02:02:27 2016 +0200 @@ -22,6 +22,22 @@ ;;; Code: +(defun gadict--trim-left (s) + "Remove whitespace at the beginning of S." + (if (string-match "\\`[ \t\n\r]+" s) + (replace-match "" t t s) + s)) + +(defun gadict--trim-right (s) + "Remove whitespace at the end of S." + (if (string-match "[ \t\n\r]+\\'" s) + (replace-match "" t t s) + s)) + +(defun gadict--trim (s) + "Remove whitespace at the beginning and end of S." + (gadict--trim-left (gadict--trim-right s))) + (defconst gadict--pos '("n" "v" "adj" "adv" "pron" "prep" "num" "conj" "int" "phr" "phr.v" "contr" "abbr" "prefix") "Defined parts of speech.") @@ -244,6 +260,70 @@ (split-string gadict-tr ","))) (end-of-line) )) +(defun gadict-nearest-headword () + "Return nearest headword looking upward." + (save-excursion + (let ( (orig (point)) limit headword ) + (re-search-backward "^__$") + (forward-line 1) + (unless (and (eq (char-before) ?\n) (eq (char-after) ?\n)) + (error "Syntax error: there is not empty line after '__'...")) + (forward-line 1) + (when (< orig (point)) + (setq orig (point))) + (setq limit (point)) + (re-search-forward "^$") + (when (< orig (point)) + (goto-char orig) + (end-of-line)) + (re-search-backward "^\\([^ ].*\\)$" limit) + (match-string 1) + ))) + +(defvar gadict-espeak-program "espeak") +(defvar gadict-espeak-program-ipa-args "-q --ipa=2") +(defvar gadict-espeak-voices-list '("en-gb" "en-us") + "What voices to show. Look to 'espeak --voices' for full list.") + +(defun gadict-espeak-ipa (str &optional voice) + (gadict--trim + (shell-command-to-string + (format "%s %s %s %s" + gadict-espeak-program + gadict-espeak-program-ipa-args + (if (stringp voice) (concat "-v" (shell-quote-argument voice)) "") + (shell-quote-argument str))))) + +(defun gadict-espeak-ipa-line (headword) + (mapconcat (lambda (voice) (format "%s: %s" + (propertize voice 'face '(:foreground "red")) + (gadict-espeak-ipa headword voice))) + (if (listp gadict-espeak-voices-list) gadict-espeak-voices-list '(nil)) + " | ")) + +;; (defun gadict-espeak-ipa-display () +;; (interactive) +;; (message (gadict-espeak-ipa-line))) + +(defvar gadict-espeak-ipa-headword nil) + +(defun gadict-espeak-ipa-display () + (when (eq major-mode 'gadict-mode) + (let ( (headword (gadict-nearest-headword)) ) + (unless (eq headword gadict-espeak-ipa-headword) + (setq gadict-espeak-ipa-headword headword) + (setq header-line-format (if headword (gadict-espeak-ipa-line headword) nil)) + (force-window-update))))) + +(defvar gadict-espeak-ipa-timer nil) +(defun gadict-espeak-ipa-enable () + (unless gadict-espeak-ipa-timer + (setq gadict-espeak-ipa-timer (run-with-idle-timer 1 t #'gadict-espeak-ipa-display)))) +(defun gadict-espeak-ipa-disable () + (when gadict-espeak-ipa-timer + (cancel-timer gadict-espeak-ipa-timer)) + (setq gadict-espeak-ipa-timer nil)) + (defun gadict-setup-keymap () "Setup gadict keymap." (define-key (current-local-map) [M-return] 'gadict-search)