]> git.eshelyaron.com Git - dotfiles.git/commitdiff
Enhance vertalen
authorEshel Yaron <eshel@dazz.io>
Wed, 15 Jun 2022 11:36:57 +0000 (14:36 +0300)
committerEshel Yaron <eshel@dazz.io>
Wed, 15 Jun 2022 11:36:57 +0000 (14:36 +0300)
.emacs.d/esy.org

index fa29d1d867d6b26272c2044972dce15d113b8260..8d27577e1e66979930b8a6c3681bb39ef8022d23 100644 (file)
@@ -133,46 +133,54 @@ For further information about Elisp headers, see [[info:elisp#Library
 :END:
 
 #+begin_src emacs-lisp
-  (defun vertalen--parse-vertalen ()
-    "Populate `tabulated-list-entries' and display the results."
+  (defun vertalen--on-success (&rest args)
+    "Process ARGS and display translation in a dedicated buffer."
+    (with-current-buffer-window "*vertalen*"
+        (with-selected-window (selected-window)
+          (unless (eq major-mode 'vertalen-mode)
+            `(nil . ((inhibit-same-window . t)))))
+        #'fit-window-to-buffer
+      (setq tabulated-list-entries (plist-get args :data))
+      (vertalen-mode)
+      (+ 2 (length tabulated-list-entries))))
+
+  (defun vertalen--parse ()
+    (require 'dom)
+    "Parse buffer and return a list of translations."
     (let* ((dom (libxml-parse-html-region (point-min) (point-max)))
            (res (dom-by-class dom ".*result-item-translations.*"))
-           (act (with-selected-window (selected-window)
-                  (unless (eq major-mode 'vertalen-mode)
-                    `(nil . ((inhibit-same-window . t)))))))
-      (with-current-buffer-window "*vertalen*" act #'fit-window-to-buffer
-        (setq tabulated-list-entries nil)
-        (dolist (elem res)
-          (setq tabulated-list-entries
-                `((nil ,(vector
-                         (apply #'concat
-                                (dom-strings
-                                 (car (dom-by-class
-                                       elem
-                                       ".*result-item-source.*"))))
-                         (mapconcat #'identity
-                                    (dom-strings
-                                     (car (dom-by-class
-                                           elem
-                                           ".*result-item-target.*")))
-                                    ", ")))
-                  . ,tabulated-list-entries)))
-        (setq tabulated-list-entries (reverse tabulated-list-entries))
-        (vertalen-mode)
-        (+ 2 (length tabulated-list-entries))))
-    (with-current-buffer "*vertalen*"
-      (mapconcat #'identity (cadar tabulated-list-entries) ": ")))
-
-  (with-eval-after-load 'define-word
-    (add-to-list 'define-word-services
-                 '(vertalen
-                   "https://www.vertalen.nu/vertaal?van=nl&naar=en&vertaal=%s"
-                   vertalen--parse-vertalen)))
+           (ret nil))
+      (dolist (elem res)
+        (setq ret
+              `((nil ,(vector
+                       (apply #'concat
+                              (dom-strings
+                               (car (dom-by-class
+                                     elem
+                                     ".*result-item-source.*"))))
+                       (mapconcat #'identity
+                                  (dom-strings
+                                   (car (dom-by-class
+                                         elem
+                                         ".*result-item-target.*")))
+                                  ", ")))
+                . ,ret)))
+      (reverse ret)))
+
+  (setq vertalen--source nil)
+  (setq vertalen-history nil)
 
   (defun vertalen (word)
     "Translate WORD."
-    (interactive "M")
-    (define-word word 'vertalen))
+    (interactive (list (read-string "Translate: " nil 'vertalen-history (thing-at-point 'word))))
+    (require 'request)
+    (setq vertalen--source word)
+    (request "https://www.vertalen.nu/vertaal"
+      :params `(("van" . "nl")
+                ("naar" . "en")
+                ("vertaal" . ,word))
+      :parser  #'vertalen--parse
+      :success #'vertalen--on-success))
 
   (defun vertalen-at-point ()
     "Translate word at point."
@@ -188,8 +196,16 @@ For further information about Elisp headers, see [[info:elisp#Library
     (setq tabulated-list-format [("Source Language" 64 t)
                                  ("Target Language" 32 t)])
     (tabulated-list-init-header)
-    (tabulated-list-print))
+    (tabulated-list-print)
+    (save-excursion
+      (goto-char (point-min))
+      (let ((inhibit-read-only t)
+            (word (search-forward vertalen--source nil t)))
+        (while word
+          (add-face-text-property (match-beginning 0) (match-end 0) 'success)
+          (setq word (search-forward vertalen--source nil t))))))
 #+end_src
+
 * Package archives
 :PROPERTIES:
 :CUSTOM_ID: package-archives
@@ -1130,7 +1146,7 @@ does in the shell.
                                                 'face 'shadow)
           completion-auto-help 'visual
           completions-max-height 16
-          completion-wrap-movement t)
+          completion-auto-wrap t)
     (define-key minibuffer-local-completion-map
                 [remap previous-line]
                 #'minibuffer-previous-completion)