Skip to content
This repository
Browse code

Bug fix for completion after typing several words (reported by Ulrik …

…Sverdrup)
  • Loading branch information...
commit d5a11a8af41fcfcc1aade6872ee8049d72673f5b 1 parent c4f231e
Peter Odding authored May 22, 2011
32  autoload/xolox/notes.vim
@@ -74,7 +74,37 @@ function! xolox#notes#select(filter) " {{{1
74 74
 endfunction
75 75
 
76 76
 function! xolox#notes#complete(arglead, cmdline, cursorpos) " {{{1
77  
-  return filter(xolox#notes#get_titles(), 'v:val =~ a:arglead')
  77
+  " Vim's support for custom command completion is a real mess, specifically
  78
+  " the completion of multi word command arguments. With or without escaping
  79
+  " of spaces, arglead will only contain the last word in the arguments passed
  80
+  " to :Note, and worse, the completion candidates we return only replace the
  81
+  " last word on the command line.
  82
+  " XXX This isn't a real command line parser; it will break on quoted pipes.
  83
+  let cmdline = split(a:cmdline, '\\\@<!|')
  84
+  let cmdargs = substitute(cmdline[-1], '^\s*\w\+\s\+', '', '')
  85
+  let arguments = split(cmdargs)
  86
+  let titles = xolox#notes#get_titles()
  87
+  if a:arglead != '' && len(arguments) == 1
  88
+    " If we are completing a single argument and we are able to replace it
  89
+    " (the user didn't type <Space><Tab> after the argument) we can select the
  90
+    " completion candidates using a substring match on the first argument
  91
+    " instead of a prefix match (I consider this to be more user friendly).
  92
+    let pattern = xolox#misc#escape#pattern(cmdargs)
  93
+    call filter(titles, "v:val =~ pattern")
  94
+  else
  95
+    " If we are completing more than one argument or the user has typed
  96
+    " <Space><Tab> after the first argument, we must select completion
  97
+    " candidates using a prefix match on all arguments because Vim doesn't
  98
+    " support replacing previous arguments (selecting completion candidates
  99
+    " using a substring match would result in invalid note titles).
  100
+    let pattern = '^' . xolox#misc#escape#pattern(cmdargs)
  101
+    call filter(titles, "v:val =~ pattern")
  102
+    " Remove the given arguments as the prefix of every completion candidate
  103
+    " because Vim refuses to replace previous arguments.
  104
+    let prevargs = '^' . xolox#misc#escape#pattern(cmdargs[0 : len(cmdargs) - len(a:arglead) - 1])
  105
+    call map(titles, 'substitute(v:val, prevargs, "", "")')
  106
+  endif
  107
+  return titles
78 108
 endfunction
79 109
 
80 110
 function! xolox#notes#save() abort " {{{1
2  plugin/notes.vim
@@ -3,7 +3,7 @@
3 3
 " Last Change: May 22, 2011
4 4
 " URL: http://peterodding.com/code/vim/notes/
5 5
 " License: MIT
6  
-" Version: 0.8.4
  6
+" Version: 0.8.5
7 7
 
8 8
 " Support for automatic update using the GLVS plug-in.
9 9
 " GetLatestVimScripts: 3375 1 :AutoInstall: notes.zip

0 notes on commit d5a11a8

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