4 Copyright (C) 1995-2016
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 ****************************************************************/
68 #define B_REMOVE (B_USER + 1)
70 /*** file scope type declarations ****************************************************************/
72 /*** file scope variables ************************************************************************/
74 static WListbox
*l_panelize
;
75 static WDialog
*panelize_dlg
;
76 static int last_listitem
;
79 static const char *panelize_section
= "Panelize";
81 /* Directory panelize */
82 static struct panelize
86 struct panelize
*next
;
89 /* --------------------------------------------------------------------------------------------- */
90 /*** file scope functions ************************************************************************/
91 /* --------------------------------------------------------------------------------------------- */
96 if (l_panelize
->pos
!= last_listitem
)
98 struct panelize
*data
= NULL
;
100 last_listitem
= l_panelize
->pos
;
101 listbox_get_current (l_panelize
, NULL
, (void **) &data
);
102 input_assign_text (pname
, data
->command
);
104 input_update (pname
, TRUE
);
108 /* --------------------------------------------------------------------------------------------- */
111 panelize_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
121 return MSG_NOT_HANDLED
;
123 /* The listbox tells us the item has changed. */
128 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
132 /* --------------------------------------------------------------------------------------------- */
140 button_flags_t flags
;
145 { B_ENTER
, DEFPUSH_BUTTON
, N_("Pane&lize") },
146 { B_REMOVE
, NORMAL_BUTTON
, N_("&Remove") },
147 { B_ADD
, NORMAL_BUTTON
, N_("&Add new") },
148 { B_CANCEL
, NORMAL_BUTTON
, N_("&Cancel") }
155 struct panelize
*current
;
162 i
= G_N_ELEMENTS (panelize_but
);
163 blen
= i
- 1; /* gaps between buttons */
167 panelize_but
[i
].text
= _(panelize_but
[i
].text
);
169 blen
+= str_term_width1 (panelize_but
[i
].text
) + 3 + 1;
170 if (panelize_but
[i
].flags
== DEFPUSH_BUTTON
)
174 panelize_cols
= COLS
- 6;
175 panelize_cols
= max (panelize_cols
, blen
+ 4);
178 dlg_create (TRUE
, 0, 0, 20, panelize_cols
, dialog_colors
, panelize_callback
, NULL
,
179 "[External panelize]", _("External panelize"), DLG_CENTER
);
181 /* add listbox to the dialogs */
183 add_widget (panelize_dlg
, groupbox_new (y
++, UX
, 12, panelize_cols
- UX
* 2, ""));
185 l_panelize
= listbox_new (y
, UX
+ 1, 10, panelize_cols
- UX
* 2 - 2, FALSE
, NULL
);
186 for (current
= panelize
; current
!= NULL
; current
= current
->next
)
187 listbox_add_item (l_panelize
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
, FALSE
);
188 listbox_select_entry (l_panelize
, listbox_search_text (l_panelize
, _("Other command")));
189 add_widget (panelize_dlg
, l_panelize
);
191 y
+= WIDGET (l_panelize
)->lines
+ 1;
192 add_widget (panelize_dlg
, label_new (y
++, UX
, _("Command")));
194 input_new (y
++, UX
, input_colors
, panelize_cols
- UX
* 2, "", "in",
195 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_COMMANDS
|
196 INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
| INPUT_COMPLETE_CD
|
197 INPUT_COMPLETE_SHELL_ESC
);
198 add_widget (panelize_dlg
, pname
);
202 add_widget (panelize_dlg
, hline_new (y
++, -1, -1));
204 x
= (panelize_cols
- blen
) / 2;
205 for (i
= 0; i
< G_N_ELEMENTS (panelize_but
); i
++)
209 b
= button_new (y
, x
,
210 panelize_but
[i
].ret_cmd
, panelize_but
[i
].flags
, panelize_but
[i
].text
, NULL
);
211 add_widget (panelize_dlg
, b
);
213 x
+= button_get_len (b
) + 1;
216 dlg_select_widget (l_panelize
);
219 /* --------------------------------------------------------------------------------------------- */
224 dlg_destroy (panelize_dlg
);
228 /* --------------------------------------------------------------------------------------------- */
231 add2panelize (char *label
, char *command
)
233 struct panelize
*current
, *old
;
237 while (current
&& strcmp (current
->label
, label
) <= 0)
240 current
= current
->next
;
245 panelize
= g_new (struct panelize
, 1);
246 panelize
->label
= label
;
247 panelize
->command
= command
;
248 panelize
->next
= current
;
252 struct panelize
*new;
253 new = g_new (struct panelize
, 1);
255 new->command
= command
;
261 /* --------------------------------------------------------------------------------------------- */
264 add2panelize_cmd (void)
266 if (pname
->buffer
!= NULL
&& *pname
->buffer
!= '\0')
270 label
= input_dialog (_("Add to external panelize"),
271 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD
, "",
272 INPUT_COMPLETE_NONE
);
273 if (label
== NULL
|| *label
== '\0')
276 add2panelize (label
, g_strdup (pname
->buffer
));
280 /* --------------------------------------------------------------------------------------------- */
283 remove_from_panelize (struct panelize
*entry
)
285 if (strcmp (entry
->label
, _("Other command")) != 0)
287 if (entry
== panelize
)
289 panelize
= panelize
->next
;
293 struct panelize
*current
= panelize
;
294 while (current
&& current
->next
!= entry
)
295 current
= current
->next
;
298 current
->next
= entry
->next
;
302 g_free (entry
->label
);
303 g_free (entry
->command
);
308 /* --------------------------------------------------------------------------------------------- */
311 do_external_panelize (char *command
)
313 int link_to_dir
, stale_link
;
315 dir_list
*list
= ¤t_panel
->dir
;
316 char line
[MC_MAXPATHLEN
];
321 external
= popen (command
, "r");
324 close_error_pipe (D_ERROR
, _("Cannot invoke command."));
327 /* Clear the counters and the directory list */
328 panel_clean_dir (current_panel
);
330 panelize_change_root (current_panel
->cwd_vpath
);
332 dir_list_init (list
);
337 if (fgets (line
, sizeof (line
), external
) == NULL
)
339 if (ferror (external
) && errno
== EINTR
)
344 if (line
[strlen (line
) - 1] == '\n')
345 line
[strlen (line
) - 1] = 0;
346 if (strlen (line
) < 1)
348 if (line
[0] == '.' && IS_PATH_SEP (line
[1]))
353 if (!handle_path (name
, &st
, &link_to_dir
, &stale_link
))
356 if (!dir_list_append (list
, name
, &st
, link_to_dir
!= 0, stale_link
!= 0))
359 file_mark (current_panel
, list
->len
- 1, 0);
361 if ((list
->len
& 31) == 0)
365 current_panel
->is_panelized
= TRUE
;
368 dir_list_init (list
);
369 else if (IS_PATH_SEP (list
->list
[0].fname
[0]))
371 vfs_path_t
*vpath_root
;
374 vpath_root
= vfs_path_from_str (PATH_SEP_STR
);
375 panel_set_cwd (current_panel
, vpath_root
);
376 ret
= mc_chdir (vpath_root
);
377 vfs_path_free (vpath_root
);
382 if (pclose (external
) < 0)
383 message (D_NORMAL
, _("External panelize"), _("Pipe close failed"));
384 close_error_pipe (D_NORMAL
, NULL
);
385 try_to_select (current_panel
, NULL
);
386 panel_re_sort (current_panel
);
390 /* --------------------------------------------------------------------------------------------- */
393 do_panelize_cd (WPanel
* panel
)
397 gboolean panelized_same
;
399 dir_list_clean (&panel
->dir
);
400 if (panelized_panel
.root_vpath
== NULL
)
401 panelize_change_root (current_panel
->cwd_vpath
);
403 if (panelized_panel
.list
.len
< 1)
404 dir_list_init (&panelized_panel
.list
);
405 else if (panelized_panel
.list
.len
> panel
->dir
.size
)
406 dir_list_grow (&panel
->dir
, panelized_panel
.list
.len
- panel
->dir
.size
);
409 list
->len
= panelized_panel
.list
.len
;
410 panel
->is_panelized
= TRUE
;
412 panelized_same
= vfs_path_equal (panelized_panel
.root_vpath
, panel
->cwd_vpath
);
414 for (i
= 0; i
< panelized_panel
.list
.len
; i
++)
416 if (panelized_same
|| DIR_IS_DOTDOT (panelized_panel
.list
.list
[i
].fname
))
418 list
->list
[i
].fnamelen
= panelized_panel
.list
.list
[i
].fnamelen
;
419 list
->list
[i
].fname
= g_strndup (panelized_panel
.list
.list
[i
].fname
,
420 panelized_panel
.list
.list
[i
].fnamelen
);
424 vfs_path_t
*tmp_vpath
;
428 vfs_path_append_new (panelized_panel
.root_vpath
, panelized_panel
.list
.list
[i
].fname
,
430 fname
= vfs_path_as_str (tmp_vpath
);
431 list
->list
[i
].fnamelen
= strlen (fname
);
432 list
->list
[i
].fname
= g_strndup (fname
, list
->list
[i
].fnamelen
);
433 vfs_path_free (tmp_vpath
);
435 list
->list
[i
].f
.link_to_dir
= panelized_panel
.list
.list
[i
].f
.link_to_dir
;
436 list
->list
[i
].f
.stale_link
= panelized_panel
.list
.list
[i
].f
.stale_link
;
437 list
->list
[i
].f
.dir_size_computed
= panelized_panel
.list
.list
[i
].f
.dir_size_computed
;
438 list
->list
[i
].f
.marked
= panelized_panel
.list
.list
[i
].f
.marked
;
439 list
->list
[i
].st
= panelized_panel
.list
.list
[i
].st
;
440 list
->list
[i
].sort_key
= panelized_panel
.list
.list
[i
].sort_key
;
441 list
->list
[i
].second_sort_key
= panelized_panel
.list
.list
[i
].second_sort_key
;
443 try_to_select (panel
, NULL
);
446 /* --------------------------------------------------------------------------------------------- */
447 /*** public functions ****************************************************************************/
448 /* --------------------------------------------------------------------------------------------- */
451 * Change root directory of panelized content.
452 * @param new_root - object with new path.
455 panelize_change_root (const vfs_path_t
* new_root
)
457 vfs_path_free (panelized_panel
.root_vpath
);
458 panelized_panel
.root_vpath
= vfs_path_clone (new_root
);
461 /* --------------------------------------------------------------------------------------------- */
464 panelize_save_panel (WPanel
* panel
)
467 dir_list
*list
= &panel
->dir
;
469 panelize_change_root (current_panel
->cwd_vpath
);
471 if (panelized_panel
.list
.len
> 0)
472 dir_list_clean (&panelized_panel
.list
);
473 if (panel
->dir
.len
== 0)
476 if (panel
->dir
.len
> panelized_panel
.list
.size
)
477 dir_list_grow (&panelized_panel
.list
, panel
->dir
.len
- panelized_panel
.list
.size
);
478 panelized_panel
.list
.len
= panel
->dir
.len
;
480 for (i
= 0; i
< panel
->dir
.len
; i
++)
482 panelized_panel
.list
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
483 panelized_panel
.list
.list
[i
].fname
=
484 g_strndup (list
->list
[i
].fname
, list
->list
[i
].fnamelen
);
485 panelized_panel
.list
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
486 panelized_panel
.list
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
487 panelized_panel
.list
.list
[i
].f
.dir_size_computed
= list
->list
[i
].f
.dir_size_computed
;
488 panelized_panel
.list
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
489 panelized_panel
.list
.list
[i
].st
= list
->list
[i
].st
;
490 panelized_panel
.list
.list
[i
].sort_key
= list
->list
[i
].sort_key
;
491 panelized_panel
.list
.list
[i
].second_sort_key
= list
->list
[i
].second_sort_key
;
495 /* --------------------------------------------------------------------------------------------- */
498 cd_panelize_cmd (void)
500 if (!SELECTED_IS_PANEL
)
501 set_display_type (MENU_PANEL_IDX
, view_listing
);
503 do_panelize_cd (PANEL (get_panel_widget (MENU_PANEL_IDX
)));
506 /* --------------------------------------------------------------------------------------------- */
509 external_panelize (void)
513 if (!vfs_current_is_local ())
515 message (D_ERROR
, MSG_ERROR
, _("Cannot run external panelize in a non-local directory"));
521 /* display file info */
522 tty_setcolor (SELECTED_COLOR
);
524 switch (dlg_run (panelize_dlg
))
535 struct panelize
*entry
;
537 listbox_get_current (l_panelize
, NULL
, (void **) &entry
);
538 remove_from_panelize (entry
);
543 target
= pname
->buffer
;
544 if (target
!= NULL
&& *target
)
546 char *cmd
= g_strdup (target
);
548 dlg_destroy (panelize_dlg
);
549 do_external_panelize (cmd
);
563 /* --------------------------------------------------------------------------------------------- */
570 keys
= mc_config_get_keys (mc_main_config
, panelize_section
, NULL
);
572 add2panelize (g_strdup (_("Other command")), g_strdup (""));
576 add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
577 add2panelize (g_strdup (_("Find rejects after patching")),
578 g_strdup ("find . -name \\*.rej -print"));
579 add2panelize (g_strdup (_("Find *.orig after patching")),
580 g_strdup ("find . -name \\*.orig -print"));
581 add2panelize (g_strdup (_("Find SUID and SGID programs")),
583 ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 -a -perm /01 \\) \\) -print"));
590 conv
= str_crt_conv_from ("UTF-8");
592 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
596 if (mc_global
.utf8_display
|| conv
== INVALID_CONV
)
597 buffer
= g_string_new (*profile_keys
);
600 buffer
= g_string_new ("");
601 if (str_convert (conv
, *profile_keys
, buffer
) == ESTR_FAILURE
)
602 g_string_assign (buffer
, *profile_keys
);
605 add2panelize (g_string_free (buffer
, FALSE
),
606 mc_config_get_string (mc_main_config
, panelize_section
, *profile_keys
,
610 str_close_conv (conv
);
616 /* --------------------------------------------------------------------------------------------- */
621 struct panelize
*current
= panelize
;
623 mc_config_del_group (mc_main_config
, panelize_section
);
624 for (; current
; current
= current
->next
)
626 if (strcmp (current
->label
, _("Other command")))
627 mc_config_set_string (mc_main_config
,
628 panelize_section
, current
->label
, current
->command
);
632 /* --------------------------------------------------------------------------------------------- */
637 struct panelize
*current
= panelize
;
638 struct panelize
*next
;
640 for (; current
; current
= next
)
642 next
= current
->next
;
643 g_free (current
->label
);
644 g_free (current
->command
);
649 /* --------------------------------------------------------------------------------------------- */