1
- " Copyright (c) 1998-2004
1
+ " Copyright (c) 1998-2006
2
2
" Michael Sharpe <feline@irendi.com>
3
3
"
4
4
" We grant permission to use, copy modify, distribute, and sell this
@@ -30,6 +30,10 @@ let loaded_alternateFile = 1
30
30
" let g:alternateExtensions_{'aspx.cs'} = "aspx"
31
31
32
32
33
+ " This variable will be increased when an extension with greater number of dots
34
+ " is added by the AddAlternateExtensionMapping call.
35
+ let s: maxDotsInExtension = 0
36
+
33
37
" Function : AddAlternateExtensionMapping (PRIVATE)
34
38
" Purpose : simple helper function to add the default alternate extension
35
39
" mappings.
@@ -51,6 +55,11 @@ function! <SID>AddAlternateExtensionMapping(extension, alternates)
51
55
if (v: errmsg != " " )
52
56
let g: alternateExtensions_ {a: extension } = a: alternates
53
57
endif
58
+
59
+ let dotsNumber = strlen (substitute (a: extension , " [^.]" , " " , " g" ))
60
+ if s: maxDotsInExtension < dotsNumber
61
+ let s: maxDotsInExtension = dotsNumber
62
+ endif
54
63
endfunction
55
64
56
65
" Add all the default extensions
@@ -204,7 +213,7 @@ endfunction
204
213
" Returns : An expanded filename if found, the empty string otherwise
205
214
" Author : Michael Sharpe (feline@irendi.com)
206
215
" History : inline code written by Bindu Wavell originally
207
- function ! <SID> FindFileInSearchPath (filename , pathList, relPathBase)
216
+ function ! <SID> FindFileInSearchPath (fileName , pathList, relPathBase)
208
217
let filepath = " "
209
218
let m = 1
210
219
let pathListLen = strlen (a: pathList )
@@ -213,7 +222,7 @@ function! <SID>FindFileInSearchPath(filename, pathList, relPathBase)
213
222
let pathSpec = <SID> GetNthItemFromList (a: pathList , m )
214
223
if (pathSpec != " " )
215
224
let path = <SID> ExpandAlternatePath (pathSpec, a: relPathBase )
216
- let fullname = path . " /" . a: filename
225
+ let fullname = path . " /" . a: fileName
217
226
let foundMatch = <SID> BufferOrFileExists (fullname)
218
227
if (foundMatch)
219
228
let filepath = fullname
@@ -324,37 +333,21 @@ endfunction
324
333
" variables echo the curly brace variable and look for an error
325
334
" message.
326
335
function ! DetermineExtension (path )
327
- let extension = fnamemodify (a: path ," :t:e" )
328
- let v: errmsg = " "
329
- silent ! echo g: alternateExtensions_ {extension}
330
- if (v: errmsg != " " )
331
- let extension = fnamemodify (a: path ," :t:e:e" )
332
- let v: errmsg = " "
333
- silent ! echo g: alternateExtensions_ {extension}
334
- if (v: errmsg != " " )
335
- let extension = fnamemodify (a: path ," :t:e:e:e" )
336
- let v: errmsg = " "
337
- silent ! echo g: alternateExtensions_ {extension}
338
- if (v: errmsg != " " )
339
- let extension = fnamemodify (a: path ," :t:e:e:e:e" )
340
- let v: errmsg = " "
341
- silent ! echo g: alternateExtensions_ {extension}
342
- if (v: errmsg != " " )
343
- let extension = fnamemodify (a: path ," :t:e:e:e:e:e" )
344
- let v: errmsg = " "
345
- silent ! echo g: alternateExtensions_ {extension}
346
- if (v: errmsg != " " )
347
- let extension = " "
348
- endif
349
- endif
350
- endif
351
- endif
352
- endif
353
- return extension
336
+ let mods = " :t"
337
+ let i = 0
338
+ while i <= s: maxDotsInExtension
339
+ let mods = mods . " :e"
340
+ let extension = fnamemodify (a: path , mods)
341
+ let v: errmsg = " "
342
+ silent ! echo g: alternateExtensions_ {extension}
343
+ if (v: errmsg == " " )
344
+ return extension
345
+ endif
346
+ let i = i + 1
347
+ endwhile
348
+ return " "
354
349
endfunction
355
350
356
- " source $HOME/vimscripts/plugin/Decho.vim
357
-
358
351
" Function : AlternateFile (PUBLIC)
359
352
" Purpose : Opens a new buffer by looking at the extension of the current
360
353
" buffer and finding the corresponding file. E.g. foo.c <--> foo.h
@@ -373,10 +366,6 @@ function! AlternateFile(splitWindow, ...)
373
366
let baseName = substitute (expand (" %:t" ), " \. " . extension . ' $' , " " , " " )
374
367
let currentPath = expand (" %:p:h" )
375
368
376
- " Decho "extension=".extension
377
- " Decho "baseName=".baseName
378
- " Decho "currentPath=".currentPath
379
-
380
369
if (a: 0 != 0 )
381
370
let newFullname = currentPath . " /" . baseName . " ." . a: 1
382
371
call <SID> FindOrCreateBuffer (newFullname, a: splitWindow )
@@ -386,9 +375,6 @@ function! AlternateFile(splitWindow, ...)
386
375
let allfiles1 = EnumerateFilesByExtension (currentPath, baseName, extension)
387
376
let allfiles2 = EnumerateFilesByExtensionInPath (baseName, extension, g: alternateSearchPath , currentPath)
388
377
389
- " Decho "allfiles1=".allfiles1
390
- " Decho "allfiles2=".allfiles2
391
-
392
378
if (allfiles1 != " " )
393
379
if (allfiles2 != " " )
394
380
let allfiles = allfiles1 . ' ,' . allfiles2
@@ -432,6 +418,9 @@ endfunction
432
418
comm! -nargs =? - bang A call AlternateFile (" n<bang>" , <f-args> )
433
419
comm! -nargs =? - bang AS call AlternateFile (" h<bang>" , <f-args> )
434
420
comm! -nargs =? - bang AV call AlternateFile (" v<bang>" , <f-args> )
421
+ if v: version >= 700
422
+ comm! -nargs =? - bang AT call AlternateFile (" t<bang>" , <f-args> )
423
+ endif
435
424
436
425
" Function : BufferOrFileExists (PRIVATE)
437
426
" Purpose : determines if a buffer or a readable file exists
@@ -442,17 +431,34 @@ comm! -nargs=? -bang AV call AlternateFile("v<bang>", <f-args>)
442
431
" filename and not the path.
443
432
function ! <SID> BufferOrFileExists (fileName)
444
433
let result = 0
445
- let bufName = fnamemodify (a: fileName ," :t" )
446
- let memBufName = bufname (bufName)
447
- if (memBufName != " " )
448
- let memBufBasename = fnamemodify (memBufName, " :t" )
449
- if (bufName == memBufBasename)
450
- let result = 2
434
+
435
+ let lastBuffer = bufnr (" $" )
436
+ let i = 1
437
+ while i <= lastBuffer
438
+ if <SID> EqualFilePaths (expand (" #" .i ." :p" ), a: fileName )
439
+ let result = 2
440
+ break
441
+ endif
442
+ let i = i + 1
443
+ endwhile
444
+
445
+ if (! result)
446
+ let bufName = fnamemodify (a: fileName ," :t" )
447
+ let memBufName = bufname (bufName)
448
+ if (memBufName != " " )
449
+ let memBufBasename = fnamemodify (memBufName, " :t" )
450
+ if (bufName == memBufBasename)
451
+ let result = 2
452
+ endif
453
+ endif
454
+
455
+ if (! result)
456
+ let result = bufexists (bufName) || bufexists (a: fileName ) || filereadable (a: fileName )
451
457
endif
452
458
endif
453
459
454
460
if (! result)
455
- let result = bufexists (bufName) || bufexists ( a: fileName ) || filereadable (a: fileName )
461
+ let result = filereadable (a: fileName )
456
462
endif
457
463
return result
458
464
endfunction
@@ -465,7 +471,7 @@ endfunction
465
471
" not exist, it creates it.
466
472
" Args : filename (IN) -- the name of the file
467
473
" doSplit (IN) -- indicates whether the window should be split
468
- " ("v", "h", "n", "v!", "h!", "n!")
474
+ " ("v", "h", "n", "v!", "h!", "n!", "t", "t!" )
469
475
" Returns : nothing
470
476
" Author : Michael Sharpe <feline@irendi.com>
471
477
" History : + bufname() was not working very well with the possibly strange
@@ -475,49 +481,98 @@ endfunction
475
481
" Allow ! to be applied to buffer/split/editing commands for more
476
482
" vim/vi like consistency
477
483
" + implemented fix from Matt Perry
478
- function ! <SID> FindOrCreateBuffer (filename , doSplit)
484
+ function ! <SID> FindOrCreateBuffer (fileName , doSplit)
479
485
" Check to see if the buffer is already open before re-opening it.
480
- let bufName = bufname (a: filename )
481
- let bufFilename = fnamemodify (a: filename ," :t" )
482
-
483
- if (bufName == " " )
484
- let bufName = bufname (bufFilename)
485
- endif
486
+ let FILENAME = a: fileName
487
+ let bufNr = -1
488
+ let lastBuffer = bufnr (" $" )
489
+ let i = 1
490
+ while i <= lastBuffer
491
+ if <SID> EqualFilePaths (expand (" #" .i ." :p" ), a: fileName )
492
+ let bufNr = i
493
+ break
494
+ endif
495
+ let i = i + 1
496
+ endwhile
497
+
498
+ if (bufNr == -1 )
499
+ let bufName = bufname (a: fileName )
500
+ let bufFilename = fnamemodify (a: fileName ," :t" )
501
+
502
+ if (bufName == " " )
503
+ let bufName = bufname (bufFilename)
504
+ endif
486
505
487
- if (bufName != " " )
488
- let tail = fnamemodify (bufName, " :t" )
489
- if (tail != bufFilename)
490
- let bufName = " "
506
+ if (bufName != " " )
507
+ let tail = fnamemodify (bufName, " :t" )
508
+ if (tail != bufFilename)
509
+ let bufName = " "
510
+ endif
511
+ endif
512
+ if (bufName != " " )
513
+ let bufNr = bufnr (bufName)
514
+ let FILENAME = bufName
491
515
endif
492
516
endif
493
517
494
518
let splitType = a: doSplit [0 ]
495
519
let bang = a: doSplit [1 ]
496
- if (bufName == " " )
520
+ if (bufNr == -1 )
497
521
" Buffer did not exist....create it
498
522
let v: errmsg= " "
499
523
if (splitType == " h" )
500
- silent ! execute " :split" .bang ." " . a: filename
524
+ silent ! execute " :split" .bang ." " . FILENAME
501
525
elseif (splitType == " v" )
502
- silent ! execute " :vsplit" .bang ." " . a: filename
526
+ silent ! execute " :vsplit" .bang ." " . FILENAME
527
+ elseif (splitType == " t" )
528
+ silent ! execute " :tab split" .bang ." " . FILENAME
503
529
else
504
- silent ! execute " :e" .bang ." " . a: filename
530
+ silent ! execute " :e" .bang ." " . FILENAME
505
531
endif
506
532
if (v: errmsg != " " )
507
533
echo v: errmsg
508
534
endif
509
535
else
536
+
537
+ " Find the correct tab corresponding to the existing buffer
538
+ let tabNr = -1
539
+ " iterate tab pages
540
+ for i in range (tabpagenr (' $' ))
541
+ " get the list of buffers in the tab
542
+ let tabList = tabpagebuflist (i + 1 )
543
+ let idx = 0
544
+ " iterate each buffer in the list
545
+ while idx < len (tabList)
546
+ " if it matches the buffer we are looking for...
547
+ if (tabList[idx] == bufNr)
548
+ " ... save the number
549
+ let tabNr = i + 1
550
+ break
551
+ endif
552
+ let idx = idx + 1
553
+ endwhile
554
+ if (tabNr != -1 )
555
+ break
556
+ endif
557
+ endfor
558
+ " switch the the tab containing the buffer
559
+ if (tabNr != -1 )
560
+ execute " tabn " .tabNr
561
+ endif
562
+
510
563
" Buffer was already open......check to see if it is in a window
511
- let bufWindow = bufwinnr (bufName )
564
+ let bufWindow = bufwinnr (bufNr )
512
565
if (bufWindow == -1 )
513
566
" Buffer was not in a window so open one
514
567
let v: errmsg= " "
515
568
if (splitType == " h" )
516
- silent ! execute " :sbuffer" .bang ." " . bufName
569
+ silent ! execute " :sbuffer" .bang ." " . FILENAME
517
570
elseif (splitType == " v" )
518
- silent ! execute " :vert sbuffer " . bufName
571
+ silent ! execute " :vert sbuffer " . FILENAME
572
+ elseif (splitType == " t" )
573
+ silent ! execute " :tab sbuffer " . FILENAME
519
574
else
520
- silent ! execute " :buffer" .bang ." " . bufName
575
+ silent ! execute " :buffer" .bang ." " . FILENAME
521
576
endif
522
577
if (v: errmsg != " " )
523
578
echo v: errmsg
@@ -529,11 +584,13 @@ function! <SID>FindOrCreateBuffer(filename, doSplit)
529
584
" something wierd happened...open the buffer
530
585
let v: errmsg= " "
531
586
if (splitType == " h" )
532
- silent ! execute " :split" .bang ." " . bufName
587
+ silent ! execute " :split" .bang ." " . FILENAME
533
588
elseif (splitType == " v" )
534
- silent ! execute " :vsplit" .bang ." " . bufName
589
+ silent ! execute " :vsplit" .bang ." " . FILENAME
590
+ elseif (splitType == " t" )
591
+ silent ! execute " :tab split" .bang ." " . FILENAME
535
592
else
536
- silent ! execute " :e" .bang ." " . bufName
593
+ silent ! execute " :e" .bang ." " . FILENAME
537
594
endif
538
595
if (v: errmsg != " " )
539
596
echo v: errmsg
@@ -542,3 +599,21 @@ function! <SID>FindOrCreateBuffer(filename, doSplit)
542
599
endif
543
600
endif
544
601
endfunction
602
+
603
+ " Function : EqualFilePaths (PRIVATE)
604
+ " Purpose : Compares two paths. Do simple string comparison anywhere but on
605
+ " Windows. On Windows take into account that file paths could differ
606
+ " in usage of separators and the fact that case does not metter.
607
+ " "c:\WINDOWS" is the same path as "c:/windows". has("win32unix") Vim
608
+ " version does not count as one having Windows path rules.
609
+ " Args : path1 (IN) -- first path
610
+ " path2 (IN) -- second path
611
+ " Returns : 1 if path1 is equal to path2, 0 otherwise.
612
+ " Author : Ilya Bobir <ilya@po4ta.com>
613
+ function ! <SID> EqualFilePaths (path1, path2)
614
+ if has (" win16" ) || has (" win32" ) || has (" win64" ) || has (" win95" )
615
+ return substitute (a: path1 , " \/ " , " \\ " , " g" ) == ? substitute (a: path2 , " \/ " , " \\ " , " g" )
616
+ else
617
+ return a: path1 == a: path2
618
+ endif
619
+ endfunction
0 commit comments