girara
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
completion.c
Go to the documentation of this file.
1 /* See LICENSE file for license and copyright information */
2 
3 #include <math.h>
4 #include <string.h>
5 #include <stdlib.h>
6 
7 #include "completion.h"
8 #include "internal.h"
9 #include "session.h"
10 #include "settings.h"
11 #include "datastructures.h"
12 #include "utils.h"
13 
14 static GtkEventBox* girara_completion_row_create(girara_session_t*, const char*, const char*, bool);
15 static void girara_completion_row_set_color(girara_session_t*, GtkEventBox*, int);
16 
17 /* completion */
19 {
20  bool group;
21  char* value;
22  GtkEventBox* widget;
23 };
24 
29 {
30  char *value;
31  char *description;
32 };
33 
38 {
39  char *value;
40  girara_list_t *elements;
41 };
42 
47 {
48  girara_list_t *groups;
49 };
50 
51 typedef struct girara_internal_completion_entry_s girara_internal_completion_entry_t;
52 
53 static void
54 completion_element_free(girara_completion_element_t* element)
55 {
56  if (element == NULL) {
57  return;
58  }
59 
60  /* free element */
61  g_free(element->value);
62  g_free(element->description);
63  g_slice_free(girara_completion_element_t, element);
64 }
65 
66 girara_completion_t*
68 {
69  girara_completion_t *completion = g_slice_new(girara_completion_t);
70  completion->groups = girara_list_new2(
72 
73  return completion;
74 }
75 
76 girara_completion_group_t*
77 girara_completion_group_create(girara_session_t* UNUSED(session), const char* name)
78 {
79  girara_completion_group_t* group = g_slice_new(girara_completion_group_t);
80 
81  group->value = name ? g_strdup(name) : NULL;
82  group->elements = girara_list_new2(
83  (girara_free_function_t) completion_element_free);
84 
85  if (group->elements == NULL) {
86  g_slice_free(girara_completion_group_t, group);
87  return NULL;
88  }
89 
90  return group;
91 }
92 
93 void
94 girara_completion_add_group(girara_completion_t* completion, girara_completion_group_t* group)
95 {
96  g_return_if_fail(completion != NULL);
97  g_return_if_fail(group != NULL);
98 
99  girara_list_append(completion->groups, group);
100 }
101 
102 void
103 girara_completion_group_free(girara_completion_group_t* group)
104 {
105  if (group == NULL) {
106  return;
107  }
108 
109  g_free(group->value);
110  girara_list_free(group->elements);
111  g_slice_free(girara_completion_group_t, group);
112 }
113 
114 void
115 girara_completion_free(girara_completion_t* completion)
116 {
117  g_return_if_fail(completion != NULL);
118 
119  girara_list_free(completion->groups);
120  /* free completion */
121  g_slice_free(girara_completion_t, completion);
122 }
123 
124 void
125 girara_completion_group_add_element(girara_completion_group_t* group, const char* name, const char* description)
126 {
127  g_return_if_fail(group != NULL);
128  g_return_if_fail(name != NULL);
129 
130  girara_completion_element_t* new_element = g_slice_new(girara_completion_element_t);
131 
132  new_element->value = g_strdup(name);
133  new_element->description = description ? g_strdup(description) : NULL;
134 
135  girara_list_append(group->elements, new_element);
136 }
137 
138 bool
139 girara_isc_completion(girara_session_t* session, girara_argument_t* argument, girara_event_t* UNUSED(event), unsigned int UNUSED(t))
140 {
141  g_return_val_if_fail(session != NULL, false);
142 
143  /* get current text */
144  gchar *input = gtk_editable_get_chars(GTK_EDITABLE(session->gtk.inputbar_entry), 0, -1);
145  if (input == NULL) {
146  return false;
147  }
148 
149  const size_t input_length = strlen(input);
150 
151  if (input_length == 0 || input[0] != ':') {
152  g_free(input);
153  return false;
154  }
155 
156  gchar** elements = NULL;
157  gint n_parameter = 0;
158  if (input_length > 1) {
159  if (g_shell_parse_argv(input + 1, &n_parameter, &elements, NULL) == FALSE) {
160  g_free(input);
161  return false;
162  }
163  } else {
164  elements = g_try_malloc0(2 * sizeof(char*));
165  if (elements == NULL) {
166  g_free(input);
167  return false;
168  }
169  elements[0] = g_strdup("");
170  }
171 
172  if (n_parameter == 1 && input[input_length-1] == ' ') {
173  n_parameter += 1;
174  }
175 
176  g_free(input);
177 
178  /* get current values */
179  gchar *current_command = (elements[0] != NULL && elements[0][0] != '\0') ? g_strdup(elements[0]) : NULL;
180  gchar *current_parameter = (elements[0] != NULL && elements[1] != NULL) ? g_strdup(elements[1]) : NULL;
181 
182  size_t current_command_length = current_command ? strlen(current_command) : 0;
183 
184  static GList* entries = NULL;
185  static GList* entries_current = NULL;
186  static char *previous_command = NULL;
187  static char *previous_parameter = NULL;
188  static bool command_mode = true;
189  static size_t previous_length = 0;
190 
191  /* delete old list iff
192  * the completion should be hidden
193  * the current command differs from the previous one
194  * the current parameter differs from the previous one
195  * no current command is given
196  */
197  if ( (argument->n == GIRARA_HIDE) ||
198  (current_parameter && previous_parameter && strcmp(current_parameter, previous_parameter)) ||
199  (current_command && previous_command && strcmp(current_command, previous_command)) ||
200  input_length != previous_length
201  )
202  {
203  if (session->gtk.results != NULL) {
204  /* destroy elements */
205  for (GList* element = entries; element; element = g_list_next(element)) {
206  girara_internal_completion_entry_t* entry = (girara_internal_completion_entry_t*) element->data;
207 
208  if (entry != NULL) {
209  gtk_widget_destroy(GTK_WIDGET(entry->widget));
210  g_free(entry->value);
211  g_slice_free(girara_internal_completion_entry_t, entry);
212  }
213  }
214 
215  g_list_free(entries);
216  entries = NULL;
217  entries_current = NULL;
218 
219  /* delete row box */
220  gtk_widget_destroy(GTK_WIDGET(session->gtk.results));
221  session->gtk.results = NULL;
222  }
223 
224  command_mode = true;
225 
226  if (argument->n == GIRARA_HIDE) {
227  g_free(previous_command);
228  previous_command = NULL;
229 
230  g_free(previous_parameter);
231  previous_parameter = NULL;
232 
233  g_strfreev(elements);
234 
235  g_free(current_command);
236  g_free(current_parameter);
237 
238  return false;
239  }
240  }
241 
242  /* create new list iff
243  * there is no current list
244  */
245  if (session->gtk.results == NULL) {
246  session->gtk.results = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0));
247 
248  if (session->gtk.results == NULL) {
249  g_free(current_command);
250  g_free(current_parameter);
251 
252  g_strfreev(elements);
253  return false;
254  }
255 
256  if (n_parameter <= 1) {
257  /* based on commands */
258  command_mode = true;
259 
260  /* create command rows */
261  GIRARA_LIST_FOREACH(session->bindings.commands, girara_command_t*, iter, command)
262  if (current_command == NULL ||
263  (command->command != NULL && !strncmp(current_command, command->command, current_command_length)) ||
264  (command->abbr != NULL && !strncmp(current_command, command->abbr, current_command_length))
265  )
266  {
267  /* create entry */
268  girara_internal_completion_entry_t* entry = g_slice_new(girara_internal_completion_entry_t);
269  entry->group = FALSE;
270  entry->value = g_strdup(command->command);
271  entry->widget = girara_completion_row_create(session, command->command, command->description, FALSE);
272 
273  entries = g_list_append(entries, entry);
274 
275  /* show entry row */
276  gtk_box_pack_start(session->gtk.results, GTK_WIDGET(entry->widget), FALSE, FALSE, 0);
277  }
278  GIRARA_LIST_FOREACH_END(session->bindings.commands, girara_command_t*, iter, command);
279  }
280 
281  /* based on parameters */
282  if (n_parameter > 1 || g_list_length(entries) == 1) {
283  /* if only one command exists try to run parameter completion */
284  if (g_list_length(entries) == 1) {
285  girara_internal_completion_entry_t* entry = g_list_first(entries)->data;
286 
287  /* unset command mode */
288  command_mode = false;
289  current_command = entry->value;
290  current_command_length = strlen(current_command);
291 
292  /* clear list */
293  gtk_widget_destroy(GTK_WIDGET(entry->widget));
294 
295  entries = g_list_remove(entries, g_list_first(entries)->data);
296  g_slice_free(girara_internal_completion_entry_t, entry);
297  }
298 
299  /* search matching command */
300  girara_command_t* command = NULL;
301  GIRARA_LIST_FOREACH(session->bindings.commands, girara_command_t*, iter, command_it)
302  if ( (current_command != NULL && command_it->command != NULL && !strncmp(current_command, command_it->command, current_command_length)) ||
303  (current_command != NULL && command_it->abbr != NULL && !strncmp(current_command, command_it->abbr, current_command_length))
304  )
305  {
306  g_free(previous_command);
307  previous_command = g_strdup(command_it->command);
308  command = command_it;
309  break;
310  }
311  GIRARA_LIST_FOREACH_END(session->bindings.commands, girara_command_t*, iter, command_it);
312 
313  if (command == NULL) {
314  g_free(current_command);
315  g_free(current_parameter);
316 
317  g_strfreev(elements);
318  return false;
319  }
320 
321  if (command->completion == NULL) {
322  girara_internal_completion_entry_t* entry = g_slice_new(girara_internal_completion_entry_t);
323  entry->group = FALSE;
324  entry->value = g_strdup(command->command);
325  entry->widget = girara_completion_row_create(session, command->command, command->description, FALSE);
326 
327  entries = g_list_append(entries, entry);
328 
329  gtk_box_pack_start(session->gtk.results, GTK_WIDGET(entry->widget), FALSE, FALSE, 0);
330  command_mode = true;
331  } else {
332  /* generate completion result
333  * XXX: the last argument should only be current_paramater ... but
334  * therefore the completion functions would need to handle NULL correctly
335  * (see cc_open in zathura). */
336  girara_completion_t *result = command->completion(session, current_parameter ? current_parameter : "");
337 
338  if (result == NULL || result->groups == NULL) {
339  g_free(current_command);
340  g_free(current_parameter);
341 
342  g_strfreev(elements);
343  return false;
344  }
345 
346  GIRARA_LIST_FOREACH(result->groups, girara_completion_group_t*, iter, group)
347  /* create group entry */
348  if (group->value != NULL) {
349  girara_internal_completion_entry_t* entry = g_slice_new(girara_internal_completion_entry_t);
350  entry->group = TRUE;
351  entry->value = g_strdup(group->value);
352  entry->widget = girara_completion_row_create(session, group->value, NULL, TRUE);
353 
354  entries = g_list_append(entries, entry);
355 
356  gtk_box_pack_start(session->gtk.results, GTK_WIDGET(entry->widget), FALSE, FALSE, 0);
357  }
358 
359  GIRARA_LIST_FOREACH(group->elements, girara_completion_element_t*, iter2, element)
360  girara_internal_completion_entry_t* entry = g_slice_new(girara_internal_completion_entry_t);
361  entry->group = FALSE;
362  entry->value = g_strdup(element->value);
363  entry->widget = girara_completion_row_create(session, element->value, element->description, FALSE);
364 
365  entries = g_list_append(entries, entry);
366 
367  gtk_box_pack_start(session->gtk.results, GTK_WIDGET(entry->widget), FALSE, FALSE, 0);
368 
369  GIRARA_LIST_FOREACH_END(group->elements, girara_completion_element_t*, iter2, element);
370  GIRARA_LIST_FOREACH_END(result->groups, girara_completion_group_t*, iter, group);
371  girara_completion_free(result);
372 
373  command_mode = false;
374  }
375  }
376 
377  if (entries != NULL) {
378  entries_current = (argument->n == GIRARA_NEXT) ? g_list_last(entries) : entries;
379  gtk_box_pack_start(session->private_data->gtk.bottom_box, GTK_WIDGET(session->gtk.results), FALSE, FALSE, 0);
380  gtk_widget_show(GTK_WIDGET(session->gtk.results));
381  }
382  }
383 
384  /* update entries */
385  unsigned int n_elements = g_list_length(entries);
386  if (entries != NULL && n_elements > 0) {
387  if (n_elements > 1) {
388  girara_completion_row_set_color(session, ((girara_internal_completion_entry_t *) entries_current->data)->widget, GIRARA_NORMAL);
389 
390  bool next_group = FALSE;
391 
392  for (unsigned int i = 0; i < n_elements; i++) {
393  if (argument->n == GIRARA_NEXT || argument->n == GIRARA_NEXT_GROUP) {
394  GList* entry = g_list_next(entries_current);
395  if (entry == NULL) {
396  entry = g_list_first(entries);
397  }
398 
399  entries_current = entry;
400  } else if (argument->n == GIRARA_PREVIOUS || argument->n == GIRARA_PREVIOUS_GROUP) {
401  GList* entry = g_list_previous(entries_current);
402  if (entry == NULL) {
403  entry = g_list_last(entries);
404  }
405 
406  entries_current = entry;
407  }
408 
409  if (((girara_internal_completion_entry_t*) entries_current->data)->group) {
410  if (command_mode == false && (argument->n == GIRARA_NEXT_GROUP || argument->n == GIRARA_PREVIOUS_GROUP)) {
411  next_group = TRUE;
412  }
413  continue;
414  } else {
415  if (command_mode == false && (next_group == 0) && (argument->n == GIRARA_NEXT_GROUP || argument->n == GIRARA_PREVIOUS_GROUP)) {
416  continue;
417  }
418  break;
419  }
420  }
421 
422  girara_completion_row_set_color(session, ((girara_internal_completion_entry_t *) entries_current->data)->widget, GIRARA_HIGHLIGHT);
423 
424  /* hide other items */
425  unsigned int n_completion_items = 15;
426  girara_setting_get(session, "n-completion-items", &n_completion_items);
427  unsigned int uh = ceil( n_completion_items / 2);
428  unsigned int lh = floor(n_completion_items / 2);
429 
430  unsigned int current_item = g_list_position(entries, entries_current);
431 
432  GList* tmpentry = entries;
433  for (unsigned int i = 0; i < n_elements; i++) {
434  if (
435  (i >= (current_item - lh) && (i <= current_item + uh)) ||
436  (i < n_completion_items && current_item < lh) ||
437  (i >= (n_elements - n_completion_items) && (current_item >= (n_elements - uh)))
438  )
439  {
440  gtk_widget_show(GTK_WIDGET(((girara_internal_completion_entry_t*) tmpentry->data)->widget));
441  } else {
442  gtk_widget_hide(GTK_WIDGET(((girara_internal_completion_entry_t*) tmpentry->data)->widget));
443  }
444 
445  tmpentry = g_list_next(tmpentry);
446  }
447  } else {
448  gtk_widget_hide(GTK_WIDGET(((girara_internal_completion_entry_t*) (g_list_nth(entries, 0))->data)->widget));
449  }
450 
451  /* update text */
452  char* temp;
453  char* escaped_value = girara_escape_string(((girara_internal_completion_entry_t *) entries_current->data)->value);
454  if (command_mode == true) {
455  char* space = (n_elements == 1) ? " " : "";
456  temp = g_strconcat(":", escaped_value, space, NULL);
457  } else {
458  temp = g_strconcat(":", previous_command, " ", escaped_value, NULL);
459  }
460 
461  gtk_entry_set_text(session->gtk.inputbar_entry, temp);
462  gtk_editable_set_position(GTK_EDITABLE(session->gtk.inputbar_entry), -1);
463  g_free(escaped_value);
464 
465  /* update previous */
466  g_free(previous_command);
467  g_free(previous_parameter);
468  previous_command = g_strdup((command_mode) ? ((girara_internal_completion_entry_t*) entries_current->data)->value : current_command);
469  previous_parameter = g_strdup((command_mode) ? current_parameter : ((girara_internal_completion_entry_t*) entries_current->data)->value);
470  previous_length = strlen(temp);
471  g_free(temp);
472  }
473 
474  g_free(current_command);
475  g_free(current_parameter);
476 
477  g_strfreev(elements);
478 
479  return false;
480 }
481 
482 static GtkEventBox*
483 girara_completion_row_create(girara_session_t* session, const char* command, const char* description, bool group)
484 {
485  GtkBox *col = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
486 
487  GtkEventBox *row = GTK_EVENT_BOX(gtk_event_box_new());
488 
489  GtkLabel *show_command = GTK_LABEL(gtk_label_new(NULL));
490  GtkLabel *show_description = GTK_LABEL(gtk_label_new(NULL));
491 
492  gtk_misc_set_alignment(GTK_MISC(show_command), 0.0, 0.0);
493  gtk_misc_set_alignment(GTK_MISC(show_description), 1.0, 0.0);
494 
495  if (group == true) {
496  gtk_misc_set_padding(GTK_MISC(show_command), 2, 4);
497  gtk_misc_set_padding(GTK_MISC(show_description), 2, 4);
498  } else {
499  gtk_misc_set_padding(GTK_MISC(show_command), 1, 1);
500  gtk_misc_set_padding(GTK_MISC(show_description), 1, 1);
501  }
502 
503  gtk_label_set_use_markup(show_command, TRUE);
504  gtk_label_set_use_markup(show_description, TRUE);
505 
506  gtk_label_set_ellipsize(show_command, PANGO_ELLIPSIZE_END);
507  gtk_label_set_ellipsize(show_description, PANGO_ELLIPSIZE_END);
508 
509  gchar* c = g_markup_printf_escaped(FORMAT_COMMAND, command ? command : "");
510  gchar* d = g_markup_printf_escaped(FORMAT_DESCRIPTION, description ? description : "");
511  gtk_label_set_markup(show_command, c);
512  gtk_label_set_markup(show_description, d);
513  g_free(c);
514  g_free(d);
515 
516  if (group == true) {
517  gtk_widget_override_color(GTK_WIDGET(show_command), GTK_STATE_NORMAL, &(session->style.completion_group_foreground));
518  gtk_widget_override_color(GTK_WIDGET(show_description), GTK_STATE_NORMAL, &(session->style.completion_group_foreground));
519  gtk_widget_override_background_color(GTK_WIDGET(row), GTK_STATE_NORMAL, &(session->style.completion_group_background));
520  } else {
521  gtk_widget_override_color(GTK_WIDGET(show_command), GTK_STATE_NORMAL, &(session->style.completion_foreground));
522  gtk_widget_override_color(GTK_WIDGET(show_description), GTK_STATE_NORMAL, &(session->style.completion_foreground));
523  gtk_widget_override_background_color(GTK_WIDGET(row), GTK_STATE_NORMAL, &(session->style.completion_background));
524  }
525 
526  gtk_widget_override_font(GTK_WIDGET(show_command), session->style.font);
527  gtk_widget_override_font(GTK_WIDGET(show_description), session->style.font);
528 
529  gtk_box_pack_start(GTK_BOX(col), GTK_WIDGET(show_command), TRUE, TRUE, 2);
530  gtk_box_pack_start(GTK_BOX(col), GTK_WIDGET(show_description), TRUE, TRUE, 2);
531 
532  gtk_container_add(GTK_CONTAINER(row), GTK_WIDGET(col));
533  gtk_widget_show_all(GTK_WIDGET(row));
534 
535  return row;
536 }
537 
538 static void
539 girara_completion_row_set_color(girara_session_t* session, GtkEventBox* row, int mode)
540 {
541  g_return_if_fail(session != NULL);
542  g_return_if_fail(row != NULL);
543 
544  GtkBox* col = GTK_BOX(gtk_bin_get_child(GTK_BIN(row)));
545  GList* items = gtk_container_get_children(GTK_CONTAINER(col));
546  GtkWidget* cmd = GTK_WIDGET(g_list_nth_data(items, 0));
547  GtkWidget* desc = GTK_WIDGET(g_list_nth_data(items, 1));
548 
549  if (mode == GIRARA_HIGHLIGHT) {
550  gtk_widget_override_color(cmd, GTK_STATE_NORMAL, &(session->style.completion_highlight_foreground));
551  gtk_widget_override_color(desc, GTK_STATE_NORMAL, &(session->style.completion_highlight_foreground));
552  gtk_widget_override_background_color(cmd, GTK_STATE_NORMAL, &(session->style.completion_highlight_background));
553  gtk_widget_override_background_color(desc, GTK_STATE_NORMAL, &(session->style.completion_highlight_background));
554  gtk_widget_override_background_color(GTK_WIDGET(row), GTK_STATE_NORMAL, &(session->style.completion_highlight_background));
555  } else {
556  gtk_widget_override_color(cmd, GTK_STATE_NORMAL, &(session->style.completion_foreground));
557  gtk_widget_override_color(desc, GTK_STATE_NORMAL, &(session->style.completion_foreground));
558  gtk_widget_override_background_color(cmd, GTK_STATE_NORMAL, &(session->style.completion_background));
559  gtk_widget_override_background_color(desc, GTK_STATE_NORMAL, &(session->style.completion_background));
560  gtk_widget_override_background_color(GTK_WIDGET(row), GTK_STATE_NORMAL, &(session->style.completion_background));
561  }
562 
563  g_list_free(items);
564 }