f858f0a9创建于 4月27日历史提交
/**
 * @file
 * Pattern functions
 *
 * @authors
 * Copyright (C) 2022-2023 Richard Russon <rich@flatcap.org>
 *
 * @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 pattern_functions Pattern functions
 *
 * Pattern functions
 */

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "functions.h"
#include "key/lib.h"
#include "menu/lib.h"
#include "pattern_data.h"

/**
 * op_generic_select_entry - Select the current entry - Implements ::pattern_function_t - @ingroup pattern_function_api
 */
static int op_generic_select_entry(struct PatternFunctionData *fdata,
                                   const struct KeyEvent *event)
{
  struct PatternData *pd = fdata->pd;
  const int index = menu_get_index(pd->menu);
  struct PatternEntry *entry = ARRAY_GET(&pd->entries, index);

  if (entry)
    buf_strcpy(pd->buf, entry->tag);

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

/**
 * op_quit - Quit this menu - Implements ::pattern_function_t - @ingroup pattern_function_api
 *
 * This function handles:
 * - OP_EXIT
 * - OP_QUIT
 */
static int op_quit(struct PatternFunctionData *fdata, const struct KeyEvent *event)
{
  struct PatternData *pd = fdata->pd;
  pd->done = true;
  pd->selection = false;
  return FR_SUCCESS;
}

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

/**
 * PatternFunctions - All the NeoMutt functions that the Pattern supports
 */
static const struct PatternFunction PatternFunctions[] = {
  // clang-format off
  { OP_GENERIC_SELECT_ENTRY,   op_generic_select_entry },
  { OP_QUIT,                   op_quit },
  { OP_EXIT,                   op_quit },
  { 0, NULL },
  // clang-format on
};

/**
 * pattern_function_dispatcher - Perform a Pattern function - Implements ::function_dispatcher_t - @ingroup dispatcher_api
 */
int pattern_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
{
  // The Dispatcher may be called on any Window in the Dialog
  struct MuttWindow *dlg = dialog_find(win);
  if (!event || !dlg || !dlg->wdata)
  {
    dispatcher_flush_on_error(FR_ERROR);
    return FR_ERROR;
  }

  const int op = event->op;
  struct Menu *menu = dlg->wdata;
  struct PatternData *pd = menu->mdata;
  if (!pd)
  {
    dispatcher_flush_on_error(FR_ERROR);
    return FR_ERROR;
  }

  struct PatternFunctionData fdata = {
    .n = NeoMutt,
    .pd = pd,
  };

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

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

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

  dispatcher_flush_on_error(rc);
  return rc;
}