gpt4 book ai didi

function - Vim vmap,将选定的文本作为参数发送给函数

转载 作者:行者123 更新时间:2023-12-04 03:04:45 27 4
gpt4 key购买 nike

简而言之,我已经编写了一些函数来节省输入,我正在尝试设置一个 vmap[ping],它允许我选择我输入的内容,并将该选择传递给一个函数(因为在命令行、输入参数(带引号)和转义反斜杠等...抵消了调用函数节省的大部分时间)

对于(一个简单的)示例,假设我有以下功能

func Test(iStr)
if a:iStr[0] =~ [a-zA-z]
echo "hello"
else
echo "hello world"
endif
endfunc

我希望能够直观地选择一些文本,然后使用一些键映射,比如 F2,它将调用 Test(iStr) 并将选择作为参数 iStr

我相信,随着更多的工作(即某种方式来指定我选择的内容应该在 Test() 中),以下

vmap <F2> :call Test()

是我所追求的。事情是我已经尝试了很多变体(猜测加上来自 :help map 的一些不可靠的推断)但我没有得到任何有用的东西。更新,我试过使用辅助函数 Test2() 和

call Test(<C-W>) 

作为它的主体...不知何故我认为我需要捕获我光标下的单词(不知何故)然后我就完成了 - 因为我可以将它从 Test2 中传递给 Test(...)


至于我想调用的函数的实际示例,下面的(不完整的)函数(和辅助函数)将允许我转换形式的表达式,比方说,

f_k^{(j)}g

f_1^{(j)}g, f_2^{(j)}g, \dots, f_{n-1}^{(j)}g, f_n^{(j)}g 

在程序方面我想

a) type the repeated term in vim
b) visually select it
c) hit some mapping key that will call SumOrSequence(iExpression, iIndex)
d) provide "k" as a parameter
e) press enter
f) see the change made by SumOrSequence(...)

SumOrSequence(...) 的代码如下:

func SumOrSequence(iExpression, iIndex)
"need to check validity of these - maybe set a default
let default = Interrogate("do with defaults? yes [y] (2,1,n,0,\",\"), yes but specify last term [d[a-Z]], no [n]")
if default == "y"
let leftTerms = 2
let rightTerms = 1
let lastTermIndex = "n"
let firstTermIndex = 0
let operator = ","
let dotType = "\\dots"
elseif default =~ 'd[a-zA-Z]'
let leftTerms = 2
let rightTerms = 1
let lastTermIndex = default[1]
let firstTermIndex = 0
let operator = Interrogate("what separates terms? add [+], subtract [-], times [*], comma [,], ampersand [&]?")
let dotType = "\\cdots"
else "so n or anything else
let leftTerms = InterrogateNumber("how many terms before dots? ")
let rightTerms = InterrogateNumber("how many terms after dots? ")
let lastTermIndex = Interrogate("what is last term index?")
let firstTermIndex = Interrogate("what is first term index?")
let operator = Interrogate("what separates terms? add [+], subtract [-], times [*], comma [,], ampersand [&]?") "need to check only any of these provided
let dotType = "\\cdots"
endif
if operator == ","
let dotType = "\\dots"
endif
if operator == "*"
let operator = "\\times"
endif
let leftCount = 0
let oExpression = ""
while leftCount < leftTerms
if leftCount > 0
let oExpression .= operator . " "
endif
let oExpression .= ReplaceIndex(a:iExpression, a:iIndex, leftCount,1)
let leftCount += 1
endwhile
let oExpression .= operator . " " . dotType . " "
let rightCount = rightTerms-1
while rightCount > 0
"here we are going to be counting backwards from some number denoting number of terms - may need to know if we actually have a number!
echo "decrement: " . HandleDecrement(lastTermIndex, rightCount)
let oExpression .= operator . " " . ReplaceIndex(a:iExpression, a:iIndex, HandleDecrement(lastTermIndex, rightCount),1)
let rightCount -= 1
endwhile
let oExpression .= operator . " " . ReplaceIndex(a:iExpression, a:iIndex, lastTermIndex,0)
echo oExpression
endfunc

func ReplaceIndex(iExpression, iIndex, iReplacement, iInsertBraces)
"the game we play here is to search for iIndex in such form that it is not part of any other string
"We should expect this to be the case if the character to the left or right of the index is not in [A-z] (or just to the right if a greek char)
let oExpression = ""
let strEndPosition = strlen(a:iExpression) - 1
let currPosition = 0
let indexLen = strlen(a:iIndex)
while currPosition <= strEndPosition
let indexCounter = 0
let foundIndex = 1
while indexCounter < indexLen
if a:iExpression[currPosition + indexCounter] == a:iIndex[indexCounter]
if a:iExpression[currPosition + indexLen] =~ '[a-zA-Z]'
let foundIndex = 0
let indexCounter = indexLen
elseif a:iExpression[currPosition -1] =~ '[a-zA-Z]' && a:iExpression[currPosition] != "\\"
let foundIndex = 0
let indexCounter = indexLen
else
let indexCounter+=1
endif
else
let indexCounter = indexLen
let foundIndex = 0
endif
endwhile
if foundIndex == 0
let oExpression .= a:iExpression[currPosition]
let currPosition+=1
else
if a:iInsertBraces == 1
let oExpression .= "{" . a:iReplacement . "}"
else
let oExpression .= a:iReplacement
endif
let currPosition+=indexLen
endif
endwhile
echo "oExpression: " . oExpression
return oExpression
endfunc

func HandleIncrement(iExpression, iIncrement)
"and what about negative numbers for iExpression!??? not handling these yet :[
let oExpression = ""
if !(a:iExpression[0] =~ '[0-9]') || a:iExpression < 10 && strlen(a:iExpression) > 1
let oExpression = a:iExpression . " + " . a:iIncrement
else
let oExpression = a:iExpression + a:iIncrement
endif
echo oExpression
return oExpression
endfunc

func HandleDecrement(iExpression, iIncrement)
"TODO and what about negative numbers for iExpression!??? not handling these yet :[
let oExpression = ""
if !(a:iExpression[0] =~ '[0-9]') || a:iExpression < 10 && strlen(a:iExpression) > 1
let oExpression = a:iExpression . " - " . a:iIncrement
else
let oExpression = a:iExpression - a:iIncrement
endif
echo oExpression
return oExpression
endfunc


func Interrogate(iQuestion)
call inputsave()
let answer = input(a:iQuestion)
call inputrestore()
return answer
endfunc

func InterrogateNumber(iQuestion)
call inputsave()
let answer = input(a:iQuestion)
call inputrestore()
"TODO what if negative number??
if !(answer[0] =~ '[0-9]')
let answer = InterrogateNumber(a:iQuestion . " you didn't enter a numerical value ")
endif
return answer
endfunc

关于映射位,我知道我似乎没有做太多工作,但假设我还有很多工作要做,我自己才能找到答案,有人能帮忙吗?


更新。好的,我有一些以笨拙的方式工作的东西,即如果我定义以下辅助函数:

func SumOrSequenceHelper()
let oIndex = Interrogate("index variable? ")
"go to last thing visually selected (I think!), yank it (putting it in the " register), then fetch it via oParam. Then pass this off to SumOrSequence
execute "normal! gvy"
let oExpression = getreg('"')
call SumOrSequence(oExpression, oIndex)
endfunc

vnoremap <F6> :call SumOrSequenceHelper()

那么一切都很好,我可以执行一个执行命令,用我从 SumOrSequence(...) 得到的内容替换我选择的内容

如果有任何改进,我们将不胜感激,但出于所有意图和目的,这个问题已经解决了:]

最佳答案

你可以使用这样的辅助函数:

func! GetSelectedText()
normal gv"xy
let result = getreg("x")
normal gv
return result
endfunc

vnoremap <F6> :call MyFunc(GetSelectedText())<cr>

还有:com -range,它可以注册一个自定义命令,对一个选择进行操作,但是界面是面向行的。

关于function - Vim vmap,将选定的文本作为参数发送给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12805922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com