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/special.h"
20 #include "utils.h"
21 
22 #include <gdk/gdkkeysyms.h>
23 
24 
cmd_swap_anchor(CmdContext * c,CmdParams * p)25 void cmd_swap_anchor(CmdContext *c, CmdParams *p)
26 {
27 	gint anchor = c->sel_anchor;
28 	c->sel_anchor = p->pos;
29 	SET_POS(p->sci, anchor, TRUE);
30 }
31 
32 
cmd_nop(CmdContext * c,CmdParams * p)33 void cmd_nop(CmdContext *c, CmdParams *p)
34 {
35 	// do nothing
36 }
37 
38 
cmd_repeat_last_command(CmdContext * c,CmdParams * p)39 void cmd_repeat_last_command(CmdContext *c, CmdParams *p)
40 {
41 	// handled in a special way - has to be separate from cmd_nop()
42 }
43 
44 
cmd_paste_inserted_text(CmdContext * c,CmdParams * p)45 void cmd_paste_inserted_text(CmdContext *c, CmdParams *p)
46 {
47 	SSM(p->sci, SCI_ADDTEXT, c->insert_buf_len, (sptr_t) c->insert_buf);
48 }
49 
50 
cmd_paste_inserted_text_leave_ins(CmdContext * c,CmdParams * p)51 void cmd_paste_inserted_text_leave_ins(CmdContext *c, CmdParams *p)
52 {
53 	cmd_paste_inserted_text(c, p);
54 	vi_set_mode(VI_MODE_COMMAND);
55 }
56 
57 
cmd_write_exit(CmdContext * c,CmdParams * p)58 void cmd_write_exit(CmdContext *c, CmdParams *p)
59 {
60 	if (c->cb->on_save(FALSE))
61 		c->cb->on_quit(FALSE);
62 }
63 
64 
cmd_force_exit(CmdContext * c,CmdParams * p)65 void cmd_force_exit(CmdContext *c, CmdParams *p)
66 {
67 	c->cb->on_quit(TRUE);
68 }
69 
70 
cmd_search_next(CmdContext * c,CmdParams * p)71 void cmd_search_next(CmdContext *c, CmdParams *p)
72 {
73 	gboolean invert = FALSE;
74 	gint pos;
75 
76 	if (p->last_kp->key == GDK_KEY_N)
77 		invert = TRUE;
78 
79 	pos = perform_search(p->sci, c->search_text, p->num, invert);
80 	if (pos >= 0)
81 		SET_POS(c->sci, pos, TRUE);
82 
83 }
84 
85 
search_current(CmdContext * c,CmdParams * p,gboolean next)86 static void search_current(CmdContext *c, CmdParams *p, gboolean next)
87 {
88 	gchar *word = get_current_word(p->sci);
89 	gint pos;
90 
91 	g_free(c->search_text);
92 	if (!word)
93 		c->search_text = NULL;
94 	else
95 	{
96 		const gchar *prefix = next ? "/" : "?";
97 		c->search_text = g_strconcat(prefix, word, NULL);
98 	}
99 	g_free(word);
100 
101 	pos = perform_search(p->sci, c->search_text, p->num, FALSE);
102 	if (pos >= 0)
103 		SET_POS(c->sci, pos, TRUE);
104 
105 }
106 
107 
cmd_search_current_next(CmdContext * c,CmdParams * p)108 void cmd_search_current_next(CmdContext *c, CmdParams *p)
109 {
110 	search_current(c, p, TRUE);
111 }
112 
113 
cmd_search_current_prev(CmdContext * c,CmdParams * p)114 void cmd_search_current_prev(CmdContext *c, CmdParams *p)
115 {
116 	search_current(c, p, FALSE);
117 }
118