Extend QUICK_INPUT and QUICK_LABELED_INPUT macros for getting completion flags via...
[midnight-commander.git] / src / filemanager / panelize.c
blobf4ea442fb4bf7f17f852de611e6a41120c0bf240
1 /*
2 External panelize
4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Janne Kukonlehto, 1995
10 Jakub Jelinek, 1995
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file panelize.c
29 * \brief Source: External panelization module
32 #include <config.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
41 #include "lib/global.h"
43 #include "lib/skin.h"
44 #include "lib/vfs/vfs.h"
45 #include "lib/mcconfig.h" /* Load/save directories panelize */
46 #include "lib/strutil.h"
47 #include "lib/util.h"
48 #include "lib/widget.h"
50 #include "src/setup.h" /* For profile_bname */
51 #include "src/history.h"
53 #include "dir.h"
54 #include "midnight.h" /* current_panel */
55 #include "panel.h" /* WPanel */
57 #include "panelize.h"
58 #include "panel.h"
60 /*** global variables ****************************************************************************/
62 /*** file scope macro definitions ****************************************************************/
64 #define UX 3
65 #define UY 2
67 #define LABELS 3
68 #define B_ADD B_USER
69 #define B_REMOVE (B_USER + 1)
71 /*** file scope type declarations ****************************************************************/
73 /*** file scope variables ************************************************************************/
75 static WListbox *l_panelize;
76 static WDialog *panelize_dlg;
77 static int last_listitem;
78 static WInput *pname;
80 static const char *panelize_section = "Panelize";
82 /* Directory panelize */
83 static struct panelize
85 char *command;
86 char *label;
87 struct panelize *next;
88 } *panelize = NULL;
90 /* --------------------------------------------------------------------------------------------- */
91 /*** file scope functions ************************************************************************/
92 /* --------------------------------------------------------------------------------------------- */
94 static void
95 update_command (void)
97 if (l_panelize->pos != last_listitem)
99 struct panelize *data = NULL;
101 last_listitem = l_panelize->pos;
102 listbox_get_current (l_panelize, NULL, (void **) &data);
103 input_assign_text (pname, data->command);
104 pname->point = 0;
105 input_update (pname, TRUE);
109 /* --------------------------------------------------------------------------------------------- */
111 static cb_ret_t
112 panelize_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
114 switch (msg)
116 case MSG_INIT:
117 case MSG_POST_KEY:
118 case MSG_FOCUS:
119 tty_setcolor (MENU_ENTRY_COLOR);
120 update_command ();
121 return MSG_HANDLED;
123 default:
124 return dlg_default_callback (w, sender, msg, parm, data);
128 /* --------------------------------------------------------------------------------------------- */
130 static void
131 init_panelize (void)
133 struct
135 int ret_cmd;
136 button_flags_t flags;
137 const char *text;
138 } panelize_but[] =
140 /* *INDENT-OFF* */
141 { B_ENTER, DEFPUSH_BUTTON, N_("Pane&lize") },
142 { B_REMOVE, NORMAL_BUTTON, N_("&Remove") },
143 { B_ADD, NORMAL_BUTTON, N_("&Add new") },
144 { B_CANCEL, NORMAL_BUTTON, N_("&Cancel") }
145 /* *INDENT-ON* */
148 size_t i;
149 int blen;
150 int panelize_cols;
151 struct panelize *current;
152 int x, y;
154 last_listitem = 0;
156 do_refresh ();
158 i = G_N_ELEMENTS (panelize_but);
159 blen = i - 1; /* gaps between buttons */
160 while (i-- != 0)
162 panelize_but[i].text = _(panelize_but[i].text);
163 blen += str_term_width1 (panelize_but[i].text) + 3 + 1;
164 if (panelize_but[i].flags == DEFPUSH_BUTTON)
165 blen += 2;
168 panelize_cols = COLS - 6;
169 panelize_cols = max (panelize_cols, blen + 4);
171 panelize_dlg =
172 create_dlg (TRUE, 0, 0, 20, panelize_cols, dialog_colors, panelize_callback, NULL,
173 "[External panelize]", _("External panelize"), DLG_CENTER);
175 /* add listbox to the dialogs */
176 y = UY;
177 add_widget (panelize_dlg, groupbox_new (y++, UX, 12, panelize_cols - UX * 2, ""));
179 l_panelize = listbox_new (y, UX + 1, 10, panelize_cols - UX * 2 - 2, FALSE, NULL);
180 for (current = panelize; current != NULL; current = current->next)
181 listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current);
182 listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
183 add_widget (panelize_dlg, l_panelize);
185 y += WIDGET (l_panelize)->lines + 1;
186 add_widget (panelize_dlg, label_new (y++, UX, _("Command")));
187 pname =
188 input_new (y++, UX, input_get_default_colors (), panelize_cols - UX * 2, "", "in",
189 INPUT_COMPLETE_DEFAULT);
190 add_widget (panelize_dlg, pname);
192 add_widget (panelize_dlg, hline_new (y++, -1, -1));
194 x = (panelize_cols - blen) / 2;
195 for (i = 0; i < G_N_ELEMENTS (panelize_but); i++)
197 WButton *b;
199 b = button_new (y, x,
200 panelize_but[i].ret_cmd, panelize_but[i].flags, panelize_but[i].text, NULL);
201 add_widget (panelize_dlg, b);
203 x += button_get_len (b) + 1;
206 dlg_select_widget (l_panelize);
209 /* --------------------------------------------------------------------------------------------- */
211 static void
212 panelize_done (void)
214 destroy_dlg (panelize_dlg);
215 repaint_screen ();
218 /* --------------------------------------------------------------------------------------------- */
220 static void
221 add2panelize (char *label, char *command)
223 struct panelize *current, *old;
225 old = NULL;
226 current = panelize;
227 while (current && strcmp (current->label, label) <= 0)
229 old = current;
230 current = current->next;
233 if (old == NULL)
235 panelize = g_new (struct panelize, 1);
236 panelize->label = label;
237 panelize->command = command;
238 panelize->next = current;
240 else
242 struct panelize *new;
243 new = g_new (struct panelize, 1);
244 new->label = label;
245 new->command = command;
246 old->next = new;
247 new->next = current;
251 /* --------------------------------------------------------------------------------------------- */
253 static void
254 add2panelize_cmd (void)
256 char *label;
258 if (pname->buffer && (*pname->buffer))
260 label = input_dialog (_("Add to external panelize"),
261 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "",
262 INPUT_COMPLETE_DEFAULT);
263 if (!label)
264 return;
265 if (!*label)
267 g_free (label);
268 return;
271 add2panelize (label, g_strdup (pname->buffer));
275 /* --------------------------------------------------------------------------------------------- */
277 static void
278 remove_from_panelize (struct panelize *entry)
280 if (strcmp (entry->label, _("Other command")) != 0)
282 if (entry == panelize)
284 panelize = panelize->next;
286 else
288 struct panelize *current = panelize;
289 while (current && current->next != entry)
290 current = current->next;
291 if (current)
293 current->next = entry->next;
297 g_free (entry->label);
298 g_free (entry->command);
299 g_free (entry);
303 /* --------------------------------------------------------------------------------------------- */
305 static void
306 do_external_panelize (char *command)
308 int status, link_to_dir, stale_link;
309 int next_free = 0;
310 struct stat st;
311 dir_list *list = &current_panel->dir;
312 char line[MC_MAXPATHLEN];
313 char *name;
314 FILE *external;
316 open_error_pipe ();
317 external = popen (command, "r");
318 if (!external)
320 close_error_pipe (D_ERROR, _("Cannot invoke command."));
321 return;
323 /* Clear the counters and the directory list */
324 panel_clean_dir (current_panel);
326 panelize_change_root (current_panel->cwd_vpath);
328 if (set_zero_dir (list))
329 next_free++;
331 while (1)
333 clearerr (external);
334 if (fgets (line, MC_MAXPATHLEN, external) == NULL)
336 if (ferror (external) && errno == EINTR)
337 continue;
338 else
339 break;
341 if (line[strlen (line) - 1] == '\n')
342 line[strlen (line) - 1] = 0;
343 if (strlen (line) < 1)
344 continue;
345 if (line[0] == '.' && line[1] == PATH_SEP)
346 name = line + 2;
347 else
348 name = line;
349 status = handle_path (list, name, &st, next_free, &link_to_dir, &stale_link);
350 if (status == 0)
351 continue;
352 if (status == -1)
353 break;
354 list->list[next_free].fnamelen = strlen (name);
355 list->list[next_free].fname = g_strndup (name, list->list[next_free].fnamelen);
356 file_mark (current_panel, next_free, 0);
357 list->list[next_free].f.link_to_dir = link_to_dir;
358 list->list[next_free].f.stale_link = stale_link;
359 list->list[next_free].f.dir_size_computed = 0;
360 list->list[next_free].st = st;
361 list->list[next_free].sort_key = NULL;
362 list->list[next_free].second_sort_key = NULL;
363 next_free++;
364 if (!(next_free & 32))
365 rotate_dash ();
368 current_panel->is_panelized = TRUE;
369 if (next_free)
371 current_panel->count = next_free;
372 if (list->list[0].fname[0] == PATH_SEP)
374 int ret;
375 panel_set_cwd (current_panel, PATH_SEP_STR);
376 ret = chdir (PATH_SEP_STR);
377 (void) ret;
380 else
382 current_panel->count = set_zero_dir (list) ? 1 : 0;
384 if (pclose (external) < 0)
385 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
386 close_error_pipe (D_NORMAL, NULL);
387 try_to_select (current_panel, NULL);
388 panel_re_sort (current_panel);
391 /* --------------------------------------------------------------------------------------------- */
393 static void
394 do_panelize_cd (struct WPanel *panel)
396 int i;
397 dir_list *list = &panel->dir;
398 gboolean panelized_same;
400 clean_dir (list, panel->count);
401 if (panelized_panel.root_vpath == NULL)
402 panelize_change_root (current_panel->cwd_vpath);
404 if (panelized_panel.count < 1)
406 if (set_zero_dir (&panelized_panel.list))
407 panelized_panel.count = 1;
409 else if (panelized_panel.count >= list->size)
411 list->list = g_try_realloc (list->list, sizeof (file_entry) * panelized_panel.count);
412 list->size = panelized_panel.count;
414 panel->count = panelized_panel.count;
415 panel->is_panelized = TRUE;
417 panelized_same = (vfs_path_cmp (panelized_panel.root_vpath, panel->cwd_vpath) == 0);
419 for (i = 0; i < panelized_panel.count; i++)
421 if (panelized_same
422 || (panelized_panel.list.list[i].fname[0] == '.'
423 && panelized_panel.list.list[i].fname[1] == '.'
424 && panelized_panel.list.list[i].fname[2] == '\0'))
426 list->list[i].fnamelen = panelized_panel.list.list[i].fnamelen;
427 list->list[i].fname = g_strndup (panelized_panel.list.list[i].fname,
428 panelized_panel.list.list[i].fnamelen);
430 else
432 vfs_path_t *tmp_vpath;
434 tmp_vpath =
435 vfs_path_append_new (panelized_panel.root_vpath, panelized_panel.list.list[i].fname,
436 NULL);
437 list->list[i].fname = vfs_path_to_str (tmp_vpath);
438 vfs_path_free (tmp_vpath);
439 list->list[i].fnamelen = strlen (list->list[i].fname);
441 list->list[i].f.link_to_dir = panelized_panel.list.list[i].f.link_to_dir;
442 list->list[i].f.stale_link = panelized_panel.list.list[i].f.stale_link;
443 list->list[i].f.dir_size_computed = panelized_panel.list.list[i].f.dir_size_computed;
444 list->list[i].f.marked = panelized_panel.list.list[i].f.marked;
445 list->list[i].st = panelized_panel.list.list[i].st;
446 list->list[i].sort_key = panelized_panel.list.list[i].sort_key;
447 list->list[i].second_sort_key = panelized_panel.list.list[i].second_sort_key;
449 try_to_select (panel, NULL);
452 /* --------------------------------------------------------------------------------------------- */
453 /*** public functions ****************************************************************************/
454 /* --------------------------------------------------------------------------------------------- */
457 * Change root directory of panelized content.
458 * @param new_root - object with new path.
460 void
461 panelize_change_root (const vfs_path_t * new_root)
463 vfs_path_free (panelized_panel.root_vpath);
464 panelized_panel.root_vpath = vfs_path_clone (new_root);
467 /* --------------------------------------------------------------------------------------------- */
469 void
470 panelize_save_panel (struct WPanel *panel)
472 int i;
473 dir_list *list = &panel->dir;
475 panelize_change_root (current_panel->cwd_vpath);
477 if (panelized_panel.count > 0)
478 clean_dir (&panelized_panel.list, panelized_panel.count);
479 if (panel->count < 1)
480 return;
482 panelized_panel.count = panel->count;
483 if (panel->count >= panelized_panel.list.size)
485 panelized_panel.list.list = g_try_realloc (panelized_panel.list.list,
486 sizeof (file_entry) * panel->count);
487 panelized_panel.list.size = panel->count;
489 for (i = 0; i < panel->count; i++)
491 panelized_panel.list.list[i].fnamelen = list->list[i].fnamelen;
492 panelized_panel.list.list[i].fname =
493 g_strndup (list->list[i].fname, list->list[i].fnamelen);
494 panelized_panel.list.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
495 panelized_panel.list.list[i].f.stale_link = list->list[i].f.stale_link;
496 panelized_panel.list.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
497 panelized_panel.list.list[i].f.marked = list->list[i].f.marked;
498 panelized_panel.list.list[i].st = list->list[i].st;
499 panelized_panel.list.list[i].sort_key = list->list[i].sort_key;
500 panelized_panel.list.list[i].second_sort_key = list->list[i].second_sort_key;
504 /* --------------------------------------------------------------------------------------------- */
506 void
507 cd_panelize_cmd (void)
509 if (get_display_type (MENU_PANEL_IDX) != view_listing)
510 set_display_type (MENU_PANEL_IDX, view_listing);
512 do_panelize_cd ((struct WPanel *) get_panel_widget (MENU_PANEL_IDX));
515 /* --------------------------------------------------------------------------------------------- */
517 void
518 external_panelize (void)
520 char *target = NULL;
522 if (!vfs_current_is_local ())
524 message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
525 return;
528 init_panelize ();
530 /* display file info */
531 tty_setcolor (SELECTED_COLOR);
533 run_dlg (panelize_dlg);
535 switch (panelize_dlg->ret_value)
537 case B_CANCEL:
538 break;
540 case B_ADD:
541 add2panelize_cmd ();
542 break;
544 case B_REMOVE:
546 struct panelize *entry;
548 listbox_get_current (l_panelize, NULL, (void **) &entry);
549 remove_from_panelize (entry);
550 break;
553 case B_ENTER:
554 target = pname->buffer;
555 if (target != NULL && *target)
557 char *cmd = g_strdup (target);
558 destroy_dlg (panelize_dlg);
559 do_external_panelize (cmd);
560 g_free (cmd);
561 repaint_screen ();
562 return;
564 break;
567 panelize_done ();
570 /* --------------------------------------------------------------------------------------------- */
572 void
573 load_panelize (void)
575 gchar **profile_keys, **keys;
576 gsize len;
577 GIConv conv;
579 conv = str_crt_conv_from ("UTF-8");
581 profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section, &len);
583 add2panelize (g_strdup (_("Other command")), g_strdup (""));
585 if (!profile_keys || *profile_keys == NULL)
587 add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
588 add2panelize (g_strdup (_("Find rejects after patching")),
589 g_strdup ("find . -name \\*.rej -print"));
590 add2panelize (g_strdup (_("Find *.orig after patching")),
591 g_strdup ("find . -name \\*.orig -print"));
592 add2panelize (g_strdup (_("Find SUID and SGID programs")),
593 g_strdup
594 ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
595 return;
598 while (*profile_keys)
600 GString *buffer;
602 if (mc_global.utf8_display || conv == INVALID_CONV)
603 buffer = g_string_new (*profile_keys);
604 else
606 buffer = g_string_new ("");
607 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
608 g_string_assign (buffer, *profile_keys);
611 add2panelize (g_string_free (buffer, FALSE),
612 mc_config_get_string (mc_main_config, panelize_section, *profile_keys, ""));
613 profile_keys++;
616 g_strfreev (keys);
617 str_close_conv (conv);
620 /* --------------------------------------------------------------------------------------------- */
622 void
623 save_panelize (void)
625 struct panelize *current = panelize;
627 mc_config_del_group (mc_main_config, panelize_section);
628 for (; current; current = current->next)
630 if (strcmp (current->label, _("Other command")))
631 mc_config_set_string (mc_main_config,
632 panelize_section, current->label, current->command);
636 /* --------------------------------------------------------------------------------------------- */
638 void
639 done_panelize (void)
641 struct panelize *current = panelize;
642 struct panelize *next;
644 for (; current; current = next)
646 next = current->next;
647 g_free (current->label);
648 g_free (current->command);
649 g_free (current);
653 /* --------------------------------------------------------------------------------------------- */