1 /*
2  * Copyright 2018 Jiri Techet <techet@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "cmds/changemode.h"
20 #include "utils.h"
21 
22 
cmd_enter_ex(CmdContext * c,CmdParams * p)23 void cmd_enter_ex(CmdContext *c, CmdParams *p)
24 {
25 	c->num = p->num;
26 	vi_enter_ex_mode();
27 }
28 
29 
cmd_enter_command(CmdContext * c,CmdParams * p)30 void cmd_enter_command(CmdContext *c, CmdParams *p)
31 {
32 	if (SSM(p->sci, SCI_AUTOCACTIVE, 0, 0) || SSM(p->sci, SCI_CALLTIPACTIVE, 0, 0))
33 		SSM(p->sci, SCI_CANCEL, 0, 0);
34 	else
35 		vi_set_mode(VI_MODE_COMMAND);
36 }
37 
38 
cmd_enter_command_single(CmdContext * c,CmdParams * p)39 void cmd_enter_command_single(CmdContext *c, CmdParams *p)
40 {
41 	vi_set_mode(VI_MODE_COMMAND_SINGLE);
42 }
43 
44 
cmd_enter_visual(CmdContext * c,CmdParams * p)45 void cmd_enter_visual(CmdContext *c, CmdParams *p)
46 {
47 	if (vi_get_mode() == VI_MODE_VISUAL)
48 	{
49 		SSM(p->sci, SCI_SETEMPTYSELECTION, p->pos, 0);
50 		vi_set_mode(VI_MODE_COMMAND);
51 	}
52 	else
53 		vi_set_mode(VI_MODE_VISUAL);
54 }
55 
56 
cmd_enter_visual_line(CmdContext * c,CmdParams * p)57 void cmd_enter_visual_line(CmdContext *c, CmdParams *p)
58 {
59 	if (vi_get_mode() == VI_MODE_VISUAL_LINE)
60 	{
61 		SSM(p->sci, SCI_SETEMPTYSELECTION, p->pos, 0);
62 		vi_set_mode(VI_MODE_COMMAND);
63 	}
64 	else
65 	{
66 		vi_set_mode(VI_MODE_VISUAL_LINE);
67 		/* just to force the scintilla notification callback to be called so we can
68 		 * select the current line */
69 		SSM(p->sci, SCI_LINEEND, 0, 0);
70 	}
71 }
72 
73 
cmd_enter_insert(CmdContext * c,CmdParams * p)74 void cmd_enter_insert(CmdContext *c, CmdParams *p)
75 {
76 	c->num = p->num;
77 	c->newline_insert = FALSE;
78 	vi_set_mode(VI_MODE_INSERT);
79 }
80 
81 
cmd_enter_replace(CmdContext * c,CmdParams * p)82 void cmd_enter_replace(CmdContext *c, CmdParams *p)
83 {
84 	c->num = p->num;
85 	c->newline_insert = FALSE;
86 	vi_set_mode(VI_MODE_REPLACE);
87 }
88 
89 
cmd_enter_insert_after(CmdContext * c,CmdParams * p)90 void cmd_enter_insert_after(CmdContext *c, CmdParams *p)
91 {
92 	cmd_enter_insert(c, p);
93 	if (p->pos < p->line_end_pos)
94 		SSM(p->sci, SCI_CHARRIGHT, 0, 0);
95 }
96 
97 
cmd_enter_insert_line_start_nonempty(CmdContext * c,CmdParams * p)98 void cmd_enter_insert_line_start_nonempty(CmdContext *c, CmdParams *p)
99 {
100 	goto_nonempty(p->sci, p->line, TRUE);
101 	cmd_enter_insert(c, p);
102 }
103 
104 
cmd_enter_insert_line_start(CmdContext * c,CmdParams * p)105 void cmd_enter_insert_line_start(CmdContext *c, CmdParams *p)
106 {
107 	SET_POS(p->sci, p->line_start_pos, TRUE);
108 	cmd_enter_insert(c, p);
109 }
110 
111 
cmd_enter_insert_line_end(CmdContext * c,CmdParams * p)112 void cmd_enter_insert_line_end(CmdContext *c, CmdParams *p)
113 {
114 	SSM(p->sci, SCI_LINEEND, 0, 0);
115 	cmd_enter_insert(c, p);
116 }
117 
118 
cmd_enter_insert_next_line(CmdContext * c,CmdParams * p)119 void cmd_enter_insert_next_line(CmdContext *c, CmdParams *p)
120 {
121 	SSM(p->sci, SCI_LINEEND, 0, 0);
122 	SSM(p->sci, SCI_NEWLINE, 0, 0);
123 	c->num = p->num;
124 	c->newline_insert = TRUE;
125 	vi_set_mode(VI_MODE_INSERT);
126 }
127 
128 
cmd_enter_insert_prev_line(CmdContext * c,CmdParams * p)129 void cmd_enter_insert_prev_line(CmdContext *c, CmdParams *p)
130 {
131 	if (p->line == 0)
132 	{
133 		SSM(p->sci, SCI_HOME, 0, 0);
134 		SSM(p->sci, SCI_NEWLINE, 0, 0);
135 		SSM(p->sci, SCI_LINEUP, 0, 0);
136 	}
137 	else
138 	{
139 		SSM(p->sci, SCI_LINEUP, 0, 0);
140 		SSM(p->sci, SCI_LINEEND, 0, 0);
141 		SSM(p->sci, SCI_NEWLINE, 0, 0);
142 	}
143 	c->num = p->num;
144 	c->newline_insert = TRUE;
145 	vi_set_mode(VI_MODE_INSERT);
146 }
147 
148 
cut_range_change_mode(CmdContext * c,CmdParams * p,gint start,gint end,gboolean line_copy,ViMode mode)149 static void cut_range_change_mode(CmdContext *c, CmdParams *p,
150 	gint start, gint end, gboolean line_copy, ViMode mode)
151 {
152 	c->line_copy = line_copy;
153 	SSM(p->sci, SCI_COPYRANGE, start, end);
154 	SSM(p->sci, SCI_DELETERANGE, start, end - start);
155 	if (mode == VI_MODE_INSERT)
156 		cmd_enter_insert(c, p);
157 	else
158 		vi_set_mode(mode);
159 }
160 
161 
cmd_enter_insert_clear_right(CmdContext * c,CmdParams * p)162 void cmd_enter_insert_clear_right(CmdContext *c, CmdParams *p)
163 {
164 	gint new_line = get_line_number_rel(p->sci, p->num - 1);
165 	gint pos = SSM(p->sci, SCI_GETLINEENDPOSITION, new_line, 0);
166 
167 	cut_range_change_mode(c, p, p->pos, pos, FALSE, VI_MODE_INSERT);
168 }
169 
170 
cmd_enter_insert_delete_char(CmdContext * c,CmdParams * p)171 void cmd_enter_insert_delete_char(CmdContext *c, CmdParams *p)
172 {
173 	gint end = NTH(p->sci, p->pos, p->num);
174 	end = end > p->line_end_pos ? p->line_end_pos : end;
175 
176 	cut_range_change_mode(c, p, p->pos, end, FALSE, VI_MODE_INSERT);
177 }
178 
179 
cmd_enter_insert_cut_line(CmdContext * c,CmdParams * p)180 void cmd_enter_insert_cut_line(CmdContext *c, CmdParams *p)
181 {
182 	gint new_line = get_line_number_rel(p->sci, p->num - 1);
183 	gint pos_end = SSM(p->sci, SCI_GETLINEENDPOSITION, new_line, 0);
184 
185 	cut_range_change_mode(c, p, p->line_start_pos, pos_end, TRUE, VI_MODE_INSERT);
186 }
187 
188 
cmd_enter_insert_cut_sel(CmdContext * c,CmdParams * p)189 void cmd_enter_insert_cut_sel(CmdContext *c, CmdParams *p)
190 {
191 	gint sel_end_pos = p->sel_start + p->sel_len;
192 	if (p->is_operator_cmd && p->line_end_pos < sel_end_pos)
193 		sel_end_pos = p->line_end_pos;
194 
195 	cut_range_change_mode(c, p, p->sel_start, sel_end_pos, FALSE, VI_MODE_INSERT);
196 }
197 
198 
cmd_enter_insert_cut_line_sel(CmdContext * c,CmdParams * p)199 void cmd_enter_insert_cut_line_sel(CmdContext *c, CmdParams *p)
200 {
201 	gint begin = p->sel_first_line_begin_pos;
202 	gint end = p->sel_last_line_end_pos;
203 
204 	cut_range_change_mode(c, p, begin, end, TRUE, VI_MODE_INSERT);
205 }
206 
207 
cmd_enter_command_cut_sel(CmdContext * c,CmdParams * p)208 void cmd_enter_command_cut_sel(CmdContext *c, CmdParams *p)
209 {
210 	gint sel_end_pos = p->sel_start + p->sel_len;
211 	if (p->is_operator_cmd && p->line_end_pos < sel_end_pos)
212 		sel_end_pos = p->line_end_pos;
213 
214 	cut_range_change_mode(c, p, p->sel_start, sel_end_pos, FALSE, VI_MODE_COMMAND);
215 }
216 
217 
cmd_enter_command_cut_line_sel(CmdContext * c,CmdParams * p)218 void cmd_enter_command_cut_line_sel(CmdContext *c, CmdParams *p)
219 {
220 	gint begin = p->sel_first_line_begin_pos;
221 	gint end = p->sel_last_line_end_pos;
222 
223 	cut_range_change_mode(c, p, begin, end, TRUE, VI_MODE_COMMAND);
224 	SET_POS(p->sci, begin, TRUE);
225 }
226 
227 
cmd_enter_command_copy_sel(CmdContext * c,CmdParams * p)228 void cmd_enter_command_copy_sel(CmdContext *c, CmdParams *p)
229 {
230 	gint sel_end_pos = p->sel_start + p->sel_len;
231 	if (p->is_operator_cmd && p->line_end_pos < sel_end_pos)
232 		sel_end_pos = p->line_end_pos;
233 
234 	c->line_copy = FALSE;
235 	SSM(p->sci, SCI_COPYRANGE, p->sel_start, sel_end_pos);
236 	vi_set_mode(VI_MODE_COMMAND);
237 	SET_POS(p->sci, p->sel_start, TRUE);
238 }
239 
240 
cmd_enter_command_copy_line_sel(CmdContext * c,CmdParams * p)241 void cmd_enter_command_copy_line_sel(CmdContext *c, CmdParams *p)
242 {
243 	gint begin = p->sel_first_line_begin_pos;
244 	gint end = p->sel_last_line_end_pos;
245 
246 	c->line_copy = TRUE;
247 	SSM(p->sci, SCI_COPYRANGE, begin, end);
248 	vi_set_mode(VI_MODE_COMMAND);
249 	SET_POS(p->sci, begin, TRUE);
250 }
251