Refactoring of show file history in the file manager.

This commit is contained in:
Andrew Borodin 2019-07-14 18:21:53 +03:00
parent 87428aa8ea
commit 00f1229af3
4 changed files with 139 additions and 32 deletions

View File

@ -72,6 +72,7 @@ libinternal_la_SOURCES = \
clipboard.c clipboard.h \
events_init.c events_init.h \
execute.c execute.h \
file_history.c file_history.h \
help.c help.h \
history.h \
keybind-defaults.c keybind-defaults.h \

114
src/file_history.c Normal file
View File

@ -0,0 +1,114 @@
/*
Load and show history of edited and viewed files
Copyright (C) 2019
Free Software Foundation, Inc.
Written by:
Andrew Borodin <aborodin@vmail.ru>, 2019.
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() */
#include "file_history.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** 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;
s = strrchr (buf, ' ');
if (s != NULL)
s = g_strndup (buf, s - buf);
else
s = g_strdup (buf);
file_list = g_list_prepend (file_list, s);
}
fclose (f);
return file_list;
}
/* --------------------------------------------------------------------------------------------- */
/*** 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 *
show_file_history (const Widget * w, int *action)
{
GList *file_list;
file_list = file_history_list_read ();
if (file_list == NULL)
return NULL;
file_list = g_list_last (file_list);
s = history_show (&file_list, w, 0, action);
file_list = g_list_first (file_list);
g_list_free_full (file_list, (GDestroyNotify) g_free);
return s;
}
/* --------------------------------------------------------------------------------------------- */

20
src/file_history.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef MC__FILE_HISTORY_H
#define MC__FILE_HISTORY_H
#include "lib/widget.h"
/*** typedefs(not structures) and defined constants **********************************************/
/*** enums ***************************************************************************************/
/*** structures declarations (and typedefs of structures)*****************************************/
/*** global variables defined in .c file *********************************************************/
char *show_file_history (const Widget * w, int *action);
/*** declarations of public functions ************************************************************/
/*** inline functions ****************************************************************************/
#endif /* MC__FILE_HISTORY_H */

View File

@ -87,6 +87,7 @@
#endif
#include "src/consaver/cons.saver.h" /* show_console_contents */
#include "src/file_history.h" /* show_file_history() */
#include "midnight.h"
@ -1006,38 +1007,12 @@ mc_maybe_editor_or_viewer (void)
/* --------------------------------------------------------------------------------------------- */
static void
show_editor_history_list (void)
show_editor_viewer_history (void)
{
char *fn;
FILE *f;
char buf[MC_MAXPATHLEN + 100];
GList *file_list = NULL;
char *s;
WPanel *panel = current_panel;
int act;
/* open file with positions */
fn = mc_config_get_full_path (MC_FILEPOS_FILE);
f = fopen (fn, "r");
g_free (fn);
if (f == NULL)
return;
while (fgets (buf, sizeof (buf), f) != NULL)
{
s = strrchr (buf, ' ');
if (s != NULL)
s = g_strndup (buf, s - buf);
else
s = g_strdup (buf);
file_list = g_list_prepend (file_list, s);
}
fclose (f);
file_list = g_list_last (file_list);
s = history_show (&file_list, WIDGET (panel), 0, &act);
s = show_file_history (WIDGET (midnight_dlg), &act);
if (s != NULL)
{
vfs_path_t *s_vpath;
@ -1069,9 +1044,6 @@ show_editor_history_list (void)
g_free (s);
vfs_path_free (s_vpath);
}
file_list = g_list_first (file_list);
g_list_free_full (file_list, (GDestroyNotify) g_free);
}
/* --------------------------------------------------------------------------------------------- */
@ -1451,7 +1423,7 @@ midnight_execute_cmd (Widget * sender, long command)
view_file_cmd ();
break;
case CK_EditorViewerHistory:
show_editor_history_list ();
show_editor_viewer_history ();
break;
case CK_Cancel:
/* don't close panels due to SIGINT */