2019-07-14 18:21:53 +03:00
|
|
|
/*
|
|
|
|
Load and show history of edited and viewed files
|
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 2020-2024
|
2019-07-14 18:21:53 +03:00
|
|
|
Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by:
|
2022-05-01 10:43:33 +03:00
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2019-2022
|
2019-07-14 18:21:53 +03:00
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h> /* file functions */
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
|
|
|
|
#include "lib/fileloc.h" /* MC_FILEPOS_FILE */
|
|
|
|
#include "lib/mcconfig.h" /* mc_config_get_full_path() */
|
2019-07-27 14:41:36 +03:00
|
|
|
#include "lib/strutil.h" /* str_term_width1() */
|
|
|
|
#include "lib/util.h" /* backup functions */
|
2019-07-14 18:21:53 +03:00
|
|
|
|
|
|
|
#include "file_history.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
2019-07-27 14:41:36 +03:00
|
|
|
#define TMP_SUFFIX ".tmp"
|
|
|
|
|
2019-07-14 18:21:53 +03:00
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
2019-07-27 14:41:36 +03:00
|
|
|
typedef struct file_history_data_t
|
|
|
|
{
|
|
|
|
char *file_name;
|
|
|
|
char *file_pos;
|
|
|
|
} file_history_data_t;
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2019-07-14 18:21:53 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static GList *
|
|
|
|
file_history_list_read (void)
|
|
|
|
{
|
|
|
|
char *fn;
|
|
|
|
FILE *f;
|
|
|
|
char buf[MC_MAXPATHLEN + 100];
|
|
|
|
GList *file_list = NULL;
|
|
|
|
|
|
|
|
/* open file with positions */
|
|
|
|
fn = mc_config_get_full_path (MC_FILEPOS_FILE);
|
|
|
|
if (fn == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
f = fopen (fn, "r");
|
|
|
|
g_free (fn);
|
|
|
|
if (f == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (fgets (buf, sizeof (buf), f) != NULL)
|
|
|
|
{
|
|
|
|
char *s;
|
2019-07-27 14:41:36 +03:00
|
|
|
file_history_data_t *fhd;
|
|
|
|
size_t len;
|
2019-07-14 18:21:53 +03:00
|
|
|
|
|
|
|
s = strrchr (buf, ' ');
|
2019-07-27 14:41:36 +03:00
|
|
|
/* FIXME: saved file position info is present in filepos file */
|
|
|
|
fhd = g_new (file_history_data_t, 1);
|
|
|
|
fhd->file_name = g_strndup (buf, s - buf);
|
|
|
|
len = strlen (s + 1);
|
|
|
|
fhd->file_pos = g_strndup (s + 1, len - 1); /* ignore '\n' */
|
|
|
|
file_list = g_list_prepend (file_list, fhd);
|
2019-07-14 18:21:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose (f);
|
|
|
|
|
|
|
|
return file_list;
|
|
|
|
}
|
|
|
|
|
2019-07-27 14:41:36 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2024-06-01 21:12:14 +03:00
|
|
|
file_history_list_write (const GList *file_list)
|
2019-07-27 14:41:36 +03:00
|
|
|
{
|
|
|
|
char *fn;
|
|
|
|
FILE *f;
|
|
|
|
gboolean write_error = FALSE;
|
|
|
|
|
|
|
|
fn = mc_config_get_full_path (MC_FILEPOS_FILE);
|
|
|
|
if (fn == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mc_util_make_backup_if_possible (fn, TMP_SUFFIX);
|
|
|
|
|
|
|
|
f = fopen (fn, "w");
|
|
|
|
if (f != NULL)
|
|
|
|
{
|
|
|
|
GString *s;
|
|
|
|
|
|
|
|
s = g_string_sized_new (128);
|
|
|
|
|
|
|
|
for (; file_list != NULL && !write_error; file_list = g_list_next (file_list))
|
|
|
|
{
|
|
|
|
file_history_data_t *fhd = (file_history_data_t *) file_list->data;
|
|
|
|
|
|
|
|
g_string_append (s, fhd->file_name);
|
|
|
|
if (fhd->file_pos != NULL)
|
|
|
|
{
|
|
|
|
g_string_append_c (s, ' ');
|
|
|
|
g_string_append (s, fhd->file_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
write_error = (fprintf (f, "%s\n", s->str) < 0);
|
|
|
|
g_string_truncate (s, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_free (s, TRUE);
|
|
|
|
|
|
|
|
fclose (f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write_error)
|
|
|
|
mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
|
|
|
|
else
|
|
|
|
mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
|
|
|
|
|
|
|
|
g_free (fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2024-06-01 21:12:14 +03:00
|
|
|
file_history_create_item (history_descriptor_t *hd, void *data)
|
2019-07-27 14:41:36 +03:00
|
|
|
{
|
|
|
|
file_history_data_t *fhd = (file_history_data_t *) data;
|
|
|
|
size_t width;
|
|
|
|
|
|
|
|
width = str_term_width1 (fhd->file_name);
|
|
|
|
hd->max_width = MAX (width, hd->max_width);
|
|
|
|
|
|
|
|
listbox_add_item (hd->listbox, LISTBOX_APPEND_AT_END, 0, fhd->file_name, fhd->file_pos, TRUE);
|
|
|
|
/* fhd->file_pos is not copied, NULLize it to prevent double free */
|
|
|
|
fhd->file_pos = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void *
|
2024-06-01 21:12:14 +03:00
|
|
|
file_history_release_item (history_descriptor_t *hd, WLEntry *le)
|
2019-07-27 14:41:36 +03:00
|
|
|
{
|
|
|
|
file_history_data_t *fhd;
|
|
|
|
|
|
|
|
(void) hd;
|
|
|
|
|
|
|
|
fhd = g_new (file_history_data_t, 1);
|
|
|
|
fhd->file_name = le->text;
|
|
|
|
le->text = NULL;
|
|
|
|
fhd->file_pos = (char *) le->data;
|
|
|
|
le->data = NULL;
|
|
|
|
|
|
|
|
return fhd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
|
|
|
file_history_free_item (void *data)
|
|
|
|
{
|
|
|
|
file_history_data_t *fhd = (file_history_data_t *) data;
|
|
|
|
|
|
|
|
g_free (fhd->file_name);
|
|
|
|
g_free (fhd->file_pos);
|
|
|
|
g_free (fhd);
|
|
|
|
}
|
|
|
|
|
2019-07-14 18:21:53 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show file history and return the selected file
|
|
|
|
*
|
|
|
|
* @param w widget used for positioning of history window
|
|
|
|
* @param action to do with file (edit, view, etc)
|
|
|
|
*
|
|
|
|
* @return name of selected file, A newly allocated string.
|
|
|
|
*/
|
|
|
|
char *
|
2024-06-01 21:12:14 +03:00
|
|
|
show_file_history (const Widget *w, int *action)
|
2019-07-14 18:21:53 +03:00
|
|
|
{
|
|
|
|
GList *file_list;
|
2019-07-27 14:41:36 +03:00
|
|
|
size_t len;
|
2019-07-27 11:44:00 +03:00
|
|
|
history_descriptor_t hd;
|
2019-07-14 18:21:53 +03:00
|
|
|
|
|
|
|
file_list = file_history_list_read ();
|
|
|
|
if (file_list == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2019-07-27 14:41:36 +03:00
|
|
|
len = g_list_length (file_list);
|
|
|
|
|
2019-07-14 18:21:53 +03:00
|
|
|
file_list = g_list_last (file_list);
|
2019-07-27 14:41:36 +03:00
|
|
|
|
2022-05-01 10:43:33 +03:00
|
|
|
history_descriptor_init (&hd, w->rect.y, w->rect.x, file_list, 0);
|
2019-07-27 14:41:36 +03:00
|
|
|
/* redefine list-specific functions */
|
|
|
|
hd.create = file_history_create_item;
|
|
|
|
hd.release = file_history_release_item;
|
|
|
|
hd.free = file_history_free_item;
|
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
history_show (&hd);
|
2019-07-27 14:41:36 +03:00
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
hd.list = g_list_first (hd.list);
|
2019-07-27 14:41:36 +03:00
|
|
|
|
|
|
|
/* Has history cleaned up or not? */
|
|
|
|
if (len != g_list_length (hd.list))
|
2021-02-09 11:39:00 +03:00
|
|
|
{
|
|
|
|
hd.list = g_list_reverse (hd.list);
|
2019-07-27 14:41:36 +03:00
|
|
|
file_history_list_write (hd.list);
|
2021-02-09 11:39:00 +03:00
|
|
|
}
|
2019-07-27 14:41:36 +03:00
|
|
|
|
|
|
|
g_list_free_full (hd.list, (GDestroyNotify) file_history_free_item);
|
2019-07-14 18:21:53 +03:00
|
|
|
|
2019-07-27 11:44:00 +03:00
|
|
|
*action = hd.action;
|
|
|
|
|
|
|
|
return hd.text;
|
2019-07-14 18:21:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|