Browse files

Initial rudimentary version

  • Loading branch information...
0 parents commit 5fe4564d249c9ce5806d9bd300ebb6bb2a1ab6b2 @adaszko committed Feb 4, 2013
Showing with 129 additions and 0 deletions.
  1. +24 −0 LICENSE
  2. +39 −0 README.md
  3. +66 −0 plugin/getline.vim
24 LICENSE
@@ -0,0 +1,24 @@
+Copyright (c) 2013, Adam Dariusz Szkoda
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 README.md
@@ -0,0 +1,39 @@
+What is it?
+===========
+
+It allows you to read a line interactively from a Vim script.
+
+
+Status
+======
+
+Unusable yet.
+
+
+Installation
+============
+
+Assuming you have Pathogen up and running:
+
+ cd ~/.vim/bundle
+ git clone git://github.com/adaszko/getline.git
+
+
+Usage
+=====
+
+ :echo getline#GetLine('> ')
+
+
+TODO
+====
+
+* Allow for caret to slide on top of character without erasing them
+* User-specified hooks on key presses
+* Right prompt computed by user code each time contents change
+
+
+License
+=======
+
+BSD3
66 plugin/getline.vim
@@ -0,0 +1,66 @@
+let s:save_cpo = &cpo
+set cpo&vim
+
+
+function! WithoutLastWord(string) " {{{
+ let result = substitute(a:string, '\v(\S+)\s+\S+$', '\1', '')
+
+ if result == a:string
+ let result = ""
+ endif
+
+ return result
+endfunction " }}}
+
+
+function! GetLine(prompt) " {{{
+ let result = ""
+
+ let displayed = a:prompt
+ echon displayed
+
+ while 1
+ let c = getchar()
+ if c == 27 " escape
+ return ""
+ endif
+
+ if type(c) == type(0)
+ if c == 13 " enter
+ break
+ elseif c == 21 " ^U
+ let result = ""
+ elseif c == 23 " ^W
+ let result = WithoutLastWord(result)
+ else
+ let result .= nr2char(c)
+ endif
+ elseif type(c) == type("")
+ if c == "\x80kb" " backspace
+ " Remove last character of input
+ if empty(result)
+ echon "\r" . repeat(' ', strlen(displayed))
+ return ""
+ else
+ let result = strpart(result, 0, strlen(result)-1)
+ endif
+ endif
+ endif
+
+ " Erase previous contents
+ echon "\r" . repeat(' ', strlen(displayed))
+
+ " Display updated contents
+ let displayed = a:prompt . result
+ echon "\r" . displayed
+ endwhile
+
+ return result
+endfunction " }}}
+
+
+let &cpo = s:save_cpo
+unlet s:save_cpo
+
+
+" vim:foldmethod=marker

0 comments on commit 5fe4564

Please sign in to comment.