-
Notifications
You must be signed in to change notification settings - Fork 2
/
python_snippets.vim
145 lines (135 loc) · 5.92 KB
/
python_snippets.vim
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"=============================================================================
" File: ftplugin/python/python_snippets.vim {{{1
" Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
" <URL:http://github.com/LucHermitte>
" License: GPLv3 with exceptions
" <URL:http://github.com/LucHermitte/lh-brackets/License.md>
" Version: 3.5.4
let s:k_version = '3.5.4'
" Created: 21st Jan 2015
"------------------------------------------------------------------------
" Description:
" Snippets of python Control Statements
"
"------------------------------------------------------------------------
" TODO:
" - move this file from lh-brachets to lh-misc (or a future lh-python in case I
" spend more time writing Python)
" }}}1
"=============================================================================
" Buffer-local Definitions {{{1
" Avoid local reinclusion {{{2
if &cp || (exists("b:loaded_ftplug_python_snippets")
\ && (b:loaded_ftplug_python_snippets >= s:k_version)
\ && !exists('g:force_reload_ftplug_python_snippets'))
finish
endif
let b:loaded_ftplug_python_snippets = s:k_version
let s:cpo_save=&cpo
set cpo&vim
" Avoid local reinclusion }}}2
"------------------------------------------------------------------------
" Local mappings {{{2
" Control statements {{{3
" --- if ---------------------------------------------------------{{{4
"--if insert "if" statement {{{5
Inoreabbr <buffer> <silent> if <C-R>=PyMapOnSingleLine('if ',
\ "\\<c-f\\>if !cursorhere!:\n!mark!")<cr>
"--,if insert "if" statement
xnoremap <buffer> <silent> <localleader>if
\ <c-\><c-n>@=lh#map#surround('if !cursorhere!:', '!mark!',
\ 1, 1, '', 1, 'if ')<cr>
xnoremap <buffer> <silent> <LocalLeader><localleader>if
\ <c-\><c-n>@=lh#map#surround('if ', "!cursorhere!:\n!mark!",
\ 0, 1, '', 1, 'if ')<cr>
nmap <buffer> <LocalLeader>if V<LocalLeader>if
nmap <buffer> <LocalLeader><LocalLeader>if ^v$<LocalLeader><LocalLeader>if
"--elif insert "elif" statement {{{5
Inoreabbr <buffer> <silent> elif <C-R>=PyMapOnSingleLine('elif ',
\ "\\<c-f\\>elif !cursorhere!:\n!mark!")<cr>
"--,elif insert "elif" statement
xnoremap <buffer> <silent> <localleader>elif
\ <c-\><c-n>@=lh#map#surround('elif !cursorhere!:', '!mark!',
\ 1, 1, '', 1, 'elif ')<cr>
xnoremap <buffer> <silent> <LocalLeader><localleader>elif
\ <c-\><c-n>@=lh#map#surround('elif ', "!cursorhere!:\n!mark!",
\ 0, 1, '', 1, 'elif ')<cr>
nmap <buffer> <LocalLeader>elif V<LocalLeader>elif
nmap <buffer> <LocalLeader><LocalLeader>elif ^v$<LocalLeader><LocalLeader>elif
"--elif insert "elif" statement {{{5
Inoreabbr <buffer> <silent> else <C-R>=PyMapOnSingleLine('else ',
\ "\\<c-f\\>else:\n")<cr>
"--,elif insert "elif" statement
xnoremap <buffer> <silent> <localleader>else
\ <c-\><c-n>@=lh#map#surround('else:!cursorhere!', '\<c-d>!mark!',
\ 1, 1, '', 1, 'else ')<cr>
nmap <buffer> <LocalLeader>else V<LocalLeader>else
" --- for --------------------------------------------------------{{{4
"--for insert "for" statement {{{5
Inoreabbr <buffer> <silent> for <C-R>=PyMapOnSingleLine('for ',
\ "\\<c-f\\>for !cursorhere!:\n!mark!")<cr>
"--,for insert "for" statement
xnoremap <buffer> <silent> <localleader>for
\ <c-\><c-n>@=lh#map#surround('for !cursorhere!:', '!mark!',
\ 1, 1, '', 1, 'for ')<cr>
xnoremap <buffer> <silent> <LocalLeader><localleader>for
\ <c-\><c-n>@=lh#map#surround('for ', "!cursorhere!:\n!mark!",
\ 0, 1, '', 1, 'for ')<cr>
nmap <buffer> <LocalLeader>for V<LocalLeader>for
nmap <buffer> <LocalLeader><LocalLeader>for ^v$<LocalLeader><LocalLeader>for
" --- while ------------------------------------------------------{{{4
"--while insert "while" statement {{{5
Inoreabbr <buffer> <silent> while <C-R>=PyMapOnSingleLine('while ',
\ "\\<c-f\\>while !cursorhere!:\n!mark!")<cr>
"--,while insert "while" statement
xnoremap <buffer> <silent> <localleader>while
\ <c-\><c-n>@=lh#map#surround('while !cursorhere!:', '!mark!',
\ 1, 1, '', 1, 'while ')<cr>
xnoremap <buffer> <silent> <LocalLeader><localleader>while
\ <c-\><c-n>@=lh#map#surround('while ', "!cursorhere!:\n!mark!",
\ 0, 1, '', 1, 'while ')<cr>
nmap <buffer> <LocalLeader>while V<LocalLeader>while
nmap <buffer> <LocalLeader><LocalLeader>while ^v$<LocalLeader><LocalLeader>while
"=============================================================================
" Global Definitions {{{1
" Avoid global reinclusion {{{2
if &cp || (exists("g:loaded_ftplug_python_snippets")
\ && (g:loaded_ftplug_python_snippets >= s:k_version)
\ && !exists('g:force_reload_ftplug_python_snippets'))
let &cpo=s:cpo_save
finish
endif
let g:loaded_ftplug_python_snippets = s:k_version
" Avoid global reinclusion }}}2
"------------------------------------------------------------------------
" Functions {{{2
" Note: most filetype-global functions are best placed into
" autoload/«your-initials»/python/«python_snippets».vim
" Keep here only the functions are are required when the ftplugin is
" loaded, like functions that help building a vim-menu for this
" ftplugin.
" Function: PyMapOnSingleLine(key, what) {{{3
function! PyMapOnSingleLine(key, what) abort
let c = col('.') - 1
let l = getline('.')
let after = strpart(l, c)
let before = strpart(l, 0, c)
" let g:msg = c.' --> <' . before . '> ## <'.after . '>'
" echomsg g:msg
if before =~ '^\s*$'
if after =~ '^\s*$'
return lh#map#insert_seq(a:key, a:what)
else " => surround condition
let ll = lh#leader#get_local()
exe "normal ".ll.ll.a:key
return ""
endif
else
return a:key
endif
endfunction
" }}}1
"------------------------------------------------------------------------
let &cpo=s:cpo_save
"=============================================================================
" vim600: set fdm=marker: