On 11/12/2011 08:04 PM, Gelonida N wrote:
On 11/13/2011 01:27 AM, AK wrote:
On 11/12/2011 06:49 PM, Gelonida N wrote:
On 11/12/2011 11:46 PM, AK wrote:
Knowing almost nothing about vim scripting,
but knowing basic vim commands and knowing python quite well
I was looking for the 'basic' commands, that would allow me to interact
with vim buffers and registers, (probably I still have to learn about
selections / marks)
My thinking was, that if I can access, buffers, registers, marks
and if I can inject key strokes, that I should be able to
write almost anything, that I would like to do (even if there might be
more elegant solutions if I knew more of the vim scripting commands)
One potential use case of setting a register would be to get
text from a selection, buffer, register, process it and dump the result
in the clipboard, such, that I could paste it into another application
(web browser / console window, whatever)
If I understood well I could do this by setting the '+' or '*' register.
I mostly use command, eval, buffers, windows, current.buffer and
current.line in vim python scripts.
From your earlier message: to get the selection, do something with it
and replace with new text, just cut it to a register using "ax normal
command, then simply insert new text at current cursor location.
Thanks a lot for your last hint. :-)
This should do the trick (without having to set a register and without
having to escape any string.
By the way I have written a lot of python vim code and I have a
bunch of utility functions that I reuse often. I'll attach
them to this message, see if you might find them useful....
But you need to reload it manually if changed, vim won't
reload it if you just have 'from util import *' on top of
a module.
Also, maybe you already thought of this, but I find this
approach handy to expose multiple python functions in
a module:
[in python file]
def delswp(): ...
arg = exist_eval("a:arg")
if arg:
locals()[arg]()
[in .vim plugin]
function! Other(arg)
:pyfile ~/.vim/python/other.py
endfu
nnoremap <c-j>s :call Other("delswp")<CR>
-ak
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
from vim import *
def iexists(var):
return int(eval("exists('%s')" % var))
exists = iexists
def exist_eval(var):
if iexists(var): return eval(var)
else: return None
def iexist_eval(var):
x = exist_eval(var)
return None if x==None else int(x)
def ieval(var):
return int(eval(var))
def input(prompt="> ", strip=True):
i = eval("input('%s ')" % prompt)
return i.strip() if i else i
def edit(path):
command("edit %s" % path)
def expand(pattern):
return eval("expand('%s')" % pattern)
def nextwin(arg=''):
command("call NextWin(%s)" % arg)
def bufnr(pattern):
return eval('bufnr("%s")' % pattern)
def buffer(bufnr):
command("buffer %s" % bufnr)
def cursor(row_or_tup=None, col=None):
if row_or_tup != None or col != None:
x = row_or_tup if col==None else (row_or_tup, col)
current.window.cursor = row_or_tup if col==None else (row_or_tup, col)
else:
return current.window.cursor
def col(n=None):
return cursor()[1] if n==None else cursor(lnum(), n)
def bcursor(row_or_tup=None, col=None):
"""Get or set buffer cursor (starting with 0-index)."""
if row_or_tup is None and col is None:
c = cursor()
return c[0]-1, c[1]
elif col is None:
cursor(row_or_tup[0]+1, row_or_tup[1])
else:
cursor(row_or_tup+1, col)
def lnum(n=None):
return cursor()[0] if n==None else cursor(n, col())
def blnum(n=None):
"""Get or set buffer line num (starting with 0-index)."""
return lnum()-1 if n==None else lnum(n+1)
def indent(ln=None):
if ln==None: ln = lnum()
return int(eval("indent(%s)" % ln))
def col(n=None):
return cursor()[1] if n==None else cursor(lnum(), n)
def set(k, v):
command("set %s=%s" % (k, v))
def normal(cmd):
command("normal! "+cmd)
def search(pat, flags=''):
"""Search for pattern. flags='b' to search backwards, returns 0 if not found."""
if flags:
flags = ", '%s'" % flags
return int(eval("search('%s'%s)" % (pat, flags)))
def search_or_end(pat, flags=''):
"""Search for `pat`, if not found, go to end of file (or beginning if searching backwards)."""
flags += 'W'
rc = search(pat, flags)
if not rc:
normal("gg" if 'b' in flags else 'G')
return rc
def nwsearch(pat, flags=''):
"""No-wrap search."""
# go to end or beginning of line to ensure we start searching from next line
if not flags : normal('$')
else : normal('0')
return search(pat, flags+'W')
def let(var, val):
command('let %s = "%s"' % (var, str(val)))
def bufappend(*args):
current.buffer.append(*args)
def getc():
"""Get char at cursor location."""
return current.line[col()]
def wincmd(cmd):
command("wincmd " + cmd)
def fnescape(fn):
return eval("fnameescape('%s')" % fn)
def bufloaded(bufname):
return ieval("bufloaded('%s')" % fnescape(bufname))
def mode(expr=0):
"""Return full mode name if `expr`=1; usually returns 'n' or 'c'."""
return eval("mode(%d)" % expr)
def nextnonblank(lnum):
return eval("nextnonblank(%s)" % lnum)
def prevnonblank(lnum):
return eval("prevnonblank(%s)" % lnum)
def pumvisible():
return eval("pumvisible()")
def winline():
"""Screen line of cursor in the window, first line returns 1."""
return int(eval("winline()"))
def lastwinnr():
"""Number of last accessed window."""
return int(eval("winnr('#')"))
def buflist(tabnum=None):
"""List of buffer numbers in tabpage `tabnum`."""
return eval("tabpagebuflist(%s)" % ('' if tabnum==None else tabnum))
def buflisted(expr):
"""Is buffer 'listed' (shows up in :ls)?"""
return int(eval("buflisted(%s)" % expr))
def up(n=1):
normal("%dk" % n)
def down(n=1):
normal("%dj" % n)