1 "=============================================================================
3 " Author : M A Aziz Ahmed (aziz@123india.com)
4 " Last update : Sun Feb 27 2000
6 "-----------------------------------------------------------------------------
7 " This file implements a file explorer. Latest version available at:
8 " http://www.freespeech.org/aziz/vim/my_macros/
9 "-----------------------------------------------------------------------------
10 " Just type ,e to launch the file explorer (this file should have been
11 " sourced) in a separate window. Type ,s to split the current window and
12 " launch explorer there. If the current buffer is modified, the window is
13 " anyway split (irrespective of ,e or ,s).
14 " It is also possible to delete files and rename files within explorer.
15 " (In UNIX, renaming doesn't seem to work, though!)
16 " The directory which explorer uses by default is determined by the 'browsedir'
18 "=============================================================================
20 nmap ,e :call ExplInitiate(0)<cr>
21 nmap ,s :call ExplInitiate(1)<cr>
23 function! ExplInitiate(split, ...)
24 if (expand("%:p:t")=="_fileExplorer.tmp")
25 echo "Already in file explorer"
30 call ExplInitializeDirName("")
32 call ExplInitializeDirName(a:1)
34 if ((&modified==1) || (a:split==1))
42 call ExplProcessFile(g:currDir)
46 function! ExplInitializeDirName(dirName)
50 let startDir=expand("%:p:h")
51 elseif ((!exists("g:currDir")) || (&bsdir=="current"))
54 let startDir=expand(g:currDir)
56 elseif (!exists("g:currDir"))
59 let startDir=expand(g:currDir)
62 let startDir = a:dirName
64 let g:currDir=(substitute(startDir,"\\","/","g"))."/"
65 " In case the ending / was already a part of getcwd(), two //s would appear
66 " at the end of g:currDir. So remove one of them
67 let g:currDir=substitute(g:currDir,"//$","/","g")
68 let g:currDir=substitute(g:currDir,"/./","/","g")
71 function! ExplProcessFile(fileName)
72 if ((isdirectory(a:fileName)) || (a:fileName==g:currDir."../"))
77 if (a:fileName==g:currDir."../")
78 let g:currDir=substitute(g:currDir,"/[^/]*/$","/","")
80 let g:currDir=a:fileName
83 " exec("cd ".escape(g:currDir, ' '))
84 call ExplDisplayFiles(g:currDir)
86 echo "Loaded contents of ".g:currDir
88 elseif (filereadable(a:fileName))
92 exec("e! ".escape(a:fileName, ' '))
93 call ExplCloseExplorer()
98 function! ExplGetFileName()
99 return g:currDir.getline(".")
102 function! ExplAddHeader()
103 " Give a very brief help
104 let @f="\" <enter> : open file or directory\n"
105 let @f=@f."\" - : go up one level c : change directory\n"
106 let @f=@f."\" r : rename file d : delete file\n"
107 let @f=@f."\" q : quit file explorer s : set this dir to current directory\n"
108 let @f=@f."\"---------------------------------------------------\n"
109 let @f=@f.". ".g:currDir."\n"
110 let @f=@f."\"---------------------------------------------------\n"
111 " Add parent directory
118 function! ExplDisplayFiles(dir)
119 let @f=glob(a:dir."*")
123 .,$g/^/call ExplMarkDirs()
128 function! ExplMarkDirs()
131 "Remove slashes if added
133 "Removes all the leading slashes and adds slashes at the end of directories
134 s;^.*\\\([^\\]*\)$;\1;e
135 s;^.*/\([^/]*\)$;\1;e
137 if (isdirectory(ExplGetFileName()))
140 " Move the file at the end so that directories appear first
146 function! ExplDeleteFile() range
153 let currLine = a:firstline
154 let lastLine = a:lastline
155 while ((currLine <= lastLine) && (stopDel==0))
157 let fileName=ExplGetFileName()
158 if (isdirectory(fileName))
159 echo fileName." : Directory deletion not supported yet"
160 let currLine = currLine + 1
163 let sure=input("Delete ".fileName."?(y/n/a/q) ")
168 if ((sure=="y") || (sure=="a"))
169 let success=delete(fileName)
172 echo "\nCannot delete ".fileName
173 let currLine = currLine + 1
176 let filesDeleted = filesDeleted + 1
177 let lastLine = lastLine - 1
182 let currLine = currLine + 1
186 echo "\n".filesDeleted." files deleted"
191 function! ExplRenameFile()
192 let fileName=ExplGetFileName()
193 if (isdirectory(fileName))
194 echo "Directory renaming not supported yet"
195 elseif (filereadable(fileName))
196 let altName=input("Rename ".fileName." to : ")
198 let success=rename(fileName, g:currDir.altName)
200 echo "Cannot rename ".fileName. " to ".altName
202 echo "Renamed ".fileName." to ".altName
205 exec("s/^\\S*$/".altName."/")
212 function! ExplGotoDir(dummy, dirName)
213 if (isdirectory(expand(a:dirName)))
214 " Guess the complete path
215 if (isdirectory(expand(getcwd()."/".a:dirName)))
216 let dirpath=getcwd()."/".a:dirName
218 let dirpath=expand(a:dirName)
220 call ExplInitializeDirName(dirpath)
221 call ExplProcessFile(g:currDir)
223 echo a:dirName." : No such directory"
227 function! ExplCloseExplorer()
228 bd! /_fileExplorer.tmp
229 if (exists("g:oldCh"))
234 function! ExplBack2PrevFile()
235 if ((@#!="") && (@#!="_fileExplorer.tmp") && (b:splitWindow==0) &&
236 \(isdirectory(@#)==0))
239 call ExplCloseExplorer()
242 function! ExplSyntaxFile()
243 if 1 || has("syntax") && exists("syntax_on") && !has("syntax_items")
244 syn match browseSynopsis "^\".*"
245 syn match browseDirectory "[^\"].*/$"
246 syn match browseCurDir "^\. .*$"
248 if !exists("g:did_browse_syntax_inits")
249 let did_browse_syntax_inits = 1
250 hi link browseSynopsis PreProc
251 hi link browseDirectory Directory
252 hi link browseCurDir Statement
257 function! ExplEditDir(fileName)
258 if (isdirectory(a:fileName))
259 " Do some processing if the path is relative..
260 let completePath=expand("%:p")
261 call ExplInitiate(0, completePath)
262 elseif ((expand("%")=="") && (bufloaded(".")==1))
263 " This is a workaround for a vim bug in Windows. When one tries to edit
265 " expand("%") *sometimes* returns a blank string
266 call ExplInitiate(0, getcwd())
272 au BufEnter _fileExplorer.tmp let oldSwap=&swapfile | set noswapfile
273 au BufLeave _fileExplorer.tmp let &swapfile=oldSwap
274 au BufEnter _fileExplorer.tmp nm <cr> :call ExplProcessFile(ExplGetFileName())<cr>
275 au BufLeave _fileExplorer.tmp nun <cr>
276 au BufEnter _fileExplorer.tmp nm - :call ExplProcessFile(g:currDir."../")<cr>
277 au BufLeave _fileExplorer.tmp nun -
278 au BufEnter _fileExplorer.tmp nm c :ChangeDirectory to:
279 au BufLeave _fileExplorer.tmp nun c
280 au BufEnter _fileExplorer.tmp nm r :call ExplRenameFile()<cr>
281 au BufLeave _fileExplorer.tmp nun r
282 au BufEnter _fileExplorer.tmp nm d :. call ExplDeleteFile()<cr>
283 au BufLeave _fileExplorer.tmp nun d
284 au BufEnter _fileExplorer.tmp vm d :call ExplDeleteFile()<cr>
285 au BufLeave _fileExplorer.tmp vun d
286 au BufEnter _fileExplorer.tmp nm q :call ExplBack2PrevFile()<cr>
287 au BufLeave _fileExplorer.tmp nun q
288 au BufEnter _fileExplorer.tmp nm s :exec ("cd ".escape(g:currDir,' '))<cr>
289 au BufLeave _fileExplorer.tmp nun s
290 au BufEnter _fileExplorer.tmp command! -nargs=+ -complete=dir ChangeDirectory call ExplGotoDir(<f-args>)
291 au BufLeave _fileExplorer.tmp delcommand ChangeDirectory
292 au BufEnter * nested call ExplEditDir(expand("%"))