mirror of
https://github.com/gryf/.vim.git
synced 2025-12-17 11:30:29 +01:00
Added vimblogger_ft, update for lucius colorscheme and pyflakes plugin
This commit is contained in:
@@ -13,6 +13,8 @@ setlocal softtabstop=4
|
||||
setlocal tabstop=4
|
||||
setlocal textwidth=78
|
||||
setlocal colorcolumn=+1
|
||||
" overwrite status line
|
||||
setlocal statusline=%<%F\ %{TagInStatusLine()}\ %h%m%r%=%(%l,%c%V%)\ %3p%%
|
||||
|
||||
set wildignore+=*.pyc
|
||||
|
||||
|
||||
@@ -85,9 +85,9 @@ def check(buffer):
|
||||
if vimenc:
|
||||
contents = contents.decode(vimenc)
|
||||
|
||||
builtins = []
|
||||
builtins = set(['__file__'])
|
||||
try:
|
||||
builtins = set(eval(vim.eval('string(g:pyflakes_builtins)')))
|
||||
builtins.update(set(eval(vim.eval('string(g:pyflakes_builtins)'))))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -26,22 +26,24 @@ Quick Installation
|
||||
|
||||
2. Download the latest release_.
|
||||
|
||||
3. Unzip ``pyflakes.vim`` and the ``pyflakes`` directory into
|
||||
3. If you're using pathogen_, unzip the contents of ``pyflakes-vim.zip`` into
|
||||
its own bundle directory, i.e. into ``~/.vim/bundle/pyflakes-vim/``.
|
||||
|
||||
Otherwise unzip ``pyflakes.vim`` and the ``pyflakes`` directory into
|
||||
``~/.vim/ftplugin/python`` (or somewhere similar on your
|
||||
`runtime path`_ that will be sourced for Python files).
|
||||
|
||||
.. _release: http://www.vim.org/scripts/script.php?script_id=2441
|
||||
.. _pathogen: http://www.vim.org/scripts/script.php?script_id=2332
|
||||
.. _runtime path: http://vimdoc.sourceforge.net/htmldoc/options.html#'runtimepath'
|
||||
|
||||
Installation
|
||||
------------
|
||||
Running from source
|
||||
-------------------
|
||||
|
||||
If you downloaded this from vim.org_, then just drop the contents of the zip
|
||||
file into ``~/.vim/ftplugin/python``.
|
||||
|
||||
Otherwise, if you're running "from source," you'll need PyFlakes on your
|
||||
PYTHONPATH somewhere. I recommend getting my PyFlakes_ fork, which retains
|
||||
column number information and has therfore has more specific error locations.
|
||||
If you're running pyflakes-vim "from source," you'll need the PyFlakes library
|
||||
on your PYTHONPATH somewhere. (It is included in the vim.org zipfile.) I recommend
|
||||
getting my PyFlakes_ fork, which retains column number information, giving more
|
||||
specific error locations.
|
||||
|
||||
.. _vim.org: http://www.vim.org/scripts/script.php?script_id=2441
|
||||
.. _PyFlakes: http://github.com/kevinw/pyflakes
|
||||
|
||||
649
ftplugin/python/pythonhelper.vim
Normal file
649
ftplugin/python/pythonhelper.vim
Normal file
@@ -0,0 +1,649 @@
|
||||
" File: pythonhelper.vim
|
||||
" Author: Michal Vitecek <fuf-at-mageo-dot-cz>
|
||||
" Version: 0.81
|
||||
" Last Modified: Oct 24, 2002
|
||||
"
|
||||
" Modified by Marius Gedminas <mgedmin@b4net.lt>
|
||||
"
|
||||
" Overview
|
||||
" --------
|
||||
" Vim script to help moving around in larger Python source files. It displays
|
||||
" current class, method or function the cursor is placed in on the status
|
||||
" line for every python file. It's more clever than Yegappan Lakshmanan's
|
||||
" taglist.vim because it takes into account indetation and comments to
|
||||
" determine what tag the cursor is placed in.
|
||||
"
|
||||
" Requirements
|
||||
" ------------
|
||||
" This script needs only VIM compiled with Python interpreter. It doesn't rely
|
||||
" on exuberant ctags utility. You can determine whether your VIM has Python
|
||||
" support by issuing command :ver and looking for +python in the list of
|
||||
" features.
|
||||
"
|
||||
" Note: The script displays current tag on the status line only in NORMAL
|
||||
" mode. This is because CursorHold event is fired up only in this mode.
|
||||
" However if you badly need to know what tag you are in even in INSERT or
|
||||
" VISUAL mode, contact me on the above specified email address and I'll send
|
||||
" you a patch that enables firing up CursorHold event in those modes as well.
|
||||
"
|
||||
" Installation
|
||||
" ------------
|
||||
" 1. Make sure your Vim has python feature on (+python). If not, you will need
|
||||
" to recompile it with --with-pythoninterp option to the configure script
|
||||
" 2. Copy script pythonhelper.vim to the $HOME/.vim/plugin directory
|
||||
" 3. Run Vim and open any python file.
|
||||
"
|
||||
" Marius Gedminas <marius@gedmin.as>:
|
||||
" 4. change 'statusline' to include
|
||||
" %{TagInStatusLine()}
|
||||
"
|
||||
if has("python")
|
||||
python << EOS
|
||||
|
||||
# import of required modules {{{
|
||||
import vim
|
||||
import time
|
||||
import sys
|
||||
import re
|
||||
# }}}
|
||||
|
||||
|
||||
# global dictionaries of tags and their line numbers, keys are buffer numbers {{{
|
||||
TAGS = {}
|
||||
TAGLINENUMBERS = {}
|
||||
BUFFERTICKS = {}
|
||||
# }}}
|
||||
|
||||
|
||||
# class PythonTag() {{{
|
||||
class PythonTag:
|
||||
# DOC {{{
|
||||
"""A simple storage class representing a python tag.
|
||||
"""
|
||||
# }}}
|
||||
|
||||
|
||||
# STATIC VARIABLES {{{
|
||||
|
||||
# tag type IDs {{{
|
||||
TAGTYPE_CLASS = 0
|
||||
TAGTYPE_METHOD = 1
|
||||
TAGTYPE_FUNCTION = 2
|
||||
# }}}
|
||||
|
||||
# tag type names {{{
|
||||
typeName = {
|
||||
TAGTYPE_CLASS : "class",
|
||||
TAGTYPE_METHOD : "method",
|
||||
TAGTYPE_FUNCTION : "function",
|
||||
}
|
||||
# }}}
|
||||
|
||||
# }}}
|
||||
|
||||
|
||||
# METHODS {{{
|
||||
|
||||
def __init__(self, type, name, fullName, lineNumber, indentLevel, parentTag):
|
||||
# DOC {{{
|
||||
"""Initializes instances of class PythonTag().
|
||||
|
||||
Parameters
|
||||
|
||||
type -- tag type
|
||||
|
||||
name -- short tag name
|
||||
|
||||
fullName -- full tag name (in dotted notation)
|
||||
|
||||
lineNumber -- line number on which the tag starts
|
||||
|
||||
indentLevel -- indentation level of the tag
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
self.type = type
|
||||
self.name = name
|
||||
self.fullName = fullName
|
||||
self.lineNumber = lineNumber
|
||||
self.indentLevel = indentLevel
|
||||
self.parentTag = parentTag
|
||||
# }}}
|
||||
|
||||
|
||||
def __str__(self):
|
||||
# DOC {{{
|
||||
"""Returns a string representation of the tag.
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
return "%s (%s) [%s, %u, %u]" % (self.name, PythonTag.typeName[self.type],
|
||||
self.fullName, self.lineNumber, self.indentLevel,)
|
||||
# }}}
|
||||
|
||||
__repr__ = __str__
|
||||
|
||||
|
||||
# }}}
|
||||
# }}}
|
||||
|
||||
|
||||
# class SimplePythonTagsParser() {{{
|
||||
class SimplePythonTagsParser:
|
||||
# DOC {{{
|
||||
"""Provides a simple python tag parser. Returns list of PythonTag()
|
||||
instances.
|
||||
"""
|
||||
# }}}
|
||||
|
||||
|
||||
# STATIC VARIABLES {{{
|
||||
|
||||
# how many chars a single tab represents (visually)
|
||||
TABSIZE = 8
|
||||
|
||||
# regexp used to get indentation and strip comments
|
||||
commentsIndentStripRE = re.compile('([ \t]*)([^\n#]*).*')
|
||||
# regexp used to get class name
|
||||
classRE = re.compile('class[ \t]+([a-zA-Z0-9_]+)[ \t]*([(:].*|$)')
|
||||
# regexp used to get method or function name
|
||||
methodRE = re.compile('def[ \t]+([^(]+).*')
|
||||
|
||||
# }}}
|
||||
|
||||
|
||||
# METHODS {{{
|
||||
|
||||
def __init__(self, source):
|
||||
# DOC {{{
|
||||
"""Initializes the instance of class SimplePythonTagsParser().
|
||||
|
||||
Parameters
|
||||
|
||||
source -- source for which the tags will be generated. It must
|
||||
provide callable method readline (i.e. as file objects do).
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
# make sure source has readline() method {{{
|
||||
if (not(hasattr(source, 'readline') and
|
||||
callable(source.readline))):
|
||||
raise AttributeError("Source must have callable readline method.")
|
||||
# }}}
|
||||
|
||||
# remember what the source is
|
||||
self.source = source
|
||||
# }}}
|
||||
|
||||
|
||||
def getTags(self):
|
||||
# DOC {{{
|
||||
"""Determines all the tags for the buffer. Returns tuple in format
|
||||
(tagLineNumbers, tags,).
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
tagLineNumbers = []
|
||||
tags = {}
|
||||
|
||||
# list (stack) of all currently active tags
|
||||
tagsStack = []
|
||||
|
||||
lineNumber = 0
|
||||
while 1:
|
||||
# get next line
|
||||
line = self.source.readline()
|
||||
|
||||
# finish if this is the end of the source {{{
|
||||
if (line == ''):
|
||||
break
|
||||
# }}}
|
||||
|
||||
lineNumber += 1
|
||||
lineMatch = self.commentsIndentStripRE.match(line)
|
||||
lineContents = lineMatch.group(2)
|
||||
# class tag {{{
|
||||
tagMatch = self.classRE.match(lineContents)
|
||||
if (tagMatch):
|
||||
currentTag = self.getPythonTag(tagsStack, lineNumber, lineMatch.group(1),
|
||||
tagMatch.group(1), self.tagClassTypeDecidingMethod)
|
||||
tagLineNumbers.append(lineNumber)
|
||||
tags[lineNumber] = currentTag
|
||||
# }}}
|
||||
# function/method/none tag {{{
|
||||
else:
|
||||
tagMatch = self.methodRE.match(lineContents)
|
||||
if (tagMatch):
|
||||
currentTag = self.getPythonTag(tagsStack, lineNumber, lineMatch.group(1),
|
||||
tagMatch.group(1), self.tagFunctionTypeDecidingMethod)
|
||||
tagLineNumbers.append(lineNumber)
|
||||
tags[lineNumber] = currentTag
|
||||
# }}}
|
||||
|
||||
# return the tags data for the source
|
||||
return (tagLineNumbers, tags,)
|
||||
# }}}
|
||||
|
||||
|
||||
def getPreviousTag(self, tagsStack):
|
||||
# DOC {{{
|
||||
"""Returns the previous tag (instance of PythonTag()) from the
|
||||
specified tag list if possible. If not, returns None.
|
||||
|
||||
Parameters
|
||||
|
||||
tagsStack -- list (stack) of currently active PythonTag() instances
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
if (len(tagsStack)):
|
||||
previousTag = tagsStack[-1]
|
||||
else:
|
||||
previousTag = None
|
||||
|
||||
# return the tag
|
||||
return previousTag
|
||||
# }}}
|
||||
|
||||
|
||||
def computeIndentLevel(self, indentChars):
|
||||
# DOC {{{
|
||||
"""Computes indent level from the specified string.
|
||||
|
||||
Parameters
|
||||
|
||||
indentChars -- white space before any other character on line
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
indentLevel = 0
|
||||
for char in indentChars:
|
||||
if (char == '\t'):
|
||||
indentLevel += self.TABSIZE
|
||||
else:
|
||||
indentLevel += 1
|
||||
|
||||
return indentLevel
|
||||
# }}}
|
||||
|
||||
|
||||
def getPythonTag(self, tagsStack, lineNumber, indentChars, tagName, tagTypeDecidingMethod):
|
||||
# DOC {{{
|
||||
"""Returns instance of PythonTag() based on the specified data.
|
||||
|
||||
Parameters
|
||||
|
||||
tagsStack -- list (stack) of tags currently active. Note: Modified
|
||||
in this method!
|
||||
|
||||
lineNumber -- current line number
|
||||
|
||||
indentChars -- characters making up the indentation level of the
|
||||
current tag
|
||||
|
||||
tagName -- short name of the current tag
|
||||
|
||||
tagTypeDecidingMethod -- reference to method that is called to
|
||||
determine type of the current tag
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
indentLevel = self.computeIndentLevel(indentChars)
|
||||
previousTag = self.getPreviousTag(tagsStack)
|
||||
# code for enclosed tag {{{
|
||||
while (previousTag):
|
||||
if (previousTag.indentLevel >= indentLevel):
|
||||
del tagsStack[-1]
|
||||
else:
|
||||
tagType = tagTypeDecidingMethod(previousTag.type)
|
||||
tag = PythonTag(tagType, tagName, "%s.%s" % (previousTag.fullName, tagName,), lineNumber, indentLevel, previousTag)
|
||||
tagsStack.append(tag)
|
||||
return tag
|
||||
previousTag = self.getPreviousTag(tagsStack)
|
||||
# }}}
|
||||
# code for tag in top indent level {{{
|
||||
else:
|
||||
tagType = tagTypeDecidingMethod(None)
|
||||
tag = PythonTag(tagType, tagName, tagName, lineNumber, indentLevel, None)
|
||||
tagsStack.append(tag)
|
||||
return tag
|
||||
# }}}
|
||||
# }}}
|
||||
|
||||
|
||||
def tagClassTypeDecidingMethod(self, previousTagsType):
|
||||
# DOC {{{
|
||||
"""Returns tag type of the current tag based on its previous tag (super
|
||||
tag) for classes.
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
return PythonTag.TAGTYPE_CLASS
|
||||
# }}}
|
||||
|
||||
|
||||
def tagFunctionTypeDecidingMethod(self, previousTagsType):
|
||||
# DOC {{{
|
||||
"""Returns tag type of the current tag based on its previous tag (super
|
||||
tag) for functions/methods.
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
if (previousTagsType == PythonTag.TAGTYPE_CLASS):
|
||||
return PythonTag.TAGTYPE_METHOD
|
||||
else:
|
||||
return PythonTag.TAGTYPE_FUNCTION
|
||||
# }}}
|
||||
|
||||
|
||||
# }}}
|
||||
# }}}
|
||||
|
||||
|
||||
# class VimReadlineBuffer() {{{
|
||||
class VimReadlineBuffer:
|
||||
# DOC {{{
|
||||
"""A simple wrapper class around vim's buffer that provides readline
|
||||
method.
|
||||
"""
|
||||
# }}}
|
||||
|
||||
|
||||
# METHODS {{{
|
||||
|
||||
def __init__(self, vimBuffer):
|
||||
# DOC {{{
|
||||
"""Initializes the instance of class VimReadlineBuffer().
|
||||
|
||||
Parameters
|
||||
|
||||
vimBuffer -- VIM's buffer
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
self.vimBuffer = vimBuffer
|
||||
self.currentLine = -1
|
||||
self.bufferLines = len(vimBuffer)
|
||||
# }}}
|
||||
|
||||
|
||||
def readline(self):
|
||||
# DOC {{{
|
||||
"""Returns next line from the buffer. If all the buffer has been read,
|
||||
returns empty string.
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
self.currentLine += 1
|
||||
|
||||
# notify end of file if we reached beyond the last line {{{
|
||||
if (self.currentLine == self.bufferLines):
|
||||
return ''
|
||||
# }}}
|
||||
|
||||
# return the line with added newline (vim stores the lines without newline)
|
||||
return "%s\n" % (self.vimBuffer[self.currentLine],)
|
||||
# }}}
|
||||
|
||||
# }}}
|
||||
# }}}
|
||||
|
||||
|
||||
def getNearestLineIndex(row, tagLineNumbers):
|
||||
# DOC {{{
|
||||
"""Returns index of line in tagLineNumbers list that is nearest to the
|
||||
current cursor row.
|
||||
|
||||
Parameters
|
||||
|
||||
row -- current cursor row
|
||||
|
||||
tagLineNumbers -- list of tags' line numbers (ie. their position)
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
nearestLineNumber = -1
|
||||
nearestLineIndex = -1
|
||||
i = 0
|
||||
for lineNumber in tagLineNumbers:
|
||||
# if the current line is nearer the current cursor position, take it {{{
|
||||
if (nearestLineNumber < lineNumber <= row):
|
||||
nearestLineNumber = lineNumber
|
||||
nearestLineIndex = i
|
||||
# }}}
|
||||
# if we've got past the current cursor position, let's end the search {{{
|
||||
if (lineNumber >= row):
|
||||
break
|
||||
# }}}
|
||||
i += 1
|
||||
return nearestLineIndex
|
||||
# }}}
|
||||
|
||||
|
||||
def getTags(bufferNumber, changedTick):
|
||||
# DOC {{{
|
||||
"""Reads the tags for the specified buffer number. Returns tuple
|
||||
(taglinenumber[buffer], tags[buffer],).
|
||||
|
||||
Parameters
|
||||
|
||||
bufferNumber -- number of the current buffer
|
||||
|
||||
changedTick -- ever increasing number used to tell if the buffer has
|
||||
been modified since the last time
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
global TAGLINENUMBERS, TAGS, BUFFERTICKS
|
||||
|
||||
# return immediately if there's no need to update the tags {{{
|
||||
if ((BUFFERTICKS.has_key(bufferNumber)) and (BUFFERTICKS[bufferNumber] == changedTick)):
|
||||
return (TAGLINENUMBERS[bufferNumber], TAGS[bufferNumber],)
|
||||
# }}}
|
||||
|
||||
# get the tags {{{
|
||||
simpleTagsParser = SimplePythonTagsParser(VimReadlineBuffer(vim.current.buffer))
|
||||
tagLineNumbers, tags = simpleTagsParser.getTags()
|
||||
# }}}
|
||||
|
||||
# update the global variables {{{
|
||||
TAGS[bufferNumber] = tags
|
||||
TAGLINENUMBERS[bufferNumber] = tagLineNumbers
|
||||
BUFFERTICKS[bufferNumber] = changedTick
|
||||
# }}}
|
||||
|
||||
# return the tags data
|
||||
return (tagLineNumbers, tags,)
|
||||
# }}}
|
||||
|
||||
|
||||
def findTag(bufferNumber, changedTick):
|
||||
# DOC {{{
|
||||
"""Tries to find the best tag for the current cursor position.
|
||||
|
||||
Parameters
|
||||
|
||||
bufferNumber -- number of the current buffer
|
||||
|
||||
changedTick -- ever increasing number used to tell if the buffer has
|
||||
been modified since the last time
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
try:
|
||||
# get the tags data for the current buffer {{{
|
||||
tagLineNumbers, tags = getTags(bufferNumber, changedTick)
|
||||
# }}}
|
||||
|
||||
# link to vim internal data {{{
|
||||
currentBuffer = vim.current.buffer
|
||||
currentWindow = vim.current.window
|
||||
row, col = currentWindow.cursor
|
||||
# }}}
|
||||
|
||||
# get the index of the nearest line
|
||||
nearestLineIndex = getNearestLineIndex(row, tagLineNumbers)
|
||||
# if any line was found, try to find if the tag is appropriate {{{
|
||||
# (ie. the cursor can be below the last tag but on a code that has nothing
|
||||
# to do with the tag, because it's indented differently, in such case no
|
||||
# appropriate tag has been found.)
|
||||
if (nearestLineIndex > -1):
|
||||
nearestLineNumber = tagLineNumbers[nearestLineIndex]
|
||||
tagInfo = tags[nearestLineNumber]
|
||||
# walk through all the lines in range (nearestTagLine, cursorRow) {{{
|
||||
for i in xrange(nearestLineNumber + 1, row):
|
||||
line = currentBuffer[i]
|
||||
# count the indentation of the line, if it's lower that the tag's, the found tag is wrong {{{
|
||||
if (len(line)):
|
||||
# compute the indentation of the line {{{
|
||||
lineStart = 0
|
||||
j = 0
|
||||
while ((j < len(line)) and (line[j].isspace())):
|
||||
if (line[j] == '\t'):
|
||||
lineStart += SimplePythonTagsParser.TABSIZE
|
||||
else:
|
||||
lineStart += 1
|
||||
j += 1
|
||||
# if the line contains only spaces, it doesn't count {{{
|
||||
if (j == len(line)):
|
||||
continue
|
||||
# }}}
|
||||
# if the next character is # (python comment), this line doesn't count {{{
|
||||
if (line[j] == '#'):
|
||||
continue
|
||||
# }}}
|
||||
# }}}
|
||||
# if the line's indentation starts before or at the nearest tag's one, the tag is wrong {{{
|
||||
while tagInfo is not None and lineStart <= tagInfo.indentLevel:
|
||||
tagInfo = tagInfo.parentTag
|
||||
# }}}
|
||||
# }}}
|
||||
# }}}
|
||||
else:
|
||||
tagInfo = None
|
||||
# }}}
|
||||
|
||||
# describe the cursor position (what tag it's in) {{{
|
||||
tagDescription = ""
|
||||
if tagInfo is not None:
|
||||
## tagDescription = "[in %s (%s)]" % (tagInfo.fullName, PythonTag.typeName[tagInfo.type],)
|
||||
tagDescription = "[%s]" % (tagInfo.fullName, )
|
||||
# }}}
|
||||
|
||||
# update the variable for the status line so it will be updated next time
|
||||
vim.command("let w:PHStatusLine=\"%s\"" % (tagDescription,))
|
||||
except:
|
||||
# spit out debugging information {{{
|
||||
ec, ei, tb = sys.exc_info()
|
||||
while (tb != None):
|
||||
if (tb.tb_next == None):
|
||||
break
|
||||
tb = tb.tb_next
|
||||
print "ERROR: %s %s %s:%u" % (ec.__name__, ei, tb.tb_frame.f_code.co_filename, tb.tb_lineno,)
|
||||
time.sleep(0.5)
|
||||
# }}}
|
||||
# }}}
|
||||
|
||||
|
||||
def deleteTags(bufferNumber):
|
||||
# DOC {{{
|
||||
"""Removes tags data for the specified buffer number.
|
||||
|
||||
Parameters
|
||||
|
||||
bufferNumber -- number of the buffer
|
||||
"""
|
||||
# }}}
|
||||
|
||||
# CODE {{{
|
||||
global TAGS, TAGLINENUMBERS, BUFFERTICKS
|
||||
|
||||
try:
|
||||
del TAGS[bufferNumber]
|
||||
del TAGLINENUMBERS[bufferNumber]
|
||||
del BUFFERTICKS[bufferNumber]
|
||||
except:
|
||||
pass
|
||||
# }}}
|
||||
|
||||
|
||||
EOS
|
||||
|
||||
" VIM functions {{{
|
||||
|
||||
function! PHCursorHold()
|
||||
" only python is supported {{{
|
||||
if (!exists('b:current_syntax') || (b:current_syntax != 'python'))
|
||||
let w:PHStatusLine = ''
|
||||
return
|
||||
endif
|
||||
" }}}
|
||||
|
||||
" call python function findTag() with the current buffer number and changed ticks
|
||||
execute 'python findTag(' . expand("<abuf>") . ', ' . b:changedtick . ')'
|
||||
endfunction
|
||||
|
||||
|
||||
function! PHBufferDelete()
|
||||
" set PHStatusLine for this window to empty string
|
||||
let w:PHStatusLine = ""
|
||||
|
||||
" call python function deleteTags() with the cur
|
||||
execute 'python deleteTags(' . expand("<abuf>") . ')'
|
||||
endfunction
|
||||
|
||||
|
||||
function! TagInStatusLine()
|
||||
" return value of w:PHStatusLine in case it's set
|
||||
if (exists("w:PHStatusLine"))
|
||||
return w:PHStatusLine
|
||||
" otherwise just return empty string
|
||||
else
|
||||
return ""
|
||||
endif
|
||||
endfunction
|
||||
|
||||
" }}}
|
||||
|
||||
|
||||
" event binding, vim customizing {{{
|
||||
|
||||
" autocommands binding
|
||||
if v:version >= 700
|
||||
autocmd CursorMoved * call PHCursorHold()
|
||||
else
|
||||
autocmd CursorHold * call PHCursorHold()
|
||||
endif
|
||||
autocmd BufDelete * silent call PHBufferDelete()
|
||||
|
||||
"" " time that determines after how long time of no activity the CursorHold event
|
||||
"" " is fired up
|
||||
"" set updatetime=1000
|
||||
""
|
||||
"" " color of the current tag in the status line (bold cyan on black)
|
||||
"" highlight User1 gui=bold guifg=cyan guibg=black
|
||||
"" " color of the modified flag in the status line (bold black on red)
|
||||
"" highlight User2 gui=bold guifg=black guibg=red
|
||||
"" " the status line will be displayed for every window
|
||||
"" set laststatus=2
|
||||
"" " set the status line to display some useful information
|
||||
"" set stl=%-f%r\ %2*%m%*\ \ \ \ %1*%{TagInStatusLine()}%*%=[%l:%c]\ \ \ \ [buf\ %n]
|
||||
|
||||
" }}}
|
||||
|
||||
" vim:foldmethod=marker
|
||||
endif " has("python")
|
||||
@@ -9,7 +9,8 @@ setlocal formatoptions=tcq "set VIms default
|
||||
let g:blogger_login="gryf73"
|
||||
let g:blogger_name="rdobosz"
|
||||
let g:blogger_browser=1
|
||||
let g:blogger_stylesheets=["css/widget_css_2_bundle.css", "css/style_custom.css", "css/style_blogger.css"]
|
||||
let g:blogger_stylesheets=["css/widget_css_2_bundle.css", "css/style_custom.css", "css/style_blogger.css", "css/wombat_pygments.css", "css/lucius_pygments.css"]
|
||||
let g:blogger_pygments_class="wombat"
|
||||
|
||||
map <F6> :PreviewBlogArticle<cr>
|
||||
map <F7> :SendBlogArticle<cr>
|
||||
|
||||
@@ -13,6 +13,11 @@ from xml.parsers.expat import ExpatError
|
||||
import vim
|
||||
|
||||
from rst2blogger.rest import blogPreview, blogArticleString
|
||||
try:
|
||||
from rst2blogger.rest import register
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
from rst2blogger.blogger import VimBlogger
|
||||
|
||||
|
||||
@@ -35,6 +40,11 @@ class Rst2Blogger(object):
|
||||
self.maxarticles = int(vim.eval("g:blogger_maxarticles"))
|
||||
self.confirm_del = int(vim.eval("g:blogger_confirm_del"))
|
||||
self.stylesheets = vim.eval("g:blogger_stylesheets")
|
||||
self.pygments_class = vim.eval("g:blogger_pygments_class")
|
||||
try:
|
||||
register(self.pygments_class)
|
||||
except NameError:
|
||||
pass
|
||||
|
||||
def preview(self):
|
||||
"""
|
||||
|
||||
@@ -17,6 +17,31 @@ try:
|
||||
from pygments.lexers import get_lexer_by_name, TextLexer
|
||||
from pygments.formatters import HtmlFormatter
|
||||
|
||||
def register(cssclass=None):
|
||||
if cssclass:
|
||||
Pygments.cssclass = cssclass
|
||||
directives.register_directive('sourcecode', Pygments)
|
||||
|
||||
def _positive_int_or_1(argument):
|
||||
"""
|
||||
Converts the argument into an integer. Returns positive integer. In
|
||||
case of integers smaller than 1, returns 1. In case of None, returns
|
||||
1.
|
||||
"""
|
||||
if argument is None:
|
||||
return 1
|
||||
|
||||
retval = 1
|
||||
try:
|
||||
retval = int(argument)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if retval < 1:
|
||||
return 1
|
||||
|
||||
return retval
|
||||
|
||||
class Pygments(Directive):
|
||||
"""
|
||||
Source code syntax highlighting.
|
||||
@@ -24,7 +49,11 @@ try:
|
||||
required_arguments = 1
|
||||
optional_arguments = 0
|
||||
final_argument_whitespace = True
|
||||
option_spec = {'linenos': _positive_int_or_1,
|
||||
'cssclass': directives.unchanged_required}
|
||||
has_content = True
|
||||
cssclass = None
|
||||
|
||||
|
||||
def run(self):
|
||||
self.assert_has_content()
|
||||
@@ -33,12 +62,26 @@ try:
|
||||
except ValueError:
|
||||
# no lexer found - use the text one instead of an exception
|
||||
lexer = TextLexer()
|
||||
|
||||
# take an arbitrary option if more than one is given
|
||||
formatter = HtmlFormatter(noclasses=True)
|
||||
kwargs = {'full': False,
|
||||
'noclasses': False}
|
||||
|
||||
if self.options and 'linenos' in self.options:
|
||||
kwargs['linenos'] = 'inline'
|
||||
kwargs['linenostart'] = self.options['linenos']
|
||||
|
||||
if Pygments.cssclass:
|
||||
kwargs['cssclass'] = Pygments.cssclass
|
||||
|
||||
if self.options and 'cssclass' in self.options:
|
||||
kwargs['cssclass'] = self.options['cssclass']
|
||||
|
||||
formatter = HtmlFormatter(**kwargs)
|
||||
|
||||
parsed = highlight(u'\n'.join(self.content), lexer, formatter)
|
||||
return [nodes.raw('', parsed, format='html')]
|
||||
|
||||
directives.register_directive('sourcecode', Pygments)
|
||||
register()
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
73
ftplugin/rst/rst2blogger/scripts/style2css.py
Executable file
73
ftplugin/rst/rst2blogger/scripts/style2css.py
Executable file
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
Generate CSS stylesheet out of provided Style class module, which is an output
|
||||
from the vim2pygments.py[1] script.
|
||||
|
||||
That stylesheet (with any necessary, additional modifications) can be used
|
||||
with vimblogger_ft[2] VIm plugin, but also for general pygments usage.
|
||||
|
||||
Usage:
|
||||
style2css <module>
|
||||
|
||||
[1] vim2pygments is part of the Pygments module, and can be found in scripts
|
||||
directory of the Pygments <http://pygments.org> distribution.
|
||||
[2] <http://www.vim.org/scripts/script.php?script_id=3367>
|
||||
"""
|
||||
|
||||
import optparse
|
||||
import os
|
||||
|
||||
from pygments.formatters import HtmlFormatter
|
||||
|
||||
|
||||
class Pygments2CSS(object):
|
||||
def __init__(self, modulefn, cssclass):
|
||||
self.style_class = None
|
||||
self.cssclass = cssclass
|
||||
if not self.cssclass.startswith("."):
|
||||
self.cssclass = "." + self.cssclass
|
||||
|
||||
mod = os.path.splitext(os.path.basename(modulefn))[0]
|
||||
|
||||
try:
|
||||
module = __import__("%s" % mod)
|
||||
except ImportError:
|
||||
print('Error: %s should be in PYTHONPATH or current'
|
||||
' directory, and should contain valid Style derived'
|
||||
' class' % modulefn)
|
||||
raise
|
||||
|
||||
for item in dir(module):
|
||||
if item != 'Style' and item.endswith('Style'):
|
||||
self.style_class = getattr(module, item)
|
||||
break
|
||||
else:
|
||||
raise ValueError("Error: Wrong module?")
|
||||
|
||||
def out(self):
|
||||
formatter = HtmlFormatter(style=self.style_class)
|
||||
print "%s { background-color: %s }" % \
|
||||
(self.cssclass, self.style_class.background_color)
|
||||
for line in formatter.get_style_defs().split("\n"):
|
||||
print "%s" % self.cssclass, line
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = optparse.OptionParser("usage: %prog [options] stylefile.py\n"
|
||||
"Where stylefile.py is a product of the"
|
||||
" vim2pygments.py script Pygments "
|
||||
"distribution.")
|
||||
parser.add_option("-c", "--class",
|
||||
dest="cssclass",
|
||||
type="str",
|
||||
help="Main CSS class name. Defaults to 'syntax'",
|
||||
default="syntax")
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
if len(args) != 1:
|
||||
parser.error("stylefile.py is required")
|
||||
|
||||
if not (os.path.exists(args[0]) and os.path.isfile(args[0])):
|
||||
parser.error("%s not found" % args[0])
|
||||
|
||||
p2css = Pygments2CSS(args[0], options.cssclass)
|
||||
p2css.out()
|
||||
@@ -278,7 +278,8 @@ class TestRst2BloggerPreview(unittest.TestCase):
|
||||
Try to post not well formed html
|
||||
"""
|
||||
Eval.value = 1
|
||||
print self.obj.preview()
|
||||
self.assertEqual(self.obj.preview(),
|
||||
'Generated HTML has been opened in browser')
|
||||
|
||||
def test_preview_save_to_file(self):
|
||||
"""
|
||||
|
||||
@@ -11,6 +11,69 @@ sys.path.insert(0, this_dir)
|
||||
from rst2blogger.rest import blogArticleString, blogPreview
|
||||
from rst2blogger.tests.shared import REST_ARTICLE
|
||||
|
||||
LINENOS1 = """
|
||||
.. sourcecode:: python
|
||||
:linenos:
|
||||
|
||||
import vim
|
||||
print vim.current.buffer.name
|
||||
|
||||
"""
|
||||
|
||||
LINENOS2 = """
|
||||
.. sourcecode:: python
|
||||
:linenos: -1
|
||||
|
||||
import vim
|
||||
print vim.current.buffer.name
|
||||
|
||||
"""
|
||||
|
||||
LINENOS3 = """
|
||||
.. sourcecode:: python
|
||||
:linenos: 0
|
||||
|
||||
import vim
|
||||
print vim.current.buffer.name
|
||||
|
||||
"""
|
||||
|
||||
LINENOS4 = """
|
||||
.. sourcecode:: python
|
||||
:linenos: 12
|
||||
|
||||
import vim
|
||||
print vim.current.buffer.name
|
||||
|
||||
"""
|
||||
|
||||
LINENOS5 = """
|
||||
.. sourcecode:: python
|
||||
:linenos: this is wrong
|
||||
|
||||
import vim
|
||||
print vim.current.buffer.name
|
||||
|
||||
"""
|
||||
|
||||
CSSCLASS1 = """
|
||||
.. sourcecode:: python
|
||||
:cssclass:
|
||||
|
||||
import vim
|
||||
print vim.current.buffer.name
|
||||
|
||||
"""
|
||||
|
||||
CSSCLASS2 = """
|
||||
.. sourcecode:: python
|
||||
:cssclass: Dessert256
|
||||
|
||||
import vim
|
||||
print vim.current.buffer.name
|
||||
|
||||
"""
|
||||
|
||||
|
||||
class TestBlogPreview(unittest.TestCase):
|
||||
"""
|
||||
@@ -75,5 +138,64 @@ class TestBlogArticleString(unittest.TestCase):
|
||||
self.assertEqual(attrs['date'], "2010-12-12T12:36:36+01:00")
|
||||
self.assertEqual(attrs['tags'], "this is a test, Blogger, rest")
|
||||
|
||||
|
||||
class TestBlogArticlePytgments(unittest.TestCase):
|
||||
"""
|
||||
Test cases for sourcecode directive
|
||||
"""
|
||||
def test_linenos_no_args(self):
|
||||
"""
|
||||
Test linenos option with no additional arguments
|
||||
"""
|
||||
html_out, _ = blogArticleString(LINENOS1)
|
||||
self.assertTrue('<pre><span class="lineno">1</span>' in html_out)
|
||||
|
||||
def test_linenos_with_arg1(self):
|
||||
"""
|
||||
Test linenos option with correct argument type but wrong value.
|
||||
Should count from 1 in this case.
|
||||
"""
|
||||
html_out, _ = blogArticleString(LINENOS2)
|
||||
self.assertTrue('<pre><span class="lineno">1</span>' in html_out)
|
||||
|
||||
def test_linenos_with_arg2(self):
|
||||
"""
|
||||
Test linenos option with correct argument type but wrong value.
|
||||
Should count from 1 in this case.
|
||||
"""
|
||||
html_out, _ = blogArticleString(LINENOS3)
|
||||
self.assertTrue('<pre><span class="lineno">1</span>' in html_out)
|
||||
|
||||
def test_linenos_with_arg3(self):
|
||||
"""
|
||||
Test linenos option with correct argument type and correct value.
|
||||
Should count from 1 in this case.
|
||||
"""
|
||||
html_out, _ = blogArticleString(LINENOS4)
|
||||
self.assertTrue('<pre><span class="lineno">12</span>' in html_out)
|
||||
|
||||
def test_linenos_with_wrong_arg(self):
|
||||
"""
|
||||
Test linenos option with wrong argument type. Should count from 1.
|
||||
"""
|
||||
html_out, _ = blogArticleString(LINENOS5)
|
||||
self.assertTrue('<pre><span class="lineno">1</span>' in html_out)
|
||||
|
||||
def test_cssclass_failure(self):
|
||||
"""
|
||||
Test cssclass option with no arguments. Should complain with system
|
||||
message.
|
||||
"""
|
||||
html_out, _ = blogArticleString(CSSCLASS1)
|
||||
self.assertTrue('System Message: ERROR/3' in html_out)
|
||||
|
||||
def test_cssclass_correct(self):
|
||||
"""
|
||||
Test cssclass option with Dessert256 as an argument. Should be used as
|
||||
a main div CSS class.
|
||||
"""
|
||||
html_out, _ = blogArticleString(CSSCLASS2)
|
||||
self.assertTrue('<div class="Dessert256">' in html_out)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
" reST to blogger vim interface.
|
||||
" Provide some convinient commands for creating preview from the reST file
|
||||
" and to send articles to blog.
|
||||
" VERSION: 0.2
|
||||
|
||||
if exists("b:did_rst_plugin")
|
||||
finish " load only once
|
||||
@@ -44,6 +45,10 @@ if !exists("g:blogger_stylesheets")
|
||||
let g:blogger_stylesheets = []
|
||||
endif
|
||||
|
||||
if !exists("g:blogger_pygments_class")
|
||||
let g:blogger_pygments_class = ""
|
||||
endif
|
||||
|
||||
python << EOF
|
||||
import os
|
||||
import sys
|
||||
@@ -53,10 +58,8 @@ import vim
|
||||
scriptdir = os.path.dirname(vim.eval('expand("<sfile>")'))
|
||||
sys.path.insert(0, scriptdir)
|
||||
|
||||
try:
|
||||
from rst2blogger.main import Rst2Blogger
|
||||
except ImportError:
|
||||
print "Plugin vimblogger cannot be loaded, due to lack of required modules"
|
||||
# Will raise exception, if one of required moudles is missing
|
||||
from rst2blogger.main import Rst2Blogger
|
||||
EOF
|
||||
|
||||
if !exists(":PreviewBlogArticle")
|
||||
|
||||
Reference in New Issue
Block a user