1- " Python-mode base functions
1+ " Pymode core functions
22
3-
4- fun ! pymode#Default (name, default) " {{{
5- " DESC: Set default value if it not exists
6- "
3+ " DESC: Check variable and set default value if it not exists
4+ fun ! pymode#default (name, default) " {{{
75 if ! exists (a: name )
86 let {a: name } = a: default
97 return 0
108 endif
119 return 1
1210endfunction " }}}
1311
12+ " DESC: Import python libs
13+ fun ! pymode#init (plugin_root, paths) " {{{
1414
15- fun ! pymode#Option (name) " {{{
16-
17- let name = ' b:pymode_ ' . a: name
18- if exists (name)
19- return eval (name)
15+ if g: pymode_python == ' disable '
16+ if g: pymode_warning
17+ call pymode#error ( " Pymode requires vim compiled with +python. Most of features will be disabled. " )
18+ endif
19+ return
2020 endif
2121
22- let name = ' g:pymode_' . a: name
23- return eval (name)
22+ PymodePython import sys, vim
23+ PymodePython sys.path .insert (0 , vim .eval (' a:plugin_root' ))
24+ PymodePython sys.path = vim .eval (' a:paths' ) + sys.path
25+
26+ endfunction " }}}
2427
28+ " DESC: Show wide message
29+ fun ! pymode#wide_message (msg) " {{{
30+ let x = &ruler | let y = &showcmd
31+ set noruler noshowcmd
32+ redraw
33+ echohl Debug | echo strpart (" [Pymode] " . a: msg , 0 , &columns - 1 ) | echohl none
34+ let &ruler = x | let &showcmd = y
2535endfunction " }}}
2636
37+ " DESC: Show error
38+ fun ! pymode#error (msg) " {{{
39+ execute " normal \<Esc> "
40+ echohl ErrorMsg
41+ echomsg " [Pymode]: error: " . a: msg
42+ echohl None
43+ endfunction " }}}
2744
28- fun ! pymode#QuickfixOpen (onlyRecognized, holdCursor, maxHeight, minHeight, jumpError) " {{{
29- " DESC: Open quickfix window
30- "
45+ " DESC: Open quickfix window
46+ fun ! pymode#quickfix_open (onlyRecognized, holdCursor, maxHeight, minHeight, jumpError) " {{{
3147 let numErrors = len (filter (getqflist (), ' v:val.valid' ))
3248 let numOthers = len (getqflist ()) - numErrors
3349 if numErrors > 0 || (! a: onlyRecognized && numOthers > 0 )
@@ -43,153 +59,47 @@ fun! pymode#QuickfixOpen(onlyRecognized, holdCursor, maxHeight, minHeight, jumpE
4359 endif
4460 redraw
4561 if numOthers > 0
46- echo printf (' Quickfix: %d(+%d)' , numErrors, numOthers)
62+ call pymode#wide_message ( printf (' Quickfix: %d(+%d)' , numErrors, numOthers) )
4763 else
48- echo printf (' Quickfix: %d' , numErrors)
49- endif
50- endfunction " }}}
51-
52-
53- fun ! pymode#PlaceSigns (bnum) " {{{
54- " DESC: Place error signs
55- "
56- if has (' signs' )
57- call pymode#Default (' b:pymode_signs' , [])
58-
59- for item in b: pymode_signs
60- execute printf (' sign unplace %d buffer=%d' , item.lnum, item.bufnr )
61- endfor
62- let b: pymode_signs = []
63-
64- if ! pymode#Default (" g:pymode_lint_signs_always_visible" , 0 ) || g: pymode_lint_signs_always_visible
65- call RopeShowSignsRulerIfNeeded ()
66- endif
67-
68- for item in filter (getqflist (), ' v:val.bufnr != ""' )
69- call add (b: pymode_signs , item)
70- execute printf (' sign place %d line=%d name=%s buffer=%d' , item.lnum, item.lnum, " Pymode" .item.type , item.bufnr )
71- endfor
72-
73- endif
74- endfunction " }}}
75-
76-
77- fun ! pymode#CheckProgram (name, append ) " {{{
78- " DESC: Check program is executable or redifined by user.
79- "
80- let name = ' g:' . a: name
81- if pymode#Default (name, a: name )
82- return 1
83- endif
84- if ! executable (eval (l: name ))
85- echoerr " Can't find '" .eval (name)." '. Please set " .name ." , or extend $PATH, " .a: append
86- return 0
64+ call pymode#wide_message (printf (' Quickfix: %d' , numErrors))
8765 endif
88- return 1
8966endfunction " }}}
9067
91-
92- fun ! pymode#TempBuffer () " {{{
93- " DESC: Open temp buffer.
94- "
95- pclose | botright 8 new
68+ " DESC: Open temp buffer.
69+ fun ! pymode#tempbuffer_open (name) " {{{
70+ pclose
71+ exe " botright 8new " . a: name
9672 setlocal buftype = nofile bufhidden = delete noswapfile nowrap previewwindow
9773 redraw
9874endfunction " }}}
9975
100-
101- fun ! pymode#ShowStr (str) " {{{
102- " DESC: Open temp buffer with `str`.
103- "
104- let g: pymode_curbuf = bufnr (" %" )
105- call pymode#TempBuffer ()
106- put ! = a: str
107- wincmd p
108- redraw
109- endfunction " }}}
110-
111-
112- fun ! pymode#ShowCommand (cmd) " {{{
113- " DESC: Run command and open temp buffer with result
114- "
115- call pymode#TempBuffer ()
116- try
117- silent exec ' r!' . a: cmd
118- catch /.*/
119- close
120- echoerr ' Command fail: ' .a: cmd
121- endtry
122- redraw
123- normal gg
124- wincmd p
125- endfunction " }}}
126-
127-
128- fun ! pymode#WideMessage (msg) " {{{
129- " DESC: Show wide message
130-
131- let x = &ruler | let y = &showcmd
132- set noruler noshowcmd
133- redraw
134- echohl Debug | echo strpart (a: msg , 0 , &columns - 1 ) | echohl none
135- let &ruler = x | let &showcmd = y
136- endfunction " }}}
137-
138-
139- fun ! pymode#BlockStart (lnum, ... ) " {{{
140- let pattern = a: 0 ? a: 1 : ' ^\s*\(@\|class\s.*:\|def\s\)'
141- let lnum = a: lnum + 1
142- let indent = 100
143- while lnum
144- let lnum = prevnonblank (lnum - 1 )
145- let test = indent (lnum)
146- let line = getline (lnum)
147- if line = ~ ' ^\s*#' " Skip comments
148- continue
149- elseif ! test " Zero-level regular line
150- return lnum
151- elseif test >= indent " Skip deeper or equal lines
152- continue
153- " Indent is strictly less at this point: check for def/class
154- elseif line = ~ pattern && line !~ ' ^\s*@'
155- return lnum
156- endif
157- let indent = indent (lnum)
158- endwhile
159- return 0
160- endfunction " }}}
161-
162-
163- fun ! pymode#BlockEnd (lnum, ... ) " {{{
164- let indent = a: 0 ? a: 1 : indent (a: lnum )
165- let lnum = a: lnum
166- while lnum
167- let lnum = nextnonblank (lnum + 1 )
168- if getline (lnum) = ~ ' ^\s*#' | continue
169- elseif lnum && indent (lnum) <= indent
170- return lnum - 1
171- endif
172- endwhile
173- return line (' $' )
76+ " DESC: Remove unused whitespaces
77+ fun ! pymode#trim_whitespaces () " {{{
78+ let cursor_pos = getpos (' .' )
79+ silent ! % s /\s\+$/ /
80+ call setpos (' .' , cursor_pos)
17481endfunction " }}}
17582
17683
177- fun ! pymode#Modeline () " {{{
178- let modeline = getline (prevnonblank (' $' ))
179- if modeline = ~ ' ^#\s\+pymode:'
180- for ex in split (modeline , ' :' )[1 :]
181- let [name, value] = split (ex , ' =' )
182- let {' b:pymode_' .name} = value
183- endfor
84+ fun ! pymode#save () " {{{
85+ if &modifiable && &modified
86+ try
87+ noautocmd write
88+ catch /E212/
89+ call pymode#error (" File modified and I can't save it. Cancel code checking." )
90+ return 0
91+ endtry
18492 endif
93+ return 1
18594endfunction " }}}
18695
187-
188- fun ! pymode#TrimWhiteSpace () " {{{
189- let cursor_pos = getpos (' .' )
190- silent ! % s /\s\+$/ /
191- call setpos (' .' , cursor_pos)
96+ fun ! pymode#reload_buf_by_nr (nr) " {{{
97+ let cur = bufnr (" " )
98+ try
99+ exe " buffer " . a: nr
100+ catch /E86/
101+ return
102+ endtry
103+ exe " e!"
104+ exe " buffer " . cur
192105endfunction " }}}
193-
194-
195- " vim: fdm = marker:fdl = 0
0 commit comments