annotate runtime/autoload/python.vim @ 31263:d8e7d725a666 v9.0.0965

patch 9.0.0965: using one window for executing autocommands is insufficient Commit: https://github.com/vim/vim/commit/e76062c078debed0df818f70e4db14ad7a7cb53a Author: Bram Moolenaar <Bram@vim.org> Date: Mon Nov 28 18:51:43 2022 +0000 patch 9.0.0965: using one window for executing autocommands is insufficient Problem: Using one window for executing autocommands is insufficient. Solution: Use up to five windows for executing autocommands.
author Bram Moolenaar <Bram@vim.org>
date Mon, 28 Nov 2022 20:00:05 +0100
parents b15334beeaa4
children b2412874362f
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('.'))
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
25 \ ->map({_, id -> id->synIDattr('name')})
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
26 \ ->match('\%(Comment\|Todo\|String\)$') >= 0},
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
27 \ [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
28 endfunction
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
29
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 " 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
31 function s:Dedented(lnum, expected)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 return indent(a:lnum) <= a:expected - shiftwidth()
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 endfunction
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 " Some other filetypes which embed Python have slightly different indent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 " 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
37 " function which is evaluated below.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 function python#GetIndent(lnum, ...)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 let ExtraFunc = a:0 > 0 ? a:1 : 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 " 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
42 " line it up with that one, otherwise add two 'shiftwidth'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 if getline(a:lnum - 1) =~ '\\$'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 return indent(a:lnum - 1)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 endif
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
47 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
48 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 " 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
51 if has('syntax_items')
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 \ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 return -1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 " Search backwards for the previous non-empty line.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 let plnum = prevnonblank(v:lnum - 1)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 if plnum == 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 " This is the first non-empty line, use zero indent.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 return 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
64 if g:python_indent.disable_parentheses_indenting == 1
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 let plindent = indent(plnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 let plnumstart = plnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67 else
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
68 " Indent inside parens.
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
69 " 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
70 " E.g.
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
71 " open_paren_not_at_EOL(100,
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
72 " (200,
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
73 " 300),
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
74 " 400)
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
75 " open_paren_at_EOL(
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
76 " 100, 200, 300, 400)
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
77 call cursor(a:lnum, 1)
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
78 let [parlnum, parcol] = s:SearchBracket(a:lnum, 'nbW')
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
79 if parlnum > 0
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
80 if parcol != col([parlnum, '$']) - 1
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
81 return parcol
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
82 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
83 return indent(parlnum)
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
84 endif
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
85 endif
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
86
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
87 call cursor(plnum, 1)
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 " 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
90 " line.
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
91 let [parlnum, _] = s:SearchBracket(plnum, 'nbW')
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 if parlnum > 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 if a:0 > 0 && ExtraFunc(parlnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 " 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
95 " If so, ignore it here - it will be handled later.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 let parlnum = 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 let plindent = indent(plnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 let plnumstart = plnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 let plindent = indent(parlnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 let plnumstart = parlnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 let plindent = indent(plnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105 let plnumstart = plnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 " When inside parenthesis: If at the first line below the parenthesis add
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 " two 'shiftwidth', otherwise same as previous line.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 " i = (a
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 " + b
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 " + c)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113 call cursor(a:lnum, 1)
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
114 let [p, _] = s:SearchBracket(a:lnum, 'bW')
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 if p > 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116 if a:0 > 0 && ExtraFunc(p)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 " Currently only used by bitbake
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 " Handle first non-empty line inside a bitbake Python task
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 if p == plnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120 return shiftwidth()
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
122
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 " Handle the user actually trying to close a bitbake Python task
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124 let line = getline(a:lnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125 if line =~ '^\s*}'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 return -2
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 " Otherwise ignore the brace
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 let p = 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132 if p == plnum
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 " When the start is inside parenthesis, only indent one 'shiftwidth'.
29659
2198955f9e27 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29450
diff changeset
134 let [pp, _] = s:SearchBracket(a:lnum, 'bW')
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135 if pp > 0
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
136 return indent(plnum)
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
137 \ + get(g:, 'pyindent_nested_paren', g:python_indent.nested_paren)->eval()
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138 endif
29840
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
139 return indent(plnum)
b15334beeaa4 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29779
diff changeset
140 \ + get(g:, 'pyindent_open_paren', g:python_indent.open_paren)->eval()
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142 if plnumstart == p
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
143 return indent(plnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
144 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
145 return plindent
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 endif
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
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 " Get the line and remove a trailing comment.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 " Use syntax highlighting attributes when possible.
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153 let pline = getline(plnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154 let pline_len = strlen(pline)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 if has('syntax_items')
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156 " 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
157 " 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
158 " too long on a long line.
29779
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
159 if synstack(plnum, pline_len)
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
160 \ ->map({_, id -> id->synIDattr('name')})
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
161 \ ->match('\%(Comment\|Todo\)$') >= 0
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 let min = 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 let max = pline_len
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 while min < max
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165 let col = (min + max) / 2
29779
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
166 if synstack(plnum, col)
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
167 \ ->map({_, id -> id->synIDattr('name')})
90a966f5c77a Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29659
diff changeset
168 \ ->match('\%(Comment\|Todo\)$') >= 0
29450
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 let max = col
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 let min = col + 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 endwhile
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 let pline = strpart(pline, 0, min - 1)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 let col = 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 while col < pline_len
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 if pline[col] == '#'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180 let pline = strpart(pline, 0, col)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181 break
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 let col = col + 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 endwhile
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 " If the previous line ended with a colon, indent this line
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188 if pline =~ ':\s*$'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 return plindent + shiftwidth()
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192 " If the previous line was a stop-execution statement...
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 " See if the user has already dedented
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195 if s:Dedented(a:lnum, indent(plnum))
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
196 " If so, trust the user
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 return -1
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 " If not, recommend one dedent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200 return indent(plnum) - shiftwidth()
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
202
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
203 " 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
204 if getline(a:lnum) =~ '^\s*\(except\|finally\)\>'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205 let lnum = a:lnum - 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
206 while lnum >= 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
207 if getline(lnum) =~ '^\s*\(try\|except\)\>'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
208 let ind = indent(lnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209 if ind >= indent(a:lnum)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
210 return -1 " indent is already less than this
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
211 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 return ind " line up with previous try or except
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
214 let lnum = lnum - 1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215 endwhile
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 return -1 " no matching "try"!
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
217 endif
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 " If the current line begins with a header keyword, dedent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
220 if getline(a:lnum) =~ '^\s*\(elif\|else\)\>'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 " Unless the previous line was a one-liner
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223 if getline(plnumstart) =~ '^\s*\(for\|if\|elif\|try\)\>'
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
224 return plindent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 " Or the user has already dedented
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
228 if s:Dedented(a:lnum, plindent)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
229 return -1
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 return plindent - shiftwidth()
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
233 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 " 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
236 " a = (b
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
237 " + c)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 " here
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
239 if parlnum > 0
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
240 " ...unless the user has already dedented
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
241 if s:Dedented(a:lnum, plindent)
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
242 return -1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
243 else
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244 return plindent
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
245 endif
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 endif
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 return -1
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249 endfunction
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
250
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
251 let &cpo = s:keepcpo
67f31c24291b Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
252 unlet s:keepcpo