Macro Vim - 扩展多个Verilog Bus
Macro Vim - expand multiple Verilog Bus
我正在尝试实现宏以将 Verilog 总线扩展为 ,这对一个变量确实有效。
但是我遇到了问题,因为我想实现如下所示的多总线
来源:
{test[13:10],thisistest[3:0],BUS[2:1]}
结果:
test[13]
test[12]
test[11]
test[10]
thisistest[3]
thisistest[2]
thisistest[1]
thisistest[0]
BUS[2]
BUS[1]
我尝试做的事情:
我制作了与
类似的功能
fun! split()
let line = getline('.')
let result = []
let list = split(line, ",")
let length = len(list)
for i in length
call ExpandIt(list[length])
endfor
endf
fun! ExpandIt()
let pat = '^\(.*\)\[\(\d\+\):\(\d\+\)\]\s*$'
let line = getline('.')
let lnr = line('.')
if line !~ pat
return
endif
let exestr = substitute(line,pat,'range(,,-1)','g')
let text = substitute(line,pat,'','g')
exec 'let range='.exestr
let result = []
for i in range
call add(result, text.'['.i.']')
endfor
call append(lnr, result)
exec lnr'.d'
endf
nnoremap <F6> :call split()<cr>
你能告诉我我应该怎么做才能走正确的路吗?
根据你添加的split()
函数中明显的错误(函数名必须大写以避免E128
,你给ExpandIt()
传递了一个参数,但它没有取任何一个,length
不能被 :for
迭代),你似乎对 Vim 脚本的理解有限,只是尝试通过暴力尝试 "make it work"。因此,在 Stack Overflow 上提问是件好事,但请注意,仅仅依靠有帮助的陌生人为您解决问题是一种片面的做法,因此请使用这个问题和以后的问题来慢慢但稳步地了解 Vim,自己成为大师!
通过进行以下修改,以下内容似乎可以满足您的要求:
- 修复上面列出的明显错误
- 将
result
列表收集到Split()
中是正确的想法,但实际插入(:call append()
)和删除原始行也必须移动到那里,并且ExpandIt()
必须 return 结果
- 同样,
ExpandIt()
需要传递提取的元素,而不是直接获取当前行
- 行中的
split()
以逗号分隔的元素需要去掉周围的{
和}
; substitute()
可以做到这一点。
fun! Split()
let line = getline('.')
let result = []
for l in split(substitute(line, '[{}]', '', 'g'), ",")
let result += ExpandIt(l)
endfor
let lnr = line('.')
call append(lnr, result)
exec lnr'.d'
endf
fun! ExpandIt(line)
let pat = '^\(.*\)\[\(\d\+\):\(\d\+\)\]\s*$'
if a:line !~ pat
return
endif
let exestr = substitute(a:line,pat,'range(,,-1)','g')
let text = substitute(a:line,pat,'','g')
exec 'let range='.exestr
let result = []
for i in range
call add(result, text.'['.i.']')
endfor
return result
endf
nnoremap <F6> :call Split()<cr>
我正在尝试实现宏以将 Verilog 总线扩展为
但是我遇到了问题,因为我想实现如下所示的多总线
来源:
{test[13:10],thisistest[3:0],BUS[2:1]}
结果:
test[13]
test[12]
test[11]
test[10]
thisistest[3]
thisistest[2]
thisistest[1]
thisistest[0]
BUS[2]
BUS[1]
我尝试做的事情: 我制作了与
类似的功能fun! split()
let line = getline('.')
let result = []
let list = split(line, ",")
let length = len(list)
for i in length
call ExpandIt(list[length])
endfor
endf
fun! ExpandIt()
let pat = '^\(.*\)\[\(\d\+\):\(\d\+\)\]\s*$'
let line = getline('.')
let lnr = line('.')
if line !~ pat
return
endif
let exestr = substitute(line,pat,'range(,,-1)','g')
let text = substitute(line,pat,'','g')
exec 'let range='.exestr
let result = []
for i in range
call add(result, text.'['.i.']')
endfor
call append(lnr, result)
exec lnr'.d'
endf
nnoremap <F6> :call split()<cr>
你能告诉我我应该怎么做才能走正确的路吗?
根据你添加的split()
函数中明显的错误(函数名必须大写以避免E128
,你给ExpandIt()
传递了一个参数,但它没有取任何一个,length
不能被 :for
迭代),你似乎对 Vim 脚本的理解有限,只是尝试通过暴力尝试 "make it work"。因此,在 Stack Overflow 上提问是件好事,但请注意,仅仅依靠有帮助的陌生人为您解决问题是一种片面的做法,因此请使用这个问题和以后的问题来慢慢但稳步地了解 Vim,自己成为大师!
通过进行以下修改,以下内容似乎可以满足您的要求:
- 修复上面列出的明显错误
- 将
result
列表收集到Split()
中是正确的想法,但实际插入(:call append()
)和删除原始行也必须移动到那里,并且ExpandIt()
必须 return 结果 - 同样,
ExpandIt()
需要传递提取的元素,而不是直接获取当前行 - 行中的
split()
以逗号分隔的元素需要去掉周围的{
和}
;substitute()
可以做到这一点。
fun! Split()
let line = getline('.')
let result = []
for l in split(substitute(line, '[{}]', '', 'g'), ",")
let result += ExpandIt(l)
endfor
let lnr = line('.')
call append(lnr, result)
exec lnr'.d'
endf
fun! ExpandIt(line)
let pat = '^\(.*\)\[\(\d\+\):\(\d\+\)\]\s*$'
if a:line !~ pat
return
endif
let exestr = substitute(a:line,pat,'range(,,-1)','g')
let text = substitute(a:line,pat,'','g')
exec 'let range='.exestr
let result = []
for i in range
call add(result, text.'['.i.']')
endfor
return result
endf
nnoremap <F6> :call Split()<cr>