contrib/gadict.el
changeset 710 59bfd8195afa
parent 652 fa97354a56d3
child 711 d6040290ee78
equal deleted inserted replaced
709:dd8b8f805163 710:59bfd8195afa
    19 ;; File association can be registered by:
    19 ;; File association can be registered by:
    20 ;;
    20 ;;
    21 ;;   (add-to-list 'auto-mode-alist (cons "\\.gadict$" 'gadict-mode))
    21 ;;   (add-to-list 'auto-mode-alist (cons "\\.gadict$" 'gadict-mode))
    22 
    22 
    23 ;;; Code:
    23 ;;; Code:
       
    24 
       
    25 (defun gadict--trim-left (s)
       
    26   "Remove whitespace at the beginning of S."
       
    27   (if (string-match "\\`[ \t\n\r]+" s)
       
    28       (replace-match "" t t s)
       
    29     s))
       
    30 
       
    31 (defun gadict--trim-right (s)
       
    32   "Remove whitespace at the end of S."
       
    33   (if (string-match "[ \t\n\r]+\\'" s)
       
    34       (replace-match "" t t s)
       
    35     s))
       
    36 
       
    37 (defun gadict--trim (s)
       
    38   "Remove whitespace at the beginning and end of S."
       
    39   (gadict--trim-left (gadict--trim-right s)))
    24 
    40 
    25 (defconst gadict--pos '("n" "v" "adj" "adv" "pron" "prep" "num" "conj" "int" "phr" "phr.v" "contr" "abbr" "prefix")
    41 (defconst gadict--pos '("n" "v" "adj" "adv" "pron" "prep" "num" "conj" "int" "phr" "phr.v" "contr" "abbr" "prefix")
    26   "Defined parts of speech.")
    42   "Defined parts of speech.")
    27 
    43 
    28 (defconst gadict--art-lang-regex (regexp-opt '("en" "ru" "uk" "la")))
    44 (defconst gadict--art-lang-regex (regexp-opt '("en" "ru" "uk" "la")))
   242               (insert lang)
   258               (insert lang)
   243               (insert ": \n"))
   259               (insert ": \n"))
   244             (split-string gadict-tr ",")))
   260             (split-string gadict-tr ",")))
   245     (end-of-line) ))
   261     (end-of-line) ))
   246 
   262 
       
   263 (defun gadict-nearest-headword ()
       
   264   "Return nearest headword looking upward."
       
   265   (save-excursion
       
   266     (let ( (orig (point)) limit headword )
       
   267       (re-search-backward "^__$")
       
   268       (forward-line 1)
       
   269       (unless (and (eq (char-before) ?\n) (eq (char-after) ?\n))
       
   270         (error "Syntax error: there is not empty line after '__'..."))
       
   271       (forward-line 1)
       
   272       (when (< orig (point))
       
   273         (setq orig (point)))
       
   274       (setq limit (point))
       
   275       (re-search-forward "^$")
       
   276       (when (< orig (point))
       
   277         (goto-char orig)
       
   278         (end-of-line))
       
   279       (re-search-backward "^\\([^ ].*\\)$" limit)
       
   280       (match-string 1)
       
   281       )))
       
   282 
       
   283 (defvar gadict-espeak-program "espeak")
       
   284 (defvar gadict-espeak-program-ipa-args "-q --ipa=2")
       
   285 (defvar gadict-espeak-voices-list '("en-gb" "en-us")
       
   286   "What voices to show. Look to 'espeak --voices' for full list.")
       
   287 
       
   288 (defun gadict-espeak-ipa (str &optional voice)
       
   289   (gadict--trim
       
   290    (shell-command-to-string
       
   291     (format "%s %s %s %s"
       
   292             gadict-espeak-program
       
   293             gadict-espeak-program-ipa-args
       
   294             (if (stringp voice) (concat "-v" (shell-quote-argument voice)) "")
       
   295             (shell-quote-argument str)))))
       
   296 
       
   297 (defun gadict-espeak-ipa-line (headword)
       
   298   (mapconcat (lambda (voice) (format "%s: %s"
       
   299                                 (propertize voice 'face '(:foreground "red"))
       
   300                                 (gadict-espeak-ipa headword voice)))
       
   301              (if (listp gadict-espeak-voices-list) gadict-espeak-voices-list '(nil))
       
   302              " | "))
       
   303 
       
   304 ;; (defun gadict-espeak-ipa-display ()
       
   305 ;;   (interactive)
       
   306 ;;   (message (gadict-espeak-ipa-line)))
       
   307 
       
   308 (defvar gadict-espeak-ipa-headword nil)
       
   309 
       
   310 (defun gadict-espeak-ipa-display ()
       
   311   (when (eq major-mode 'gadict-mode)
       
   312     (let ( (headword (gadict-nearest-headword)) )
       
   313       (unless (eq headword gadict-espeak-ipa-headword)
       
   314         (setq gadict-espeak-ipa-headword headword)
       
   315         (setq header-line-format (if headword (gadict-espeak-ipa-line headword) nil))
       
   316         (force-window-update)))))    
       
   317 
       
   318 (defvar gadict-espeak-ipa-timer nil)
       
   319 (defun gadict-espeak-ipa-enable ()
       
   320   (unless gadict-espeak-ipa-timer
       
   321     (setq gadict-espeak-ipa-timer (run-with-idle-timer 1 t #'gadict-espeak-ipa-display))))
       
   322 (defun gadict-espeak-ipa-disable ()
       
   323   (when gadict-espeak-ipa-timer
       
   324     (cancel-timer gadict-espeak-ipa-timer))
       
   325   (setq gadict-espeak-ipa-timer nil))
       
   326 
   247 (defun gadict-setup-keymap ()
   327 (defun gadict-setup-keymap ()
   248   "Setup gadict keymap."
   328   "Setup gadict keymap."
   249   (define-key (current-local-map) [M-return] 'gadict-search)
   329   (define-key (current-local-map) [M-return] 'gadict-search)
   250   (define-key (current-local-map) [S-return] 'gadict-insert-translation)
   330   (define-key (current-local-map) [S-return] 'gadict-insert-translation)
   251   (define-key (current-local-map) [C-return] 'gadict-insert-template-in-order))
   331   (define-key (current-local-map) [C-return] 'gadict-insert-template-in-order))