用户讨论:Corey

来自Ubuntu中文
Corey留言 | 贡献2008年4月14日 (一) 17:00的版本 (这是一个小修改)
跳到导航跳到搜索

喜欢使用ubuntu和vim,但是都不是很了解,都在慢慢学!

最近写的一个gvim的marks脚本。方便自己方便大家!

链接 http://www.vim.org/scripts/script.php?script_id=2194

vim marks

" ---------------------------------------------------------------------
" put on one sign
fun! Place_sign()

if !exists("s:Cs_sign_number")
let s:Cs_sign_number = 1
endif

if s:Cs_sign_number > 99
echo "Sorry, you only can use these marks less 100!"
return -1
else
let s:Cs_sign_number = (s:mylist[len(s:mylist) - 1][0] * 1) + 1
endif

let vLn = "".line(".")
let vFileName = expand("%:p")

let vFlagNum = (s:Cs_sign_number < 10 ? "0" . s:Cs_sign_number : s:Cs_sign_number)
let newItem = [vFlagNum,vLn,vFileName]
let vIndex = s:Check_list(newItem)

if vIndex > -1
call s:Remove_sign(vIndex)
else
silent! exe 'sign define CS' . vFlagNum . ' text='. vFlagNum .' texthl=ErrorMsg'
silent! exe 'sign place ' . vFlagNum . ' line=' . vLn . ' name=CS'. vFlagNum . ' file=' . vFileName

"let s:Cs_sign_number = s:Cs_sign_number + 1
let s:mylist = s:mylist + [newItem]
" record the last index.
let s:myIndex = len(s:mylist) - 1
let s:deleteFlag = 0
endif
"echo s:mylist
endfun

" ---------------------------------------------------------------------
" Remove all signs
fun! Remove_all_signs()

silent! exe 'sign unplace *'
if len(s:mylist) > 1
let i = remove(s:mylist, 1, -1)
let s:Cs_sign_number = 1
endif
"echo s:mylist
endfun

" ---------------------------------------------------------------------
" Goto prev sign:
fun! Goto_prev_sign()

if len(s:mylist) > 1
if s:deleteFlag == 0
let s:myIndex = s:myIndex - 1
endif
let s:deleteFlag = 0

if s:myIndex <= 0
let s:myIndex = len(s:mylist) - 1
endif
call s:Sign_jump(s:mylist[s:myIndex])
endif
endfun

" ---------------------------------------------------------------------
" Goto next sign:
fun! Goto_next_sign()

let s:deleteFlag = 0
if len(s:mylist) > 1
let s:myIndex = s:myIndex + 1
if ((s:myIndex >= len(s:mylist)) || (s:myIndex == 1))
let s:myIndex = 1
endif
call s:Sign_jump(s:mylist[s:myIndex])
endif
endfun
" ---------------------------------------------------------------------
" Save_signs_to_file
fun! Save_signs_to_file()

call s:Get_signs_file_name()
let tempList = []
for item in s:mylist
let tempList = tempList + [item[0] . "#" . item[1]. "#" . item[2]]
endfor
let writeFlag = writefile(tempList, s:outputFileName)

endfun
" ---------------------------------------------------------------------
" Load_signs_from_file
fun! Load_signs_from_file()

call s:Get_signs_file_name()
if filereadable(s:outputFileName)
let tempList = [[]]
let iflag = 0
for line in readfile(s:outputFileName)
let first = stridx(line, "#", 0)
let second = stridx(line, "#", first + 1)
if iflag != 0
let tempList = tempList + strpart(line, 0, first), strpart(line, first + 1, second - first - 1), strpart(line, second + 1)
else
let tempList = strpart(line, 0, first), strpart(line, first + 1, second - first - 1), strpart(line, second + 1)
endif
let iflag = 1
endfor
let s:mylist = tempList
endif

call s:Flash_signs()

"echo s:mylist
endfun

" ---------------------------------------------------------------------
fun! s:Get_signs_file_name()

if exists("g:Signs_file_path_corey")
let s:outputFileName = g:Signs_file_path_corey . "_DO_NOT_DELETE_IT"
endif
endfun

" ---------------------------------------------------------------------
" Remove one sign
fun! s:Remove_sign(aIndex)

if len(s:mylist) > 1
silent! exe 'sign unplace ' .s:mylist[a:aIndex][0] . ' file=' . s:mylist[a:aIndex][2]

" record the before item
let s:tmplist = s:mylist[a:aIndex - 1]

let i = remove(s:mylist, a:aIndex)

" record the current index.
let s:myIndex = s:Check_list(s:tmplist)
let s:deleteFlag = 1
"echo s:mylist
endif
endfun

" ---------------------------------------------------------------------
fun! s:Flash_signs()

silent! exe 'sign unplace *'
if len(s:mylist) > 1
for item in s:mylist
silent! exe 'sign define CS' . item[0] . ' text='. item[0] .' texthl=ErrorMsg'
silent! exe 'sign place ' . item[0] . ' line=' . item[1] . ' name=CS'. item[0] . ' file=' . item[2]
endfor
endif
let s:Cs_sign_number = s:mylist[len(s:mylist) - 1][0] * 1 + 1
"let s:myIndex = 1 ##you don't need reset the pointer
endfun

" ---------------------------------------------------------------------
" if line number and file name both same, return the aitem's index of s:mylist
" else return -1
" index 0 of s:mylist is the output message in the record file.
fun! s:Check_list(aItem)

let vResult = -1
let index = 0

for item in s:mylist
" file name is ignoring case
if ((s:Compare(item[1], a:aItem[1]) == 1) && (s:Compare(item[2],a:aItem[2]) == 1))
return index
endif
let index = index + 1
endfor

return vResult
endfun

" ---------------------------------------------------------------------
" Move_sign
fun! Move_sign()

let s:tempItem = ["","",""]
let vRLn = "".line(".")
let vRFileName = expand("%:p")

let s:tempItem[1] = vRLn
let s:tempItem[2] = vRFileName
"echo s:tempItem
let vRIndex = s:Check_list(s:tempItem)

if (s:remarkItem[0] ==# "REMARK" )
if vRIndex > 0
silent! exe 'sign define CS' . s:mylist[vRIndex][0] . ' text='. s:mylist[vRIndex][0] .' texthl=Search'
silent! exe 'sign place ' . s:mylist[vRIndex][0] . ' line=' . vRLn . ' name=CS'. s:mylist[vRIndex][0] . ' file=' . vRFileName
let s:remarkItem = s:mylist[vRIndex]
let s:myIndex = vRIndex
"echo s:remarkItem
endif
else
let pionter = s:Check_list(s:remarkItem)
"echo vRIndex ."|" .pionter
if ((vRIndex < 0) && (pionter > 0))
silent! exe 'sign unplace ' .s:remarkItem[0] . ' file=' . s:remarkItem[2]
"silent! exe 'sign undefine' .s:remarkItem[0]
silent! exe 'sign define CS' . s:remarkItem[0] . ' text='. s:remarkItem[0] .' texthl=ErrorMsg'
silent! exe 'sign place ' . s:remarkItem[0] . ' line=' . vRLn . ' name=CS' . s:remarkItem[0] . ' file=' . vRFileName
let s:mylist[pionter][1] = vRLn
let s:mylist[pionter][2] = vRFileName
"echo s:mylist[pionter]
let s:myIndex = pionter
let s:remarkItem = ["REMARK","SEARCH","FLAG"]
endif
endif
endfun

" -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
" all of them used for the jump.
fun! s:Sign_jump(aSignItem)
let bufferExits = s:GetTabpage(a:aSignItem)

if bufferExits > 0
silent! exe 'tabn ' . bufferExits
silent! exe 'sign jump '. a:aSignItem[0] . ' file='. a:aSignItem[2]
else
call s:Open_file(a:aSignItem[2])
silent! exe 'sign place ' . a:aSignItem[0] . ' line=' . a:aSignItem[1] . ' name=CS'. a:aSignItem[0] . ' file=' . a:aSignItem[2]
silent! exe 'sign jump '. a:aSignItem[0] . ' file='. a:aSignItem[2]
endif

endfun

" ---------------------------------------------------------------------
" GetTabpage
fun! s:GetTabpage(aSignItem)

let bufname = expand("%:p")
if s:Compare(bufname,a:aSignItem[2]) == 1
return tabpagenr()
endif

let i = 0

while i < tabpagenr('$')

if i == 0
silent! exe 'tabfirst'
else
silent! exe 'tabnext'
endif
let bufname = expand("%:p")

if s:Compare(bufname,a:aSignItem[2]) == 1
return i + 1
endif

let i = i + 1
endwhile

return -1
endfun
" ---------------------------------------------------------------------
" compare
fun! s:Compare(a1,a2)
if s:win32Flag == 1
if a:a1 ==? a:a2
return 1
endif
else
if a:a1 ==# a:a2
return 1
endif
endif
return 0
endfun
" ---------------------------------------------------------------------
" open file