" ------------------------------------------------------------------------------
" File: plugin/directoryBrowser.vim - Directory Browser -
" Directory listing and file operation in vim
" Author: Alexandre Viau
" Maintainer: Alexandre Viau
"
" Licence: This program is free software; you can redistribute it and/or
" modify it under the terms of the GNU General Public License.
" See http://www.gnu.org/copyleft/gpl.txt
" This program is distributed in the hope that it will be
" useful, but WITHOUT ANY WARRANTY; without even the implied
" warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"
" Version: 4.2
"
" Files: plugin/directoryBrowser.vim
"
" History:
" 1.0 2012-08-04
" - First release
" 2.0 2012-08-09
" - Put 36 caracters instead of 39 between beginning of line and filename in cPath
" - Added \. to \a to \z and \A to \Z
" 3.0 2012-08-23
" - \a to \z list a:\ to z:\ root directories in current buffer (appended)
" - \A to \Z list a:\ to z:\ root directories in new buffer
" - -a to -z list a:\ to z:\ root directories in split window
" - -A to -Z list a:\ to z:\ root directories in vertical split window
" - =a to =z list a:\ to z:\ root directories in new tab
" - f/ Change backslash to forwardslash on the current line
" - f Change forwardslash to backslash on the current line
" - ff Change backslash to double backslash on the current line
" - fff Change forwardslash to double backslash on the current line
" - ffff Change doublebackslash to backslash the current line
" - copy current file's path+filename relative to current directory with backslash
" - copy current file's path+filename relative to current directory with forward slash
" - copy current file's path only (with backslash)
" - copy current file's path only (with forward slash)
" - copy current file's filename only (without path)
" - copy current file's path+filename relative to current directory with the line number (in utl format)
" - ! Layout 1 (2 vertical panes)
" - @ Layout 2 (2 horizontal panes)
" - # Layout 3 (3 panes: 2 vertical, 1 horizontal)
" - $ Layout 4 (4 panes)
" - ^ Layout 6 (6 panes)
" - * Layout 8 (8 panes)
" - ) Layout 10 (10 panes)
" - - toggle show file owner
" - 0 to change directory attributes
" - 9 to change directory time display and sorting
" - a interchanged with A
" - b changed list buffers to open in new tab instead of opening in current tab (not related to directory browsing but useful. Requires utl.vim)
" - I open file in internet explorer
" - L to list current directory and its subdirectories recursively
" - M open a "contextual menu" to run operations on files (requires the utl plugin)
" - n new file in new tab
" - N new file in current tab
" - O open file in notepad
" - q show available volumes
" - Q show available volumes with names and details about shares and computers
" - T open directory in new tab
" - u open filename in clipboard in current tab
" - U open filename in clipboard in new tab
" - w to save (append) directory listing to disk (useful for shorcuts)
" - W to save (overwrite) directory listing to disk
" - X to show directory and subdirectories structure of current directory using the "tree" command
" - y list the directory of the path in clipboard (with or without filename) in a new split window
" - Y list the directory of the path in clipboard (with or without filename) in the current buffer
" - open utl link under cursor (utl plugin required)
" - z open utl link under cursor in a split window (utl plugin required)
" - Z open utl link under cursor in tab (utl plugin required)
" - Remove dirOfStr language requirement. Searching for a language specific string was not needed.
" - Changes with cursor position and listing position specially using i and m.
" - Set g:dir instead of g:dirBuf as defaut when doing \A..\Z (listing drives using capital letters) with is more convenient because this way one my list content of another drive in the same buffer using the \ shortcut key so one may have all his directory browsing inside the same buffer, with is nice if saved to file using w for later viewing and continuation.
" - Added F to list the directory of the current file
" - Changed behavior of i "go to previous dir listing", now it will go to top of current dir then to previous dir if i is typed a second time.
" - Modified search string to find current directory (not to conflict with the directory showned by the tree command)
" - uu, uuu, uuuu, uuuuu, UU, UUU, UUUU, uur abbreviations used with the utl plugin to insert links and references
" 3.1
" - Don't use version 3.0, use version 3.1 instead.
" - open default 'browsing history' file called 'dirlist_', useful for fast browsing
" 3.2
" - Modified the behavior of the C command, in some cases it was not changing to the directory
" 3.3
" - J show browsing history (show list of paths and files browsed in this session. Useful to go back to previously browsed directories even to go return to directories of files that were opened or run etc. The list may be saved with the w command. Utl plugin required.
" - Removed a tab character that was invisible after the z command
"
" 4.0 - 2012-09-04 11:43:43 (mar.) Changes to many code not to use registers to keep paths info, but global variables instead. These global variables may be used anywhere in vim, and if the registers are needed to paste path information, do p to copy the path info from the variables to the registers. Now registers will not be overwritten by the plugin except for the @H register which contains the browsing history.
" - 2012-09-04 11:45:27 (mar.) There are new global variables similar to the variable to keep path info, but they keep the previous file path info. This is used to execute actions from one window or tab to another, one example is the contextual menu, other example may be project files with link to compile, run etc.
" - 2012-09-03 11:05:17 (lun.) Modified M now the contextual menu is very useful as it opens in a split window and may execute actions on the file where the cursor was before switching to the menu window. That file may be a file in edition or a directory listing. So instead of copying links at the top of the file like in previous versions of the plugin, now the menu is opened in a split window (or a tab). It is still possible to put links at the top of files if the links are specific to the file, but it is more convenient to have it in a split window. Autocommands onEnter and onLeave window/tab where added to make this possible, these autocommands copy the paths when entering or leaving a window/tab and keep the previous path in registers and if the file moved to is the menu file, the current file path registers do not contain the path of the menu file like other files but they contain the path of the previous file, so that it may be used for lauching actions on the previous file be it a file in edition or a file in a directory listing.
" - 2012-09-03 23:25:23 (lun.) I included a big part of my own to-date contextual menu as an example in this plugin usage section. This one works with the new modifications concerning the menu file displayed in a window/tab.
" - 2012-09-04 00:18:07 (mar.) The paths are not copied anymore to the registers at each operations, only when the user presses p the paths are copied to register for pasting.
" - 2012-08-31 05:36:44 (ven.) Modified to execute the command in the current path of the current directory listing if no path specified.
" - 2012-08-31 05:41:22 (ven.) Modified i and m to changed copy the path at the same time the listing is changed and to change vim's directory to the copied directory.
"
" 4.1 - 2012-09-04 22:08:16 (mar.) Fix 2 places in code (in dirDelete and recursive grep) that were not changed to use the path variables.
" - 2012-09-05 23:38:32 (mer.) Added open utl link under cursor (utl plugin required)
" 4.2
" - 2012-09-08 14:47:07 (sam.) Modified h, l behavior. Added the ability to remember last position of the cursor in a directory so that if a user returns to a previously browsed directory, the cursor will position itself to the file it was last time in that directory.
"
" 4.3
" - 2012-09-11 22:11:06 (mar.) Corrected bug concerning the @H browsing history register.
" - 2012-09-11 22:11:46 (mar.) Corrected issues with cursor position on previously browsed files and directory.
" - 2012-09-11 21:30:55 (mar.) Corrected bug when first time vim opened and directory listed, there was an error message saying that empty dictionnary, that is because no directory was browsed so the variables to index the dictionnary were empty.
" - 2012-09-11 21:30:49 (mar.) Small amelioration of the isDir() detection function, added \s\{4\} to the time regex. I add problems with a file menu that this function was detecting as a directory listing but it was not.
" - 2012-09-11 22:36:21 (mar.) Two commands added to the compile/run sample menu in the help section, these 2 commands are to source a vim script and source a vim macro.
"
" 4.4
" - 15.09.2012 11:42:05 (??) Small modification to the search string for finding previously browsed directory.
"
" Overview
" --------
" This plugin is currently only for Windows but could be easily ported for another OS. Some optional features require cygwin and/or the utl plugin.
"
" The dir plugin allows fast directory browsing inside vim buffers,
" split windows or tabs. It uses the windows dir command to list
" directories inside buffers and it provides several commands
" to be executed on the listed files or directories including
" dir listing, sorting, filtering, recursive file searching,
" recursive file greping, file renaming, file deleting,
" file cloning, file executing and file opening/editing in buffer,
" tab, window and external tools and changing listing options.
"
" The way it works is simple. It insert the output of the windows dir
" command at the end of a buffer and then after the user has moved
" the cursor to one of the lines of this directory listing and
" executes one of the pre-defined (or user defined) command mappings,
" the path of the directory and the name of the file/directory are
" copied to some registers and are used to execute the command on the
" file or directory.
"
" For example if the command mapping \C is executed the directory of
" c:\ is listed at the end of a new split window (every drive may be
" accessed using \A, \B, \C, ..., \Z or \a..\z to append a listing to current buffer).
"
" Volume in drive C has no label.
" Volume Serial Number is C0B2-2A46
"
" Directory of c:\
"
" 2013-05-08 22:01 .vim-fuf-data
" 2012-07-10 23:22 clip
" 2012-07-23 06:36 cygwin
" 2012-07-31 12:29 Documents and Settings
" 2012-08-01 04:27 oraclexe
" 2012-07-31 12:27 Program Files
" 2012-08-02 10:32 repository
" 2012-08-02 01:58 temp
" 2012-07-08 23:24 test
" 2012-08-03 23:31 tmp
" 2012-08-02 08:01 WINNT
" 2012-08-04 09:11 27ÿ396 _viminfo
" 2012-07-07 00:46 128 test.ctl
" 2012-07-07 00:45 94 test.sql
" 2012-07-26 00:05 74 test.txt
" 10 File(s) 95ÿ932 bytes
" 12 Dir(s) 2ÿ694ÿ692ÿ864 bytes free
"
" If the user moves the cursor on the "Program Files" for example,
" and presses l, the content of the "Program Files"
" directory will be listed in this same buffer after this listing
" of the c:\ directory. The listing of the "Program Files"
" directory will be positionned correctly at the top of the screen
" so the content of the c:\ will not be seed anymore unless the
" user moves the cursor upwards outside of the current screen. If
" The user presses then h the directory will go one level
" up the directory tree back to the c:\ directory.
"
" So all the commands are executed on the line where the cursor is
" positionned to. Many commands are already available to be
" be executed on the directories or files where the cursor is
" positionned to, and also the users may add some to their
" owned vimrc file like this user defined command for example here
" below which sends the current file where the cursor is positionned
" to an external tool:
"
" nmap y :call g:cPath() \| silent exe '!start I:\data\AutoHotkey\repository_usb_only\repository.exe "' . g:dirCs . '"'
"
" This mapping is in my vimrc and not in the plugin because it is
" a tool that is used only by myself. Here the g:cPath() function
" will take care to copy the path of the current file to the variable
" g:dirCs, which contains the full path, then
" then the external command is executed on this path. So many of the
" commands work this way.
"
" The movements between directories happen quite fast.
" The dir command was used because it performed faster on the test
" computer than the cygwin ls command. So portability was somehow
" sacrificed for performance. But it would be easy to adapt this
" plugin to make it work with the ls command on linux if the
" dir command is not available.
"
" Note that when the g:dir or its variants g:dir, g:dirTab, g:dirSplit etc
" are executed, the encoding is changed so that the dir command output
" is displayed correctly. The encoding is changed with the following command
" set encoding=cp850 " DOS 850: Western European
" You may change this encoding or remove/comment the command if you
" don't want your encoding to be changed each time a dir command is issued.
"
" Installation
" ------------
" Copy the directoryBrowser.vim file to the vim plugin directory and restart vim,
" or open directoryBrowser.vim for editing and do :so %.
"
" Optionnaly install the utl.vim plugin developed by Stephan Bittner,
" which add text hyperlinking habilities. Used with this directoryBrowser.vim plugin,
" it will allow to jump to the files hyperlinked in the results of
" the grep and find commands as well as the buffer listing commands.
"
" Usage
" -----
" Note that any mappings may be changed. If you don't like the usage of or \ before some commands you may change this easily. may be changed by for example.
"
" \a to \z list a:\ to z:\ root directories in current buffer (appended)
" \A to \Z list a:\ to z:\ root directories in new buffer
" -a to -z list a:\ to z:\ root directories in split window
" -A to -Z list a:\ to z:\ root directories in vertical split window
" =a to =z list a:\ to z:\ root directories in new tab
" f/ Change backslash to forwardslash on the current line
" f Change forwardslash to backslash on the current line
" ff Change backslash to double backslash on the current line
" fff Change forwardslash to double backslash on the current line
" ffff Change doublebackslash to backslash the current line
" ; Command shortcuts (Optional. This dictionnary g:cmdDict may be moved to your vimrc and may contain the commands you want. It is used with the g:cmdExe function and the mapping ; and it is not absolutly part of this plugin. I put it inside the plugin because it may be a fast way to open directories). If commands were previously entered, you may use the up/down or ctrl+p/ctrl+n on the command line after doing ;
" : Show list of command shortcuts
" execute command on the vim command line (fast if escape remapped to capslock). If no path specified, executes in the current path of current directory listing.
" open default 'browsing history' file called 'dirlist_', useful for fast browsing
" search files (including in subdirectories). Cygwin should be installed for this command to work.
" grep files (including in subdirectories). Cygwin should be installed for this command to work. It will ask for keywords to search and a file filter by which by default is *.
" copy current file's path+filename relative to current directory with backslash
" copy current file's path+filename relative to current directory with forward slash
" copy current file's path only (with backslash)
" copy current file's path only (with forward slash)
" copy current file's filename only (without path)
" copy current file's path+filename relative to current directory with the line number (in utl format)
" ! Layout 1 (2 vertical panes)
" @ Layout 2 (2 horizontal panes)
" # Layout 3 (3 panes: 2 vertical, 1 horizontal)
" $ Layout 4 (4 panes)
" ^ Layout 6 (6 panes)
" * Layout 8 (8 panes)
" ) Layout 10 (10 panes)
" - toggle show file owner
" 1 sort directory by name ascending
" 2 sort directory by name descending
" 3 sort directory by type ascending
" 4 sort directory by type descending
" 5 sort directory by size ascending
" 6 sort directory by size descending
" 7 sort directory by date ascending
" 8 sort directory by date descending
" 0 to change directory attributes
" 9 to change directory time display and sorting
" . reload directory (refresh)
" a write path to open (windows explorer address bar like) (append to current buffer)
" A write path to open (windows explorer address bar like) (open in new buffer)
" b list buffers to open in new tab (not related to directory browsing but useful. Requires utl.vim)
" B list buffers to delete (not related to directory browsing but useful. Requires utl.vim)
" c duplicate (clone) file
" C open current directory in command prompt
" d new directory
" D delete file or directory
" e open file in current buffer
" f Set filter to show only certain files example: *.txt or pic*.jpg
" F list directory of current file
" g grep current file (file is opened inside vim and vimgrep is used. No need for cygwin here.)
" h up directory, locate where the cursor was last time in this directory
" i go to previous dir listing (if many listings were done in the same buffer it allows to go directly back to a previous listing)
" I open file in internet explorer
" j preview file (rapidly includes the file into the current buffer. Do k to remove the file and go to next file in the dir listing. Doing successively j and k allows to preview quickly one file after another. If directory listings are saved to disk, they may be quickly opened using this mapping j to list them and browse them.)
" J show browsing history (show list of paths and files browsed in this session. Useful to go back to previously browsed directories even to return to directories of files that were opened or run etc. The list may be saved with the w command. Utl plugin required.
" k used with preview file to remove the preview and go down one line to next file to preview
" l list directory (go inside subdir), locate where the cursor was last time in this directory
" L list directory recursively (go inside subdir)
" m go to next dir listing (if many listings were done in the same buffer it allows to go directly to the next listing)
" M open a "contextual menu" in a split window to run operations on files. This requires the utl plugin. The contextual menu is a text file containing utl links that are executed using paths saved to registers. For example here's the content of a contextual menu file, it contains some links to compile csharp code and to edit the menu itself, you may create a menu to your liking.
"- Edit
"
"
"
"
"
"
"- Open
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"- Copy to
"
"
"
"
"
"
"
"
"
"
"
"
"
"- Compile/run
"
"
"rem
"
"'
"
"//
"//
"//
"//
"//
"//
"//
"- Vimscript code
"
"
"
"
"
"
" n new file in new tab
" N new file in current tab
" o open list dir pointed to by a windows .lnk file (dosen't work for all .lnk to directory files, to improve)
" O open file in notepad
" p copy file path and name. The path is copied in several formats to different variables. p will refresh these variables with current path and copy those to corresponding registers ex: g:dirPz to register @z, g:dirPf to register @f (the exception is g:dirPs to register @*). Often the paths needs to be pasted to files for linking or documentation etc. See function s:divPath()
" 1- in g:dirCp (directory only) with \
" 2- in g:dirC* (directory and filename) with \
" 3- in g:dirCz (directory only) with /
" 4- in g:dirCx (directory and filename) with /
" 5- in g:dirCf (filename only)
" P show current path
" q show available volumes
" Q show available volumes with names, plus, details about shares and computers
" r run file
" R rename file or directory
" s open file in split
" S list current directory in a split window (no need to have already a directory browser opened)
" t open file in tab
" T list current directory in a tab (no need to have already a directory browser opened)
" u open filename in clipboard in current tab
" U open filename in clipboard in new tab
" v open file in vsplit
" V list current directory in a vsplit window (no need to have already a directory browser opened)
" w write append directory listing to disk
" W write overwrite directory listing to disk
" x open current directory in windows explorer
" X Show tree of directory and subdirectories using the tree command
" y list the directory of the path in clipboard (with or without filename) in a new buffer
"