blob: 96e0244fedc7d27a72f3e3c10554c487f6841737 [file] [log] [blame]
Bram Moolenaar09de1752016-08-08 22:26:48 +02001" Test for signs
2
3if !has('signs')
4 finish
5endif
6
7func Test_sign()
8 new
9 call setline(1, ['a', 'b', 'c', 'd'])
10
Bram Moolenaar446a9732016-08-10 21:36:23 +020011 " Define some signs.
12 " We can specify icons even if not all versions of vim support icons as
13 " icon is ignored when not supported. "(not supported)" is shown after
14 " the icon name when listing signs.
Bram Moolenaar09de1752016-08-08 22:26:48 +020015 sign define Sign1 text=x
Bram Moolenaar446a9732016-08-10 21:36:23 +020016 try
17 sign define Sign2 text=xy texthl=Title linehl=Error icon=../../pixmaps/stock_vim_find_help.png
18 catch /E255:/
19 " ignore error: E255: Couldn't read in sign data!
20 " This error can happen when running in gui.
21 " Some gui like Motif do not support the png icon format.
22 endtry
Bram Moolenaar09de1752016-08-08 22:26:48 +020023
24 " Test listing signs.
25 let a=execute('sign list')
Bram Moolenaar446a9732016-08-10 21:36:23 +020026 call assert_match("^\nsign Sign1 text=x \nsign Sign2 icon=../../pixmaps/stock_vim_find_help.png .*text=xy linehl=Error texthl=Title$", a)
Bram Moolenaar09de1752016-08-08 22:26:48 +020027
28 let a=execute('sign list Sign1')
29 call assert_equal("\nsign Sign1 text=x ", a)
30
Bram Moolenaar446a9732016-08-10 21:36:23 +020031 " Split the window to the bottom to verify sign jump will stay in the current window
32 " if the buffer is displayed there.
33 let bn = bufnr('%')
34 let wn = winnr()
35 exe 'sign place 41 line=3 name=Sign1 buffer=' . bn
Bram Moolenaar09de1752016-08-08 22:26:48 +020036 1
Bram Moolenaar446a9732016-08-10 21:36:23 +020037 bot split
38 exe 'sign jump 41 buffer=' . bufnr('%')
39 call assert_equal('c', getline('.'))
40 call assert_equal(3, winnr())
41 call assert_equal(bn, bufnr('%'))
42 call assert_notequal(wn, winnr())
43
44 " Create a new buffer and check that ":sign jump" switches to the old buffer.
45 1
46 new foo
47 call assert_notequal(bn, bufnr('%'))
48 exe 'sign jump 41 buffer=' . bn
49 call assert_equal(bn, bufnr('%'))
Bram Moolenaar09de1752016-08-08 22:26:48 +020050 call assert_equal('c', getline('.'))
51
Bram Moolenaar446a9732016-08-10 21:36:23 +020052 " Redraw to make sure that screen redraw with sign gets exercised,
53 " with and without 'rightleft'.
54 if has('rightleft')
55 set rightleft
56 redraw
57 set norightleft
58 endif
59 redraw
Bram Moolenaar09de1752016-08-08 22:26:48 +020060
Bram Moolenaar446a9732016-08-10 21:36:23 +020061 " Check that we can't change sign.
62 call assert_fails("exe 'sign place 40 name=Sign1 buffer=' . bufnr('%')", 'E885:')
63
64 " Check placed signs
Bram Moolenaar09de1752016-08-08 22:26:48 +020065 let a=execute('sign place')
Bram Moolenaar446a9732016-08-10 21:36:23 +020066 call assert_equal("\n--- Signs ---\nSigns for [NULL]:\n line=3 id=41 name=Sign1\n", a)
Bram Moolenaar09de1752016-08-08 22:26:48 +020067
Bram Moolenaar446a9732016-08-10 21:36:23 +020068 " Unplace the sign and try jumping to it again should fail.
69 sign unplace 41
Bram Moolenaar09de1752016-08-08 22:26:48 +020070 1
Bram Moolenaar446a9732016-08-10 21:36:23 +020071 call assert_fails("exe 'sign jump 41 buffer=' . bufnr('%')", 'E157:')
Bram Moolenaar09de1752016-08-08 22:26:48 +020072 call assert_equal('a', getline('.'))
73
74 " Unplace sign on current line.
Bram Moolenaar446a9732016-08-10 21:36:23 +020075 exe 'sign place 42 line=4 name=Sign2 buffer=' . bufnr('%')
Bram Moolenaar09de1752016-08-08 22:26:48 +020076 4
77 sign unplace
78 let a=execute('sign place')
79 call assert_equal("\n--- Signs ---\n", a)
80
81 " Try again to unplace sign on current line, it should fail this time.
82 call assert_fails('sign unplace', 'E159:')
83
84 " Unplace all signs.
Bram Moolenaar446a9732016-08-10 21:36:23 +020085 exe 'sign place 41 line=3 name=Sign1 buffer=' . bufnr('%')
Bram Moolenaar09de1752016-08-08 22:26:48 +020086 sign unplace *
87 let a=execute('sign place')
88 call assert_equal("\n--- Signs ---\n", a)
89
Bram Moolenaar446a9732016-08-10 21:36:23 +020090 " Check :jump with file=...
91 edit foo
92 call setline(1, ['A', 'B', 'C', 'D'])
93
94 try
95 sign define Sign3 text=y texthl=DoesNotExist linehl=DoesNotExist icon=doesnotexist.xpm
96 catch /E255:/
97 " ignore error: E255: it can happens for guis.
98 endtry
99
100 let fn = expand('%:p')
101 exe 'sign place 43 line=2 name=Sign3 file=' . fn
102 edit bar
103 call assert_notequal(fn, expand('%:p'))
104 exe 'sign jump 43 file=' . fn
105 call assert_equal('B', getline('.'))
106
Bram Moolenaar06b056e2018-12-14 19:37:08 +0100107 " can't define a sign with a non-printable character as text
108 call assert_fails("sign define Sign4 text=\e linehl=Comment", 'E239:')
109 call assert_fails("sign define Sign4 text=a\e linehl=Comment", 'E239:')
110 call assert_fails("sign define Sign4 text=\ea linehl=Comment", 'E239:')
111
112 " Only 1 or 2 character text is allowed
113 call assert_fails("sign define Sign4 text=abc linehl=Comment", 'E239:')
114 call assert_fails("sign define Sign4 text= linehl=Comment", 'E239:')
115 call assert_fails("sign define Sign4 text=\ ab linehl=Comment", 'E239:')
116
117 " define sign with whitespace
118 sign define Sign4 text=\ X linehl=Comment
119 sign undefine Sign4
120 sign define Sign4 linehl=Comment text=\ X
121 sign undefine Sign4
122
123 sign define Sign5 text=X\ linehl=Comment
124 sign undefine Sign5
125 sign define Sign5 linehl=Comment text=X\
126 sign undefine Sign5
127
128 " define sign with backslash
129 sign define Sign4 text=\\\\ linehl=Comment
130 sign undefine Sign4
131 sign define Sign4 text=\\ linehl=Comment
132 sign undefine Sign4
133
Bram Moolenaar09de1752016-08-08 22:26:48 +0200134 " After undefining the sign, we should no longer be able to place it.
135 sign undefine Sign1
136 sign undefine Sign2
Bram Moolenaar446a9732016-08-10 21:36:23 +0200137 sign undefine Sign3
138 call assert_fails("exe 'sign place 41 line=3 name=Sign1 buffer=' . bufnr('%')", 'E155:')
139endfunc
Bram Moolenaar09de1752016-08-08 22:26:48 +0200140
Bram Moolenaar446a9732016-08-10 21:36:23 +0200141" Undefining placed sign is not recommended.
142" Quoting :help sign
143"
144" :sign undefine {name}
145" Deletes a previously defined sign. If signs with this {name}
146" are still placed this will cause trouble.
147func Test_sign_undefine_still_placed()
148 new foobar
149 sign define Sign text=x
150 exe 'sign place 41 line=1 name=Sign buffer=' . bufnr('%')
151 sign undefine Sign
152
153 " Listing placed sign should show that sign is deleted.
154 let a=execute('sign place')
155 call assert_equal("\n--- Signs ---\nSigns for foobar:\n line=1 id=41 name=[Deleted]\n", a)
156
157 sign unplace 41
158 let a=execute('sign place')
159 call assert_equal("\n--- Signs ---\n", a)
Bram Moolenaar09de1752016-08-08 22:26:48 +0200160endfunc
161
162func Test_sign_completion()
163 sign define Sign1 text=x
164 sign define Sign2 text=y
165
166 call feedkeys(":sign \<C-A>\<C-B>\"\<CR>", 'tx')
167 call assert_equal('"sign define jump list place undefine unplace', @:)
168
169 call feedkeys(":sign define Sign \<C-A>\<C-B>\"\<CR>", 'tx')
170 call assert_equal('"sign define Sign icon= linehl= text= texthl=', @:)
171
172 call feedkeys(":sign define Sign linehl=Spell\<C-A>\<C-B>\"\<CR>", 'tx')
173 call assert_equal('"sign define Sign linehl=SpellBad SpellCap SpellLocal SpellRare', @:)
174
Bram Moolenaar64cefed2016-08-29 23:06:28 +0200175 call writefile(['foo'], 'XsignOne')
176 call writefile(['bar'], 'XsignTwo')
177 call feedkeys(":sign define Sign icon=Xsig\<C-A>\<C-B>\"\<CR>", 'tx')
178 call assert_equal('"sign define Sign icon=XsignOne XsignTwo', @:)
179 call delete('XsignOne')
180 call delete('XsignTwo')
Bram Moolenaar446a9732016-08-10 21:36:23 +0200181
Bram Moolenaar09de1752016-08-08 22:26:48 +0200182 call feedkeys(":sign undefine \<C-A>\<C-B>\"\<CR>", 'tx')
183 call assert_equal('"sign undefine Sign1 Sign2', @:)
184
185 call feedkeys(":sign place 1 \<C-A>\<C-B>\"\<CR>", 'tx')
186 call assert_equal('"sign place 1 buffer= file= line= name=', @:)
187
188 call feedkeys(":sign place 1 name=\<C-A>\<C-B>\"\<CR>", 'tx')
189 call assert_equal('"sign place 1 name=Sign1 Sign2', @:)
190
191 call feedkeys(":sign unplace 1 \<C-A>\<C-B>\"\<CR>", 'tx')
192 call assert_equal('"sign unplace 1 buffer= file=', @:)
193
194 call feedkeys(":sign list \<C-A>\<C-B>\"\<CR>", 'tx')
195 call assert_equal('"sign list Sign1 Sign2', @:)
196
197 call feedkeys(":sign jump 1 \<C-A>\<C-B>\"\<CR>", 'tx')
198 call assert_equal('"sign jump 1 buffer= file=', @:)
199
200 sign undefine Sign1
201 sign undefine Sign2
Bram Moolenaar09de1752016-08-08 22:26:48 +0200202endfunc
203
204func Test_sign_invalid_commands()
205 call assert_fails('sign', 'E471:')
Bram Moolenaar446a9732016-08-10 21:36:23 +0200206 call assert_fails('sign jump', 'E471:')
Bram Moolenaar09de1752016-08-08 22:26:48 +0200207 call assert_fails('sign xxx', 'E160:')
208 call assert_fails('sign define', 'E156:')
Bram Moolenaar446a9732016-08-10 21:36:23 +0200209 call assert_fails('sign define Sign1 xxx', 'E475:')
Bram Moolenaar09de1752016-08-08 22:26:48 +0200210 call assert_fails('sign undefine', 'E156:')
211 call assert_fails('sign list xxx', 'E155:')
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200212 call assert_fails('sign place 1 buffer=999', 'E158:')
Bram Moolenaar09de1752016-08-08 22:26:48 +0200213 call assert_fails('sign define Sign2 text=', 'E239:')
214endfunc
Bram Moolenaarbfd096d2016-08-17 22:29:09 +0200215
216func Test_sign_delete_buffer()
217 new
218 sign define Sign text=x
219 let bufnr = bufnr('%')
220 new
221 exe 'bd ' . bufnr
222 exe 'sign place 61 line=3 name=Sign buffer=' . bufnr
223 call assert_fails('sign jump 61 buffer=' . bufnr, 'E934:')
224 sign unplace 61
225 sign undefine Sign
226endfunc