#+TITLE: reverso.el Emacs client for [[https://www.reverso.net/][Reverso]]. The implemented features are: - [[https://www.reverso.net/text-translation][Translation]] - [[https://context.reverso.net/translation/][Context]] (AKA bilingual concordances) - [[https://www.reverso.net/spell-checker/english-spelling-grammar/][Grammar check]] - [[https://synonyms.reverso.net/synonym/][Synonyms search]] * Installation The package isn't yet available anywhere but in this repository. My preferred way for such cases is [[https://github.com/jwiegley/use-package][use-package]] and [[https://github.com/radian-software/straight.el][straight.el]]: #+begin_src emacs-lisp (use-package reverso :straight (:host github :repo "SqrtMinusOne/reverso.el")) #+end_src Or clone the repository, add it to the =load-path= and =require= the package. * Usage There's a single entrypoint for all implemented functions, =M-x reverso=. The UI is implemented with the excellent [[https://github.com/magit/transient/][transient.el]]. ** Input handling All the commands handle the input as follows. By default, the input string is empty. If a command is launched with a region selected, use the string of that region. If launched with the prefix argument (=C-u=), use the entire buffer. Results are displayed in =reverso-result-mode= buffers. If launched in that buffer, the command uses the input string for the buffer. If launched with =C-u=, it uses the output string of that buffer (if available). ** Translation Run =M-x reverso t= or =M-x reverso-translate= to invoke the translation transient. [[./img/translation-transient.png]] The "Source language" and "Target language" parameters are self-explanatory; just note that in the general case not every language is compatible with every other language. "Swap languages" tries to swap them. If "Brief translation output" is enabled, the output buffer will only show the translated version of the string. [[./img/translation-res.png]] Otherwise, the result buffer can include the following sections: - *Source text* and *Translation* - *Corrected text*, if available - *Context results*, if available Context results usually appear for short strings, like in the example from the screenshot. ** Context Run =M-x reverso c= or =M-x reverso-context= to invoke context search (or [[https://en.wikipedia.org/w/index.php?title=Online_bilingual_concordance&redirect=no][bilingual concordances]], essentially a Rosetta stone generator). The input/output UI looks almost the same as in the translation command. Strange enough though, direct context search usually yields different results than the "Context results" section of the translation command, so you might want to check out both if you want more data. ** Synonyms Run =M-x reverso s= or =M-x reverso-synonyms= to invoke the synonyms search. [[./img/synonyms-transient.png]] [[./img/synonyms-res.png]] If necessary, the results are split into sections by parts of speech. A section for each part of speech includes up to three subsections: - Synonyms - Examples - Antonyms ** Grammar check Run =M-x reverso g= or =M-x reverso-grammar= to invoke the grammar check. [[./img/grammar-transient.png]] As of now, only English, French, Spanish and Italian languages are available. [[./img/grammar-res.png]] The results may include the following sections: - *Source text*, where errors are highlighted with =reverso-error-face= - *Corrected text* - *Corrections* ** Grammar check in buffer It may be handy to apply the grammar check to the current buffer, that is, without using another buffer to display the results. For that purpose, there is =M-x reverso b= or =M-x reverso-grammar-buffer=. [[./img/grammar-buffer-transient.png]] Running =e= there (or =M-x reverso-check-buffer=) uses the current buffer as input and displays any errors in finds with [[https://www.gnu.org/software/emacs/manual/html_node/elisp/Overlays.html][overlays]]. If a region is selected, the check is limited to that region. There are a couple of caveats there. First, the service considers each linebreak as a new line, which is incompatible with [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Filling.html][filling text]], i.e. breaking it into lines of a specified width. The "Remove linebreaks" option (=l=) is a workaround for this. Second, the service usually freaks out with special syntax, for instance, Org Mode links. The third partly follows from the second, because the service usually finds errors in hidden parts of Org links. That is a problem because a completely hidden overlay may be somewhat hard to access. So either skip these errors or run =M-x org-toggle-link-display= in Org files beforehand. Finally, (and this concerns all other methods as well), the API usually limits the input size. So, if the service returns an error, try running the command on a smaller region of the buffer. [[./img/grammar-buffer-res.png]] When the cursor is on an error, the "Information" section shows the details. "Fix error" (=f= or =M-x reverso-check-fix-at-point=) opens a completion interface with possible fixes; "Ignore error" (=i= or =M-x reverso-check-ignore-error=) just removes the overlay and jumps to the next error. "Previous error" (=p= or =M-x reverso-check-prev-error=), "Next error" (=n= or =M-x reverso-check-next-error=), "First error" (=P= or =M-x reverso-check-first-error=) and "Last error" (=L= or =M-x reverso-check-last-error=) serve to navigate the error list. "Clear" (=c= or =M-x reverso-clear=) removes error overlays. If a region is selected, remove overlays only in that region; otherwise, remove them from the entire buffer. * Caveats Before we go any further, here are some general caveats to be aware of. First, the package is using a reverse-engineered API, so all the obvious consequences apply. Although this service has been used in this way for some time already. Second, the cap on the input size was already mentioned. The obvious workaround is running the command on a region of lesser size. Third, there are reports that Reverso dispatches *bans by IP* to particularly zealous users, so watch out for that if you are doing a lot of automated queries. This is also the reason why I didn't implement running one command on multiple consequential regions. Fourth, be careful with what you send to the service. Don't accidentally send something confidential (like a password) or anything that can be used against you in some other way. Although the service is [[https://www.reverso.net/privacy.aspx?lang=EN][GDPR-compliant on paper]], there's no way for us to actually verify that. * Customization Run =M-x customize-group reverso= to see the available parameters. Here are some. If you don't need all 17 languages, you can set the =reverso-languages= variable to limit the list: #+begin_src emacs-lisp (setq reverso-languages '(english german russian)) #+end_src If the length of =reverso-languages= is greater than =reverso-language-completing-read-threshold=, switching a language in transient buffers will invoke =completing-read= (i.e. minibuffer completion). Otherwise, switching will just switch to the next one. =reverso-max-display-lines-in-input= controls how many lines can be displayed in the input section of a transient buffer. The available faces: - =reverso-highlight-face= - =reverso-error-face= - =reverso-heading-face= - =reverso-keyword-face= - =reverso-definition-face= are inherited from the faces of =transient.el= and =basic-faces= to look nice. * Elisp API In case you want to do something in Emacs Lisp, there are 4 main functions that call the Reverso API: - =reverso--translate= - =reverso--get-context= - =reverso--get-grammar= - =reverso--get-context= Take a look at the docstrings for more information. Every function is asynchronous, and the results are returned via a callback. Reverso occasionally changes its list of available languages and the compatibility matrix, so if you change any of these, run =reverso-verify-settings= to check for errors. * Alternatives and observations One translation service everyone is familiar with is [[https://translate.google.com/][Google Translate]], so of course, there's an [[https://github.com/atykhonov/google-translate][Emacs client]] for it. The [[https://github.com/emacs-grammarly][emacs-grammarly]] package series provides the Elisp API for [[https://www.grammarly.com/][Grammarly]] (a grammar checking service) and a bunch of frontends for it. Unlike Reverso, Grammarly has an official API (so you don't risk getting an IP ban), and the allowed input size is much greater. Moreover, Grammarly is less bothered by Org and Markdown syntax, although it still doesn't like inline code blocks. Grammarly generally seems to be better at grammar-checking than Reverso, especially when it comes to rephrasing wordy sentences and punctuation. However, Grammarly also gives more false positives. Another notable grammar-checking solution is [[https://languagetool.org/][LanguageTool]], which you can [[https://dev.languagetool.org/http-server][run offline]] and use with its [[https://github.com/mhayashi1120/Emacs-langtool][Emacs package]]. This one has the obvious advantage of having no limits on usage and not sending your data to a 3rd party server you can't control. But it still doesn't like markup syntaxes. [[https://github.com/valentjn/ltex-ls][LTeX LS]] is a LanguageTool-based language server, designed specifically to work with markup files like Org, Markdown, LaTeX, and a bunch of others. The [[https://www.npmjs.com/package/reverso-api][reverso-api]] npm package implements the same commands in JavaScript. It also provided invaluable information for creating this package.