4 Copyright (C) 1995-2017
5 Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * \brief Source: External panelization module
36 #include <sys/types.h>
40 #include "lib/global.h"
43 #include "lib/vfs/vfs.h"
44 #include "lib/mcconfig.h" /* Load/save directories panelize */
45 #include "lib/strutil.h"
47 #include "lib/widget.h"
49 #include "src/setup.h" /* For profile_bname */
50 #include "src/history.h"
53 #include "midnight.h" /* current_panel */
54 #include "layout.h" /* rotate_dash() */
55 #include "panel.h" /* WPanel */
59 /*** global variables ****************************************************************************/
61 /*** file scope macro definitions ****************************************************************/
67 #define B_REMOVE (B_USER + 1)
69 /*** file scope type declarations ****************************************************************/
71 /*** file scope variables ************************************************************************/
73 static WListbox
*l_panelize
;
74 static WDialog
*panelize_dlg
;
75 static int last_listitem
;
78 static const char *panelize_section
= "Panelize";
80 /* Directory panelize */
81 static struct panelize
85 struct panelize
*next
;
88 /* --------------------------------------------------------------------------------------------- */
89 /*** file scope functions ************************************************************************/
90 /* --------------------------------------------------------------------------------------------- */
95 if (l_panelize
->pos
!= last_listitem
)
97 struct panelize
*data
= NULL
;
99 last_listitem
= l_panelize
->pos
;
100 listbox_get_current (l_panelize
, NULL
, (void **) &data
);
101 input_assign_text (pname
, data
->command
);
103 input_update (pname
, TRUE
);
107 /* --------------------------------------------------------------------------------------------- */
110 panelize_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
115 case MSG_NOTIFY
: /* MSG_NOTIFY is fired by the listbox to tell us the item has changed. */
120 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
124 /* --------------------------------------------------------------------------------------------- */
132 button_flags_t flags
;
137 { B_ENTER
, DEFPUSH_BUTTON
, N_("Pane&lize") },
138 { B_REMOVE
, NORMAL_BUTTON
, N_("&Remove") },
139 { B_ADD
, NORMAL_BUTTON
, N_("&Add new") },
140 { B_CANCEL
, NORMAL_BUTTON
, N_("&Cancel") }
147 struct panelize
*current
;
154 i
= G_N_ELEMENTS (panelize_but
);
155 blen
= i
- 1; /* gaps between buttons */
159 panelize_but
[i
].text
= _(panelize_but
[i
].text
);
161 blen
+= str_term_width1 (panelize_but
[i
].text
) + 3 + 1;
162 if (panelize_but
[i
].flags
== DEFPUSH_BUTTON
)
166 panelize_cols
= COLS
- 6;
167 panelize_cols
= MAX (panelize_cols
, blen
+ 4);
170 dlg_create (TRUE
, 0, 0, 20, panelize_cols
, WPOS_CENTER
, FALSE
, dialog_colors
,
171 panelize_callback
, NULL
, "[External panelize]", _("External panelize"));
173 /* add listbox to the dialogs */
175 add_widget (panelize_dlg
, groupbox_new (y
++, UX
, 12, panelize_cols
- UX
* 2, ""));
177 l_panelize
= listbox_new (y
, UX
+ 1, 10, panelize_cols
- UX
* 2 - 2, FALSE
, NULL
);
178 for (current
= panelize
; current
!= NULL
; current
= current
->next
)
179 listbox_add_item (l_panelize
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
, FALSE
);
180 listbox_select_entry (l_panelize
, listbox_search_text (l_panelize
, _("Other command")));
181 add_widget (panelize_dlg
, l_panelize
);
183 y
+= WIDGET (l_panelize
)->lines
+ 1;
184 add_widget (panelize_dlg
, label_new (y
++, UX
, _("Command")));
186 input_new (y
++, UX
, input_colors
, panelize_cols
- UX
* 2, "", "in",
187 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_COMMANDS
|
188 INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
| INPUT_COMPLETE_CD
|
189 INPUT_COMPLETE_SHELL_ESC
);
190 add_widget (panelize_dlg
, pname
);
194 add_widget (panelize_dlg
, hline_new (y
++, -1, -1));
196 x
= (panelize_cols
- blen
) / 2;
197 for (i
= 0; i
< G_N_ELEMENTS (panelize_but
); i
++)
201 b
= button_new (y
, x
,
202 panelize_but
[i
].ret_cmd
, panelize_but
[i
].flags
, panelize_but
[i
].text
, NULL
);
203 add_widget (panelize_dlg
, b
);
205 x
+= button_get_len (b
) + 1;
208 widget_select (WIDGET (l_panelize
));
211 /* --------------------------------------------------------------------------------------------- */
216 dlg_destroy (panelize_dlg
);
220 /* --------------------------------------------------------------------------------------------- */
223 add2panelize (char *label
, char *command
)
225 struct panelize
*current
, *old
;
229 while (current
&& strcmp (current
->label
, label
) <= 0)
232 current
= current
->next
;
237 panelize
= g_new (struct panelize
, 1);
238 panelize
->label
= label
;
239 panelize
->command
= command
;
240 panelize
->next
= current
;
244 struct panelize
*new;
245 new = g_new (struct panelize
, 1);
247 new->command
= command
;
253 /* --------------------------------------------------------------------------------------------- */
256 add2panelize_cmd (void)
258 if (!input_is_empty (pname
))
262 label
= input_dialog (_("Add to external panelize"),
263 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD
, "",
264 INPUT_COMPLETE_NONE
);
265 if (label
== NULL
|| *label
== '\0')
268 add2panelize (label
, g_strdup (pname
->buffer
));
272 /* --------------------------------------------------------------------------------------------- */
275 remove_from_panelize (struct panelize
*entry
)
277 if (strcmp (entry
->label
, _("Other command")) != 0)
279 if (entry
== panelize
)
281 panelize
= panelize
->next
;
285 struct panelize
*current
= panelize
;
286 while (current
&& current
->next
!= entry
)
287 current
= current
->next
;
290 current
->next
= entry
->next
;
294 g_free (entry
->label
);
295 g_free (entry
->command
);
300 /* --------------------------------------------------------------------------------------------- */
303 do_external_panelize (char *command
)
305 int link_to_dir
, stale_link
;
307 dir_list
*list
= ¤t_panel
->dir
;
308 char line
[MC_MAXPATHLEN
];
313 external
= popen (command
, "r");
316 close_error_pipe (D_ERROR
, _("Cannot invoke command."));
319 /* Clear the counters and the directory list */
320 panel_clean_dir (current_panel
);
322 panelize_change_root (current_panel
->cwd_vpath
);
324 dir_list_init (list
);
329 if (fgets (line
, sizeof (line
), external
) == NULL
)
331 if (ferror (external
) && errno
== EINTR
)
336 if (line
[strlen (line
) - 1] == '\n')
337 line
[strlen (line
) - 1] = 0;
338 if (strlen (line
) < 1)
340 if (line
[0] == '.' && IS_PATH_SEP (line
[1]))
345 if (!handle_path (name
, &st
, &link_to_dir
, &stale_link
))
348 if (!dir_list_append (list
, name
, &st
, link_to_dir
!= 0, stale_link
!= 0))
351 file_mark (current_panel
, list
->len
- 1, 0);
353 if ((list
->len
& 31) == 0)
357 current_panel
->is_panelized
= TRUE
;
359 if (list
->len
> 1 && IS_PATH_SEP (list
->list
[1].fname
[0]))
361 vfs_path_t
*vpath_root
;
364 vpath_root
= vfs_path_from_str (PATH_SEP_STR
);
365 panel_set_cwd (current_panel
, vpath_root
);
366 ret
= mc_chdir (vpath_root
);
367 vfs_path_free (vpath_root
);
372 if (pclose (external
) < 0)
373 message (D_NORMAL
, _("External panelize"), _("Pipe close failed"));
374 close_error_pipe (D_NORMAL
, NULL
);
375 try_to_select (current_panel
, NULL
);
376 panel_re_sort (current_panel
);
380 /* --------------------------------------------------------------------------------------------- */
383 do_panelize_cd (WPanel
* panel
)
387 gboolean panelized_same
;
389 dir_list_clean (&panel
->dir
);
390 if (panelized_panel
.root_vpath
== NULL
)
391 panelize_change_root (current_panel
->cwd_vpath
);
393 if (panelized_panel
.list
.len
< 1)
394 dir_list_init (&panelized_panel
.list
);
395 else if (panelized_panel
.list
.len
> panel
->dir
.size
)
396 dir_list_grow (&panel
->dir
, panelized_panel
.list
.len
- panel
->dir
.size
);
399 list
->len
= panelized_panel
.list
.len
;
400 panel
->is_panelized
= TRUE
;
402 panelized_same
= vfs_path_equal (panelized_panel
.root_vpath
, panel
->cwd_vpath
);
404 for (i
= 0; i
< panelized_panel
.list
.len
; i
++)
406 if (panelized_same
|| DIR_IS_DOTDOT (panelized_panel
.list
.list
[i
].fname
))
408 list
->list
[i
].fnamelen
= panelized_panel
.list
.list
[i
].fnamelen
;
409 list
->list
[i
].fname
= g_strndup (panelized_panel
.list
.list
[i
].fname
,
410 panelized_panel
.list
.list
[i
].fnamelen
);
414 vfs_path_t
*tmp_vpath
;
418 vfs_path_append_new (panelized_panel
.root_vpath
, panelized_panel
.list
.list
[i
].fname
,
420 fname
= vfs_path_as_str (tmp_vpath
);
421 list
->list
[i
].fnamelen
= strlen (fname
);
422 list
->list
[i
].fname
= g_strndup (fname
, list
->list
[i
].fnamelen
);
423 vfs_path_free (tmp_vpath
);
425 list
->list
[i
].f
.link_to_dir
= panelized_panel
.list
.list
[i
].f
.link_to_dir
;
426 list
->list
[i
].f
.stale_link
= panelized_panel
.list
.list
[i
].f
.stale_link
;
427 list
->list
[i
].f
.dir_size_computed
= panelized_panel
.list
.list
[i
].f
.dir_size_computed
;
428 list
->list
[i
].f
.marked
= panelized_panel
.list
.list
[i
].f
.marked
;
429 list
->list
[i
].st
= panelized_panel
.list
.list
[i
].st
;
430 list
->list
[i
].sort_key
= panelized_panel
.list
.list
[i
].sort_key
;
431 list
->list
[i
].second_sort_key
= panelized_panel
.list
.list
[i
].second_sort_key
;
433 try_to_select (panel
, NULL
);
436 /* --------------------------------------------------------------------------------------------- */
437 /*** public functions ****************************************************************************/
438 /* --------------------------------------------------------------------------------------------- */
441 * Change root directory of panelized content.
442 * @param new_root - object with new path.
445 panelize_change_root (const vfs_path_t
* new_root
)
447 vfs_path_free (panelized_panel
.root_vpath
);
448 panelized_panel
.root_vpath
= vfs_path_clone (new_root
);
451 /* --------------------------------------------------------------------------------------------- */
454 panelize_save_panel (WPanel
* panel
)
457 dir_list
*list
= &panel
->dir
;
459 panelize_change_root (current_panel
->cwd_vpath
);
461 if (panelized_panel
.list
.len
> 0)
462 dir_list_clean (&panelized_panel
.list
);
463 if (panel
->dir
.len
== 0)
466 if (panel
->dir
.len
> panelized_panel
.list
.size
)
467 dir_list_grow (&panelized_panel
.list
, panel
->dir
.len
- panelized_panel
.list
.size
);
468 panelized_panel
.list
.len
= panel
->dir
.len
;
470 for (i
= 0; i
< panel
->dir
.len
; i
++)
472 panelized_panel
.list
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
473 panelized_panel
.list
.list
[i
].fname
=
474 g_strndup (list
->list
[i
].fname
, list
->list
[i
].fnamelen
);
475 panelized_panel
.list
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
476 panelized_panel
.list
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
477 panelized_panel
.list
.list
[i
].f
.dir_size_computed
= list
->list
[i
].f
.dir_size_computed
;
478 panelized_panel
.list
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
479 panelized_panel
.list
.list
[i
].st
= list
->list
[i
].st
;
480 panelized_panel
.list
.list
[i
].sort_key
= list
->list
[i
].sort_key
;
481 panelized_panel
.list
.list
[i
].second_sort_key
= list
->list
[i
].second_sort_key
;
485 /* --------------------------------------------------------------------------------------------- */
488 cd_panelize_cmd (void)
490 if (!SELECTED_IS_PANEL
)
491 set_display_type (MENU_PANEL_IDX
, view_listing
);
493 do_panelize_cd (PANEL (get_panel_widget (MENU_PANEL_IDX
)));
496 /* --------------------------------------------------------------------------------------------- */
499 external_panelize (void)
501 if (!vfs_current_is_local ())
503 message (D_ERROR
, MSG_ERROR
, _("Cannot run external panelize in a non-local directory"));
509 /* display file info */
510 tty_setcolor (SELECTED_COLOR
);
512 switch (dlg_run (panelize_dlg
))
523 struct panelize
*entry
;
525 listbox_get_current (l_panelize
, NULL
, (void **) &entry
);
526 remove_from_panelize (entry
);
531 if (!input_is_empty (pname
))
535 cmd
= g_strdup (pname
->buffer
);
536 dlg_destroy (panelize_dlg
);
537 do_external_panelize (cmd
);
551 /* --------------------------------------------------------------------------------------------- */
558 keys
= mc_config_get_keys (mc_global
.main_config
, panelize_section
, NULL
);
560 add2panelize (g_strdup (_("Other command")), g_strdup (""));
564 add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
565 add2panelize (g_strdup (_("Find rejects after patching")),
566 g_strdup ("find . -name \\*.rej -print"));
567 add2panelize (g_strdup (_("Find *.orig after patching")),
568 g_strdup ("find . -name \\*.orig -print"));
569 add2panelize (g_strdup (_("Find SUID and SGID programs")),
571 ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 -a -perm /01 \\) \\) -print"));
578 conv
= str_crt_conv_from ("UTF-8");
580 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
584 if (mc_global
.utf8_display
|| conv
== INVALID_CONV
)
585 buffer
= g_string_new (*profile_keys
);
588 buffer
= g_string_new ("");
589 if (str_convert (conv
, *profile_keys
, buffer
) == ESTR_FAILURE
)
590 g_string_assign (buffer
, *profile_keys
);
593 add2panelize (g_string_free (buffer
, FALSE
),
594 mc_config_get_string (mc_global
.main_config
, panelize_section
,
598 str_close_conv (conv
);
604 /* --------------------------------------------------------------------------------------------- */
609 struct panelize
*current
= panelize
;
611 mc_config_del_group (mc_global
.main_config
, panelize_section
);
612 for (; current
; current
= current
->next
)
614 if (strcmp (current
->label
, _("Other command")))
615 mc_config_set_string (mc_global
.main_config
,
616 panelize_section
, current
->label
, current
->command
);
620 /* --------------------------------------------------------------------------------------------- */
625 struct panelize
*current
= panelize
;
626 struct panelize
*next
;
628 for (; current
; current
= next
)
630 next
= current
->next
;
631 g_free (current
->label
);
632 g_free (current
->command
);
637 /* --------------------------------------------------------------------------------------------- */