(defvar 'autoauto-translate-region-mode nil
"Automatically translate the selection.")
(make-variable-buffer-local 'auto-translate-region-mode)
(defvar 'autoauto-translate--last-text nil)
(make-variable-buffer-local 'auto-translate--last-text)
(defvar 'autoauto-translate--last-translation nil)
(make-variable-buffer-local 'auto-translate--last-translation)
(defun auto-translate-call-engine (text)
"Translate TEXT and return the result."
TODO)
(defun auto-translate-region-redisplay ()
"Redisplay the last cached region translation."
;; Display auto-translate--last-translation somewhere
TODO)
(defun auto-translate-region-now ()
"Display the region's translation."
;; Get the selected text and normalize it a bit
(let* ((raw-text (buffer-substring-no-properties (region-beginning) (region-end)))
(text (replace-regexp-in-string "\\`[^[:alpha:]]+" ""
(replace-regexp-in-string "[^[:alpha:]]+\\'" "" raw-text))))
(unless (string= text auto-translate--last-text)
;; The selected text has changed, so update the translation
(setq auto-translate--last-translation (auto-translate-call-engine text)
auto-translate--last-text text))
;; (Re)display the translated text
(auto-translate-region-redisplay)))
(defun auto-translate-region-maybe ()
"Display the region's translation if automatic region translation is active
and the mark is active."
(if (and auto-translate-region-mode mark-active)
(auto-translate-region-translate)))
Gilles 'SO- stop being evil'
- 22.9k
- 4
- 59
- 92