1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/**
* vimb - a webkit based vim like browser.
*
* Copyright (C) 2012-2013 Daniel Carl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include "config.h"
#include "main.h"
#include "completion.h"
#include "util.h"
#include "history.h"
#include "bookmark.h"
#include "command.h"
#include "setting.h"
#include "ex.h"
#define TAG_INDICATOR '!'
extern VbCore vb;
static struct {
GtkWidget *win;
GtkWidget *tree;
int count; /* command count before the completed content */
char *prefix; /* prefix that marks the completion ':', '/', ':open', ... */
int active; /* number of the current active tree item */
char *text; /* text of the current active tree item */
} comp;
static void init_completion(GtkTreeModel *model);
static void show(gboolean back);
static void move_cursor(gboolean back);
static gboolean tree_selection_func(GtkTreeSelection *selection,
GtkTreeModel *model, GtkTreePath *path, gboolean selected, gpointer data);
gboolean completion_complete(gboolean back)
{
VbInputType type;
char *input;
const char *prefix, *suffix;
GtkListStore *store = NULL;
gboolean res = false, sort = true;
/* TODO give the type of completion to this function - because we have to
* handle also abreviated commands like ':op foo<tab>' */
input = vb_get_input_text();
type = vb_get_input_parts(input, VB_INPUT_ALL, &prefix, &suffix);
if (vb.mode->flags & FLAG_COMPLETION) {
if (comp.text && !strcmp(input, comp.text)) {
/* step through the next/prev completion item */
move_cursor(back);
return true;
}
/* if current input isn't the content of the completion item, stop
* completion and start it after that again */
completion_clean();
}
/* create the list store model */
store = gtk_list_store_new(COMPLETION_STORE_NUM, G_TYPE_STRING, G_TYPE_STRING);
if (type == VB_INPUT_SET) {
res = setting_fill_completion(store, suffix);
} else if (type == VB_INPUT_OPEN || type == VB_INPUT_TABOPEN) {
/* if search string begins with TAG_INDICATOR lookup the bookmarks */
if (suffix && *suffix == TAG_INDICATOR) {
res = bookmark_fill_completion(store, suffix + 1);
} else {
res = history_fill_completion(store, HISTORY_URL, suffix);
}
sort = false;
} else if (type == VB_INPUT_COMMAND) {
char *command = NULL;
/* remove counts before command and save it to print it later in inputbox */
comp.count = g_ascii_strtoll(suffix, &command, 10);
res = ex_fill_completion(store, command);
/* we have a special sorting of the ex commands so we don't should
* reorder them for the completion */
sort = false;
} else if (type == VB_INPUT_SEARCH_FORWARD || type == VB_INPUT_SEARCH_BACKWARD) {
res = history_fill_completion(store, HISTORY_SEARCH, suffix);
} else if (type == VB_INPUT_BOOKMARK_ADD) {
res = bookmark_fill_tag_completion(store, suffix);
}
if (!res) {
return false;
}
/* apply the defualt sorting to the first tree model comlumn */
if (sort) {
gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), COMPLETION_STORE_FIRST, GTK_SORT_ASCENDING);
}
/* set the submode flag */
vb.mode->flags |= FLAG_COMPLETION;
OVERWRITE_STRING(comp.prefix, prefix);
init_completion(GTK_TREE_MODEL(store));
show(back);
return true;
}
void completion_clean(void)
{
if (vb.mode->flags & FLAG_COMPLETION) {
/* remove completion flag from mode */
vb.mode->flags &= ~FLAG_COMPLETION;
if (comp.win) {
gtk_widget_destroy(comp.win);
comp.win = comp.tree = NULL;
}
OVERWRITE_STRING(comp.prefix, NULL);
OVERWRITE_STRING(comp.text, NULL);
comp.count = 0;
}
}
static void init_completion(GtkTreeModel *model)
{
GtkCellRenderer *renderer;
GtkTreeSelection *selection;
GtkTreeViewColumn *column;
GtkRequisition size;
int height, width;
/* prepare the tree view */
comp.win = gtk_scrolled_window_new(NULL, NULL);
comp.tree = gtk_tree_view_new();
#ifndef HAS_GTK3
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(comp.win), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
#endif
gtk_box_pack_end(GTK_BOX(vb.gui.box), comp.win, false, false, 0);
gtk_container_add(GTK_CONTAINER(comp.win), comp.tree);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(comp.tree), false);
/* we have only on line per item so we can use the faster fixed heigh mode */
gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(comp.tree), true);
gtk_tree_view_set_model(GTK_TREE_VIEW(comp.tree), model);
g_object_unref(model);
VB_WIDGET_OVERRIDE_TEXT(comp.tree, VB_GTK_STATE_NORMAL, &vb.style.comp_fg[VB_COMP_NORMAL]);
VB_WIDGET_OVERRIDE_BASE(comp.tree, VB_GTK_STATE_NORMAL, &vb.style.comp_bg[VB_COMP_NORMAL]);
VB_WIDGET_OVERRIDE_TEXT(comp.tree, VB_GTK_STATE_SELECTED, &vb.style.comp_fg[VB_COMP_ACTIVE]);
VB_WIDGET_OVERRIDE_BASE(comp.tree, VB_GTK_STATE_SELECTED, &vb.style.comp_bg[VB_COMP_ACTIVE]);
/* prepare the selection */
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(comp.tree));
gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
gtk_tree_selection_set_select_function(selection, tree_selection_func, NULL, NULL);
/* get window dimension */
gtk_window_get_size(GTK_WINDOW(vb.gui.window), &width, &height);
/* prepare first column */
column = gtk_tree_view_column_new();
gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_append_column(GTK_TREE_VIEW(comp.tree), column);
renderer = gtk_cell_renderer_text_new();
g_object_set(renderer,
"font-desc", vb.style.comp_font,
"ellipsize", PANGO_ELLIPSIZE_MIDDLE,
NULL
);
gtk_tree_view_column_pack_start(column, renderer, true);
gtk_tree_view_column_add_attribute(column, renderer, "text", COMPLETION_STORE_FIRST);
gtk_tree_view_column_set_min_width(column, 2 * width/3);
/* prepare second column */
#ifdef FEATURE_TITLE_IN_COMPLETION
column = gtk_tree_view_column_new();
gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_append_column(GTK_TREE_VIEW(comp.tree), column);
renderer = gtk_cell_renderer_text_new();
g_object_set(renderer,
"font-desc", vb.style.comp_font,
"ellipsize", PANGO_ELLIPSIZE_END,
NULL
);
gtk_tree_view_column_pack_start(column, renderer, true);
gtk_tree_view_column_add_attribute(column, renderer, "text", COMPLETION_STORE_SECOND);
#endif
/* use max 1/3 of window height for the completion */
#ifdef HAS_GTK3
gtk_widget_get_preferred_size(comp.tree, NULL, &size);
height /= 3;
gtk_scrolled_window_set_min_content_height(
GTK_SCROLLED_WINDOW(comp.win),
size.height > height ? height : size.height
);
#else
gtk_widget_size_request(comp.tree, &size);
height /= 3;
if (size.height > height) {
gtk_widget_set_size_request(comp.win, -1, height);
}
#endif
}
static void show(gboolean back)
{
/* this prevents the first item to be placed out of view if the completion
* is shown */
gtk_widget_show_all(comp.win);
while (gtk_events_pending()) {
gtk_main_iteration();
}
/* set to -1 to have the cursor on first or last item set in move_cursor */
comp.active = -1;
move_cursor(back);
}
static void move_cursor(gboolean back)
{
int rows;
GtkTreePath *path;
GtkTreeView *tree = GTK_TREE_VIEW(comp.tree);
rows = gtk_tree_model_iter_n_children(gtk_tree_view_get_model(tree), NULL);
if (back) {
/* step back */
if (--comp.active < 0) {
comp.active = rows - 1;
}
} else {
/* step forward */
if (++comp.active >= rows) {
comp.active = 0;
}
}
/* get new path and move cursor to it */
path = gtk_tree_path_new_from_indices(comp.active, -1);
gtk_tree_view_set_cursor(tree, path, NULL, false);
gtk_tree_path_free(path);
}
static gboolean tree_selection_func(GtkTreeSelection *selection,
GtkTreeModel *model, GtkTreePath *path, gboolean selected, gpointer data)
{
char *value;
GtkTreeIter iter;
/* if not selected means the item is going to be selected which we are
* interested in */
if (!selected && gtk_tree_model_get_iter(model, &iter, path)) {
gtk_tree_model_get(model, &iter, COMPLETION_STORE_FIRST, &value, -1);
/* save the content of the selected item so wen can access it easy */
if (comp.text) {
g_free(comp.text);
comp.text = NULL;
}
if (comp.count) {
comp.text = g_strdup_printf("%s%d%s", comp.prefix, comp.count, value);
} else {
comp.text = g_strconcat(comp.prefix, value, NULL);
}
/* print the text also into inputbox */
vb_set_input_text(comp.text);
g_free(value);
}
return true;
}
|