4 Copyright (C) 1995-2020
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") }
149 struct panelize
*current
;
156 i
= G_N_ELEMENTS (panelize_but
);
157 blen
= i
- 1; /* gaps between buttons */
161 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
)
168 panelize_cols
= COLS
- 6;
169 panelize_cols
= MAX (panelize_cols
, blen
+ 4);
172 dlg_create (TRUE
, 0, 0, 20, panelize_cols
, WPOS_CENTER
, FALSE
, dialog_colors
,
173 panelize_callback
, NULL
, "[External panelize]", _("External panelize"));
174 g
= GROUP (panelize_dlg
);
176 /* add listbox to the dialogs */
178 group_add_widget (g
, groupbox_new (y
++, UX
, 12, panelize_cols
- UX
* 2, ""));
180 l_panelize
= listbox_new (y
, UX
+ 1, 10, panelize_cols
- UX
* 2 - 2, FALSE
, NULL
);
181 for (current
= panelize
; current
!= NULL
; current
= current
->next
)
182 listbox_add_item (l_panelize
, LISTBOX_APPEND_AT_END
, 0, current
->label
, current
, FALSE
);
183 listbox_select_entry (l_panelize
, listbox_search_text (l_panelize
, _("Other command")));
184 group_add_widget (g
, l_panelize
);
186 y
+= WIDGET (l_panelize
)->lines
+ 1;
187 group_add_widget (g
, label_new (y
++, UX
, _("Command")));
189 input_new (y
++, UX
, input_colors
, panelize_cols
- UX
* 2, "", "in",
190 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_COMMANDS
|
191 INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
| INPUT_COMPLETE_CD
|
192 INPUT_COMPLETE_SHELL_ESC
);
193 group_add_widget (g
, pname
);
195 group_add_widget (g
, hline_new (y
++, -1, -1));
197 x
= (panelize_cols
- blen
) / 2;
198 for (i
= 0; i
< G_N_ELEMENTS (panelize_but
); i
++)
202 b
= button_new (y
, x
,
203 panelize_but
[i
].ret_cmd
, panelize_but
[i
].flags
, panelize_but
[i
].text
, NULL
);
204 group_add_widget (g
, b
);
206 x
+= button_get_len (b
) + 1;
209 widget_select (WIDGET (l_panelize
));
212 /* --------------------------------------------------------------------------------------------- */
217 dlg_destroy (panelize_dlg
);
221 /* --------------------------------------------------------------------------------------------- */
224 add2panelize (char *label
, char *command
)
226 struct panelize
*current
;
227 struct panelize
*old
= NULL
;
230 while (current
!= NULL
&& strcmp (current
->label
, label
) <= 0)
233 current
= current
->next
;
238 panelize
= g_new (struct panelize
, 1);
239 panelize
->label
= label
;
240 panelize
->command
= command
;
241 panelize
->next
= current
;
245 struct panelize
*new;
247 new = g_new (struct panelize
, 1);
249 new->command
= command
;
255 /* --------------------------------------------------------------------------------------------- */
258 add2panelize_cmd (void)
260 if (!input_is_empty (pname
))
264 label
= input_dialog (_("Add to external panelize"),
265 _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD
, "",
266 INPUT_COMPLETE_NONE
);
267 if (label
== NULL
|| *label
== '\0')
270 add2panelize (label
, g_strdup (pname
->buffer
));
274 /* --------------------------------------------------------------------------------------------- */
277 remove_from_panelize (struct panelize
*entry
)
279 if (strcmp (entry
->label
, _("Other command")) != 0)
281 if (entry
== panelize
)
282 panelize
= panelize
->next
;
285 struct panelize
*current
= panelize
;
287 while (current
!= NULL
&& current
->next
!= entry
)
288 current
= current
->next
;
291 current
->next
= entry
->next
;
294 g_free (entry
->label
);
295 g_free (entry
->command
);
300 /* --------------------------------------------------------------------------------------------- */
303 do_external_panelize (char *command
)
305 dir_list
*list
= ¤t_panel
->dir
;
309 external
= popen (command
, "r");
310 if (external
== NULL
)
312 close_error_pipe (D_ERROR
, _("Cannot invoke command."));
315 /* Clear the counters and the directory list */
316 panel_clean_dir (current_panel
);
318 panelize_change_root (current_panel
->cwd_vpath
);
320 dir_list_init (list
);
324 char line
[MC_MAXPATHLEN
];
327 gboolean link_to_dir
, stale_link
;
331 if (fgets (line
, sizeof (line
), external
) == NULL
)
333 if (ferror (external
) != 0 && errno
== EINTR
)
339 if (line
[len
- 1] == '\n')
340 line
[len
- 1] = '\0';
345 if (line
[0] == '.' && IS_PATH_SEP (line
[1]))
348 if (!handle_path (name
, &st
, &link_to_dir
, &stale_link
))
351 if (!dir_list_append (list
, name
, &st
, link_to_dir
, stale_link
))
354 file_mark (current_panel
, list
->len
- 1, 0);
356 if ((list
->len
& 31) == 0)
360 current_panel
->is_panelized
= TRUE
;
361 panelize_absolutize_if_needed (current_panel
);
363 if (pclose (external
) < 0)
364 message (D_NORMAL
, _("External panelize"), _("Pipe close failed"));
365 close_error_pipe (D_NORMAL
, NULL
);
366 try_to_select (current_panel
, NULL
);
367 panel_re_sort (current_panel
);
371 /* --------------------------------------------------------------------------------------------- */
374 do_panelize_cd (WPanel
* panel
)
378 gboolean panelized_same
;
380 dir_list_clean (&panel
->dir
);
381 if (panelized_panel
.root_vpath
== NULL
)
382 panelize_change_root (current_panel
->cwd_vpath
);
384 if (panelized_panel
.list
.len
< 1)
385 dir_list_init (&panelized_panel
.list
);
386 else if (panelized_panel
.list
.len
> panel
->dir
.size
)
387 dir_list_grow (&panel
->dir
, panelized_panel
.list
.len
- panel
->dir
.size
);
390 list
->len
= panelized_panel
.list
.len
;
392 panelized_same
= vfs_path_equal (panelized_panel
.root_vpath
, panel
->cwd_vpath
);
394 for (i
= 0; i
< panelized_panel
.list
.len
; i
++)
396 if (panelized_same
|| DIR_IS_DOTDOT (panelized_panel
.list
.list
[i
].fname
))
398 list
->list
[i
].fnamelen
= panelized_panel
.list
.list
[i
].fnamelen
;
399 list
->list
[i
].fname
= g_strndup (panelized_panel
.list
.list
[i
].fname
,
400 panelized_panel
.list
.list
[i
].fnamelen
);
404 vfs_path_t
*tmp_vpath
;
408 vfs_path_append_new (panelized_panel
.root_vpath
, panelized_panel
.list
.list
[i
].fname
,
410 fname
= vfs_path_as_str (tmp_vpath
);
411 list
->list
[i
].fnamelen
= strlen (fname
);
412 list
->list
[i
].fname
= g_strndup (fname
, list
->list
[i
].fnamelen
);
413 vfs_path_free (tmp_vpath
);
415 list
->list
[i
].f
.link_to_dir
= panelized_panel
.list
.list
[i
].f
.link_to_dir
;
416 list
->list
[i
].f
.stale_link
= panelized_panel
.list
.list
[i
].f
.stale_link
;
417 list
->list
[i
].f
.dir_size_computed
= panelized_panel
.list
.list
[i
].f
.dir_size_computed
;
418 list
->list
[i
].f
.marked
= panelized_panel
.list
.list
[i
].f
.marked
;
419 list
->list
[i
].st
= panelized_panel
.list
.list
[i
].st
;
420 list
->list
[i
].sort_key
= panelized_panel
.list
.list
[i
].sort_key
;
421 list
->list
[i
].second_sort_key
= panelized_panel
.list
.list
[i
].second_sort_key
;
424 panel
->is_panelized
= TRUE
;
425 panelize_absolutize_if_needed (panel
);
427 try_to_select (panel
, NULL
);
430 /* --------------------------------------------------------------------------------------------- */
431 /*** public functions ****************************************************************************/
432 /* --------------------------------------------------------------------------------------------- */
435 * Change root directory of panelized content.
436 * @param new_root - object with new path.
439 panelize_change_root (const vfs_path_t
* new_root
)
441 vfs_path_free (panelized_panel
.root_vpath
);
442 panelized_panel
.root_vpath
= vfs_path_clone (new_root
);
445 /* --------------------------------------------------------------------------------------------- */
448 panelize_save_panel (WPanel
* panel
)
451 dir_list
*list
= &panel
->dir
;
453 panelize_change_root (current_panel
->cwd_vpath
);
455 if (panelized_panel
.list
.len
> 0)
456 dir_list_clean (&panelized_panel
.list
);
457 if (panel
->dir
.len
== 0)
460 if (panel
->dir
.len
> panelized_panel
.list
.size
)
461 dir_list_grow (&panelized_panel
.list
, panel
->dir
.len
- panelized_panel
.list
.size
);
462 panelized_panel
.list
.len
= panel
->dir
.len
;
464 for (i
= 0; i
< panel
->dir
.len
; i
++)
466 panelized_panel
.list
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
467 panelized_panel
.list
.list
[i
].fname
=
468 g_strndup (list
->list
[i
].fname
, list
->list
[i
].fnamelen
);
469 panelized_panel
.list
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
470 panelized_panel
.list
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
471 panelized_panel
.list
.list
[i
].f
.dir_size_computed
= list
->list
[i
].f
.dir_size_computed
;
472 panelized_panel
.list
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
473 panelized_panel
.list
.list
[i
].st
= list
->list
[i
].st
;
474 panelized_panel
.list
.list
[i
].sort_key
= list
->list
[i
].sort_key
;
475 panelized_panel
.list
.list
[i
].second_sort_key
= list
->list
[i
].second_sort_key
;
479 /* --------------------------------------------------------------------------------------------- */
482 * Conditionally switches a panel's directory to "/" (root).
484 * If a panelized panel's listing contain absolute paths, this function
485 * sets the panel's directory to "/". Otherwise it does nothing.
489 * This makes tokenized strings like "%d/%p" work. This also makes other
490 * places work where such naive concatenation is done in code (e.g., when
491 * pressing ctrl+shift+enter, for CK_PutCurrentFullSelected).
495 * You should always call this function after you populate the listing
496 * of a panelized panel.
499 panelize_absolutize_if_needed (WPanel
* panel
)
501 const dir_list
*const list
= &panel
->dir
;
503 /* Note: We don't support mixing of absolute and relative paths, which is
504 * why it's ok for us to check only the 1st entry. */
505 if (list
->len
> 1 && g_path_is_absolute (list
->list
[1].fname
))
509 root
= vfs_path_from_str (PATH_SEP_STR
);
510 panel_set_cwd (panel
, root
);
511 if (panel
== current_panel
)
513 vfs_path_free (root
);
517 /* --------------------------------------------------------------------------------------------- */
520 cd_panelize_cmd (void)
522 if (!SELECTED_IS_PANEL
)
523 create_panel (MENU_PANEL_IDX
, view_listing
);
525 do_panelize_cd (PANEL (get_panel_widget (MENU_PANEL_IDX
)));
528 /* --------------------------------------------------------------------------------------------- */
531 external_panelize (void)
533 if (!vfs_current_is_local ())
535 message (D_ERROR
, MSG_ERROR
, _("Cannot run external panelize in a non-local directory"));
541 /* display file info */
542 tty_setcolor (SELECTED_COLOR
);
544 switch (dlg_run (panelize_dlg
))
555 struct panelize
*entry
;
557 listbox_get_current (l_panelize
, NULL
, (void **) &entry
);
558 remove_from_panelize (entry
);
563 if (!input_is_empty (pname
))
567 cmd
= g_strdup (pname
->buffer
);
568 dlg_destroy (panelize_dlg
);
569 do_external_panelize (cmd
);
583 /* --------------------------------------------------------------------------------------------- */
590 keys
= mc_config_get_keys (mc_global
.main_config
, panelize_section
, NULL
);
592 add2panelize (g_strdup (_("Other command")), g_strdup (""));
596 add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
597 add2panelize (g_strdup (_("Find rejects after patching")),
598 g_strdup ("find . -name \\*.rej -print"));
599 add2panelize (g_strdup (_("Find *.orig after patching")),
600 g_strdup ("find . -name \\*.orig -print"));
601 add2panelize (g_strdup (_("Find SUID and SGID programs")),
603 ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 -a -perm /01 \\) \\) -print"));
610 conv
= str_crt_conv_from ("UTF-8");
612 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
616 if (mc_global
.utf8_display
|| conv
== INVALID_CONV
)
617 buffer
= g_string_new (*profile_keys
);
620 buffer
= g_string_new ("");
621 if (str_convert (conv
, *profile_keys
, buffer
) == ESTR_FAILURE
)
622 g_string_assign (buffer
, *profile_keys
);
625 add2panelize (g_string_free (buffer
, FALSE
),
626 mc_config_get_string (mc_global
.main_config
, panelize_section
,
630 str_close_conv (conv
);
636 /* --------------------------------------------------------------------------------------------- */
641 struct panelize
*current
;
643 mc_config_del_group (mc_global
.main_config
, panelize_section
);
645 for (current
= panelize
; current
!= NULL
; current
= current
->next
)
646 if (strcmp (current
->label
, _("Other command")) != 0)
647 mc_config_set_string (mc_global
.main_config
,
648 panelize_section
, current
->label
, current
->command
);
651 /* --------------------------------------------------------------------------------------------- */
656 struct panelize
*current
, *next
;
658 for (current
= panelize
; current
!= NULL
; current
= next
)
660 next
= current
->next
;
661 g_free (current
->label
);
662 g_free (current
->command
);
667 /* --------------------------------------------------------------------------------------------- */