# -*- coding: utf-8 -*-

from powerline.renderer import Renderer


class VimRenderer(Renderer):
	'''Powerline vim segment renderer.
	'''
	def __init__(self, theme):
		super(VimRenderer, self).__init__(theme)
		self.hl_groups = {}

	def hl(self, fg=None, bg=None, attr=None):
		'''Highlight a segment.

		If an argument is None, the argument is ignored. If an argument is
		False, the argument is reset to the terminal defaults. If an argument
		is a valid color or attribute, it's added to the vim highlight group.
		'''
		# We don't need to explicitly reset attributes in vim, so skip those calls
		if not attr and not bg and not fg:
			return ''

		if not (fg, bg, attr) in self.hl_groups:
			hl_group = {
				'ctermfg': 'NONE',
				'guifg': 'NONE',
				'ctermbg': 'NONE',
				'guibg': 'NONE',
				'attr': ['NONE'],
				'name': '',
			}

			if fg is not None and fg is not False:
				hl_group['ctermfg'] = fg[0]
				hl_group['guifg'] = fg[1]

			if bg is not None and bg is not False:
				hl_group['ctermbg'] = bg[0]
				hl_group['guibg'] = bg[1]

			if attr:
				hl_group['attr'] = []
				if attr & self.ATTR_BOLD:
					hl_group['attr'].append('bold')
				if attr & self.ATTR_ITALIC:
					hl_group['attr'].append('italic')
				if attr & self.ATTR_UNDERLINE:
					hl_group['attr'].append('underline')

			hl_group['name'] = 'Pl_' + \
				str(hl_group['ctermfg']) + '_' + \
				str(hl_group['guifg']) + '_' + \
				str(hl_group['ctermbg']) + '_' + \
				str(hl_group['guibg']) + '_' + \
				''.join(hl_group['attr'])

			self.hl_groups[(fg, bg, attr)] = hl_group

		return '%#' + self.hl_groups[(fg, bg, attr)]['name'] + '#'

@@ -59,16 +59,28 @@
 nderer%0A%0A
+import vim%0A%0A
 %0Aclass V
@@ -142,16 +142,45 @@
 r.%0A%09'''%0A
+%09PERCENT_PLACEHOLDER = u'%EE%80%80'%0A%0A
 %09def __i
@@ -242,16 +242,16 @@
 (theme)%0A
-
 %09%09self.h
@@ -265,16 +265,200 @@
 s = %7B%7D%0A%0A
+%09def render(self, mode, width=None):%0A%09%09statusline = super(VimRenderer, self).render(mode, width)%0A%09%09statusline = statusline.replace(self.PERCENT_PLACEHOLDER, '%25%25')%0A%09%09return statusline%0A%0A
 %09def hl(
@@ -1783,16 +1783,505 @@
 _group%0A%0A
+%09%09%09# Create highlighting group in vim%0A%09%09%09vim.command('hi %7Bgroup%7D ctermfg=%7Bctermfg%7D guifg=%7Bguifg%7D guibg=%7Bguibg%7D ctermbg=%7Bctermbg%7D cterm=%7Battr%7D gui=%7Battr%7D'.format(%0A%09%09%09%09%09group=hl_group%5B'name'%5D,%0A%09%09%09%09%09ctermfg=hl_group%5B'ctermfg'%5D,%0A%09%09%09%09%09guifg='#%7B0:06x%7D'.format(hl_group%5B'guifg'%5D) if hl_group%5B'guifg'%5D != 'NONE' else 'NONE',%0A%09%09%09%09%09ctermbg=hl_group%5B'ctermbg'%5D,%0A%09%09%09%09%09guibg='#%7B0:06x%7D'.format(hl_group%5B'guibg'%5D) if hl_group%5B'guibg'%5D != 'NONE' else 'NONE',%0A%09%09%09%09%09attr=','.join(hl_group%5B'attr'%5D),%0A%09%09%09%09))%0A%0A
 %09%09return

Update vim renderer to handle all vim-specific stuff