From 4854d00dc2458bcc0725624af3a15c58f773f5cb Mon Sep 17 00:00:00 2001 From: simonz <151792307+rpi-simonz@users.noreply.github.com> Date: Sat, 13 Sep 2025 21:52:15 +0200 Subject: [PATCH 1/3] Avoid trailing newline after translation of a linewise selection Workaround: Change linewise selection to characterwise. --- autoload/deepl.vim | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/autoload/deepl.vim b/autoload/deepl.vim index 44db456..79809f5 100644 --- a/autoload/deepl.vim +++ b/autoload/deepl.vim @@ -25,6 +25,12 @@ function! deepl#v(target_lang, source_lang = "") let l:paste = &paste set paste + " Avoid superfluous trailing newline after translating a visual linewise + " selection by changing it to a characterwise selection. + if mode() == "V" + execute "normal! \`h\gv" + endif + try " Apply transformation to the text if a:source_lang == "" From 1c80e3ed6ff57c7ab43d32c5782a6025320624b0 Mon Sep 17 00:00:00 2001 From: rpi-simonz Date: Wed, 28 Jan 2026 22:10:37 +0100 Subject: [PATCH 2/3] Deactivate woarkaround again. No longer needed. --- autoload/deepl.vim | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/autoload/deepl.vim b/autoload/deepl.vim index bef4178..f51735e 100644 --- a/autoload/deepl.vim +++ b/autoload/deepl.vim @@ -14,6 +14,14 @@ function! deepl#translate(input, target_lang, source_lang = "") let cmd = cmd .. ' -d ' .. shellescape(json_encode(data)) try + " Note: Somewhere in Q4/2025-Q1/2026 DeepL seems to have changed the API + " and since then all(?) newlines are translated to spaces in the + " reply. + " So the measure below for the trailing newline is no longer needed. + " + " {"target_lang":"EN","text":["Zeile1\nZeile2\n"]} + " ==> + " {"translations":[{"text":"Line1 Line2 ","detected_source_language":"DE"}]} const res = json_decode(system(cmd)) return res["translations"][0]["text"] @@ -31,9 +39,9 @@ function! deepl#v(target_lang, source_lang = "") " Avoid superfluous trailing newline after translating a visual linewise " selection by changing it to a characterwise selection. - if mode() == "V" - execute "normal! \`h\gv" - endif + "if mode() == "V" + " execute "normal! \`h\gv" + "endif try " Apply transformation to the text From b99352b39926bfaa5077744f6e5d0e116a8d058b Mon Sep 17 00:00:00 2001 From: rpi-simonz Date: Thu, 29 Jan 2026 09:31:49 +0100 Subject: [PATCH 3/3] Improve paragraph handling. --- autoload/deepl.vim | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/autoload/deepl.vim b/autoload/deepl.vim index f51735e..2e83aee 100644 --- a/autoload/deepl.vim +++ b/autoload/deepl.vim @@ -1,7 +1,7 @@ " Send a translation request to deepl using curl function! deepl#translate(input, target_lang, source_lang = "") let data = #{ - \ text: [a:input], + \ text: split(a:input, '\n\n'), \ target_lang: a:target_lang, \ } if a:source_lang != "" @@ -17,13 +17,24 @@ function! deepl#translate(input, target_lang, source_lang = "") " Note: Somewhere in Q4/2025-Q1/2026 DeepL seems to have changed the API " and since then all(?) newlines are translated to spaces in the " reply. - " So the measure below for the trailing newline is no longer needed. + " So the workaround below for the trailing newline is no longer needed. " " {"target_lang":"EN","text":["Zeile1\nZeile2\n"]} " ==> " {"translations":[{"text":"Line1 Line2 ","detected_source_language":"DE"}]} const res = json_decode(system(cmd)) - return res["translations"][0]["text"] + " But now the preformatting (via newlines) gets lost... + " + " This is fixed here by splitting the text on double newlines (paragraph + " boundaries) above and join it after translation again the same way. + " + let res2 = [] + for p in res["translations"] + call add(res2, p["text"]) + endfor + " Remove final trailing space character + " (needed when translating a visual linewise block) + return substitute(join(res2, "\n\n"), " $", "", "") catch /.*/ echoerr "error: " .. v:exception @@ -37,6 +48,7 @@ function! deepl#v(target_lang, source_lang = "") let l:paste = &paste set paste + " Workaround no longer needed, see above. " Avoid superfluous trailing newline after translating a visual linewise " selection by changing it to a characterwise selection. "if mode() == "V"