Skip to content
This repository
Browse code

Switching to a buffer by number instead of name

  • Loading branch information...
commit e49854ef091cd3c848fd33ebcda2c4ec8ba5ea01 1 parent bda09a3
Adam Szkoda authored March 11, 2013
13  autoload/chbuf.vim
... ...
@@ -1,8 +1,9 @@
1  
-" TODO There may be two buffers of the same name --- pick the first one
  1
+" TODO Implement more suitable matching algorithm
2 2
 " TODO Tab-completion of longest common prefix
3 3
 " TODO Make Tab behave as enter when longest common prefix is unambiguous
4 4
 " TODO Score higher subsequences occuring after directory separator
5 5
 " TODO Make functions script-private once they are sufficiently tested
  6
+" DONE There may be two buffers of the same name --- pick the first one
6 7
 " DONE Score expand('#') file highest and select it initially
7 8
 " DONE Filter out current buffer in choices
8 9
 " DONE Show only path basenames as suggestions
@@ -76,20 +77,18 @@ function! MatchBuffer(input) " {{{
76 77
 
77 78
     call filter(buffers, printf('v:val.name =~ "%s"', escape(a:input, '"')))
78 79
 
79  
-    call map(buffers, 'v:val.basename')
80  
-
81 80
     return buffers
82 81
 endfunction " }}}
83 82
 function! BufferNameCallback(input) " {{{
84 83
     let buffers = MatchBuffer(a:input)
85  
-    let cols = &columns - len(buffers) - 1 - 1
86 84
 
87 85
     if len(buffers) == 0
88  
-        return ''
  86
+        return [bufnr('%'), '']
89 87
     endif
90 88
 
91  
-    let caption = join(buffers)
92  
-    return ' ↦ ' . caption
  89
+    let basenames = map(copy(buffers), 'v:val.basename')
  90
+    let caption = ' ↦ ' . join(basenames)
  91
+    return [buffers[0].number, caption]
93 92
 endfunction " }}}
94 93
 function! PromptBuffer() " {{{
95 94
     return getline#GetLine('∷ ', 'BufferNameCallback')
16  autoload/getline.vim
@@ -2,17 +2,13 @@ let s:save_cpo = &cpo
2 2
 set cpo&vim
3 3
 
4 4
 
5  
-function! s:Echo(line)
  5
+function! s:Echo(line) " {{{
6 6
     echon strpart(a:line, 0, &columns - 1)
7  
-endfunction
8  
-
9  
-
  7
+endfunction " }}}
10 8
 function! s:ClearLine(contents) " {{{
11 9
     let rubber = "\r" . repeat(' ', strlen(a:contents)) . "\r"
12 10
     call s:Echo(rubber)
13 11
 endfunction! " }}}
14  
-
15  
-
16 12
 function! s:WithoutLastWord(string) " {{{
17 13
     let result = substitute(a:string, '\v(\S+)\s+\S+$', '\1', '')
18 14
 
@@ -22,11 +18,9 @@ function! s:WithoutLastWord(string) " {{{
22 18
 
23 19
     return result
24 20
 endfunction " }}}
25  
-
26  
-
27 21
 function! getline#GetLine(prompt, get_status) " {{{
28 22
     let line = ""
29  
-    let status = call(a:get_status, [line])
  23
+    let [choice, status] = call(a:get_status, [line])
30 24
 
31 25
     let displayed = a:prompt . line . status
32 26
     call s:Echo(displayed)
@@ -62,14 +56,14 @@ function! getline#GetLine(prompt, get_status) " {{{
62 56
         endif
63 57
 
64 58
         call s:ClearLine(displayed)
65  
-        let status = call(a:get_status, [line])
  59
+        let [choice, status] = call(a:get_status, [line])
66 60
         let displayed = a:prompt . line . status
67 61
         call s:Echo("\r" . displayed)
68 62
         call s:Echo("\r" . strpart(displayed, 0, strlen(a:prompt) + strlen(line)))
69 63
     endwhile
70 64
 
71 65
     call s:ClearLine(displayed)
72  
-    return line
  66
+    return choice
73 67
 endfunction " }}}
74 68
 
75 69
 

0 notes on commit e49854e

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