contrib/gadict.el
changeset 745 4679b9e75798
parent 744 4d78dbc5a9b0
child 746 73abbe6d4bd3
equal deleted inserted replaced
744:4d78dbc5a9b0 745:4679b9e75798
    36 
    36 
    37 (defun gadict--trim (s)
    37 (defun gadict--trim (s)
    38   "Remove whitespace at the beginning and end of S."
    38   "Remove whitespace at the beginning and end of S."
    39   (gadict--trim-left (gadict--trim-right s)))
    39   (gadict--trim-left (gadict--trim-right s)))
    40 
    40 
       
    41 
       
    42 
    41 (defun gadict--espeak-cleanup (s)
    43 (defun gadict--espeak-cleanup (s)
    42   "Cleanup espeak IPA output."
    44   "Cleanup espeak IPA output."
    43   (replace-regexp-in-string "[\x200D]" "" (gadict--trim s)))
    45   (replace-regexp-in-string "[\x200D]" "" (gadict--trim s)))
       
    46 
       
    47 (defvar gadict-espeak-enabled nil
       
    48   "Is espeak used.")
       
    49 
       
    50 (defvar gadict-espeak-program "espeak")
       
    51 (defvar gadict-espeak-program-ipa-args "-q --ipa=2")
       
    52 ;; "en" "en-gb" "en-us" "en-sc"
       
    53 (defvar gadict-espeak-voices-list '("en-gb" "en-us")
       
    54   "What voices to show. Look to 'espeak --voices' for full list.")
       
    55 (defvar gadict-espeak-default-voice "en-us"
       
    56   "Default voice for espeak. Used in article template.")
       
    57 
       
    58 (defun gadict-espeak-ipa (str &optional voice)
       
    59   (gadict--espeak-cleanup
       
    60    (shell-command-to-string
       
    61     (format "%s %s %s %s"
       
    62             gadict-espeak-program
       
    63             gadict-espeak-program-ipa-args
       
    64             (if (stringp voice) (concat "-v" (shell-quote-argument voice)) "")
       
    65             (shell-quote-argument str)))))
       
    66 
       
    67 (defun gadict-espeak-headline-line (headword)
       
    68   (mapconcat (lambda (voice) (format "%s: %s"
       
    69                                 (propertize voice 'face '(:foreground "red"))
       
    70                                 (gadict-espeak-ipa headword voice)))
       
    71              (if (listp gadict-espeak-voices-list) gadict-espeak-voices-list '(nil))
       
    72              " | "))
       
    73 
       
    74 ;; (defun gadict-espeak-headline-display ()
       
    75 ;;   (interactive)
       
    76 ;;   (message (gadict-espeak-headline-line)))
       
    77 
       
    78 (defvar gadict-espeak-headline-headword nil)
       
    79 
       
    80 (defun gadict-espeak-headline-display ()
       
    81   (when (eq major-mode 'gadict-mode)
       
    82     (let ( (headword (condition-case nil (gadict-nearest-headword) (error nil))) )
       
    83       (unless (eq headword gadict-espeak-headline-headword)
       
    84         (setq gadict-espeak-headline-headword headword)
       
    85         (setq header-line-format (if headword (gadict-espeak-headline-line headword) nil))
       
    86         (force-mode-line-update)))))
       
    87 
       
    88 (defvar gadict-espeak-headline-timer nil)
       
    89 (defun gadict-espeak-headline-enable ()
       
    90   (unless gadict-espeak-headline-timer
       
    91     (setq gadict-espeak-headline-timer (run-with-idle-timer 1 t #'gadict-espeak-headline-display))))
       
    92 (defun gadict-espeak-headline-disable ()
       
    93   (when gadict-espeak-headline-timer
       
    94     (cancel-timer gadict-espeak-headline-timer))
       
    95   (setq gadict-espeak-headline-timer nil))
       
    96 
       
    97 
    44 
    98 
    45 (defconst gadict--pos '("n" "v" "adj" "adv" "pron" "det" "prep" "num" "conj" "int" "phr" "phr.v" "contr" "abbr" "prefix")
    99 (defconst gadict--pos '("n" "v" "adj" "adv" "pron" "det" "prep" "num" "conj" "int" "phr" "phr.v" "contr" "abbr" "prefix")
    46   "Defined parts of speech.")
   100   "Defined parts of speech.")
    47 
   101 
    48 (defconst gadict--art-lang-regex (regexp-opt '("en" "ru" "uk" "la")))
   102 (defconst gadict--art-lang-regex (regexp-opt '("en" "ru" "uk" "la")))
   292         (end-of-line))
   346         (end-of-line))
   293       (re-search-backward "^\\([^ ].*\\)$" limit)
   347       (re-search-backward "^\\([^ ].*\\)$" limit)
   294       (match-string 1)
   348       (match-string 1)
   295       )))
   349       )))
   296 
   350 
   297 (defvar gadict-espeak-enabled nil
       
   298   "Is espeak used.")
       
   299 
       
   300 (defvar gadict-espeak-program "espeak")
       
   301 (defvar gadict-espeak-program-ipa-args "-q --ipa=2")
       
   302 ;; "en" "en-gb" "en-us" "en-sc"
       
   303 (defvar gadict-espeak-voices-list '("en-gb" "en-us")
       
   304   "What voices to show. Look to 'espeak --voices' for full list.")
       
   305 (defvar gadict-espeak-default-voice "en-us"
       
   306   "Default voice for espeak. Used in article template.")
       
   307 
       
   308 (defun gadict-espeak-ipa (str &optional voice)
       
   309   (gadict--espeak-cleanup
       
   310    (shell-command-to-string
       
   311     (format "%s %s %s %s"
       
   312             gadict-espeak-program
       
   313             gadict-espeak-program-ipa-args
       
   314             (if (stringp voice) (concat "-v" (shell-quote-argument voice)) "")
       
   315             (shell-quote-argument str)))))
       
   316 
       
   317 (defun gadict-espeak-headline-line (headword)
       
   318   (mapconcat (lambda (voice) (format "%s: %s"
       
   319                                 (propertize voice 'face '(:foreground "red"))
       
   320                                 (gadict-espeak-ipa headword voice)))
       
   321              (if (listp gadict-espeak-voices-list) gadict-espeak-voices-list '(nil))
       
   322              " | "))
       
   323 
       
   324 ;; (defun gadict-espeak-headline-display ()
       
   325 ;;   (interactive)
       
   326 ;;   (message (gadict-espeak-headline-line)))
       
   327 
       
   328 (defvar gadict-espeak-headline-headword nil)
       
   329 
       
   330 (defun gadict-espeak-headline-display ()
       
   331   (when (eq major-mode 'gadict-mode)
       
   332     (let ( (headword (condition-case nil (gadict-nearest-headword) (error nil))) )
       
   333       (unless (eq headword gadict-espeak-headline-headword)
       
   334         (setq gadict-espeak-headline-headword headword)
       
   335         (setq header-line-format (if headword (gadict-espeak-headline-line headword) nil))
       
   336         (force-mode-line-update)))))
       
   337 
       
   338 (defvar gadict-espeak-headline-timer nil)
       
   339 (defun gadict-espeak-headline-enable ()
       
   340   (unless gadict-espeak-headline-timer
       
   341     (setq gadict-espeak-headline-timer (run-with-idle-timer 1 t #'gadict-espeak-headline-display))))
       
   342 (defun gadict-espeak-headline-disable ()
       
   343   (when gadict-espeak-headline-timer
       
   344     (cancel-timer gadict-espeak-headline-timer))
       
   345   (setq gadict-espeak-headline-timer nil))
       
   346 
       
   347 (defun gadict-setup-keymap ()
   351 (defun gadict-setup-keymap ()
   348   "Setup gadict keymap."
   352   "Setup gadict keymap."
   349   (define-key (current-local-map) [M-return] 'gadict-search)
   353   (define-key (current-local-map) [M-return] 'gadict-search)
   350   (define-key (current-local-map) [S-return] 'gadict-insert-translation)
   354   (define-key (current-local-map) [S-return] 'gadict-insert-translation)
   351   (define-key (current-local-map) [C-return] 'gadict-insert-template-in-order))
   355   (define-key (current-local-map) [C-return] 'gadict-insert-template-in-order))