~tsdh/rdictcc

59cda517a88d70c542d633f5f62b4c78c1c8a036 — Tassilo Horn 2 months ago 27853a7 main
Overhaul rdictcc-permanent-translation-mode
1 files changed, 22 insertions(+), 44 deletions(-)

M rdictcc.el
M rdictcc.el => rdictcc.el +22 -44
@@ 369,55 369,33 @@ mouse pointer."
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; permanent translation mode

(defun rdictcc-forward-char (&optional n)
  (interactive "p")
  (forward-char n)
  (rdictcc-translate-word-at-point t))

(defun rdictcc-backward-char (&optional n)
  (interactive "p")
  (backward-char n)
  (rdictcc-translate-word-at-point t))

(defun rdictcc-next-line (&optional arg try-vscroll)
  (interactive "p")
  (next-line arg try-vscroll)
  (rdictcc-translate-word-at-point t))

(defun rdictcc-previous-line (&optional arg try-vscroll)
  (interactive "p")
  (previous-line arg try-vscroll)
  (rdictcc-translate-word-at-point t))

(defun rdictcc-forward-word (&optional arg)
  (interactive "p")
  (forward-word arg)
  (rdictcc-translate-word-at-point t))

(defun rdictcc-backward-word (&optional arg)
  (interactive "p")
  (backward-word arg)
  (rdictcc-translate-word-at-point t))

(defvar rdictcc-permanent-translation-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map [remap forward-char]   'rdictcc-forward-char)
    (define-key map [remap backward-char]  'rdictcc-backward-char)
    (define-key map [remap next-line]      'rdictcc-next-line)
    (define-key map [remap previous-line]  'rdictcc-previous-line)
    (define-key map [remap forward-word]   'rdictcc-forward-word)
    (define-key map [remap backward-word]  'rdictcc-backward-word)
    map)
  "The keymap used in `rdictcc-permanent-translation-mode'.")
(defcustom rdictcc-permanent-translation--delay 0.2
  "Delay for auto-translation with `rdictcc-permanent-translation-mode'."
  :type 'number)

(defvar rdictcc-permanent-translation--timer nil)

(defun rdictcc-permanent-translation--show ()
  (save-excursion
    (rdictcc-translate-word-at-point t)))

(defun rdictcc-permanent-translation--refresh ()
  (when (timerp rdictcc-permanent-translation--timer)
    (cancel-timer rdictcc-permanent-translation--timer))
  (run-with-idle-timer rdictcc-permanent-translation--delay
                       nil
                       #'rdictcc-permanent-translation--show))

(define-minor-mode rdictcc-permanent-translation-mode
  "Refresh the `rdictcc-buffer' after every point movement.
This will remap most point movement commands to rdictcc functions
that first move point and then update the translation buffer."
  nil
  " RDictCcPT"
  nil
  rdictcc-permanent-translation-mode-map)
  :lighter " RDictCcPT"
  (if rdictcc-permanent-translation-mode
      (add-hook 'post-command-hook
                #'rdictcc-permanent-translation--refresh nil t)
    (remove-hook 'post-command-hook
                 #'rdictcc-permanent-translation--refresh t)))

;;; end permanent translation mode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;