1 /*
2  * Copyright 2019 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/motion-word.h"
20 
21 typedef gboolean (*CharacterPredicate)(gchar c);
22 
23 
get_current(ScintillaObject * sci,gchar * ch,gint * pos)24 static void get_current(ScintillaObject *sci, gchar *ch, gint *pos)
25 {
26 	*pos = SSM(sci, SCI_GETCURRENTPOS, 0, 0);
27 	*ch = SSM(sci, SCI_GETCHARAT, *pos, 0);
28 }
29 
30 
move_left(ScintillaObject * sci,gchar * ch,gint * pos)31 static void move_left(ScintillaObject *sci, gchar *ch, gint *pos)
32 {
33 	*pos = PREV(sci, *pos);
34 	*ch = SSM(sci, SCI_GETCHARAT, *pos, 0);
35 }
36 
37 
move_right(ScintillaObject * sci,gchar * ch,gint * pos)38 static void move_right(ScintillaObject *sci, gchar *ch, gint *pos)
39 {
40 	*pos = NEXT(sci, *pos);
41 	*ch = SSM(sci, SCI_GETCHARAT, *pos, 0);
42 }
43 
44 
is_wordchar(gchar c)45 static gboolean is_wordchar(gchar c)
46 {
47 	return g_ascii_isalnum(c) || c == '_' || (c >= 192 && c <= 255);
48 }
49 
50 
is_space(gchar c)51 static gboolean is_space(gchar c)
52 {
53 	return g_ascii_isspace(c);
54 }
55 
56 
is_nonspace(gchar c)57 static gboolean is_nonspace(gchar c)
58 {
59 	return !is_space(c);
60 }
61 
62 
is_nonwordchar(gchar c)63 static gboolean is_nonwordchar(gchar c)
64 {
65 	return !is_wordchar(c) && !is_space(c);
66 }
67 
68 
skip_to_left(CharacterPredicate is_in_group,ScintillaObject * sci,gchar * ch,gint * pos)69 static gboolean skip_to_left(CharacterPredicate is_in_group, ScintillaObject *sci, gchar *ch, gint *pos)
70 {
71 	gboolean moved = FALSE;
72 	while (is_in_group(*ch) && *pos > 0)
73 	{
74 		move_left(sci, ch, pos);
75 		moved = TRUE;
76 	}
77 	return moved;
78 }
79 
80 
skip_to_right(CharacterPredicate is_in_group,ScintillaObject * sci,gchar * ch,gint * pos,gint len)81 static gboolean skip_to_right(CharacterPredicate is_in_group, ScintillaObject *sci, gchar *ch, gint *pos, gint len)
82 {
83 	gboolean moved = FALSE;
84 	while (is_in_group(*ch) && *pos < len)
85 	{
86 		move_right(sci, ch, pos);
87 		moved = TRUE;
88 	}
89 	return moved;
90 }
91 
92 
cmd_goto_next_word(CmdContext * c,CmdParams * p)93 void cmd_goto_next_word(CmdContext *c, CmdParams *p)
94 {
95 	gint i;
96 	gint len = SSM(p->sci, SCI_GETLENGTH, 0, 0);
97 
98 	for (i = 0; i < p->num; i++)
99 	{
100 		gint pos;
101 		gchar ch;
102 
103 		get_current(p->sci, &ch, &pos);
104 
105 		if (!skip_to_right(is_wordchar, p->sci, &ch, &pos, len))
106 			skip_to_right(is_nonwordchar, p->sci, &ch, &pos, len);
107 		skip_to_right(is_space, p->sci, &ch, &pos, len);
108 
109 		if (!is_space(ch))
110 			SET_POS(p->sci, pos, TRUE);
111 	}
112 }
113 
114 
cmd_goto_previous_word(CmdContext * c,CmdParams * p)115 void cmd_goto_previous_word(CmdContext *c, CmdParams *p)
116 {
117 	gint i;
118 	for (i = 0; i < p->num; i++)
119 	{
120 		gint pos;
121 		gchar ch;
122 
123 		get_current(p->sci, &ch, &pos);
124 		move_left(p->sci, &ch, &pos);
125 
126 		skip_to_left(is_space, p->sci, &ch, &pos);
127 		if (!skip_to_left(is_wordchar, p->sci, &ch, &pos))
128 			skip_to_left(is_nonwordchar, p->sci, &ch, &pos);
129 
130 		if (pos != 0 || is_space(ch))
131 			move_right(p->sci, &ch, &pos);
132 		if (!is_space(ch))
133 			SET_POS(p->sci, pos, TRUE);
134 	}
135 }
136 
137 
cmd_goto_next_word_end(CmdContext * c,CmdParams * p)138 void cmd_goto_next_word_end(CmdContext *c, CmdParams *p)
139 {
140 	gint i;
141 	gint len = SSM(p->sci, SCI_GETLENGTH, 0, 0);
142 
143 	for (i = 0; i < p->num; i++)
144 	{
145 		gint pos;
146 		gchar ch;
147 
148 		get_current(p->sci, &ch, &pos);
149 		move_right(p->sci, &ch, &pos);
150 
151 		skip_to_right(is_space, p->sci, &ch, &pos, len);
152 		if (!skip_to_right(is_wordchar, p->sci, &ch, &pos, len))
153 			skip_to_right(is_nonwordchar, p->sci, &ch, &pos, len);
154 
155 		if (pos < len - 1 || is_space(ch))
156 			move_left(p->sci, &ch, &pos);
157 		if (!is_space(ch))
158 			SET_POS(p->sci, pos, TRUE);
159 	}
160 }
161 
162 
cmd_goto_previous_word_end(CmdContext * c,CmdParams * p)163 void cmd_goto_previous_word_end(CmdContext *c, CmdParams *p)
164 {
165 	gint i;
166 	for (i = 0; i < p->num; i++)
167 	{
168 		gint pos;
169 		gchar ch;
170 
171 		get_current(p->sci, &ch, &pos);
172 
173 		if (!skip_to_left(is_wordchar, p->sci, &ch, &pos))
174 			skip_to_left(is_nonwordchar, p->sci, &ch, &pos);
175 		skip_to_left(is_space, p->sci, &ch, &pos);
176 
177 		if (!is_space(ch))
178 			SET_POS(p->sci, pos, TRUE);
179 	}
180 }
181 
182 
cmd_goto_next_word_space(CmdContext * c,CmdParams * p)183 void cmd_goto_next_word_space(CmdContext *c, CmdParams *p)
184 {
185 	gint i;
186 	gint len = SSM(p->sci, SCI_GETLENGTH, 0, 0);
187 
188 	for (i = 0; i < p->num; i++)
189 	{
190 		gint pos;
191 		gchar ch;
192 
193 		get_current(p->sci, &ch, &pos);
194 
195 		skip_to_right(is_nonspace, p->sci, &ch, &pos, len);
196 		skip_to_right(is_space, p->sci, &ch, &pos, len);
197 
198 		if (!is_space(ch))
199 			SET_POS(p->sci, pos, TRUE);
200 	}
201 }
202 
203 
cmd_goto_previous_word_space(CmdContext * c,CmdParams * p)204 void cmd_goto_previous_word_space(CmdContext *c, CmdParams *p)
205 {
206 	gint i;
207 	for (i = 0; i < p->num; i++)
208 	{
209 		gint pos;
210 		gchar ch;
211 
212 		get_current(p->sci, &ch, &pos);
213 		move_left(p->sci, &ch, &pos);
214 
215 		skip_to_left(is_space, p->sci, &ch, &pos);
216 		skip_to_left(is_nonspace, p->sci, &ch, &pos);
217 
218 		if (pos != 0 || is_space(ch))
219 			move_right(p->sci, &ch, &pos);
220 		if (!is_space(ch))
221 			SET_POS(p->sci, pos, TRUE);
222 	}
223 }
224 
225 
cmd_goto_next_word_end_space(CmdContext * c,CmdParams * p)226 void cmd_goto_next_word_end_space(CmdContext *c, CmdParams *p)
227 {
228 	gint i;
229 	gint len = SSM(p->sci, SCI_GETLENGTH, 0, 0);
230 
231 	for (i = 0; i < p->num; i++)
232 	{
233 		gint pos;
234 		gchar ch;
235 
236 		get_current(p->sci, &ch, &pos);
237 		move_right(p->sci, &ch, &pos);
238 
239 		skip_to_right(is_space, p->sci, &ch, &pos, len);
240 		skip_to_right(is_nonspace, p->sci, &ch, &pos, len);
241 
242 		if (pos < len - 1 || is_space(ch))
243 			move_left(p->sci, &ch, &pos);
244 		if (!is_space(ch))
245 			SET_POS(p->sci, pos, TRUE);
246 	}
247 }
248 
249 
cmd_goto_previous_word_end_space(CmdContext * c,CmdParams * p)250 void cmd_goto_previous_word_end_space(CmdContext *c, CmdParams *p)
251 {
252 	gint i;
253 	for (i = 0; i < p->num; i++)
254 	{
255 		gint pos;
256 		gchar ch;
257 
258 		get_current(p->sci, &ch, &pos);
259 
260 		skip_to_left(is_nonspace, p->sci, &ch, &pos);
261 		skip_to_left(is_space, p->sci, &ch, &pos);
262 
263 		if (!is_space(ch))
264 			SET_POS(p->sci, pos, TRUE);
265 	}
266 }
267