Skip to content

Commit

Permalink
history: add function dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
flatcap committed Apr 30, 2022
1 parent dff8ce8 commit bb8307e
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Makefile.autosetup
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ $(PWD)/helpbar:
###############################################################################
# libhistory
LIBHISTORY= libhistory.a
LIBHISTORYOBJS= history/config.o history/dlg_history.o history/history.o
LIBHISTORYOBJS= history/config.o history/dlg_history.o history/functions.o \
history/history.o
CLEANFILES+= $(LIBHISTORY) $(LIBHISTORYOBJS)
ALLOBJS+= $(LIBHISTORYOBJS)

Expand Down
25 changes: 8 additions & 17 deletions history/dlg_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
*/

#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "mutt/lib.h"
Expand All @@ -64,6 +65,7 @@
#include "lib.h"
#include "menu/lib.h"
#include "format_flags.h"
#include "functions.h"
#include "keymap.h"
#include "mutt_logging.h"
#include "muttlib.h"
Expand Down Expand Up @@ -139,13 +141,15 @@ void dlg_select_history(char *buf, size_t buflen, char **matches, int match_coun
menu->mdata = matches;
menu->mdata_free = NULL; // Menu doesn't own the data

struct HistoryData hd = { false, false, buf, buflen,
menu, matches, match_count };
dlg->wdata = &hd;

// ---------------------------------------------------------------------------
// Event Loop
int op = OP_NULL;
int rc;
do
{
rc = FR_UNKNOWN;
menu_tagging_dispatcher(menu->win, op);
window_redraw(NULL);

Expand All @@ -160,26 +164,13 @@ void dlg_select_history(char *buf, size_t buflen, char **matches, int match_coun
}
mutt_clear_error();

switch (op)
{
case OP_GENERIC_SELECT_ENTRY:
{
const int index = menu_get_index(menu);
mutt_str_copy(buf, matches[index], buflen);
rc = FR_DONE;
break;
}

case OP_EXIT:
rc = FR_DONE;
break;
}
int rc = history_function_dispatcher(dlg, op);

if (rc == FR_UNKNOWN)
rc = menu_function_dispatcher(menu->win, op);
if (rc == FR_UNKNOWN)
rc = global_function_dispatcher(NULL, op);
} while (rc != FR_DONE);
} while (!hd.done);
// ---------------------------------------------------------------------------

simple_dialog_free(&dlg);
Expand Down
105 changes: 105 additions & 0 deletions history/functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* @file
* History functions
*
* @authors
* Copyright (C) 2022 Richard Russon <[email protected]>
*
* @copyright
* This program 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 2 of the License, or (at your option) any later
* version.
*
* This program 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/>.
*/

/**
* @page history_functions History functions
*
* History functions
*/

#include "config.h"
#include "mutt/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "functions.h"
#include "menu/lib.h"
#include "opcodes.h"

/**
* op_exit - Exit this menu - Implements ::history_function_t - @ingroup history_function_api
*/
static int op_exit(struct HistoryData *hd, int op)
{
hd->done = true;
hd->selection = false;
return FR_SUCCESS;
}

/**
* op_generic_select_entry - Select the current entry - Implements ::history_function_t - @ingroup history_function_api
*/
static int op_generic_select_entry(struct HistoryData *hd, int op)
{
const int index = menu_get_index(hd->menu);
mutt_str_copy(hd->buf, hd->matches[index], hd->buflen);

hd->done = true;
hd->selection = true;
return FR_SUCCESS;
}

// -----------------------------------------------------------------------------

/**
* HistoryFunctions - All the NeoMutt functions that the History supports
*/
struct HistoryFunction HistoryFunctions[] = {
// clang-format off
{ OP_EXIT, op_exit },
{ OP_GENERIC_SELECT_ENTRY, op_generic_select_entry },
{ 0, NULL },
// clang-format on
};

/**
* history_function_dispatcher - Perform a History function - Implements ::function_dispatcher_t - @ingroup dispatcher_api
*/
int history_function_dispatcher(struct MuttWindow *win, int op)
{
if (!win || !win->wdata)
return FR_UNKNOWN;

struct MuttWindow *dlg = dialog_find(win);
if (!dlg)
return FR_ERROR;

struct HistoryData *hd = dlg->wdata;

int rc = FR_UNKNOWN;
for (size_t i = 0; HistoryFunctions[i].op != OP_NULL; i++)
{
const struct HistoryFunction *fn = &HistoryFunctions[i];
if (fn->op == op)
{
rc = fn->function(hd, op);
break;
}
}

if (rc == FR_UNKNOWN) // Not our function
return rc;

const char *result = dispacher_get_retval_name(rc);
mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));

return rc;
}
70 changes: 70 additions & 0 deletions history/functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @file
* History functions
*
* @authors
* Copyright (C) 2022 Richard Russon <[email protected]>
*
* @copyright
* This program 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 2 of the License, or (at your option) any later
* version.
*
* This program 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/>.
*/

#ifndef MUTT_HISTORY_FUNCTIONS_H
#define MUTT_HISTORY_FUNCTIONS_H

#include <stdbool.h>
#include <stdio.h>

struct MuttWindow;

/**
* struct HistoryData - Data to pass to the History Functions
*/
struct HistoryData
{
bool done; ///< Should we close the Dialog?
bool selection; ///< Was a selection made?
char *buf; ///< Buffer for the results
size_t buflen; ///< Length of the results buffer
struct Menu *menu; ///< History Menu
char **matches; ///< History entries
int match_count; ///< Number of history entries
};

/**
* @defgroup history_function_api History Function API
* @ingroup dispatcher_api
*
* Prototype for a History Function
*
* @param menu Menu
* @param op Operation to perform, e.g. OP_GENERIC_SELECT_ENTRY
* @retval enum #FunctionRetval
*/
typedef int (*history_function_t)(struct HistoryData *pd, int op);

/**
* struct HistoryFunction - A NeoMutt function
*/
struct HistoryFunction
{
int op; ///< Op code, e.g. OP_GENERIC_SELECT_ENTRY
history_function_t function; ///< Function to call
};

extern struct HistoryFunction HistoryFunctions[];

int history_function_dispatcher(struct MuttWindow *win, int op);

#endif /* MUTT_HISTORY_FUNCTIONS_H */
1 change: 1 addition & 0 deletions history/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* | :------------------- | :--------------------------- |
* | history/config.c | @subpage history_config |
* | history/dlghistory.c | @subpage history_dlg_history |
* | history/functions.c | @subpage history_functions |
* | history/history.c | @subpage history_history |
*/

Expand Down

0 comments on commit bb8307e

Please sign in to comment.