Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Version 6.0.2: Initial upload

  • Loading branch information...
commit 269f9fadcec4be9c48f5842dec07224edd67abe3 0 parents
authored July 25, 2001 vim-scripts committed October 17, 2010
20  README
... ...
@@ -0,0 +1,20 @@
  1
+This is a mirror of http://www.vim.org/scripts/script.php?script_id=42
  2
+
  3
+With bufexplorer, you can quickly and easily switch between buffers by using the one of the default public interfaces:
  4
+
  5
+  '\be' (normal open)  or
  6
+  '\bs' (force horizontal split open)  or
  7
+  '\bv' (force vertical split open)
  8
+
  9
+Once the bufexplorer window is open you can use the normal movement keys (hjkl) to move around and then use <Enter> or <Left-Mouse-Click> to select the buffer you would like to open. If you would like to have the selected buffer opened in a new tab, simply press either <Shift-Enter> or 't'. Please note that when opening a buffer in a tab, that if the buffer is already in another tab, bufexplorer can switch to that tab automatically for you if you would like. More about that in the supplied VIM help.
  10
+
  11
+Bufexplorer also offers various options including:
  12
+- Display the list of buffers in various sort orders including:
  13
+    - Most Recently Used (MRU) which is the default
  14
+    - Buffer number
  15
+    - File name
  16
+    - File extension
  17
+    - Full file path name
  18
+- Delete buffer from list 
  19
+
  20
+For more about options, sort orders, configuration options, etc. please see the supplied VIM help.
403  plugin/bufexplorer.vim
... ...
@@ -0,0 +1,403 @@
  1
+"=============================================================================
  2
+"     Name Of File: bufexplorer.vim
  3
+"      Description: Buffer Explorer Plugin
  4
+"       Maintainer: Jeff Lanzarotta (frizbeefanatic@yahoo.com)
  5
+"      Last Change: Thursday, June 28, 2001
  6
+"          Version: 6.0.2
  7
+"            Usage: Normally, this file should reside in the plugins
  8
+"                   directory and be automatically sourced. If not, you must
  9
+"                   manually source this file using ':source bufexplorer.vim'.
  10
+"
  11
+"                   Run ':BufExplorer' to launch the explorer and runs the
  12
+"                   user-specified command in the current window, or
  13
+"                   ':SBufExplorer' to launch the explorer and run the
  14
+"                   user-specified command in the the newly split window.
  15
+"                  
  16
+"                   You may want to add the following three key mappings to
  17
+"                   your _vimrc/.vimrc file.
  18
+"                   
  19
+"                     map <Leader>b :BufExplorer<cr>
  20
+"                     map <Leader>B :SBufExplorer<cr>
  21
+"                     map <c-leftmouse> :BufExplorer<cr>
  22
+"
  23
+"                   If the current buffer is modified, the current window is
  24
+"                   always split.
  25
+"                  
  26
+"                   To control where the new split windows goes relative to
  27
+"                   the current window, use the variable:"                   
  28
+"                     let g:bufExplSplitBelow=0  " Put new window above
  29
+"                                                " current.
  30
+"                     let g:bufExplSplitBelow=1  " Put new window below
  31
+"                                                " current.
  32
+"                   The default for this is to split 'above'.
  33
+"=============================================================================
  34
+
  35
+" Has this already been loaded?
  36
+if exists("loaded_bufexplorer")
  37
+  finish
  38
+endif
  39
+
  40
+let loaded_bufexplorer = 1
  41
+
  42
+" 
  43
+" Create commands.
  44
+" 
  45
+if !exists(':BufExplorer')
  46
+  command BufExplorer :call s:StartBufExplorer(0)
  47
+endif
  48
+if !exists(':SBufExplorer')
  49
+  command SBufExplorer :call s:StartBufExplorer(1)
  50
+endif
  51
+
  52
+" 
  53
+" Show detailed help?
  54
+" 
  55
+if !exists("g:bufExplorerDetailedHelp")
  56
+  let g:bufExplorerDetailedHelp = 0
  57
+endif
  58
+
  59
+" When opening a new windows, split the new windows below or above the
  60
+" current window?  1 = below, 0 = above.
  61
+if !exists("g:bufExplSplitBelow")
  62
+  let g:bufExplSplitBelow = &splitbelow
  63
+endif
  64
+
  65
+" 
  66
+" StartBufExplorer
  67
+" 
  68
+function! s:StartBufExplorer(split)
  69
+  " Save the user's settings.
  70
+  let saveSplitBelow = &splitbelow
  71
+
  72
+  " Save current and alternate buffer numbers for later.
  73
+  let s:currentBufferNumber = bufnr("%")
  74
+  let s:alternateBufferNumber = bufnr("#")
  75
+  
  76
+  " Set to our new values.
  77
+  let &splitbelow = g:bufExplSplitBelow
  78
+
  79
+  if a:split || (&modified && &hidden == 0)
  80
+    sp [BufExplorer]
  81
+    let w:bufExplorerSplitWindow = 1
  82
+  else
  83
+    e [BufExplorer]
  84
+    let w:bufExplorerSplitWindow = 0
  85
+  endif
  86
+
  87
+  call s:DisplayBuffers()
  88
+  
  89
+  " Restore the user's settings.
  90
+  let &splitbelow = saveSplitBelow
  91
+
  92
+  unlet saveSplitBelow
  93
+endfunction
  94
+
  95
+" 
  96
+" DisplayBuffers.
  97
+" 
  98
+function! s:DisplayBuffers()
  99
+  " Turn off the swapfile, set the buffer type so that it won't get written,
  100
+  " and so that it will get deleted when it gets hidden.
  101
+  setlocal modifiable
  102
+  setlocal noswapfile
  103
+  setlocal buftype=nofile
  104
+  setlocal bufhidden=delete
  105
+  setlocal nowrap
  106
+  
  107
+  " Prevent a report of our actions from showing up.
  108
+  let oldRep = &report
  109
+  let save_sc = &showcmd
  110
+  let &report = 10000
  111
+  set noshowcmd 
  112
+ 
  113
+  if has("syntax")
  114
+    syn match bufExplorerHelp    "^\"[ -].*"
  115
+    syn match bufExplorerHelpEnd "^\"=.*$"
  116
+
  117
+    if !exists("g:did_bufexplorer_syntax_inits")
  118
+      let g:did_bufexplorer_syntax_inits = 1
  119
+      hi link bufExplorerHelp Special
  120
+      hi link bufExplorerHelpEnd Special
  121
+    endif
  122
+  endif
  123
+  
  124
+  if exists("s:longHelp")
  125
+    let w:longHelp = s:longHelp
  126
+  else
  127
+    let w:longHelp = g:bufExplorerDetailedHelp
  128
+  endif
  129
+ 
  130
+  nnoremap <buffer> <cr> :call <SID>SelectBuffer()<cr>
  131
+  nnoremap <buffer> d :call <SID>DeleteBuffer()<cr>
  132
+  nnoremap <buffer> q :call <SID>BackToPreviousBuffer()<cr>
  133
+  nnoremap <buffer> ? :call <SID>ToggleHelp()<cr>
  134
+  nnoremap <buffer> <2-leftmouse> :call <SID>DoubleClick()<cr>
  135
+ 
  136
+  " Delete all lines in buffer.
  137
+  1,$d _
  138
+
  139
+  call s:AddHeader()
  140
+  $ d
  141
+  call s:ShowBuffers()
  142
+ 
  143
+  normal! zz
  144
+
  145
+  let &report = oldRep
  146
+  let &showcmd = save_sc
  147
+
  148
+  unlet oldRep save_sc
  149
+  
  150
+  " Prevent the buffer from being modified.
  151
+  setlocal nomodifiable
  152
+endfunction
  153
+
  154
+" 
  155
+" AddHeader.
  156
+" 
  157
+function! s:AddHeader()
  158
+  1
  159
+  if w:longHelp == 1
  160
+    let header = "\" Buffer Explorer\n"
  161
+    let header = header."\" ----------------\n"
  162
+    let header = header."\" <enter> or Mouse-Double-Click : open buffer under cursor\n"
  163
+    let header = header."\" d : delete buffer.\n" 
  164
+    let header = header."\" q : quit the Buffer Explorer\n"
  165
+    let header = header."\" ? : toggle this help\n"
  166
+  else
  167
+    let header = "\" Press ? for Help\n"
  168
+  endif
  169
+
  170
+  let header = header."\"=\n"
  171
+  put! =header
  172
+
  173
+  unlet header
  174
+endfunction
  175
+
  176
+" 
  177
+" ShowBuffers.
  178
+" 
  179
+function! s:ShowBuffers()
  180
+  let oldRep = &report
  181
+  let save_sc = &showcmd
  182
+  let &report = 10000
  183
+  set noshowcmd 
  184
+  
  185
+  let _NBuffers = bufnr("$")     " Get the number of the last buffer.
  186
+  let _i = 0                     " Set the buffer index to zero.
  187
+
  188
+  let fileNames = ""
  189
+
  190
+  " Loop through every buffer less than the total number of buffers.
  191
+  while(_i <= _NBuffers)
  192
+    let _i = _i + 1
  193
+   
  194
+    " Make sure the buffer in question is listed.
  195
+    if(getbufvar(_i, '&buflisted') == 1)
  196
+      " Get the name of the buffer.
  197
+      let _BufName = bufname(_i)
  198
+     
  199
+      " Check to see if the buffer is a blank or not. If the buffer does have
  200
+      " a name, process it.
  201
+      if(strlen(_BufName))
  202
+        if(matchstr(_BufName, "BufExplorer\]") == "")
  203
+          if(bufnr(_BufName) == s:currentBufferNumber)
  204
+            let fileNames = fileNames.'%'
  205
+          else
  206
+            if(bufnr(_BufName) == s:alternateBufferNumber)
  207
+              let fileNames = fileNames.'#'
  208
+            else
  209
+              let fileNames = fileNames.' '
  210
+            endif
  211
+          endif
  212
+
  213
+          if(getbufvar(_i, '&hidden') == 1)
  214
+            let fileNames = fileNames.'h'
  215
+          else
  216
+            let fileNames = fileNames.' '
  217
+          endif
  218
+          
  219
+          if(getbufvar(_i, '&readonly') == 1)
  220
+            let fileNames = fileNames.'='
  221
+          else
  222
+            if(getbufvar(_i, '&modified') == 1)
  223
+              let fileNames = fileNames.'+'
  224
+            else
  225
+              if(getbufvar(_i, '&modifiable') == 0)
  226
+                let fileNames = fileNames.'-'
  227
+              else
  228
+                let fileNames = fileNames.' '
  229
+              endif
  230
+            endif
  231
+          endif
  232
+
  233
+          let fileNames = fileNames.' '
  234
+ 
  235
+          let fileNames = fileNames._BufName."\n"
  236
+        endif
  237
+      endif
  238
+    endif
  239
+  endwhile
  240
+
  241
+  put =fileNames
  242
+
  243
+  let &report = oldRep
  244
+  let &showcmd = save_sc
  245
+  
  246
+  unlet! fileNames _NBuffers _i oldRep save_sc _BufName
  247
+endfunction
  248
+
  249
+" 
  250
+" SelectBuffer.
  251
+" 
  252
+function! s:SelectBuffer()
  253
+  let save_sc = &showcmd
  254
+  set noshowcmd 
  255
+  
  256
+  let _cfile = getline('.')
  257
+
  258
+  " Are we on a line with a file name?
  259
+  if _cfile =~'^"'
  260
+    unlet _cfile
  261
+    return
  262
+  endif
  263
+ 
  264
+  " Skip over the readonly, modified indicators if there is any.
  265
+  let _cfile = strpart(_cfile,4,strlen(_cfile))
  266
+  "let _cfile = substitute(_cfile, "\\", "\\\\", "g")
  267
+
  268
+  if(strlen(_cfile))
  269
+    " Get the buffer number associated with this filename.
  270
+    let _bufnr = bufnr(_cfile)
  271
+
  272
+    if(_bufnr != -1)             " If the buffer exists.
  273
+      " Switch to the previously open buffer. This sets the alternate file
  274
+      " to the correct one, so that when we switch to the new buffer, the
  275
+      " alternate buffer is correct.
  276
+      exec("b! ".s:currentBufferNumber)
  277
+      " Open the new buffer.
  278
+      exec("b! "._bufnr)
  279
+    endif
  280
+  else
  281
+    if(@# != "" && (getbufvar('#', '&buflisted') == 1))
  282
+      exec("e #")
  283
+    endif
  284
+  endif
  285
+
  286
+  let &showcmd = save_sc
  287
+  
  288
+  unlet! _cfile _bufnr save_sc
  289
+endfunction
  290
+
  291
+" 
  292
+" Delete selected buffer from list.
  293
+" 
  294
+function! s:DeleteBuffer()
  295
+  let oldRep = &report
  296
+  let &report = 10000
  297
+  let save_sc = &showcmd
  298
+  set noshowcmd 
  299
+  
  300
+  setlocal modifiable
  301
+  
  302
+  let _cfile = getline('.')
  303
+  " Skip over the readonly, modified indicators if there is any.
  304
+  let _cfile = strpart(_cfile,4,strlen(_cfile))
  305
+"  let _cfile = substitute(_cfile, "\\", "\\\\", "g")
  306
+  
  307
+  " Check it the file exists and is readable.
  308
+  if filereadable(_cfile)
  309
+    " Delete the buffer selected.
  310
+    exec("bd ".(bufnr(_cfile)))
  311
+    " Delete the buffer's name from the list.
  312
+    d _
  313
+  endif
  314
+
  315
+  setlocal nomodifiable
  316
+
  317
+  let &report = oldRep
  318
+  let &showcmd = save_sc
  319
+
  320
+  unlet _cfile oldRep save_sc
  321
+endfunction
  322
+
  323
+" 
  324
+" Back To Previous Buffer.
  325
+"
  326
+function! s:BackToPreviousBuffer()
  327
+  let save_sc = &showcmd
  328
+  set noshowcmd 
  329
+
  330
+  if(w:bufExplorerSplitWindow == 1)
  331
+    exec("close!")
  332
+  endif
  333
+
  334
+  if(s:alternateBufferNumber != -1)
  335
+    exec("b! ".s:alternateBufferNumber)
  336
+  endif
  337
+
  338
+  if(s:currentBufferNumber != -1)
  339
+    exec("b! ".s:currentBufferNumber)
  340
+  endif
  341
+ 
  342
+  let &showcmd = save_sc
  343
+
  344
+  unlet save_sc
  345
+endfunction
  346
+
  347
+" 
  348
+" Toggle between short and long help
  349
+" 
  350
+function! s:ToggleHelp()
  351
+  if exists("w:longHelp") && w:longHelp==0
  352
+    let w:longHelp=1
  353
+    let s:longHelp=1
  354
+  else
  355
+    let w:longHelp=0
  356
+    let s:longHelp=0
  357
+  endif
  358
+  
  359
+  " Allow modification
  360
+  setlocal modifiable
  361
+  call s:UpdateHeader()
  362
+  
  363
+  " Disallow modification
  364
+  setlocal nomodifiable
  365
+endfunction
  366
+
  367
+" 
  368
+" Update the header
  369
+" 
  370
+function! s:UpdateHeader()
  371
+  let oldRep = &report
  372
+  let save_sc = &showcmd
  373
+  let &report = 10000
  374
+  set noshowcmd 
  375
+  
  376
+  " Save position
  377
+  normal! mt
  378
+  
  379
+  " Remove old header
  380
+  0
  381
+  1,/^"=/ d
  382
+  
  383
+  " Add new header
  384
+  call s:AddHeader()
  385
+  
  386
+  " Go back where we came from if possible.
  387
+  0
  388
+  if line("'t") != 0
  389
+    normal! `t
  390
+  endif
  391
+
  392
+  let &report = oldRep
  393
+  let &showcmd = save_sc
  394
+
  395
+  unlet oldRep save_sc
  396
+endfunction
  397
+
  398
+"
  399
+" Double click with the mouse
  400
+"
  401
+function s:DoubleClick()
  402
+  call s:SelectBuffer()
  403
+endfun

0 notes on commit 269f9fa

Please sign in to comment.
Something went wrong with that request. Please try again.