blob: a9674353462e203b100334ca37c69ab73d090714 [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 Moolenaar09de1752016-08-08 22:26:48 +0200107 " After undefining the sign, we should no longer be able to place it.
108 sign undefine Sign1
109 sign undefine Sign2
Bram Moolenaar446a9732016-08-10 21:36:23 +0200110 sign undefine Sign3
111 call assert_fails("exe 'sign place 41 line=3 name=Sign1 buffer=' . bufnr('%')", 'E155:')
112endfunc
Bram Moolenaar09de1752016-08-08 22:26:48 +0200113
Bram Moolenaar446a9732016-08-10 21:36:23 +0200114" Undefining placed sign is not recommended.
115" Quoting :help sign
116"
117" :sign undefine {name}
118" Deletes a previously defined sign. If signs with this {name}
119" are still placed this will cause trouble.
120func Test_sign_undefine_still_placed()
121 new foobar
122 sign define Sign text=x
123 exe 'sign place 41 line=1 name=Sign buffer=' . bufnr('%')
124 sign undefine Sign
125
126 " Listing placed sign should show that sign is deleted.
127 let a=execute('sign place')
128 call assert_equal("\n--- Signs ---\nSigns for foobar:\n line=1 id=41 name=[Deleted]\n", a)
129
130 sign unplace 41
131 let a=execute('sign place')
132 call assert_equal("\n--- Signs ---\n", a)
Bram Moolenaar09de1752016-08-08 22:26:48 +0200133endfunc
134
135func Test_sign_completion()
136 sign define Sign1 text=x
137 sign define Sign2 text=y
138
139 call feedkeys(":sign \<C-A>\<C-B>\"\<CR>", 'tx')
140 call assert_equal('"sign define jump list place undefine unplace', @:)
141
142 call feedkeys(":sign define Sign \<C-A>\<C-B>\"\<CR>", 'tx')
143 call assert_equal('"sign define Sign icon= linehl= text= texthl=', @:)
144
145 call feedkeys(":sign define Sign linehl=Spell\<C-A>\<C-B>\"\<CR>", 'tx')
146 call assert_equal('"sign define Sign linehl=SpellBad SpellCap SpellLocal SpellRare', @:)
147
Bram Moolenaar64cefed2016-08-29 23:06:28 +0200148 call writefile(['foo'], 'XsignOne')
149 call writefile(['bar'], 'XsignTwo')
150 call feedkeys(":sign define Sign icon=Xsig\<C-A>\<C-B>\"\<CR>", 'tx')
151 call assert_equal('"sign define Sign icon=XsignOne XsignTwo', @:)
152 call delete('XsignOne')
153 call delete('XsignTwo')
Bram Moolenaar446a9732016-08-10 21:36:23 +0200154
Bram Moolenaar09de1752016-08-08 22:26:48 +0200155 call feedkeys(":sign undefine \<C-A>\<C-B>\"\<CR>", 'tx')
156 call assert_equal('"sign undefine Sign1 Sign2', @:)
157
158 call feedkeys(":sign place 1 \<C-A>\<C-B>\"\<CR>", 'tx')
159 call assert_equal('"sign place 1 buffer= file= line= name=', @:)
160
161 call feedkeys(":sign place 1 name=\<C-A>\<C-B>\"\<CR>", 'tx')
162 call assert_equal('"sign place 1 name=Sign1 Sign2', @:)
163
164 call feedkeys(":sign unplace 1 \<C-A>\<C-B>\"\<CR>", 'tx')
165 call assert_equal('"sign unplace 1 buffer= file=', @:)
166
167 call feedkeys(":sign list \<C-A>\<C-B>\"\<CR>", 'tx')
168 call assert_equal('"sign list Sign1 Sign2', @:)
169
170 call feedkeys(":sign jump 1 \<C-A>\<C-B>\"\<CR>", 'tx')
171 call assert_equal('"sign jump 1 buffer= file=', @:)
172
173 sign undefine Sign1
174 sign undefine Sign2
Bram Moolenaar09de1752016-08-08 22:26:48 +0200175endfunc
176
177func Test_sign_invalid_commands()
178 call assert_fails('sign', 'E471:')
Bram Moolenaar446a9732016-08-10 21:36:23 +0200179 call assert_fails('sign jump', 'E471:')
Bram Moolenaar09de1752016-08-08 22:26:48 +0200180 call assert_fails('sign xxx', 'E160:')
181 call assert_fails('sign define', 'E156:')
Bram Moolenaar446a9732016-08-10 21:36:23 +0200182 call assert_fails('sign define Sign1 xxx', 'E475:')
Bram Moolenaar09de1752016-08-08 22:26:48 +0200183 call assert_fails('sign undefine', 'E156:')
184 call assert_fails('sign list xxx', 'E155:')
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200185 call assert_fails('sign place 1 buffer=999', 'E158:')
Bram Moolenaar09de1752016-08-08 22:26:48 +0200186 call assert_fails('sign define Sign2 text=', 'E239:')
187endfunc
Bram Moolenaarbfd096d2016-08-17 22:29:09 +0200188
189func Test_sign_delete_buffer()
190 new
191 sign define Sign text=x
192 let bufnr = bufnr('%')
193 new
194 exe 'bd ' . bufnr
195 exe 'sign place 61 line=3 name=Sign buffer=' . bufnr
196 call assert_fails('sign jump 61 buffer=' . bufnr, 'E934:')
197 sign unplace 61
198 sign undefine Sign
199endfunc