NeoMutt  2025-12-11-949-g4870ee
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
functions_fuzzy.c File Reference

Sidebar fuzzy search functions. More...

#include "config.h"
#include <stdbool.h>
#include <string.h>
#include "private.h"
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "editor/lib.h"
#include "fuzzy/lib.h"
#include "history/lib.h"
#include "index/lib.h"
#include "key/lib.h"
#include "functions_sidebar.h"
+ Include dependency graph for functions_fuzzy.c:

Go to the source code of this file.

Functions

struct MenuDefinitionsb_fuzzy_init_menu (void)
 Initialise the Fuzzy Search Menu - Implements ::init_keys_api.
 
int sb_fuzzy_function_dispatcher (struct MuttWindow *win, const struct KeyEvent *event)
 Perform a Fuzzy Search function - Implements function_dispatcher_t -.
 
static void sidebar_matcher_cb (const char *text, void *data)
 React to keys as they are entered - Implements get_field_callback_t.
 
int op_sidebar_start_search (struct SidebarFunctionData *fdata, const struct KeyEvent *event)
 Selects the last unhidden mailbox - Implements sidebar_function_t -.
 

Variables

static const struct SidebarFunction FuzzyFunctions []
 All the NeoMutt functions that the Sidebar Fuzzy Search supports.
 

Detailed Description

Sidebar fuzzy search functions.

Authors
  • Richard Russon

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/.

Definition in file functions_fuzzy.c.

Function Documentation

◆ sb_fuzzy_init_menu()

struct MenuDefinition * sb_fuzzy_init_menu ( void )

Initialise the Fuzzy Search Menu - Implements ::init_keys_api.

Return values
ptrMenuDefinition for the fuzzy search

Definition at line 48 of file functions_fuzzy.c.

49{
50 struct MenuDefinition *md = menudef_new();
51
52 struct SubMenu *sm_fuzzy = fuzzy_get_submenu();
53 ASSERT(sm_fuzzy);
54 struct SubMenu *sm_editor = editor_get_submenu();
55 ASSERT(sm_editor);
56
57 km_menu_add_submenu(md, sm_fuzzy);
58 km_menu_add_submenu(md, sm_editor);
59
60 return md;
61}
struct SubMenu * editor_get_submenu(void)
Get the Editor SubMenu.
Definition functions.c:587
struct SubMenu * fuzzy_get_submenu(void)
Get the Fuzzy SubMenu.
Definition functions.c:91
void km_menu_add_submenu(struct MenuDefinition *md, struct SubMenu *sm)
Add a SubMenu to a Menu Definition.
Definition init.c:123
struct MenuDefinition * menudef_new(void)
Create a new MenuDefinition.
Definition menudef.c:84
#define ASSERT(COND)
Definition signal2.h:59
Functions for a Dialog or Window.
Definition menudef.h:44
Collection of related functions.
Definition menudef.h:33
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sidebar_matcher_cb()

static void sidebar_matcher_cb ( const char * text,
void * data )
static

React to keys as they are entered - Implements get_field_callback_t.

Definition at line 120 of file functions_fuzzy.c.

121{
122 struct MuttWindow *win = data;
123 struct SidebarWindowData *wdata = win->wdata;
124 wdata->hil_index = -1;
125 wdata->repage = true;
126
127 struct SbEntry **sbep = NULL;
128
129 if (text[0] == '\0')
130 {
131 ARRAY_FOREACH(sbep, &wdata->entries)
132 {
133 struct SbEntry *sbe = *sbep;
134 sbe->mailbox->visible = true;
135 sbe->score = -1;
136 if (wdata->hil_index == -1)
137 wdata->hil_index = ARRAY_FOREACH_IDX_sbep;
138 }
139 wdata->win->actions |= WA_RECALC;
140 return;
141 }
142
143 struct FuzzyOptions opts = { .smart_case = true };
144 struct FuzzyResult result = { 0 };
145 int best_score = -1;
146 int best_index = -1;
147 struct Buffer *buf = buf_pool_get();
148
149 ARRAY_FOREACH(sbep, &wdata->entries)
150 {
151 struct SbEntry *sbe = *sbep;
152 buf_printf(buf, "%s %s", sbe->box, sbe->display);
153 int score = fuzzy_match(text, buf_string(buf), FUZZY_ALGO_SUBSEQ, &opts, &result);
154 if (score > 0)
155 {
156 // Extra 2 points for new (unseen) mail
157 // 1 point for old (seen) mail
158 score += (2 * sbe->mailbox->msg_new);
159 score += (sbe->mailbox->msg_unread - sbe->mailbox->msg_new);
160 }
161 sbe->score = score;
162 if (score >= 0)
163 {
164 if ((best_score == -1) || (score > best_score))
165 {
166 best_score = score;
167 best_index = ARRAY_FOREACH_IDX_sbep;
168 }
169 sbe->mailbox->visible = true;
170 }
171 else
172 {
173 sbe->mailbox->visible = false;
174 }
175 }
176
177 wdata->hil_index = best_index;
178 wdata->win->actions |= WA_RECALC;
179 buf_pool_release(&buf);
180}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:168
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
@ FUZZY_ALGO_SUBSEQ
Subsequence matching algorithm.
Definition lib.h:82
int fuzzy_match(const char *pattern, const char *candidate, enum FuzzyAlgo algo, const struct FuzzyOptions *opts, struct FuzzyResult *out)
Perform fuzzy matching.
Definition fuzzy.c:58
@ WA_RECALC
Recalculate the contents of the Window.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
String manipulation buffer.
Definition buffer.h:36
Options for fuzzy matching.
Definition lib.h:92
Result of a fuzzy match.
Definition lib.h:103
int msg_new
Number of new messages.
Definition mailbox.h:94
bool visible
True if a result of "mailboxes".
Definition mailbox.h:132
int msg_unread
Number of unread messages.
Definition mailbox.h:91
void * wdata
Private data.
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Info about folders in the sidebar.
Definition private.h:40
int score
Fuzzy-match score.
Definition private.h:47
char display[256]
Formatted string to display.
Definition private.h:42
struct Mailbox * mailbox
Mailbox this represents.
Definition private.h:44
char box[256]
Mailbox path (possibly abbreviated)
Definition private.h:41
Sidebar private Window data -.
Definition private.h:89
int hil_index
Highlighted mailbox.
Definition private.h:96
struct MuttWindow * win
Sidebar Window.
Definition private.h:90
struct SbEntryArray entries
Items to display in the sidebar.
Definition private.h:92
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ FuzzyFunctions

const struct SidebarFunction FuzzyFunctions[]
static
Initial value:
= {
{ OP_FIRST_ENTRY, op_sidebar_first },
{ OP_LAST_ENTRY, op_sidebar_last },
{ OP_NEXT_ENTRY, op_sidebar_next },
{ OP_NEXT_PAGE, op_sidebar_page_down },
{ OP_PREV_ENTRY, op_sidebar_prev },
{ OP_PREV_PAGE, op_sidebar_page_up },
{ 0, NULL },
}
int op_sidebar_page_down(struct SidebarFunctionData *fdata, const struct KeyEvent *event)
Selects the first entry in the next page of mailboxes - Implements sidebar_function_t -.
int op_sidebar_prev(struct SidebarFunctionData *fdata, const struct KeyEvent *event)
Selects the previous unhidden mailbox - Implements sidebar_function_t -.
int op_sidebar_page_up(struct SidebarFunctionData *fdata, const struct KeyEvent *event)
Selects the last entry in the previous page of mailboxes - Implements sidebar_function_t -.
int op_sidebar_next(struct SidebarFunctionData *fdata, const struct KeyEvent *event)
Selects the next unhidden mailbox - Implements sidebar_function_t -.
int op_sidebar_first(struct SidebarFunctionData *fdata, const struct KeyEvent *event)
Selects the first unhidden mailbox - Implements sidebar_function_t -.
int op_sidebar_last(struct SidebarFunctionData *fdata, const struct KeyEvent *event)
Selects the last unhidden mailbox - Implements sidebar_function_t -.

All the NeoMutt functions that the Sidebar Fuzzy Search supports.

Definition at line 66 of file functions_fuzzy.c.

66 {
67 // clang-format off
68 { OP_FIRST_ENTRY, op_sidebar_first },
69 { OP_LAST_ENTRY, op_sidebar_last },
70 { OP_NEXT_ENTRY, op_sidebar_next },
71 { OP_NEXT_PAGE, op_sidebar_page_down },
72 { OP_PREV_ENTRY, op_sidebar_prev },
73 { OP_PREV_PAGE, op_sidebar_page_up },
74 { 0, NULL },
75 // clang-format on
76};