Skip to content

Commit

Permalink
Added hooks for each operation
Browse files Browse the repository at this point in the history
  • Loading branch information
SqrtMinusOne committed Nov 25, 2023
1 parent b371a23 commit 6348fae
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions reverso.el
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,18 @@ A random one is be picked at package initialization.")
reverso--user-agents)
"User-Agent to use for reverso.el requests.")

(defvar reverso--operation-hook nil
"Hook run after an operation.
The operations are:
- `reverso--translate'
- `reverso--get-context'
- `reverso--get-synonyms'
- `reverso--get-grammar'
The hook is called with two arguments: the operation name (the
function symbol) and the result of the operation.")

(defun reverso--translate (text source target cb)
"Translate TEXT from language SOURCE to TARGET.
Expand All @@ -288,9 +300,9 @@ The result is an alist with the following keys:
- `:detected-language': the detected source language
- `:translation': a string with translated text
- `:context-results': a list with found contexts.
An item of the list is an alist with the keys:
- `:source': a string in the source language
- `:target': a string in the target language"
One item of the list is an alist with the keys:
- `:source': string in the source language
- `:target': string in the target language"
(when (string-empty-p text)
(user-error "Empty input!"))
(unless (and (alist-get source reverso--language-mapping)
Expand Down Expand Up @@ -324,8 +336,12 @@ The result is an alist with the following keys:
:encoding 'utf-8
:success (cl-function
(lambda (&key data &allow-other-keys)
(funcall cb (reverso--alist-remove-empty-values
(reverso--translate-parse data)))))
(let ((res (reverso--alist-remove-empty-values
(reverso--translate-parse data))))
(run-hook-with-args
'reverso--operation-hook
'reverso--translate res)
(funcall cb res))))
:error (cl-function
(lambda (&key error-thrown &allow-other-keys)
(message "Error!: %S" error-thrown)))))
Expand Down Expand Up @@ -441,8 +457,12 @@ The result is a list of alists with the keys:
:encoding 'utf-8
:success (cl-function
(lambda (&key data &allow-other-keys)
(funcall cb (reverso--alist-remove-empty-values
(reverso--get-context-parse data)))))
(let ((res (reverso--alist-remove-empty-values
(reverso--get-context-parse data))))
(run-hook-with-args
'reverso--operation-hook
'reverso--get-context res)
(funcall cb res))))
:error (cl-function
(lambda (&key error-thrown &allow-other-keys)
(message "Error!: %S" error-thrown)))))
Expand Down Expand Up @@ -489,8 +509,12 @@ The result is a list of alists with the following keys:
:encoding 'utf-8
:success (cl-function
(lambda (&key data &allow-other-keys)
(funcall cb (reverso--alist-remove-empty-values
(reverso--get-synonyms-parse data)))))
(let ((res (reverso--alist-remove-empty-values
(reverso--get-synonyms-parse data))))
(run-hook-with-args
'reverso--operation-hook
'reverso--get-synonyms res)
(funcall cb res))))
:error (cl-function
(lambda (&key error-thrown &allow-other-keys)
(message "Error!: %S" error-thrown)))))
Expand Down Expand Up @@ -584,8 +608,12 @@ The result is an alist with the following keys:
:encoding 'utf-8
:success (cl-function
(lambda (&key data &allow-other-keys)
(funcall cb (reverso--alist-remove-empty-values
(reverso--get-grammar-parse text data)))))
(let ((res (reverso--alist-remove-empty-values
(reverso--get-grammar-parse text data))))
(run-hook-with-args
'reverso--operation-hook
'reverso--get-grammar res)
(funcall cb res))))
:error (cl-function
(lambda (&key error-thrown &allow-other-keys)
(message "Error!: %S" error-thrown)))))
Expand Down

0 comments on commit 6348fae

Please sign in to comment.