vim: add a line splitter that understands quotes
authorBart Trojanowski <bart@jukie.net>
Fri, 27 Nov 2009 03:24:33 +0000 (22:24 -0500)
committerBart Trojanowski <bart@jukie.net>
Fri, 27 Nov 2009 03:24:33 +0000 (22:24 -0500)
this makes it possible to tokenise expressions like
   :NotMuch compose 'subject: one two three' to:bart@jukie.net

vim/plugin/notmuch.vim

index 15958f18c02c407d93056717aa858b825c4cfc47..7c69bc8bfc34e41469e43d61a9624faa88c0960a 100644 (file)
@@ -863,6 +863,46 @@ function! s:NM_shell_escape(word)
         return '''' . word . ''''
 endfunction
 
         return '''' . word . ''''
 endfunction
 
+" this function was taken from git.vim, then fixed up
+" http://github.com/motemen/git-vim
+function! s:NM_shell_split(cmd)
+        let l:split_cmd = []
+        let cmd = a:cmd
+        let iStart = 0
+        while 1
+                let t = match(cmd, '\S', iStart)
+                if t < iStart
+                        break
+                endif
+                let iStart = t
+
+                let iSpace = match(cmd, '\v(\s|$)', iStart)
+                if iSpace < iStart
+                        break
+                endif
+
+                let iQuote1 = match(cmd, '\(^["'']\|[^\\]\@<=["'']\)', iStart)
+                if iQuote1 > iSpace || iQuote1 < iStart
+                        let iEnd = iSpace - 1
+                        let l:split_cmd += [ cmd[iStart : iEnd] ]
+                else
+                        let q = cmd[iQuote1]
+                        let iQuote2 = match(cmd, '[^\\]\@<=[' . q . ']', iQuote1 + 1)
+                        if iQuote2 < iQuote1
+                                throw 'No matching ' . q . ' quote'
+                        endif
+                        let iEnd = iQuote2
+                        let l:split_cmd += [ cmd[iStart+1 : iEnd-1 ] ]
+                endif
+
+
+                let iStart = iEnd + 1
+        endwhile
+
+        return l:split_cmd
+endfunction
+
+
 function! s:NM_run(args)
         let words = a:args
         call map(words, 's:NM_shell_escape(v:val)')
 function! s:NM_run(args)
         let words = a:args
         call map(words, 's:NM_shell_escape(v:val)')
@@ -996,12 +1036,12 @@ function! NotMuch(args)
                 let args = 'folders'
         endif
 
                 let args = 'folders'
         endif
 
-        let words = split(args)
-        if words[0] == 'folders'
+        let words = <SID>NM_shell_split(args)
+        if words[0] == 'folders' || words[0] == 'f'
                 let words = words[1:]
                 call <SID>NM_cmd_folders(words)
 
                 let words = words[1:]
                 call <SID>NM_cmd_folders(words)
 
-        elseif words[0] == 'search'
+        elseif words[0] == 'search' || words[0] == 's'
                 if len(words) > 1
                         let words = words[1:]
                 elseif exists('b:nm_search_words')
                 if len(words) > 1
                         let words = words[1:]
                 elseif exists('b:nm_search_words')