From 839f48b907a898bb579ba0792b78d874b2e89d7c Mon Sep 17 00:00:00 2001 From: Wenlong Che Date: Tue, 15 Jul 2008 00:00:00 +0000 Subject: [PATCH] Version 2.5 In this version, I add a prompt line which contain the tag name and its counts when we do the multi-definition searching. A bug about folding in VIM is fixed. In srcexpl.vim, I desrcibed that how the Source Explorer works with the Taglist and the MiniBufExplorer on my platform. --- plugin/srcexpl.vim | 707 +++++++++++++++++++++++++++------------------ 1 file changed, 432 insertions(+), 275 deletions(-) diff --git a/plugin/srcexpl.vim b/plugin/srcexpl.vim index 055d6a7..e25c802 100644 --- a/plugin/srcexpl.vim +++ b/plugin/srcexpl.vim @@ -1,28 +1,29 @@ """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" File Name: srcexpl.vim -" Abstract: A (G)VIM plugin for exploring the C/C++ -" source code based on 'tags' and 'quickfix'. -" Author: Wenlong Che -" EMail: chewenlong @ buaa.edu.cn -" Version: 2.4 -" Last Change: July 8, 2008 +" File Name: srcexpl.vim +" Abstract: A (G)VIM plugin for exploring the source code +" based on 'tags' and 'quickfix' works like the +" context window in Souce Insight. +" Author: Wenlong Che +" EMail: chewenlong @ buaa.edu.cn +" Version: 2.5 +" Last Change: July 15, 2008 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " The_setting_example_in_my_vimrc_file:-) -" // Set 300 ms for refreshing the the Source Explorer -" let g:SrcExpl_RefreshTime = 300 -" // Set the window height of the Source Explorer -" let g:SrcExpl_WinHeight = 9 -" // Let the Source Explorer update the tags file when initializing -" let g:SrcExpl_UpdateTags = 1 +" // Set 300 ms for refreshing the Source Explorer +" let g:SrcExpl_refreshTime = 300 +" // Set the window height of Source Explorer +" let g:SrcExpl_winHeight = 9 +" // Let the Source Explorer update the tags file when opening +" let g:SrcExpl_updateTags = 1 " // Set "Space" key for refresh the Source Explorer manually -" let g:SrcExpl_RefreshMapKey = "" +" let g:SrcExpl_refreshKey = "" " // Set "Ctrl-b" key for back from the definition context -" let g:SrcExpl_GoBackMapKey = "" +" let g:SrcExpl_gobackKey = "" " // The switch of the Source Explorer " nmap :SrcExplToggle @@ -30,7 +31,7 @@ """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" Avoid reloading the plugin again +" Avoid reloading {{{ if exists('loaded_srcexpl') finish @@ -39,104 +40,156 @@ endif let loaded_srcexpl = 1 let s:save_cpo = &cpoptions -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" }}} + +" VIM version control {{{ -" Source Explorer Plugin version control +" The VIM version control for running the Source Explorer if v:version < 700 " Tell users the reason echohl WarningMsg | - \ echo "You need VIM v7.0 or later for SrcExpl Plugin" + \ echo "You need VIM 7.0 and above for running the Source Explorer" \ | echohl None finish endif set cpoptions&vim -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" }}} + +" User interfaces {{{ + +" User interface for switching the Source Explorer -" User interface for switching the Source Explorer Plugin command! -nargs=0 -bar SrcExplToggle \ call SrcExpl_Toggle() -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" - " User interface for changing the " height of the Source Explorer Window -if !exists('g:SrcExpl_WinHeight') - let g:SrcExpl_WinHeight = 10 +if !exists('g:SrcExpl_winHeight') + let g:SrcExpl_winHeight = 10 endif " User interface for setting the " update time interval of each refreshing -if !exists('g:SrcExpl_RefreshTime') - let g:SrcExpl_RefreshTime = 500 +if !exists('g:SrcExpl_refreshTime') + let g:SrcExpl_refreshTime = 500 endif -" User interface for update tags -" file when loading SrcExpl Plugin -if !exists('g:SrcExpl_UpdateTags') - let g:SrcExpl_UpdateTags = 0 +" User interface to update the 'tags' +" file when loading the Source Explorer +if !exists('g:SrcExpl_updateTags') + let g:SrcExpl_updateTags = 0 endif -" User interface for back from +" User interface to go back from " the definition context -if !exists('g:SrcExpl_GoBackMapKey') - let g:SrcExpl_GoBackMapKey = '' +if !exists('g:SrcExpl_gobackKey') + let g:SrcExpl_gobackKey = '' endif " User interface for refreshing one " definition searching manually -if !exists('g:SrcExpl_RefreshMapKey') - let g:SrcExpl_RefreshMapKey = '' +if !exists('g:SrcExpl_refreshKey') + let g:SrcExpl_refreshKey = '' endif -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" }}} + +" Global varialbes {{{ + +" Buffer title for buffer listing +let s:SrcExpl_bufTitle = 'Source_Explorer' + +" The log file path for debug +let s:SrcExpl_logPath = './srcexpl.log' -" Buffer Title for buffer listing -let s:SrcExpl_BufTitle = '__Source_Explorer__' " The whole path of 'tags' file -let s:SrcExpl_TagsFilePath = '' +let s:SrcExpl_tagsPath = '' + " The key word symbol for exploring -let s:SrcExpl_Symbol = '' -" Whole file path being explored now -let s:SrcExpl_EditFilePath = '' +let s:SrcExpl_symbol = '' + " Original work path when initilizing -let s:SrcExpl_RawWorkPath = '' -" ID number of srcexpl.vim -let s:SrcExpl_ScriptID = 0 +let s:SrcExpl_rawWorkPath = '' + +" Debug Switch for logging the debug information +let s:SrcExpl_isDebug = 0 + +" ID number of SrcExpl.vim +let s:SrcExpl_scriptID = 0 + +" Whole file path being explored now +let s:SrcExpl_currPath = '' + " Current line number of the key word symbol -let s:SrcExpl_CurrLine = 0 +let s:SrcExpl_currLine = 0 + " Current col number of the key word symbol -let s:SrcExpl_CurrCol = 0 +let s:SrcExpl_currCol = 0 + " Source Explorer switch flag -let s:SrcExpl_Switch = 0 +let s:SrcExpl_isOpen = 0 + " Source Explorer status: -" 1: Sigle-definition -" 2: Multi-definitions -" 3: No such definition -let s:SrcExpl_Status = 0 +" 1: Single definition +" 2: Multi-definitions +" 3: No such tag definition +let s:SrcExpl_status = 0 + +" }}} """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" NOTE: You gugs can change this function by yourselves -" in order to adapt the editor window position for -" the Source Explorer position. +" NOTE: The graph below shows my work platform with some VIM +" plugins, including 'Taglist', 'Source Explorer', and +" 'MiniBufExplorer'. + +" +----------------------------------------------------------+ +" |" Press to|[1:demo.c]* | +" | | | +" |-demo.c--------|-MiniBufExplorer--------------------------| +" | | | +" |function |/* This is the editor window. */ | +" | foo | | +" | bar |void foo(void) | +" | |{ | +" |~ |} | +" |~ | | +" |~ |void bar(void) | +" |~ |{ | +" |~ |} | +" |~ | | +" |~ |~ | +" |-__Tag_List__--|-demo.c-----------------------------------| +" |Source Explorer V2.5 | +" | | +" |~ | +" |~ | +" |~ | +" |-Source Explorer------------------------------------------| +" |: | +" +----------------------------------------------------------+ + +" TODO: You can change this function by yourselves in order +" to adapt the position of the editor window for the +" Source Explorer position. function! g:SrcExpl_OtherPluginAdapter() - " If the Taglist Plugin existed + " The Taglist existed if bufname("%") == "__Tag_List__" " Move the cursor to its right window. - " Because I used to put the taglist - " Window on my left. + " Because I usually put the taglist + " window on my left. (See the graph above) silent! wincmd l endif - " If the MiniBufExplorer Plugin existed + " The MiniBufExplorer existed if bufname("%") == "-MiniBufExplorer-" " Move the cursor to the window behind. - " Because I used to put the minibufexpl - " Window on the top position. + " Because I usually put the minibufexpl + " window on the top. (See the graph above) silent! wincmd j endif @@ -144,78 +197,86 @@ endfunction """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_Refresh() {{{ + " Refresh the Source Explorer window and update the status function! g:SrcExpl_Refresh() - let l:exp = '' - let l:rtn = 0 + let l:exp = "" + let l:rtn = 0 - " Only Source Explorer window is valid + " Only Source Explorer window is valid if &previewwindow return endif - " Avoid errors of multi-buffers - if &modified - " Tell the user what has happened + " Avoid errors of multi-buffers + if &modified + " Tell the user what has happened echohl ErrorMsg | - \ echo "SrcExpl: The current file was not saved." + \ echo "SrcExpl: The file you are editing is not saved." \ | echohl None - return - endif + return + endif " Get the symbol under the cursor let l:rtn = SrcExpl_GetSymbol() " The symbol is invalid if l:rtn != 0 return endif - let l:exp = '\C\<' . s:SrcExpl_Symbol . '\>' - - " Explore the source code using tag tool + " call SrcExpl_Debug('s:SrcExpl_symbol is (' . s:SrcExpl_symbol . ')') + let l:exp = '\C\<' . s:SrcExpl_symbol . '\>' + " First move to the Source Explorer window + silent! wincmd P + if &previewwindow + call SrcExpl_SetCurr() + " Go back to the privious window + silent! wincmd p + " Indeed back to the editor window + call g:SrcExpl_OtherPluginAdapter() + endif + " Explore the tag using tag tool " First Just try to get the definition of the symbol try - " First move to the Source Explorer window - silent! wincmd P - if &previewwindow - " Get the whole file path of the buffer before tag - let s:SrcExpl_EditFilePath = expand("%:p") - " Get the current line before tag - let s:SrcExpl_CurrLine = line(".") - " Get the current colum before tag - let s:SrcExpl_CurrCol = col(".") - " Go back to the privious window - silent! wincmd p - " Indeed back to the editor window - call g:SrcExpl_OtherPluginAdapter() - endif " Begin to tag the symbol exe 'silent ' . 'ptag /' . l:exp catch - " Tag failed - let s:SrcExpl_Status = 3 - " Tell the Source Explorer window - call SrcExpl_NoDef() - " Go back to the privious window again - silent! wincmd p - " Indeed back to the editor window - call g:SrcExpl_OtherPluginAdapter() - return + " Call the 'gd' command to get the local definition + " let l:rtn = SrcExpl_SearchDecl(l:exp) + " if l:rtn < 0 + " Tag failed + let s:SrcExpl_status = 3 + " Tell the Source Explorer window + call SrcExpl_NoDef() + " else + " Tag to the local definition + " let s:SrcExpl_status = 1 + " endif + " Go back to the privious window again + silent! wincmd p + " Indeed back to the editor window + call g:SrcExpl_OtherPluginAdapter() + return endtry " Tag successfully and move to the preview window silent! wincmd P if &previewwindow " Judge that if or not point to the definition - if (s:SrcExpl_EditFilePath == expand("%:p")) && - \ (s:SrcExpl_CurrLine == line(".")) && - \ (s:SrcExpl_CurrCol == col(".")) + if (s:SrcExpl_currPath ==# expand("%:p")) + \ && (s:SrcExpl_currLine == line(".")) + \ && (s:SrcExpl_currCol == col(".")) " Mulitple definitions - let s:SrcExpl_Status = 2 + let s:SrcExpl_status = 2 " List the multi-definitions in the Source Explorer call SrcExpl_ListTags(l:exp) else " Source Explorer Has pointed to the definition already - let s:SrcExpl_Status = 1 - " Make the definition hightlight + let s:SrcExpl_status = 1 + " Match the symbol call SrcExpl_MatchExpr() + " Highlight the symbol + call SrcExpl_HiExpr() + " Set the current editor property + call SrcExpl_SetCurr() endif " Go back to the privious window again silent! wincmd p @@ -223,9 +284,9 @@ function! g:SrcExpl_Refresh() call g:SrcExpl_OtherPluginAdapter() endif -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_GoBack() {{{ " Go Back from the definition context. " Users can call this function using their mapping key. @@ -236,11 +297,15 @@ function! g:SrcExpl_GoBack() if (!&previewwindow) " Jump back to the privous place exe "normal \" + " Open the folding if exists + if has("folding") + silent! . foldopen! + endif endif -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_EnterWin() {{{ " Opreation when WinEnter Event happens @@ -269,60 +334,103 @@ function! SrcExpl_EnterWin() \ :call g:SrcExpl_GoBack() " Unmapping the exact mapping of 'double-click' and 'enter' if maparg("<2-LeftMouse>", "n") == - \ ":call " . s:SrcExpl_ScriptID . + \ ":call " . s:SrcExpl_scriptID . \ "SrcExpl_Jump()" nunmap <2-LeftMouse> endif endif if maparg("", "n") == ":call " . - \ s:SrcExpl_ScriptID . "SrcExpl_Jump()" + \ s:SrcExpl_scriptID . "SrcExpl_Jump()" nunmap endif endif -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_Debug() {{{ -" Highlight the Symbol of definition +" Log the supplied debug information along with the time + +function! SrcExpl_Debug(log) + + " Debug switch is on + if s:SrcExpl_isDebug == 1 + " Log file path is valid + if s:SrcExpl_logPath != '' + " Output to the log file + exe "redir >> " . s:SrcExpl_logPath + " Add the current time + silent echon strftime("%H:%M:%S") . ": " . a:log . "\r\n" + redir END + endif + endif + +endfunction " }}} + +" SrcExpl_SetCurr() {{{ + +" Save the current file path, line number and colum number + +function! SrcExpl_SetCurr() + + " Get the whole file path of the buffer before tag + let s:SrcExpl_currPath = expand("%:p") + " Get the current line before tag + let s:SrcExpl_currLine = line(".") + " Get the current colum before tag + let s:SrcExpl_currCol = col(".") + +endfunction " }}} + +" SrcExpl_MatchExpr() {{{ + +" Match the Symbol of definition function! SrcExpl_MatchExpr() " First open the folding if exists if has("folding") - silent! .foldopen + silent! . foldopen! endif " Match the symbol and make it highlight - call search("$", "b") - let s:SrcExpl_Symbol = substitute(s:SrcExpl_Symbol, + call search("$", "b") + let s:SrcExpl_symbol = substitute(s:SrcExpl_symbol, \ '\\', '\\\\', "") - call search('\C\<\V' . s:SrcExpl_Symbol . '\>') - " Set the highlight color + call search('\V\C\<' . s:SrcExpl_symbol . '\>') + +endfunction " }}} + +" SrcExpl_HiExpr() {{{ + +" Highlight the Symbol of definition + +function! SrcExpl_HiExpr() + + " Set the highlight color hi SrcExpl_HighLight term=bold guifg=Black guibg=Magenta ctermfg=Black ctermbg=Magenta - " Highlight - exe 'match SrcExpl_HighLight "\%' . line(".") . 'l\%' . + " Highlight + exe 'match SrcExpl_HighLight "\%' . line(".") . 'l\%' . \ col(".") . 'c\k*"' - " Save the file path, the current line and the current - " col of the definition - let s:SrcExpl_EditFilePath = expand("%:p") - let s:SrcExpl_CurrLine = line(".") - let s:SrcExpl_CurrCol = col(".") -endfunction! +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_SelToJump() {{{ " Select one of multi-definitions, and jump to there. function! SrcExpl_SelToJump() + " If point to the Jump list head, just avoid that + if line(".") == 1 + return + endif + let l:i = 0 let l:f = "" let l:s = "" " Get the item data that user selected let l:list = getline(".") - " Traverse the prompt string until get the " file path while !((l:list[l:i] == ']') && @@ -352,9 +460,9 @@ function! SrcExpl_SelToJump() " then we add the '\' in front of it. if (l:list[l:i] == '*') let l:s = l:s . '\' . '*' - elseif (l:list[l:i] == '[') + elseif (l:list[l:i] == '[') let l:s = l:s . '\' . '[' - elseif (l:list[l:i] == ']') + elseif (l:list[l:i] == ']') let l:s = l:s . '\' . ']' else let l:s = l:s . l:list[l:i] @@ -366,21 +474,14 @@ function! SrcExpl_SelToJump() " Indeed back to the editor window call g:SrcExpl_OtherPluginAdapter() " Open the file of definition context - exe "edit " . s:SrcExpl_TagsFilePath . l:f - " Use EX Pattern to Jump to the exact line of the definition + exe "edit " . s:SrcExpl_tagsPath . l:f + " Use Ex Command to Jump to the exact line of the definition silent! exe l:s - if has("folding") - silent! .foldopen - endif - " Match the symbol word under the cursor - call search("$", "b") - let s:SrcExpl_Symbol = substitute(s:SrcExpl_Symbol, - \ '\\', '\\\\', "") - call search('\V\<' . s:SrcExpl_Symbol . '\>') + call SrcExpl_MatchExpr() -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_Jump() {{{ " Jump to the editor window and point to the definition @@ -392,13 +493,13 @@ function! SrcExpl_Jump() return endif " Do we get the definition already? - if (bufname("%") == s:SrcExpl_BufTitle) - if s:SrcExpl_Status == 3 " No definition + if (bufname("%") == s:SrcExpl_bufTitle) + if s:SrcExpl_status == 3 " No definition return endif endif " We got multiple definitions - if s:SrcExpl_Status == 2 + if s:SrcExpl_status == 2 call SrcExpl_SelToJump() return endif @@ -406,29 +507,29 @@ function! SrcExpl_Jump() silent! wincmd p " Indeed back to the editor window call g:SrcExpl_OtherPluginAdapter() - " We got only one definition - if s:SrcExpl_Status == 1 + " We got only one definition + if s:SrcExpl_status == 1 " Open the buffer using editor - exe "edit " . s:SrcExpl_EditFilePath + exe "edit " . s:SrcExpl_currPath " Jump to the context line of that symbol - call cursor(s:SrcExpl_CurrLine, s:SrcExpl_CurrCol) - if has("folding") - silent! .foldopen - endif + call cursor(s:SrcExpl_currLine, s:SrcExpl_currCol) + call SrcExpl_MatchExpr() endif -endfunction +endfunction " }}} + +" SrcExpl_NoDef() {{{ " Report to the Source Explorer what happens function! SrcExpl_NoDef() " Do the Source Explorer exsited already? - let l:bufnum = bufnr(s:SrcExpl_BufTitle) - + let l:bufnum = bufnr(s:SrcExpl_bufTitle) + " Not existed if l:bufnum == -1 " Create a new buffer - let l:wcmd = s:SrcExpl_BufTitle + let l:wcmd = s:SrcExpl_bufTitle else " Edit the existing buffer let l:wcmd = '+buffer' . l:bufnum @@ -444,121 +545,166 @@ function! SrcExpl_NoDef() setlocal buftype=nofile " Report the reason why Source Explorer " can not point to the definition - if s:SrcExpl_Status == 3 - " Delete all lines in buffer. - 1,$d _ - " Goto the end of the buffer put the buffer list - $ - " Display the version of the Source Explorer - put! ='Definition Not Found' - " Delete the extra trailing blank line - $ d _ + if s:SrcExpl_status == 3 + " Delete all lines in buffer. + 1,$d _ + " Goto the end of the buffer put the buffer list + $ + " Display the version of the Source Explorer + put! ='Definition Not Found' + " Cancel all the hightlighted words + match none + " Delete the extra trailing blank line + $ d _ endif " Make it unmodifiable again setlocal nomodifiable endif -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_ListTags() {{{ " Traversal the tags infomation from the tags file and list them function! SrcExpl_ListTags(exp) - " The tags file must be available, or quit. - if s:SrcExpl_TagsFilePath == "" - let s:SrcExpl_Status = 3 + " The tags file must be available, or quit. + if s:SrcExpl_tagsPath == "" + let s:SrcExpl_status = 3 call SrcExpl_NoDef() return endif - let l:bufnum = bufnr("__Source_Explorer__") + " Do the Source Explorer exsited already? + let l:bufnum = bufnr(s:SrcExpl_bufTitle) + " Create a new buffer if l:bufnum == -1 " Create a new buffer - let l:wcmd = "__Source_Explorer__" + let l:wcmd = s:SrcExpl_bufTitle else " Edit the existing buffer let l:wcmd = '+buffer' . l:bufnum endif " Reopen the Source Explorer idle window exe "silent! " . "pedit " . l:wcmd - " Return to the preview window + " Return to the preview window silent! wincmd P - " Done - if &previewwindow + " Done + if &previewwindow " Reset the proprity of the Source Explorer setlocal modifiable setlocal buflisted setlocal buftype=nofile - " Delete all lines in buffer - 1,$d _ - " Get the tags dictionary array - let l:list = taglist(a:exp) - let l:indx = 0 - " Loop for listing each tag from tags file - while 1 - " First get each tag list - let l:dict = get(l:list, l:indx, {}) - " There is one tag - if l:dict != {} - " Goto the end of the buffer put the buffer list - $ - put! ='[File Path]: '. l:dict['filename'] - \ . ' ' . '[EX Pattern]: ' . l:dict['cmd'] - else " Traversal finished - break - endif - let l:indx += 1 - endwhile - endif - " Delete the extra trailing blank line - $ d _ - " Move the cursor to the top of the Source Explorer window - exe "normal! gg" + " Delete all lines in buffer + 1,$d _ + " Get the tags dictionary array + let l:list = taglist(a:exp) + " Begin build the Jump List for exploring the tags + put! = '[Jump List]: '. s:SrcExpl_symbol . ' (' . len(l:list) . ') ' + " Match the symbol + call SrcExpl_MatchExpr() + " Highlight the symbol + call SrcExpl_HiExpr() + " Loop key & index + let l:indx = 0 + " Loop for listing each tag from tags file + while 1 + " First get each tag list + let l:dict = get(l:list, l:indx, {}) + " There is one tag + if l:dict != {} + " Goto the end of the buffer put the buffer list + $ + put! ='[File Name]: '. l:dict['filename'] + \ . ' ' . '[Ex Command]: ' . l:dict['cmd'] + else " Traversal finished + break + endif + let l:indx += 1 + endwhile + endif + " Delete the extra trailing blank line + $ d _ + " Move the cursor to the top of the Source Explorer window + exe "normal! gg" " Back to the first line setlocal nomodifiable -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_SearchDecl() {{{ + +" Search the local decleration -" Get key word symbol under the current cursor +function! SrcExpl_SearchDecl(exp) + + " Get the original cursor position + let l:oldline = line(".") + let l:oldcol = col(".") + " Try to search the local decleration + if searchdecl(a:exp, 0, 1) != 0 + " Search failed + return -1 + endif + " Get the new cursor position + let l:newline = line(".") + let l:newcol = col(".") + " Go back to the original cursor position + call cursor(l:oldline, l:oldcol) + " Preview the context + exe "silent " . "pedit " . expand("%:p") + " Go to the Preview window + silent! wincmd P + " Indeed in the Preview window + if &previewwindow + " Go to the new cursor position + call cursor(l:newline, l:newcol) + " Match the symbol + call SrcExpl_MatchExpr() + " Highlight the symbol + call SrcExpl_HiExpr() + endif + " Search successfully + return 0 + +endfunction " }}} + +" SrcExpl_GetSymbol() {{{ + +" Get the valid symbol under the current cursor function! SrcExpl_GetSymbol() " Get the current charactor under the cursor let l:cchar = getline('.')[col('.') - 1] - " Change it to ASCII code - let l:ascii = eval(char2nr(l:cchar)) + " Get the current word under the cursor + let l:cword = expand("") " Judge that if or not the charactor is invalid, " beause only 0-9, a-z, A-Z, and '_' are valid - if (l:ascii >= 48 && l:ascii <= 57) || - \ (l:ascii >= 65 && l:ascii <= 90) || - \ (l:ascii >= 97 && l:ascii <= 122) || - \ (l:ascii == 95) - " if the key word symbol has been explored + if (l:cchar =~ '\w') && (l:cword =~ '\w') + " If the key word symbol has been explored " just now, we will not explore that again - if s:SrcExpl_Symbol ==# expand("") + if s:SrcExpl_symbol ==# l:cword return -1 " Get a new key word symbol else - let s:SrcExpl_Symbol = expand("") + let s:SrcExpl_symbol = l:cword endif " Invalid charactor else - if s:SrcExpl_Symbol == '' - return -1 " Second, third ... + if s:SrcExpl_symbol == '' + return -2 " Second, third ... else " First - let s:SrcExpl_Symbol = '' + let s:SrcExpl_symbol = '' endif endif return 0 -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_GetInput() {{{ " Get the word inputed by user on the command line window @@ -567,30 +713,32 @@ function! SrcExpl_GetInput(note) " Be sure synchronize call inputsave() " Get the input content - let l:input = input(a:note) + let l:input = input(a:note) " Save the content call inputrestore() " Tell SrcExpl return l:input -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_ProbeTags() {{{ " Probe if or not there is a 'tags' file under the project PATH function! SrcExpl_ProbeTags() + " First get current work directory let l:tmp = getcwd() + " Get the raw work path - if l:tmp != s:SrcExpl_RawWorkPath + if l:tmp != s:SrcExpl_rawWorkPath " First load Source Explorer - if s:SrcExpl_RawWorkPath == "" + if s:SrcExpl_rawWorkPath == "" " Save that - let s:SrcExpl_RawWorkPath = l:tmp + let s:SrcExpl_rawWorkPath = l:tmp endif " Go to the raw work path - exe "cd " . s:SrcExpl_RawWorkPath + exe "cd " . s:SrcExpl_rawWorkPath endif let l:tmp = "" @@ -610,32 +758,34 @@ function! SrcExpl_ProbeTags() " Indeed in the system root dir if l:tmp == getcwd() " Clean the buffer - let s:SrcExpl_TagsFilePath = "" + let s:SrcExpl_tagsPath = "" " Have found a 'tags' file already else " UNIXs OS or MAC OS-X if has("unix") || has("macunix") if getcwd()[strlen(getcwd()) - 1] != '/' - let s:SrcExpl_TagsFilePath = + let s:SrcExpl_tagsPath = \ getcwd() . '/' endif " WINDOWS 95/98/ME/NT/2000/XP elseif has("win32") if getcwd()[strlen(getcwd()) - 1] != '\' - let s:SrcExpl_TagsFilePath = + let s:SrcExpl_tagsPath = \ getcwd() . '\' endif else " Other operating system echohl ErrorMsg | - \ echo "SrcExpl Plugin: Not support on this OS." + \ echo "SrcExpl: Not support on this OS platform." \ | echohl None endif endif -endfunction + call SrcExpl_Debug("s:SrcExpl_tagsPath is (" . s:SrcExpl_tagsPath . ")") -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +endfunction " }}} + +" SrcExpl_CloseWin() {{{ " Close the Source Explorer window and delete its buffer @@ -645,15 +795,15 @@ function! SrcExpl_CloseWin() pclose " Judge if or not the Source Explorer " buffer had been deleted - let l:bufnum = bufnr(s:SrcExpl_BufTitle) + let l:bufnum = bufnr(s:SrcExpl_bufTitle) " Existed indeed if l:bufnum != -1 - exe "bdelete! " . s:SrcExpl_BufTitle + exe "bdelete! " . s:SrcExpl_bufTitle endif -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_OpenWin() {{{ " Open the Source Explorer window under the bottom of (G)Vim, " and set the buffer's proprity of the Source Explorer @@ -661,7 +811,7 @@ endfunction function! SrcExpl_OpenWin() " Open the Source Explorer window as the idle one - exe "silent! " . "pedit " . s:SrcExpl_BufTitle + exe "silent! " . "pedit " . s:SrcExpl_bufTitle " Jump to the Source Explorer silent! wincmd P " Open successfully and jump to it indeed @@ -670,14 +820,14 @@ function! SrcExpl_OpenWin() setlocal buflisted " No exact file setlocal buftype=nofile - " Delete all lines in buffer - 1,$d _ - " Goto the end of the buffer - $ - " Display the version of the Source Explorer - put! ='Source Explorer V2.4' - " Delete the extra trailing blank line - $ d _ + " Delete all lines in buffer + 1,$d _ + " Goto the end of the buffer + $ + " Display the version of the Source Explorer + put! ='Source Explorer V2.5' + " Delete the extra trailing blank line + $ d _ " Make it no modifiable setlocal nomodifiable " Put it on the bottom of (G)Vim @@ -688,9 +838,9 @@ function! SrcExpl_OpenWin() " Indeed back to the editor window call g:SrcExpl_OtherPluginAdapter() -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_Cleanup() {{{ " Clean up the rubbish and free the mapping resouces @@ -709,21 +859,21 @@ function! SrcExpl_Cleanup() unmap endif " Unmap the user's key - if maparg(g:SrcExpl_RefreshMapKey, 'n') == + if maparg(g:SrcExpl_refreshKey, 'n') == \ ":call g:SrcExpl_Refresh()" - exe "unmap " . g:SrcExpl_RefreshMapKey + exe "unmap " . g:SrcExpl_refreshKey endif " Unmap the user's key - if maparg(g:SrcExpl_GoBackMapKey, 'n') == + if maparg(g:SrcExpl_gobackKey, 'n') == \ ":call g:SrcExpl_GoBack()" - exe "unmap " . g:SrcExpl_GoBackMapKey + exe "unmap " . g:SrcExpl_gobackKey endif " Unload the autocmd group silent! autocmd! SrcExpl_AutoCmd -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_Init() {{{ " Initialize the Source Explorer proprities @@ -732,7 +882,7 @@ function! SrcExpl_Init() " Access the Tags file call SrcExpl_ProbeTags() " Found one Tags file - if s:SrcExpl_TagsFilePath != "" + if s:SrcExpl_tagsPath != "" " Compiled with 'Quickfix' feature if !has("quickfix") " Can not create preview window without quickfix feature @@ -742,7 +892,7 @@ function! SrcExpl_Init() return -1 endif " Have found 'tags' file and update that - if g:SrcExpl_UpdateTags == 1 + if g:SrcExpl_updateTags != 0 " Call the external 'ctags' program silent !ctags -R * endif @@ -750,19 +900,19 @@ function! SrcExpl_Init() " Ask user if or not create a tags file echohl Question | \ let l:answer = SrcExpl_GetInput("SrcExpl: " - \ . "'tags' file isn't found in your PATHs.\n" - \ . "Create one in the current directory? (y or n)") + \ . "The 'tags' file isn't found in your PATH.\n" + \ . "Create one in the current directory now? (y or n)") \ | echohl None " They do if l:answer == "y" || l:answer == "yes" " Back from the root directory - exe "cd " . s:SrcExpl_RawWorkPath + exe "cd " . s:SrcExpl_rawWorkPath " Call the external 'ctags' program silent !ctags -R * " Rejudge the tags file if existed call SrcExpl_ProbeTags() " Maybe there is no 'ctags' program in user's system - if s:SrcExpl_TagsFilePath == "" + if s:SrcExpl_tagsPath == "" " Tell them what happened echohl ErrorMsg | \ echo "SrcExpl: Execute 'ctags' program failed." @@ -776,27 +926,27 @@ function! SrcExpl_Init() endif endif " First set the height of preview window - exe "set previewheight=". string(g:SrcExpl_WinHeight) + exe "set previewheight=". string(g:SrcExpl_winHeight) " Load the Tags file into buffer - exe "silent! " . "pedit " . s:SrcExpl_TagsFilePath . "tags" + exe "silent! " . "pedit " . s:SrcExpl_tagsPath . "tags" " Set the actual update time according to user's requestion " 1000 milliseconds by default - exe "set updatetime=" . string(g:SrcExpl_RefreshTime) + exe "set updatetime=" . string(g:SrcExpl_refreshTime) " Map the user's key to go back from the " definition context. - if g:SrcExpl_GoBackMapKey != "" - exe "nnoremap " . g:SrcExpl_GoBackMapKey . + if g:SrcExpl_gobackKey != "" + exe "nnoremap " . g:SrcExpl_gobackKey . \ " :call g:SrcExpl_GoBack()" endif " Map the user's key to refresh the definition " updating manually. - if g:SrcExpl_RefreshMapKey != "" - exe "nnoremap " . g:SrcExpl_RefreshMapKey . + if g:SrcExpl_refreshKey != "" + exe "nnoremap " . g:SrcExpl_refreshKey . \ " :call g:SrcExpl_Refresh()" endif - " First get the srcexpl.vim's ID + " First get the SrcExpl.vim's ID map xx xx - let s:SrcExpl_ScriptID = substitute(maparg('xx'), + let s:SrcExpl_scriptID = substitute(maparg('xx'), \ '\(\d\+_\)xx$', '\1', '') unmap xx " Then form an autocmd group @@ -809,17 +959,18 @@ function! SrcExpl_Init() " Initialize successfully return 0 -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" SrcExpl_Toggle() {{{ -" The User Interface function for open / close -" the Source Explorer +" The User Interface function for open / close the Source Explorer function! SrcExpl_Toggle() + call SrcExpl_Debug("s:SrcExpl_isOpen is (" . s:SrcExpl_isOpen . ")") + " Already closed - if s:SrcExpl_Switch == 0 + if s:SrcExpl_isOpen == 0 " Initialize the proprities let l:rtn = SrcExpl_Init() " Initialize failed @@ -830,24 +981,30 @@ function! SrcExpl_Toggle() " Create the window call SrcExpl_OpenWin() " Set the switch flag on - let s:SrcExpl_Switch = 1 + let s:SrcExpl_isOpen = 1 " Already Opened else " Set the switch flag off - let s:SrcExpl_Switch = 0 + let s:SrcExpl_isOpen = 0 " Close the window call SrcExpl_CloseWin() " Do the cleaning work - call SrcExpl_Cleanup() + call SrcExpl_Cleanup() endif -endfunction +endfunction " }}} -"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" Avoid side effects {{{ set cpoptions& let &cpoptions = s:save_cpo unlet s:save_cpo +" }}} + +"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" + +" vim:foldmethod=marker:tabstop=4 + """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""