summaryrefslogtreecommitdiffstats
path: root/powerline/segments/vim/plugin/commandt.py
blob: 7e5262efff900c5dc859f5d273ee7b43acdcdf9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

try:
	import vim
except ImportError:
	vim = object()

from powerline.bindings.vim import create_ruby_dpowerline


def initialize():
	global initialized
	if initialized:
		return
	initialized = True
	create_ruby_dpowerline()
	vim.command((
		# When using :execute (vim.command uses the same code) one should not 
		# use << EOF.
		'''
		ruby
		if (not ($command_t.respond_to? 'active_finder'))
			def $command_t.active_finder
				@active_finder and @active_finder.class.name or ''
			end
		end
		if (not ($command_t.respond_to? 'path'))
			def $command_t.path
				@path or ''
			end
		end
		def $powerline.commandt_set_active_finder
			::VIM::command "let g:powerline_commandt_reply = '#{$command_t.active_finder}'"
		end
		def $powerline.commandt_set_path
			::VIM::command "let g:powerline_commandt_reply = '#{($command_t.path or '').gsub(/'/, "''")}'"
		end
		'''
	))


initialized = False


def finder(pl):
	'''Display Command-T finder name

	Requires $command_t.active_finder and methods (code above may monkey-patch 
	$command_t to add them). All Command-T finders have ``CommandT::`` module 
	prefix, but it is stripped out (actually, any ``CommandT::`` substring will 
	be stripped out).

	Highlight groups used: ``commandt:finder``.
	'''
	initialize()
	vim.command('ruby $powerline.commandt_set_active_finder')
	return [{
		'highlight_groups': ['commandt:finder'],
		'contents': vim.eval('g:powerline_commandt_reply').replace('CommandT::', '').replace('Finder::', '')
	}]


FINDERS_WITHOUT_PATH = set((
	'CommandT::MRUBufferFinder',
	'CommandT::BufferFinder',
	'CommandT::TagFinder',
	'CommandT::JumpFinder',
	'CommandT::Finder::MRUBufferFinder',
	'CommandT::Finder::BufferFinder',
	'CommandT::Finder::TagFinder',
	'CommandT::Finder::JumpFinder',
))


def path(pl):
	'''Display path used by Command-T

	Requires $command_t.active_finder and .path methods (code above may 
	monkey-patch $command_t to add them).

	$command_t.active_finder is required in order to omit displaying path for 
	finders ``MRUBufferFinder``, ``BufferFinder``, ``TagFinder`` and 
	``JumpFinder`` (pretty much any finder, except ``FileFinder``).

	Highlight groups used: ``commandt:path``.
	'''
	initialize()
	vim.command('ruby $powerline.commandt_set_active_finder')
	finder = vim.eval('g:powerline_commandt_reply')
	if finder in FINDERS_WITHOUT_PATH:
		return None
	vim.command('ruby $powerline.commandt_set_path')
	return [{
		'highlight_groups': ['commandt:path'],
		'contents': vim.eval('g:powerline_commandt_reply')
	}]