LINUX.ORG.RU

Забиндить внешнюю команду в виме

 ,


0

2

Написал полезную команду, как бы ее теперь забиндить на хот-кей в виме? Что бы работал только над выделенным текстом, регистром или как еще это можно сделать?

Скрипт переводит текст «лучшим в мире переводчиком».

Нужно еще сделать передачу текста в скрипт. Пока так:

#!/bin/bash

text="hello world"
data='{"jsonrpc":"2.0","method": "LMT_handle_jobs","params":{"jobs":[{"kind":"default","sentences":[{"text":"'"${text}"'","id":1,"prefix":""}],"raw_en_context_before":[],"raw_en_context_after":[],"preferred_num_beams":4}],"lang":{"target_lang":"RU","preference":{"weight":{},"default":"default"},"source_lang_computed":"EN"},"priority":1,"commonJobParams":{"quality":"normal","mode":"translate","browserType":1,"textType":"plaintext"},"timestamp":1726658014839},"id":2300004}'

r=$(curl 'https://www2.deepl.com/jsonrpc?method=LMT_handle_jobs' '--http2' -H @header -b cookie --data-ascii "${data}" 2>/dev/null | jq -r '.result.translations[0].beams')
echo №1
echo $(echo "${r}"|jq .[0].sentences[0].text) 
echo №2
echo $(echo "${r}"|jq .[1].sentences[0].text) 
echo №3
echo $(echo "${r}"|jq .[2].sentences[0].text) 
echo №4
echo $(echo "${r}"|jq .[3].sentences[0].text) 
echo №5
echo $(echo "${r}"|jq .[4].sentences[0].text) 
echo №6
echo $(echo "${r}"|jq .[5].sentences[0].text) 
echo №7
echo $(echo "${r}"|jq .[6].sentences[0].text) 



Последнее исправление: Sergeyka (всего исправлений: 1)

Передать просто:

:'<,'>:w !script.sh

Вот как получить обратно придумал только редирект в какое-нибудь фифо, которое тут же читать.

anonymous
()
Ответ на: комментарий от anonymous

его конечно можно как-то здесь использовать, но я бы использовал попапы и простой system() или systemlist().

habamax ★★★
()

Можно проще, но проще не интересно:

https://asciinema.org/a/CQbCsMH785bRbLjjyqNo8K4Vn

vim9script

var popup_borderchars = get(g:, "popup_borderchars", ['─', '│', '─', '│', '┌', '┐', '┘', '└'])
var popup_borderhighlight = get(g:, "popup_borderhighlight", ['Normal'])
var popup_highlight = get(g:, "popup_highlight", 'Normal')

# Shows popup window at cursor position
def ShowAtCursor(text: any, Setup: func(number) = null_function): number
    var new_text = text
    if text->type() == v:t_string
        new_text = text->trim("\<CR>")
    else
        new_text = text->mapnew((_, v) => v->trim("\<CR>"))
    endif
    var winid = popup_create(new_text, {
        padding: [0, 1, 0, 1],
        border: [],
        borderchars: popup_borderchars,
        borderhighlight: popup_borderhighlight,
        highlight: popup_highlight,
        pos: screencol() > &columns / 1.7 ? "botright" : "botleft",
        line: 'cursor-1',
  col: 'cursor',
        moved: 'WORD',
        mapping: 0,
        filter: (winid, key) => {
            if key == "\<Space>"
                win_execute(winid, "normal! \<C-d>\<C-d>")
                return true
            elseif key == "j"
                win_execute(winid, "normal! \<C-d>")
                return true
            elseif key == "k"
                win_execute(winid, "normal! \<C-u>")
                return true
            elseif key == "g"
                win_execute(winid, "normal! gg")
                return true
            elseif key == "G"
                win_execute(winid, "normal! G")
                return true
            endif
            if key == "\<ESC>"
                popup_close(winid)
                return true
            endif
            return true
        }
    })
    if Setup != null_function
        Setup(winid)
    endif
    return winid
enddef

def Translate()
    var vis_text = getregion(getpos('.'), getpos('v'), {type: mode()})
    # use your script here within systemlist function
    # var translated_text = systemlist('deeplshellscript', vis_text)->trim()

    # here just an example of calling tr to make lowercase -> uppercase and
    # show it in popup
    var translated_text = systemlist('tr [:lower:] [:upper:]', vis_text)
    ShowAtCursor(translated_text)
enddef

xnoremap K <scriptcmd>Translate()<cr>
habamax ★★★
()
Для того чтобы оставить комментарий войдите или зарегистрируйтесь.