4 Copyright (C) 1995-2025
5 Free Software Foundation, Inc.
10 Andrew Borodin <aborodin@vmail.ru> 2011-2023
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 <https://www.gnu.org/licenses/>.
29 * \brief Source: External panelization module
34 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/vfs/vfs.h"
39 #include "lib/mcconfig.h" // Load/save directories panelize
40 #include "lib/strutil.h"
41 #include "lib/widget.h"
42 #include "lib/util.h" // mc_pipe_t
44 #include "src/history.h"
46 #include "filemanager.h" // current_panel
47 #include "layout.h" // rotate_dash()
48 #include "panel.h" // WPanel, dir.h
52 /*** global variables ****************************************************************************/
54 /*** file scope macro definitions ****************************************************************/
60 #define B_REMOVE (B_USER + 1)
62 /*** file scope type declarations ****************************************************************/
70 /*** forward declarations (file scope functions) *************************************************/
72 /*** file scope variables ************************************************************************/
74 static WListbox
*l_panelize
;
75 static WDialog
*panelize_dlg
;
76 static int last_listitem
;
78 static GSList
*panelize
= NULL
;
80 static const char *panelize_section
= "Panelize";
82 /* --------------------------------------------------------------------------------------------- */
83 /*** file scope functions ************************************************************************/
84 /* --------------------------------------------------------------------------------------------- */
87 panelize_entry_free (gpointer data
)
89 panelize_entry_t
*entry
= (panelize_entry_t
*) data
;
91 g_free (entry
->command
);
92 g_free (entry
->label
);
96 /* --------------------------------------------------------------------------------------------- */
99 panelize_entry_cmp_by_label (gconstpointer a
, gconstpointer b
)
101 const panelize_entry_t
*entry
= (const panelize_entry_t
*) a
;
102 const char *label
= (const char *) b
;
104 return strcmp (entry
->label
, label
);
107 /* --------------------------------------------------------------------------------------------- */
110 panelize_entry_add_to_listbox (gpointer data
, gpointer user_data
)
112 panelize_entry_t
*entry
= (panelize_entry_t
*) data
;
116 listbox_add_item (l_panelize
, LISTBOX_APPEND_AT_END
, 0, entry
->label
, entry
, FALSE
);
119 /* --------------------------------------------------------------------------------------------- */
122 update_command (void)
124 if (l_panelize
->current
!= last_listitem
)
126 panelize_entry_t
*data
= NULL
;
128 last_listitem
= l_panelize
->current
;
129 listbox_get_current (l_panelize
, NULL
, (void **) &data
);
130 input_assign_text (pname
, data
->command
);
132 input_update (pname
, TRUE
);
136 /* --------------------------------------------------------------------------------------------- */
139 panelize_callback (Widget
*w
, Widget
*sender
, widget_msg_t msg
, int parm
, void *data
)
144 group_default_callback (w
, NULL
, MSG_INIT
, 0, NULL
);
147 case MSG_NOTIFY
: // MSG_NOTIFY is fired by the listbox to tell us the item has changed.
152 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
156 /* --------------------------------------------------------------------------------------------- */
159 external_panelize_init (void)
164 button_flags_t flags
;
167 { B_ENTER
, DEFPUSH_BUTTON
, N_ ("Pane&lize") },
168 { B_REMOVE
, NORMAL_BUTTON
, N_ ("&Remove") },
169 { B_ADD
, NORMAL_BUTTON
, N_ ("&Add new") },
170 { B_CANCEL
, NORMAL_BUTTON
, N_ ("&Cancel") },
184 i
= G_N_ELEMENTS (panelize_but
);
185 blen
= i
- 1; // gaps between buttons
189 panelize_but
[i
].text
= _ (panelize_but
[i
].text
);
191 blen
+= str_term_width1 (panelize_but
[i
].text
) + 3 + 1;
192 if (panelize_but
[i
].flags
== DEFPUSH_BUTTON
)
196 panelize_cols
= COLS
- 6;
197 panelize_cols
= MAX (panelize_cols
, blen
+ 4);
200 dlg_create (TRUE
, 0, 0, 20, panelize_cols
, WPOS_CENTER
, FALSE
, dialog_colors
,
201 panelize_callback
, NULL
, "[External panelize]", _ ("External panelize"));
202 g
= GROUP (panelize_dlg
);
204 // add listbox to the dialogs
206 group_add_widget (g
, groupbox_new (y
++, UX
, 12, panelize_cols
- UX
* 2, ""));
208 l_panelize
= listbox_new (y
, UX
+ 1, 10, panelize_cols
- UX
* 2 - 2, FALSE
, NULL
);
209 g_slist_foreach (panelize
, panelize_entry_add_to_listbox
, NULL
);
210 listbox_set_current (l_panelize
, listbox_search_text (l_panelize
, _ ("Other command")));
211 group_add_widget (g
, l_panelize
);
213 y
+= WIDGET (l_panelize
)->rect
.lines
+ 1;
214 group_add_widget (g
, label_new (y
++, UX
, _ ("Command")));
215 pname
= input_new (y
++, UX
, input_colors
, panelize_cols
- UX
* 2, "", "in",
216 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_COMMANDS
217 | INPUT_COMPLETE_VARIABLES
| INPUT_COMPLETE_USERNAMES
| INPUT_COMPLETE_CD
218 | INPUT_COMPLETE_SHELL_ESC
);
219 group_add_widget (g
, pname
);
221 group_add_widget (g
, hline_new (y
++, -1, -1));
223 x
= (panelize_cols
- blen
) / 2;
224 for (i
= 0; i
< G_N_ELEMENTS (panelize_but
); i
++)
228 b
= button_new (y
, x
, panelize_but
[i
].ret_cmd
, panelize_but
[i
].flags
, panelize_but
[i
].text
,
230 group_add_widget (g
, b
);
232 x
+= button_get_len (b
) + 1;
235 widget_select (WIDGET (l_panelize
));
238 /* --------------------------------------------------------------------------------------------- */
241 external_panelize_done (void)
243 widget_destroy (WIDGET (panelize_dlg
));
247 /* --------------------------------------------------------------------------------------------- */
250 add2panelize (char *label
, char *command
)
252 panelize_entry_t
*entry
;
254 entry
= g_try_new (panelize_entry_t
, 1);
257 entry
->label
= label
;
258 entry
->command
= command
;
260 panelize
= g_slist_insert_sorted (panelize
, entry
, panelize_entry_cmp_by_label
);
264 /* --------------------------------------------------------------------------------------------- */
267 add2panelize_cmd (void)
269 if (!input_is_empty (pname
))
273 label
= input_dialog (_ ("Add to external panelize"), _ ("Enter command label:"),
274 MC_HISTORY_FM_PANELIZE_ADD
, "", INPUT_COMPLETE_NONE
);
275 if (label
== NULL
|| *label
== '\0')
278 add2panelize (label
, input_get_text (pname
));
282 /* --------------------------------------------------------------------------------------------- */
285 remove_from_panelize (panelize_entry_t
*entry
)
287 if (strcmp (entry
->label
, _ ("Other command")) != 0)
289 panelize
= g_slist_remove (panelize
, entry
);
290 panelize_entry_free (entry
);
294 /* --------------------------------------------------------------------------------------------- */
297 do_external_panelize (const char *command
)
299 dir_list
*list
= ¤t_panel
->dir
;
301 GError
*error
= NULL
;
302 GString
*remain_file_name
= NULL
;
304 external
= mc_popen (command
, TRUE
, TRUE
, &error
);
305 if (external
== NULL
)
307 message (D_ERROR
, _ ("External panelize"), "%s", error
->message
);
308 g_error_free (error
);
312 // Clear the counters and the directory list
313 panel_clean_dir (current_panel
);
315 panel_panelize_change_root (current_panel
, current_panel
->cwd_vpath
);
317 dir_list_init (list
);
324 // init buffers before call of mc_pread()
325 external
->out
.len
= MC_PIPE_BUFSIZE
;
326 external
->err
.len
= MC_PIPE_BUFSIZE
;
327 external
->err
.null_term
= TRUE
;
329 mc_pread (external
, &error
);
333 message (D_ERROR
, MSG_ERROR
, _ ("External panelize:\n%s"), error
->message
);
334 g_error_free (error
);
338 if (external
->err
.len
> 0)
339 message (D_ERROR
, MSG_ERROR
, _ ("External panelize:\n%s"), external
->err
.buf
);
341 if (external
->out
.len
== MC_PIPE_STREAM_EOF
)
344 if (external
->out
.len
== 0)
347 if (external
->out
.len
== MC_PIPE_ERROR_READ
)
349 message (D_ERROR
, MSG_ERROR
,
350 _ ("External panelize:\nfailed to read data from child stdout:\n%s"),
351 unix_error_string (external
->out
.error
));
357 while (ok
&& (line
= mc_pstream_get_string (&external
->out
)) != NULL
)
360 gboolean link_to_dir
, stale_link
;
363 // handle a \n-separated file list
365 if (line
->str
[line
->len
- 1] == '\n')
367 // entire file name or last chunk
369 g_string_truncate (line
, line
->len
- 1);
371 // join filename chunks
372 if (remain_file_name
!= NULL
)
374 g_string_append_len (remain_file_name
, line
->str
, line
->len
);
375 g_string_free (line
, TRUE
);
376 line
= remain_file_name
;
377 remain_file_name
= NULL
;
382 // first or middle chunk of file name
384 if (remain_file_name
== NULL
)
385 remain_file_name
= line
;
388 g_string_append_len (remain_file_name
, line
->str
, line
->len
);
389 g_string_free (line
, TRUE
);
397 if (name
[0] == '.' && IS_PATH_SEP (name
[1]))
400 if (handle_path (name
, &st
, &link_to_dir
, &stale_link
))
402 ok
= dir_list_append (list
, name
, &st
, link_to_dir
, stale_link
);
406 file_mark (current_panel
, list
->len
- 1, 0);
408 if ((list
->len
& 31) == 0)
413 g_string_free (line
, TRUE
);
417 if (remain_file_name
!= NULL
)
418 g_string_free (remain_file_name
, TRUE
);
420 mc_pclose (external
, NULL
);
422 current_panel
->is_panelized
= TRUE
;
423 panel_panelize_absolutize_if_needed (current_panel
);
425 panel_set_current_by_name (current_panel
, NULL
);
426 panel_re_sort (current_panel
);
430 /* --------------------------------------------------------------------------------------------- */
431 /*** public functions ****************************************************************************/
432 /* --------------------------------------------------------------------------------------------- */
435 external_panelize_cmd (void)
437 if (!vfs_current_is_local ())
439 message (D_ERROR
, MSG_ERROR
, _ ("Cannot run external panelize in a non-local directory"));
443 external_panelize_init ();
446 tty_setcolor (SELECTED_COLOR
);
448 switch (dlg_run (panelize_dlg
))
459 panelize_entry_t
*entry
;
461 listbox_get_current (l_panelize
, NULL
, (void **) &entry
);
462 remove_from_panelize (entry
);
467 if (!input_is_empty (pname
))
471 cmd
= input_get_text (pname
);
472 widget_destroy (WIDGET (panelize_dlg
));
473 do_external_panelize (cmd
);
484 external_panelize_done ();
487 /* --------------------------------------------------------------------------------------------- */
490 external_panelize_load (void)
494 keys
= mc_config_get_keys (mc_global
.main_config
, panelize_section
, NULL
);
496 add2panelize (g_strdup (_ ("Other command")), g_strdup (""));
500 add2panelize (g_strdup (_ ("Modified git files")), g_strdup ("git ls-files --modified"));
501 add2panelize (g_strdup (_ ("Find rejects after patching")),
502 g_strdup ("find . -name \\*.rej -print"));
503 add2panelize (g_strdup (_ ("Find *.orig after patching")),
504 g_strdup ("find . -name \\*.orig -print"));
505 add2panelize (g_strdup (_ ("Find SUID and SGID programs")),
506 g_strdup ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 "
507 "-a -perm /01 \\) \\) -print"));
514 conv
= str_crt_conv_from ("UTF-8");
516 for (profile_keys
= keys
; *profile_keys
!= NULL
; profile_keys
++)
520 if (mc_global
.utf8_display
|| conv
== INVALID_CONV
)
521 buffer
= g_string_new (*profile_keys
);
524 buffer
= g_string_new ("");
525 if (str_convert (conv
, *profile_keys
, buffer
) == ESTR_FAILURE
)
526 g_string_assign (buffer
, *profile_keys
);
530 g_string_free (buffer
, FALSE
),
531 mc_config_get_string (mc_global
.main_config
, panelize_section
, *profile_keys
, ""));
534 str_close_conv (conv
);
540 /* --------------------------------------------------------------------------------------------- */
543 external_panelize_save (void)
547 mc_config_del_group (mc_global
.main_config
, panelize_section
);
549 for (l
= panelize
; l
!= NULL
; l
= g_slist_next (l
))
551 panelize_entry_t
*current
= (panelize_entry_t
*) l
->data
;
553 if (strcmp (current
->label
, _ ("Other command")) != 0)
554 mc_config_set_string (mc_global
.main_config
, panelize_section
, current
->label
,
559 /* --------------------------------------------------------------------------------------------- */
562 external_panelize_free (void)
564 g_clear_slist (&panelize
, panelize_entry_free
);
568 /* --------------------------------------------------------------------------------------------- */