annotate runtime/autoload/python.vim @ 32407:d6b579475543 v9.0.1535

patch 9.0.1535: test commented out in a wrong way Commit: https://github.com/vim/vim/commit/a4467c433a767cc2dc046ff134094c1b6305b678 Author: Bram Moolenaar <Bram@vim.org> Date: Tue May 9 22:07:11 2023 +0100 patch 9.0.1535: test commented out in a wrong way Problem: Test commented out in a wrong way. Solution: Use legacy script comment character.
author Bram Moolenaar <Bram@vim.org>
date Tue, 09 May 2023 23:15:04 +0200
parents b2412874362f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 " Support for Python indenting, see runtime/indent/python.vim
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 let s:keepcpo= &cpo
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 set cpo&vim
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
6 " need to inspect some old g:pyindent_* variables to be backward compatible
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
7 let g:python_indent = extend(get(g:, 'python_indent', {}), #{
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
8 \ closed_paren_align_last_line: v:true,
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
9 \ open_paren: get(g:, 'pyindent_open_paren', 'shiftwidth() * 2'),
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
10 \ nested_paren: get(g:, 'pyindent_nested_paren', 'shiftwidth()'),
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
11 \ continue: get(g:, 'pyindent_continue', 'shiftwidth() * 2'),
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
12 "\ searchpair() can be slow, limit the time to 150 msec or what is put in
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
13 "\ g:python_indent.searchpair_timeout
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
14 \ searchpair_timeout: get(g:, 'pyindent_searchpair_timeout', 150),
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
15 "\ Identing inside parentheses can be very slow, regardless of the searchpair()
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
16 "\ timeout, so let the user disable this feature if he doesn't need it
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
17 \ disable_parentheses_indenting: get(g:, 'pyindent_disable_parentheses_indenting', v:false),
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
18 \ }, 'keep')
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
19
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
20 let s:maxoff = 50 " maximum number of lines to look backwards for ()
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
21
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
22 function s:SearchBracket(fromlnum, flags)
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
23 return searchpairpos('[[({]', '', '[])}]', a:flags,
29779
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
24 \ {-> synstack('.', col('.'))
32061
b2412874362f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29840
diff changeset
25 \ ->indexof({_, id -> synIDattr(id, 'name') =~ '\%(Comment\|Todo\|String\)$'}) >= 0},
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
26 \ [0, a:fromlnum - s:maxoff]->max(), g:python_indent.searchpair_timeout)
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
27 endfunction
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
28
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 " See if the specified line is already user-dedented from the expected value.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 function s:Dedented(lnum, expected)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 return indent(a:lnum) <= a:expected - shiftwidth()
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 endfunction
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 " Some other filetypes which embed Python have slightly different indent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 " rules (e.g. bitbake). Those filetypes can pass an extra funcref to this
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 " function which is evaluated below.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 function python#GetIndent(lnum, ...)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 let ExtraFunc = a:0 > 0 ? a:1 : 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 " If this line is explicitly joined: If the previous line was also joined,
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 " line it up with that one, otherwise add two 'shiftwidth'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 if getline(a:lnum - 1) =~ '\\$'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 return indent(a:lnum - 1)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 endif
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
46 return indent(a:lnum - 1) + get(g:, 'pyindent_continue', g:python_indent.continue)->eval()
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49 " If the start of the line is in a string don't change the indent.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 if has('syntax_items')
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 return -1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 " Search backwards for the previous non-empty line.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 let plnum = prevnonblank(v:lnum - 1)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 if plnum == 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 " This is the first non-empty line, use zero indent.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 return 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
63 if g:python_indent.disable_parentheses_indenting == 1
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 let plindent = indent(plnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 let plnumstart = plnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 else
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
67 " Indent inside parens.
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
68 " Align with the open paren unless it is at the end of the line.
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
69 " E.g.
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
70 " open_paren_not_at_EOL(100,
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
71 " (200,
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
72 " 300),
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
73 " 400)
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
74 " open_paren_at_EOL(
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
75 " 100, 200, 300, 400)
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
76 call cursor(a:lnum, 1)
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
77 let [parlnum, parcol] = s:SearchBracket(a:lnum, 'nbW')
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
78 if parlnum > 0
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
79 if parcol != col([parlnum, '$']) - 1
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
80 return parcol
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
81 elseif getline(a:lnum) =~ '^\s*[])}]' && !g:python_indent.closed_paren_align_last_line
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
82 return indent(parlnum)
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
83 endif
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
84 endif
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
85
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
86 call cursor(plnum, 1)
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
87
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 " If the previous line is inside parenthesis, use the indent of the starting
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 " line.
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
90 let [parlnum, _] = s:SearchBracket(plnum, 'nbW')
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91 if parlnum > 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 if a:0 > 0 && ExtraFunc(parlnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 " We may have found the opening brace of a bitbake Python task, e.g. 'python do_task {'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 " If so, ignore it here - it will be handled later.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95 let parlnum = 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 let plindent = indent(plnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 let plnumstart = plnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 let plindent = indent(parlnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 let plnumstart = parlnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 let plindent = indent(plnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 let plnumstart = plnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107 " When inside parenthesis: If at the first line below the parenthesis add
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 " two 'shiftwidth', otherwise same as previous line.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 " i = (a
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 " + b
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 " + c)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 call cursor(a:lnum, 1)
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
113 let [p, _] = s:SearchBracket(a:lnum, 'bW')
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 if p > 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 if a:0 > 0 && ExtraFunc(p)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116 " Currently only used by bitbake
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 " Handle first non-empty line inside a bitbake Python task
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 if p == plnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 return shiftwidth()
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
122 " Handle the user actually trying to close a bitbake Python task
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 let line = getline(a:lnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124 if line =~ '^\s*}'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125 return -2
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128 " Otherwise ignore the brace
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 let p = 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 if p == plnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132 " When the start is inside parenthesis, only indent one 'shiftwidth'.
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
133 let [pp, _] = s:SearchBracket(a:lnum, 'bW')
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134 if pp > 0
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
135 return indent(plnum)
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
136 \ + get(g:, 'pyindent_nested_paren', g:python_indent.nested_paren)->eval()
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137 endif
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
138 return indent(plnum)
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
139 \ + get(g:, 'pyindent_open_paren', g:python_indent.open_paren)->eval()
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
140 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 if plnumstart == p
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142 return indent(plnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
143 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
144 return plindent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
145 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
147 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 " Get the line and remove a trailing comment.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 " Use syntax highlighting attributes when possible.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 let pline = getline(plnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153 let pline_len = strlen(pline)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154 if has('syntax_items')
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 " If the last character in the line is a comment, do a binary search for
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156 " the start of the comment. synID() is slow, a linear search would take
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157 " too long on a long line.
29779
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
158 if synstack(plnum, pline_len)
32061
b2412874362f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29840
diff changeset
159 \ ->indexof({_, id -> synIDattr(id, 'name') =~ '\%(Comment\|Todo\)$'}) >= 0
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 let min = 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
161 let max = pline_len
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 while min < max
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 let col = (min + max) / 2
29779
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
164 if synstack(plnum, col)
32061
b2412874362f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29840
diff changeset
165 \ ->indexof({_, id -> synIDattr(id, 'name') =~ '\%(Comment\|Todo\)$'}) >= 0
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166 let max = col
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168 let min = col + 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170 endwhile
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 let pline = strpart(pline, 0, min - 1)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 let col = 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 while col < pline_len
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 if pline[col] == '#'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 let pline = strpart(pline, 0, col)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 break
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180 let col = col + 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181 endwhile
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
183
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 " If the previous line ended with a colon, indent this line
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 if pline =~ ':\s*$'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 return plindent + shiftwidth()
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 " If the previous line was a stop-execution statement...
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 " See if the user has already dedented
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192 if s:Dedented(a:lnum, indent(plnum))
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 " If so, trust the user
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 return -1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
196 " If not, recommend one dedent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 return indent(plnum) - shiftwidth()
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
198 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
199
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200 " If the current line begins with a keyword that lines up with "try"
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
202 let lnum = a:lnum - 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
203 while lnum >= 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
204 if getline(lnum) =~ '^\s*\(try\|except\)\>'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205 let ind = indent(lnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
206 if ind >= indent(a:lnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
207 return -1 " indent is already less than this
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
208 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209 return ind " line up with previous try or except
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
210 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
211 let lnum = lnum - 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 endwhile
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 return -1 " no matching "try"!
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
214 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 " If the current line begins with a header keyword, dedent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
217 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
218
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
219 " Unless the previous line was a one-liner
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
220 if getline(plnumstart) =~ '^\s*\(for\|if\|elif\|try\)\>'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221 return plindent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
224 " Or the user has already dedented
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 if s:Dedented(a:lnum, plindent)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 return -1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
228
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
229 return plindent - shiftwidth()
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
230 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
231
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
232 " When after a () construct we probably want to go back to the start line.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
233 " a = (b
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234 " + c)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 " here
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
236 if parlnum > 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
237 " ...unless the user has already dedented
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 if s:Dedented(a:lnum, plindent)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
239 return -1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
240 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
241 return plindent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
242 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
243 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
245 return -1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 endfunction
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
247
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
248 let &cpo = s:keepcpo
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249 unlet s:keepcpo