1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/**
* @file
* Menu functions
*
* @authors
* Copyright (C) 2022 Pietro Cerutti <gahr@gahr.ch>
* 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 menu_functions Menu functions
*
* Menu functions
*/
#include "config.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "functions.h"
#include "lib.h"
#include "editor/lib.h"
#include "history/lib.h"
#include "protos.h"
#include "type.h"
extern char *SearchBuffers[];
#define MUTT_SEARCH_UP 1
#define MUTT_SEARCH_DOWN 2
/**
* search - Search a menu
* @param menu Menu to search
* @param op Search operation, e.g. OP_SEARCH_NEXT
* @retval >=0 Index of matching item
* @retval -1 Search failed, or was cancelled
*/
static int search(struct Menu *menu, int op)
{
int rc = -1;
int wrap = 0;
int search_dir;
regex_t re = { 0 };
struct Buffer *buf = buf_pool_get();
char *search_buf = ((menu->type < MENU_MAX)) ? SearchBuffers[menu->type] : NULL;
if (!(search_buf && *search_buf) || ((op != OP_SEARCH_NEXT) && (op != OP_SEARCH_OPPOSITE)))
{
buf_strcpy(buf, search_buf && (search_buf[0] != '\0') ? search_buf : "");
if ((mw_get_field(((op == OP_SEARCH) || (op == OP_SEARCH_NEXT)) ? _("Search for: ") : _("Reverse search for: "),
buf, MUTT_COMP_CLEAR, HC_OTHER, NULL, NULL) != 0) ||
buf_is_empty(buf))
{
goto done;
}
if (menu->type < MENU_MAX)
{
mutt_str_replace(&SearchBuffers[menu->type], buf_string(buf));
search_buf = SearchBuffers[menu->type];
}
menu->search_dir = ((op == OP_SEARCH) || (op == OP_SEARCH_NEXT)) ?
MUTT_SEARCH_DOWN :
MUTT_SEARCH_UP;
}
search_dir = (menu->search_dir == MUTT_SEARCH_UP) ? -1 : 1;
if (op == OP_SEARCH_OPPOSITE)
search_dir = -search_dir;
if (search_buf)
{
uint16_t flags = mutt_mb_is_lower(search_buf) ? REG_ICASE : 0;
rc = REG_COMP(&re, search_buf, REG_NOSUB | flags);
}
if (rc != 0)
{
regerror(rc, &re, buf->data, buf->dsize);
mutt_error("%s", buf_string(buf));
rc = -1;
goto done;
}
rc = menu->current + search_dir;
search_next:
if (wrap)
mutt_message(_("Search wrapped to top"));
while ((rc >= 0) && (rc < menu->max))
{
if (menu->search(menu, &re, rc) == 0)
{
regfree(&re);
goto done;
}
rc += search_dir;
}
const bool c_wrap_search = cs_subset_bool(menu->sub, "wrap_search");
if (c_wrap_search && (wrap++ == 0))
{
rc = (search_dir == 1) ? 0 : menu->max - 1;
goto search_next;
}
regfree(&re);
mutt_error(_("Not found"));
rc = -1;
done:
buf_pool_release(&buf);
return rc;
}
// -----------------------------------------------------------------------------
/**
* menu_movement - Handle all the common Menu movements - Implements ::menu_function_t - @ingroup menu_function_api
*/
static int menu_movement(struct Menu *menu, int op)
{
switch (op)
{
case OP_BOTTOM_PAGE:
menu_bottom_page(menu);
return FR_SUCCESS;
case OP_CURRENT_BOTTOM:
menu_current_bottom(menu);
return FR_SUCCESS;
case OP_CURRENT_MIDDLE:
menu_current_middle(menu);
return FR_SUCCESS;
case OP_CURRENT_TOP:
menu_current_top(menu);
return FR_SUCCESS;
case OP_FIRST_ENTRY:
menu_first_entry(menu);
return FR_SUCCESS;
case OP_HALF_DOWN:
menu_half_down(menu);
return FR_SUCCESS;
case OP_HALF_UP:
menu_half_up(menu);
return FR_SUCCESS;
case OP_LAST_ENTRY:
menu_last_entry(menu);
return FR_SUCCESS;
case OP_MIDDLE_PAGE:
menu_middle_page(menu);
return FR_SUCCESS;
case OP_NEXT_ENTRY:
menu_next_entry(menu);
return FR_SUCCESS;
case OP_NEXT_LINE:
menu_next_line(menu);
return FR_SUCCESS;
case OP_NEXT_PAGE:
menu_next_page(menu);
return FR_SUCCESS;
case OP_PREV_ENTRY:
menu_prev_entry(menu);
return FR_SUCCESS;
case OP_PREV_LINE:
menu_prev_line(menu);
return FR_SUCCESS;
case OP_PREV_PAGE:
menu_prev_page(menu);
return FR_SUCCESS;
case OP_TOP_PAGE:
menu_top_page(menu);
return FR_SUCCESS;
default:
return FR_UNKNOWN;
}
}
/**
* menu_search - Handle Menu searching - Implements ::menu_function_t - @ingroup menu_function_api
*/
static int menu_search(struct Menu *menu, int op)
{
if (menu->search)
{
int index = search(menu, op);
if (index != -1)
menu_set_index(menu, index);
}
return FR_SUCCESS;
}
/**
* op_help - Show the help screen - Implements ::menu_function_t - @ingroup menu_function_api
*/
static int op_help(struct Menu *menu, int op)
{
mutt_help(menu->type);
menu->redraw = MENU_REDRAW_FULL;
return FR_SUCCESS;
}
/**
* op_jump - Jump to an index number - Implements ::menu_function_t - @ingroup menu_function_api
*/
static int op_jump(struct Menu *menu, int op)
{
if (menu->max == 0)
{
mutt_error(_("No entries"));
return FR_SUCCESS;
}
const int digit = op - OP_JUMP;
if ((digit > 0) && (digit < 10))
{
mutt_unget_ch('0' + digit);
}
struct Buffer *buf = buf_pool_get();
if ((mw_get_field(_("Jump to: "), buf, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) == 0) &&
!buf_is_empty(buf))
{
int n = 0;
if (mutt_str_atoi_full(buf_string(buf), &n) && (n > 0) && (n < (menu->max + 1)))
{
menu_set_index(menu, n - 1); // msg numbers are 0-based
}
else
{
mutt_error(_("Invalid index number"));
}
}
buf_pool_release(&buf);
return FR_SUCCESS;
}
// -----------------------------------------------------------------------------
/**
* MenuFunctions - All the NeoMutt functions that the Menu supports
*/
static const struct MenuFunction MenuFunctions[] = {
// clang-format off
{ OP_BOTTOM_PAGE, menu_movement },
{ OP_CURRENT_BOTTOM, menu_movement },
{ OP_CURRENT_MIDDLE, menu_movement },
{ OP_CURRENT_TOP, menu_movement },
{ OP_FIRST_ENTRY, menu_movement },
{ OP_HALF_DOWN, menu_movement },
{ OP_HALF_UP, menu_movement },
{ OP_HELP, op_help },
{ OP_JUMP, op_jump },
{ OP_JUMP_1, op_jump },
{ OP_JUMP_2, op_jump },
{ OP_JUMP_3, op_jump },
{ OP_JUMP_4, op_jump },
{ OP_JUMP_5, op_jump },
{ OP_JUMP_6, op_jump },
{ OP_JUMP_7, op_jump },
{ OP_JUMP_8, op_jump },
{ OP_JUMP_9, op_jump },
{ OP_LAST_ENTRY, menu_movement },
{ OP_MIDDLE_PAGE, menu_movement },
{ OP_NEXT_ENTRY, menu_movement },
{ OP_NEXT_LINE, menu_movement },
{ OP_NEXT_PAGE, menu_movement },
{ OP_PREV_ENTRY, menu_movement },
{ OP_PREV_LINE, menu_movement },
{ OP_PREV_PAGE, menu_movement },
{ OP_SEARCH, menu_search },
{ OP_SEARCH_NEXT, menu_search },
{ OP_SEARCH_OPPOSITE, menu_search },
{ OP_SEARCH_REVERSE, menu_search },
{ OP_TOP_PAGE, menu_movement },
{ 0, NULL },
// clang-format on
};
/**
* menu_function_dispatcher - Perform a Menu function - Implements ::function_dispatcher_t - @ingroup dispatcher_api
*/
int menu_function_dispatcher(struct MuttWindow *win, int op)
{
if (!win || !win->wdata)
return FR_UNKNOWN;
struct Menu *menu = win->wdata;
int rc = FR_UNKNOWN;
for (size_t i = 0; MenuFunctions[i].op != OP_NULL; i++)
{
const struct MenuFunction *fn = &MenuFunctions[i];
if (fn->op == op)
{
rc = fn->function(menu, op);
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));
return rc;
}
|