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/excmds.h"
20 #include "cmds/edit.h"
21 #include "utils.h"
22
excmd_save(CmdContext * c,ExCmdParams * p)23 void excmd_save(CmdContext *c, ExCmdParams *p)
24 {
25 c->cb->on_save(p->force);
26 }
27
28
excmd_save_all(CmdContext * c,ExCmdParams * p)29 void excmd_save_all(CmdContext *c, ExCmdParams *p)
30 {
31 c->cb->on_save_all(p->force);
32 }
33
34
excmd_quit(CmdContext * c,ExCmdParams * p)35 void excmd_quit(CmdContext *c, ExCmdParams *p)
36 {
37 c->cb->on_quit(p->force);
38 }
39
40
excmd_save_quit(CmdContext * c,ExCmdParams * p)41 void excmd_save_quit(CmdContext *c, ExCmdParams *p)
42 {
43 if (c->cb->on_save(p->force))
44 c->cb->on_quit(p->force);
45 }
46
47
excmd_save_all_quit(CmdContext * c,ExCmdParams * p)48 void excmd_save_all_quit(CmdContext *c, ExCmdParams *p)
49 {
50 if (c->cb->on_save_all(p->force))
51 c->cb->on_quit(p->force);
52 }
53
54
excmd_repeat_subst(CmdContext * c,ExCmdParams * p)55 void excmd_repeat_subst(CmdContext *c, ExCmdParams *p)
56 {
57 const gchar *flags = p->param1;
58 if (!flags)
59 flags = "g";
60 perform_substitute(c->sci, c->substitute_text, p->range_from, p->range_to, flags);
61 }
62
63
excmd_repeat_subst_orig_flags(CmdContext * c,ExCmdParams * p)64 void excmd_repeat_subst_orig_flags(CmdContext *c, ExCmdParams *p)
65 {
66 perform_substitute(c->sci, c->substitute_text, p->range_from, p->range_to, NULL);
67 }
68
69
prepare_cmd_params(CmdParams * params,CmdContext * c,ExCmdParams * p)70 static void prepare_cmd_params(CmdParams *params, CmdContext *c, ExCmdParams *p)
71 {
72 gint start = SSM(c->sci, SCI_POSITIONFROMLINE, p->range_from, 0);
73 SET_POS(c->sci, start, TRUE);
74 cmd_params_init(params, c->sci, p->range_to - p->range_from + 1, FALSE, NULL, FALSE, 0, 0);
75 }
76
77
excmd_yank(CmdContext * c,ExCmdParams * p)78 void excmd_yank(CmdContext *c, ExCmdParams *p)
79 {
80 CmdParams params;
81 prepare_cmd_params(¶ms, c, p);
82 cmd_copy_line(c, ¶ms);
83 }
84
85
excmd_put(CmdContext * c,ExCmdParams * p)86 void excmd_put(CmdContext *c, ExCmdParams *p)
87 {
88 CmdParams params;
89 prepare_cmd_params(¶ms, c, p);
90 cmd_paste_after(c, ¶ms);
91 }
92
93
excmd_undo(CmdContext * c,ExCmdParams * p)94 void excmd_undo(CmdContext *c, ExCmdParams *p)
95 {
96 SSM(c->sci, SCI_UNDO, 0, 0);
97 }
98
99
excmd_redo(CmdContext * c,ExCmdParams * p)100 void excmd_redo(CmdContext *c, ExCmdParams *p)
101 {
102 SSM(c->sci, SCI_REDO, 0, 0);
103 }
104
105
excmd_shift_left(CmdContext * c,ExCmdParams * p)106 void excmd_shift_left(CmdContext *c, ExCmdParams *p)
107 {
108 CmdParams params;
109 prepare_cmd_params(¶ms, c, p);
110 cmd_unindent(c, ¶ms);
111 }
112
113
excmd_shift_right(CmdContext * c,ExCmdParams * p)114 void excmd_shift_right(CmdContext *c, ExCmdParams *p)
115 {
116 CmdParams params;
117 prepare_cmd_params(¶ms, c, p);
118 cmd_indent(c, ¶ms);
119 }
120
121
excmd_delete(CmdContext * c,ExCmdParams * p)122 void excmd_delete(CmdContext *c, ExCmdParams *p)
123 {
124 CmdParams params;
125 prepare_cmd_params(¶ms, c, p);
126 cmd_delete_line(c, ¶ms);
127 }
128
excmd_join(CmdContext * c,ExCmdParams * p)129 void excmd_join(CmdContext *c, ExCmdParams *p)
130 {
131 CmdParams params;
132 prepare_cmd_params(¶ms, c, p);
133 cmd_join_lines(c, ¶ms);
134 }
135
136
excmd_copy(CmdContext * c,ExCmdParams * p)137 void excmd_copy(CmdContext *c, ExCmdParams *p)
138 {
139 CmdParams params;
140 gint dest = SSM(c->sci, SCI_POSITIONFROMLINE, p->dest, 0);
141 excmd_yank(c, p);
142 SET_POS(c->sci, dest, TRUE);
143 cmd_params_init(¶ms, c->sci, 1, FALSE, NULL, FALSE, 0, 0);
144 cmd_paste_after(c, ¶ms);
145 }
146
147
excmd_move(CmdContext * c,ExCmdParams * p)148 void excmd_move(CmdContext *c, ExCmdParams *p)
149 {
150 CmdParams params;
151 gint dest;
152
153 if (p->dest >= p->range_from && p->dest <= p->range_to)
154 return;
155
156 excmd_delete(c, p);
157 if (p->dest > p->range_to)
158 p->dest -= p->range_to - p->range_from + 1;
159 dest = SSM(c->sci, SCI_POSITIONFROMLINE, p->dest, 0);
160 SET_POS(c->sci, dest, TRUE);
161 cmd_params_init(¶ms, c->sci, 1, FALSE, NULL, FALSE, 0, 0);
162 cmd_paste_after(c, ¶ms);
163 }
164