: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."
(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
'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)