1# vim:fileencoding=utf-8:noet
2from __future__ import (unicode_literals, division, absolute_import, print_function)
3
4try:
5	import vim
6except ImportError:
7	vim = object()
8
9from powerline.bindings.vim import create_ruby_dpowerline
10
11
12def initialize():
13	global initialized
14	if initialized:
15		return
16	initialized = True
17	create_ruby_dpowerline()
18	vim.command((
19		# When using :execute (vim.command uses the same code) one should not
20		# use << EOF.
21		'''
22		ruby
23		if (not ($command_t.respond_to? 'active_finder'))
24			def $command_t.active_finder
25				@active_finder and @active_finder.class.name or ''
26			end
27		end
28		if (not ($command_t.respond_to? 'path'))
29			def $command_t.path
30				@path or ''
31			end
32		end
33		def $powerline.commandt_set_active_finder
34			::VIM::command "let g:powerline_commandt_reply = '#{$command_t.active_finder}'"
35		end
36		def $powerline.commandt_set_path
37			::VIM::command "let g:powerline_commandt_reply = '#{($command_t.path or '').gsub(/'/, "''")}'"
38		end
39		'''
40	))
41
42
43initialized = False
44
45
46def finder(pl):
47	'''Display Command-T finder name
48
49	Requires $command_t.active_finder and methods (code above may monkey-patch
50	$command_t to add them). All Command-T finders have ``CommandT::`` module
51	prefix, but it is stripped out (actually, any ``CommandT::`` substring will
52	be stripped out).
53
54	Highlight groups used: ``commandt:finder``.
55	'''
56	initialize()
57	vim.command('ruby $powerline.commandt_set_active_finder')
58	return [{
59		'highlight_groups': ['commandt:finder'],
60		'contents': vim.eval('g:powerline_commandt_reply').replace('CommandT::', '').replace('Finder::', '')
61	}]
62
63
64FINDERS_WITHOUT_PATH = set((
65	'CommandT::MRUBufferFinder',
66	'CommandT::BufferFinder',
67	'CommandT::TagFinder',
68	'CommandT::JumpFinder',
69	'CommandT::Finder::MRUBufferFinder',
70	'CommandT::Finder::BufferFinder',
71	'CommandT::Finder::TagFinder',
72	'CommandT::Finder::JumpFinder',
73))
74
75
76def path(pl):
77	'''Display path used by Command-T
78
79	Requires $command_t.active_finder and .path methods (code above may
80	monkey-patch $command_t to add them).
81
82	$command_t.active_finder is required in order to omit displaying path for
83	finders ``MRUBufferFinder``, ``BufferFinder``, ``TagFinder`` and
84	``JumpFinder`` (pretty much any finder, except ``FileFinder``).
85
86	Highlight groups used: ``commandt:path``.
87	'''
88	initialize()
89	vim.command('ruby $powerline.commandt_set_active_finder')
90	finder = vim.eval('g:powerline_commandt_reply')
91	if finder in FINDERS_WITHOUT_PATH:
92		return None
93	vim.command('ruby $powerline.commandt_set_path')
94	return [{
95		'highlight_groups': ['commandt:path'],
96		'contents': vim.eval('g:powerline_commandt_reply')
97	}]
98