Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…Sverdrup)
  • Loading branch information...
commit d5a11a8af41fcfcc1aade6872ee8049d72673f5b 1 parent c4f231e
@xolox authored
Showing with 32 additions and 2 deletions.
  1. +31 −1 autoload/xolox/notes.vim
  2. +1 −1  plugin/notes.vim
View
32 autoload/xolox/notes.vim
@@ -74,7 +74,37 @@ function! xolox#notes#select(filter) " {{{1
endfunction
function! xolox#notes#complete(arglead, cmdline, cursorpos) " {{{1
- return filter(xolox#notes#get_titles(), 'v:val =~ a:arglead')
+ " Vim's support for custom command completion is a real mess, specifically
+ " the completion of multi word command arguments. With or without escaping
+ " of spaces, arglead will only contain the last word in the arguments passed
+ " to :Note, and worse, the completion candidates we return only replace the
+ " last word on the command line.
+ " XXX This isn't a real command line parser; it will break on quoted pipes.
+ let cmdline = split(a:cmdline, '\\\@<!|')
+ let cmdargs = substitute(cmdline[-1], '^\s*\w\+\s\+', '', '')
+ let arguments = split(cmdargs)
+ let titles = xolox#notes#get_titles()
+ if a:arglead != '' && len(arguments) == 1
+ " If we are completing a single argument and we are able to replace it
+ " (the user didn't type <Space><Tab> after the argument) we can select the
+ " completion candidates using a substring match on the first argument
+ " instead of a prefix match (I consider this to be more user friendly).
+ let pattern = xolox#misc#escape#pattern(cmdargs)
+ call filter(titles, "v:val =~ pattern")
+ else
+ " If we are completing more than one argument or the user has typed
+ " <Space><Tab> after the first argument, we must select completion
+ " candidates using a prefix match on all arguments because Vim doesn't
+ " support replacing previous arguments (selecting completion candidates
+ " using a substring match would result in invalid note titles).
+ let pattern = '^' . xolox#misc#escape#pattern(cmdargs)
+ call filter(titles, "v:val =~ pattern")
+ " Remove the given arguments as the prefix of every completion candidate
+ " because Vim refuses to replace previous arguments.
+ let prevargs = '^' . xolox#misc#escape#pattern(cmdargs[0 : len(cmdargs) - len(a:arglead) - 1])
+ call map(titles, 'substitute(v:val, prevargs, "", "")')
+ endif
+ return titles
endfunction
function! xolox#notes#save() abort " {{{1
View
2  plugin/notes.vim
@@ -3,7 +3,7 @@
" Last Change: May 22, 2011
" URL: http://peterodding.com/code/vim/notes/
" License: MIT
-" Version: 0.8.4
+" Version: 0.8.5
" Support for automatic update using the GLVS plug-in.
" GetLatestVimScripts: 3375 1 :AutoInstall: notes.zip
Please sign in to comment.
Something went wrong with that request. Please try again.