From 2ec49b58148328eda0fc9dd086b873bcf2529830 Mon Sep 17 00:00:00 2001
From: "Kenneth Benzie (Benie)" <benie@infektor.net>
Date: Sun, 3 Apr 2016 13:49:50 +0100
Subject: [PATCH] Add vim mappings around yapf python formatter

---
 after/ftdetect/python.vim | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/after/ftdetect/python.vim b/after/ftdetect/python.vim
index 3a67625..3f5a63a 100644
--- a/after/ftdetect/python.vim
+++ b/after/ftdetect/python.vim
@@ -1,2 +1,34 @@
 " Python convention is to use 4 space tabs.
 setlocal tabstop=4 shiftwidth=4 softtabstop=4
+
+" Python formatting mappings {{{
+" Supported yapf styles: pep8, google
+let g:yapf_style='pep8'
+" A wrapper function to restore the cursor position, window position,
+" and last search after running a command.
+function! Preserve(command)
+  " Save the last search
+  let last_search=@/
+  " Save the current cursor position
+  let save_cursor = getpos(".")
+  " Save the window position
+  normal H
+  let save_window = getpos(".")
+  call setpos('.', save_cursor)
+
+  " Do the business:
+  execute a:command
+
+  " Restore the last_search
+  let @/=last_search
+  " Restore the window position
+  call setpos('.', save_window)
+  normal zt
+  " Restore the cursor position
+  call setpos('.', save_cursor)
+endfunction
+nmap <leader>ff :call Preserve(
+      \ "silent exec '0,$!yapf --style ".g:yapf_style."'")<CR>
+" TODO Add mapping for single line formatting
+" TODO Add mapping for text block formatting
+" }}}