From 8cf2a86672adc1bd3babd940c2e06df7d2042fbc Mon Sep 17 00:00:00 2001
From: ZyX <kp-pav@yandex.ru>
Date: Sun, 9 Nov 2014 23:52:11 +0300
Subject: [PATCH] Add support for capslock plugin

---
 docs/source/configuration/segments/vim.rst    |  6 +++
 powerline/bindings/vim/__init__.py            | 16 ++++++++
 .../colorschemes/vim/__main__.json            | 37 ++++++++++---------
 .../config_files/themes/vim/default.json      |  5 +++
 powerline/segments/vim/plugin/capslock.py     | 30 +++++++++++++++
 5 files changed, 76 insertions(+), 18 deletions(-)
 create mode 100644 powerline/segments/vim/plugin/capslock.py

diff --git a/docs/source/configuration/segments/vim.rst b/docs/source/configuration/segments/vim.rst
index ace646c4..cb7b3429 100644
--- a/docs/source/configuration/segments/vim.rst
+++ b/docs/source/configuration/segments/vim.rst
@@ -32,3 +32,9 @@ NERDTree segments
 
 .. automodule:: powerline.segments.vim.plugin.nerdtree
    :members:
+
+Capslock segments
+-----------------
+
+.. automodule:: powerline.segments.vim.plugin.capslock
+   :members:
diff --git a/powerline/bindings/vim/__init__.py b/powerline/bindings/vim/__init__.py
index cf06b4eb..3f69dfb1 100644
--- a/powerline/bindings/vim/__init__.py
+++ b/powerline/bindings/vim/__init__.py
@@ -124,6 +124,22 @@ else:
 	vim_get_func = VimFunc
 
 
+if hasattr(vim, 'Function'):
+	def vim_func_exists(f):
+		try:
+			vim.Function(f)
+		except ValueError:
+			return False
+		else:
+			return True
+else:
+	def vim_func_exists(f):
+		try:
+			return bool(int(vim.eval('type(function("{0}")) == 2'.format(f))))
+		except vim.error:
+			return False
+
+
 if type(vim) is object:
 	vim_get_func = lambda *args, **kwargs: None
 
diff --git a/powerline/config_files/colorschemes/vim/__main__.json b/powerline/config_files/colorschemes/vim/__main__.json
index 96505185..1f408836 100644
--- a/powerline/config_files/colorschemes/vim/__main__.json
+++ b/powerline/config_files/colorschemes/vim/__main__.json
@@ -1,23 +1,24 @@
 {
 	"groups": {
-		"branch_clean":     "branch",
-		"environment":      "information:unimportant",
-		"file_size":        "information:unimportant",
-		"file_format":      "information:unimportant",
-		"file_encoding":    "file_format",
-		"file_type":        "file_format",
-		"branch":           "information:additional",
-		"file_scheme":      "file_name",
-		"file_directory":   "information:additional",
-		"file_name_empty":  "file_directory",
-		"line_percent":     "information:additional",
-		"line_count":       "line_current",
-		"position":         "information:additional",
-		"single_tab":       "line_current",
-		"many_tabs":        "line_current",
-		"bufnr":            "file_directory",
-		"winnr":            "information:unimportant",
-		"tabnr":            "file_directory",
+		"branch_clean":       "branch",
+		"environment":        "information:unimportant",
+		"file_size":          "information:unimportant",
+		"file_format":        "information:unimportant",
+		"file_encoding":      "file_format",
+		"file_type":          "file_format",
+		"branch":             "information:additional",
+		"file_scheme":        "file_name",
+		"file_directory":     "information:additional",
+		"file_name_empty":    "file_directory",
+		"line_percent":       "information:additional",
+		"line_count":         "line_current",
+		"position":           "information:additional",
+		"single_tab":         "line_current",
+		"many_tabs":          "line_current",
+		"bufnr":              "file_directory",
+		"winnr":              "information:unimportant",
+		"tabnr":              "file_directory",
+		"capslock_indicator": "paste_indicator",
 
 		"csv:column_number": "line_current",
 		"csv:column_name":   "line_current_symbol",
diff --git a/powerline/config_files/themes/vim/default.json b/powerline/config_files/themes/vim/default.json
index af0955b2..44738ed0 100644
--- a/powerline/config_files/themes/vim/default.json
+++ b/powerline/config_files/themes/vim/default.json
@@ -15,6 +15,11 @@
 				"exclude_modes": ["nc"],
 				"priority": 10
 			},
+			{
+				"function": "powerline.segments.vim.plugin.capslock.capslock_indicator",
+				"include_modes": ["i", "R", "Rv"],
+				"priority": 10
+			},
 			{
 				"function": "branch",
 				"exclude_modes": ["nc"],
diff --git a/powerline/segments/vim/plugin/capslock.py b/powerline/segments/vim/plugin/capslock.py
new file mode 100644
index 00000000..824d55b9
--- /dev/null
+++ b/powerline/segments/vim/plugin/capslock.py
@@ -0,0 +1,30 @@
+# 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 vim_func_exists
+from powerline.theme import requires_segment_info
+
+
+@requires_segment_info
+def capslock_indicator(pl, segment_info, text='CAPS'):
+	'''Shows the indicator if tpope/vim-capslock plugin is enabled
+
+	.. _note::
+		In the current state plugin automatically disables itself when leaving 
+		insert mode. So trying to use this segment not in insert or replace 
+		modes is useless.
+
+	:param str text:
+		String to show when software capslock presented by this plugin is 
+		active.
+	'''
+	if not vim_func_exists('CapsLockStatusline'):
+		return None
+	# CapsLockStatusline() function returns an empty string when plugin is 
+	# disabled. If it is not then string is non-empty.
+	return text if vim.eval('CapsLockStatusline()') else None