NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
lib.h File Reference

Edit a string. More...

#include <stddef.h>
#include "enter.h"
#include "functions.h"
#include "state.h"
#include "wdata.h"
#include "core/lib.h"
#include "history/lib.h"
+ Include dependency graph for lib.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef void(* get_field_callback_t) (const char *text, void *data)
 

Functions

void editor_init_keys (struct SubMenu *sm_generic)
 Initialise the Editor Keybindings - Implements ::init_keys_api.
 
int mw_get_field (const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
 Ask the user for a string -.
 
int mw_get_field_notify (const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata, get_field_callback_t callback, void *cb_data, const struct MenuDefinition *md, function_dispatcher_t fn_disp)
 Ask the user for a string and call a notify function on keypress.
 
void replace_part (struct EnterState *es, size_t from, const char *buf)
 Search and replace on a buffer.
 

Detailed Description

Edit a string.

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 lib.h.

Typedef Documentation

◆ get_field_callback_t

typedef void(* get_field_callback_t) (const char *text, void *data)

Definition at line 67 of file lib.h.

Function Documentation

◆ editor_init_keys()

void editor_init_keys ( struct SubMenu * sm_generic)

Initialise the Editor Keybindings - Implements ::init_keys_api.

Definition at line 131 of file functions.c.

132{
133 struct MenuDefinition *md = NULL;
134 struct SubMenu *sm = NULL;
135
137 md = km_register_menu(MENU_EDITOR, "editor");
138 km_menu_add_submenu(md, sm);
140
141 MdEditor = md;
142 SmEditor = sm;
143}
static const struct MenuOpSeq EditorDefaultBindings[]
Key bindings for the Editor Menu.
Definition functions.c:90
struct SubMenu * SmEditor
Editor functions.
Definition functions.c:49
static const struct MenuFuncOp OpEditor[]
Functions for the Editor Menu.
Definition functions.c:55
struct MenuDefinition * MdEditor
Editor Menu Definition.
Definition functions.c:46
void km_menu_add_submenu(struct MenuDefinition *md, struct SubMenu *sm)
Add a SubMenu to a Menu Definition.
Definition init.c:123
struct SubMenu * km_register_submenu(const struct MenuFuncOp functions[])
Register a submenu.
Definition init.c:91
struct MenuDefinition * km_register_menu(int menu, const char *name)
Register a menu.
Definition init.c:107
void km_menu_add_bindings(struct MenuDefinition *md, const struct MenuOpSeq bindings[])
Add Keybindings to a Menu.
Definition init.c:136
Functions for a Dialog or Window.
Definition menu.h:80
Collection of related functions.
Definition menu.h:68
@ MENU_EDITOR
Text entry area.
Definition type.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mw_get_field_notify()

int mw_get_field_notify ( const char * prompt,
struct Buffer * buf,
CompletionFlags complete,
enum HistoryClass hclass,
const struct CompleteOps * comp_api,
void * cdata,
get_field_callback_t callback,
void * cb_data,
const struct MenuDefinition * md,
function_dispatcher_t fn_disp )

Ask the user for a string and call a notify function on keypress.

Parameters
[in]promptPrompt
[in]bufBuffer for the result
[in]hclassHistory class to use
[in]completeFlags, see CompletionFlags
[in]comp_apiAuto-completion API
[in]cdataAuto-completion private data
[in]callbackCallback function used for notification
[in]cb_dataData to pass to callback function
[in]mdMenu Definition
[in]fn_dispFunction dispatcher
Return values
0Selection made
-1Aborted

Definition at line 265 of file window.c.

269{
272
274 if (complete & MUTT_COMP_UNBUFFERED)
275 flags = GETCH_IGNORE_MACRO;
276
277 struct Buffer *cbuf = NULL;
278 if (callback)
279 cbuf = buf_pool_get();
280
281 if (!md)
282 md = MdEditor;
283
284 int rc = 0;
285
286 struct EnterState *es = enter_state_new();
287
288 win->help_data = EditorHelp;
289 win->help_md = MdEditor;
290
292 struct MuttWindow *old_focus = window_set_focus(win);
293
294 mbstate_t mbstate = { 0 };
295 // clang-format off
296 struct EnterWindowData wdata = { buf, complete, es, hclass, comp_api, cdata, prompt, ENTER_REDRAW_NONE, (complete & MUTT_COMP_PASS), true, NULL, 0, &mbstate, 0, false, NULL, 0, 0 };
297 // clang-format on
298
299 win->wdata = &wdata;
300 win->wdata_free = NULL; // No need, we hold the data
301 win->recalc = enter_recalc;
302 win->repaint = enter_repaint;
304
305 window_redraw(win);
306
307 if (es->wbuf[0] == L'\0')
308 {
309 /* Initialise wbuf from buf */
310 wdata.state->wbuflen = 0;
311 wdata.state->lastchar = mutt_mb_mbstowcs(&wdata.state->wbuf, &wdata.state->wbuflen,
312 0, buf_string(wdata.buffer));
314 }
315 else
316 {
318 wdata.first = false;
319 }
320
321 do
322 {
323 memset(&mbstate, 0, sizeof(mbstate));
324
325 do
326 {
327 if (wdata.redraw != ENTER_REDRAW_NONE)
328 win->actions |= WA_REPAINT;
329
331 window_redraw(NULL);
333 struct KeyEvent event = km_dokey(md, flags);
334 if ((event.op == OP_TIMEOUT) || (event.op == OP_REPAINT))
335 {
336 continue;
337 }
338
339 if (event.op == OP_ABORT)
340 {
341 rc = -1;
342 goto bye;
343 }
344
345 if (event.op == OP_NULL)
346 {
347 if (complete & MUTT_COMP_PASS)
348 mutt_debug(LL_DEBUG5, "Got char *\n");
349 else
350 mutt_debug(LL_DEBUG5, "Got char %c (0x%02x)\n", event.ch, event.ch);
351
352 if (self_insert(&wdata, event.ch))
353 {
354 rc = 0;
355 goto bye;
356 }
357 win->actions |= WA_REPAINT;
358 goto notify;
359 }
360 else
361 {
362 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(event.op),
363 event.op);
364 }
365
366 wdata.first = false;
367 if ((event.op != OP_EDITOR_COMPLETE) && (event.op != OP_EDITOR_COMPLETE_QUERY))
368 wdata.tabs = 0;
369
371
372 int rc_disp = FR_UNKNOWN;
373 if (fn_disp)
374 {
375 rc_disp = fn_disp(cb_data, &event);
376 if (rc_disp == FR_SUCCESS)
377 rc_disp = FR_CONTINUE;
378 }
379
380 if (rc_disp == FR_UNKNOWN)
381 rc_disp = enter_function_dispatcher(win, &event);
382
383 switch (rc_disp)
384 {
385 case FR_NO_ACTION:
386 {
387 if (self_insert(&wdata, event.ch))
388 {
389 rc = 0;
390 goto bye;
391 }
392 break;
393 }
394 case FR_CONTINUE: // repaint
395 rc = 1;
396 goto bye;
397
398 case FR_DONE:
399 wdata.done = true;
400 rc = 0;
401 break;
402
403 case FR_SUCCESS:
404 rc = 0;
405 break;
406
407 case FR_UNKNOWN:
408 case FR_ERROR:
409 default:
410 mutt_beep(false);
411 }
412
413 notify:
414 if (callback)
415 {
416 buf_mb_wcstombs(cbuf, wdata.state->wbuf, wdata.state->lastchar);
417 callback(buf_string(cbuf), cb_data);
418 }
419
420 } while (!wdata.done);
421
422 bye:
424 FREE(&wdata.tempbuf);
425 } while (rc == 1);
426
427 completion_data_free(&wdata.cd);
429 window_set_focus(old_focus);
430 mutt_window_free(&win);
431
432 if (rc == 0)
433 buf_fix_dptr(buf);
434 else
435 buf_reset(buf);
436
437 buf_pool_release(&cbuf);
438
439 enter_state_free(&es);
440
441 return rc;
442}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition buffer.c:182
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
void mutt_beep(bool force)
Irritate the user.
Definition curs_lib.c:68
void completion_data_free(struct CompletionData **ptr)
Free the Completion Data.
Definition data.c:53
@ FR_SUCCESS
Valid function - successfully performed.
Definition dispatcher.h:40
@ FR_DONE
Exit the Dialog.
Definition dispatcher.h:36
@ FR_UNKNOWN
Unknown function.
Definition dispatcher.h:34
@ FR_ERROR
Valid function - error occurred.
Definition dispatcher.h:39
@ FR_CONTINUE
Remain in the Dialog.
Definition dispatcher.h:35
@ FR_NO_ACTION
Valid function - no action performed.
Definition dispatcher.h:38
struct EnterState * enter_state_new(void)
Create a new EnterState.
Definition state.c:74
void enter_state_free(struct EnterState **ptr)
Free an EnterState.
Definition state.c:38
#define MUTT_COMP_PASS
Password mode (no echo)
Definition wdata.h:44
#define MUTT_COMP_UNBUFFERED
Ignore macro buffer.
Definition wdata.h:45
@ ENTER_REDRAW_NONE
Nothing to redraw.
Definition wdata.h:36
@ ENTER_REDRAW_LINE
Redraw entire line.
Definition wdata.h:38
@ ENTER_REDRAW_INIT
Go to end of line and redraw.
Definition wdata.h:37
static const struct Mapping EditorHelp[]
Help Bar for the Command Line Editor.
Definition window.c:47
bool self_insert(struct EnterWindowData *wdata, int ch)
Insert a normal character.
Definition window.c:87
struct KeyEvent km_dokey(const struct MenuDefinition *md, GetChFlags flags)
Determine what a keypress should do.
Definition get.c:434
uint8_t GetChFlags
Flags for mutt_getch(), e.g. GETCH_NO_FLAGS.
Definition get.h:33
#define GETCH_IGNORE_MACRO
Don't use MacroEvents.
Definition get.h:35
#define GETCH_NO_FLAGS
No flags are set.
Definition get.h:34
int enter_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform an Enter function - Implements function_dispatcher_t -.
Definition functions.c:510
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
static int enter_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition window.c:169
static bool enter_recursor(struct MuttWindow *win)
Recursor the Window - Implements MuttWindow::recursor() -.
Definition window.c:242
static int enter_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition window.c:180
void mutt_hist_reset_state(enum HistoryClass hclass)
Move the 'current' position to the end of the History.
Definition history.c:580
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
size_t mutt_mb_mbstowcs(wchar_t **pwbuf, size_t *pwbuflen, size_t i, const char *buf)
Convert a string from multibyte to wide characters.
Definition mbyte.c:292
void buf_mb_wcstombs(struct Buffer *dest, const wchar_t *wstr, size_t wlen)
Convert a string from wide to multibyte characters.
Definition mbyte.c:257
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
void msgcont_push_window(struct MuttWindow *win)
Add a window to the Container Stack.
Definition msgcont.c:100
struct MuttWindow * msgcont_pop_window(void)
Remove the last Window from the Container Stack.
Definition msgcont.c:57
enum MuttCursorState mutt_curses_set_cursor(enum MuttCursorState state)
Set the cursor state.
Definition mutt_curses.c:94
@ MUTT_CURSOR_INVISIBLE
Hide the cursor.
Definition mutt_curses.h:65
@ MUTT_CURSOR_VISIBLE
Display a normal cursor.
Definition mutt_curses.h:66
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
void mutt_window_free(struct MuttWindow **ptr)
Free a Window and its children.
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new Window.
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
@ WT_CUSTOM
Window with a custom drawing function.
Definition mutt_window.h:94
@ MUTT_WIN_ORIENT_VERTICAL
Window uses all available vertical space.
Definition mutt_window.h:38
#define WA_REPAINT
Redraw the contents of the Window.
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition mutt_window.h:52
@ MUTT_WIN_SIZE_FIXED
Window has a fixed size.
Definition mutt_window.h:47
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition opcodes.c:48
#define OP_TIMEOUT
1 second with no events
Definition opcodes.h:35
#define OP_REPAINT
Repaint is needed.
Definition opcodes.h:34
#define OP_ABORT
$abort_key pressed (Ctrl-G)
Definition opcodes.h:36
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
Keep our place when entering a string.
Definition state.h:32
size_t wbuflen
Length of buffer.
Definition state.h:34
wchar_t * wbuf
Buffer for the string being entered.
Definition state.h:33
size_t lastchar
Position of the last character.
Definition state.h:35
Data to fill the Enter Window.
Definition wdata.h:51
int tabs
Number of times the user has hit tab.
Definition wdata.h:68
void * cdata
Auto-Completion private data.
Definition wdata.h:58
struct CompletionData * cd
Auto-completion state data.
Definition wdata.h:72
struct Buffer * buffer
struct Buffer for the result
Definition wdata.h:53
bool done
Is text-entry done?
Definition wdata.h:70
bool first
First time through, no input yet.
Definition wdata.h:64
wchar_t * tempbuf
Buffer used by completion.
Definition wdata.h:65
const struct CompleteOps * comp_api
Auto-Completion API.
Definition wdata.h:57
const char * prompt
Prompt.
Definition wdata.h:61
struct EnterState * state
Current state of text entry.
Definition wdata.h:55
enum EnterRedrawFlags redraw
What needs redrawing? See EnterRedrawFlags.
Definition wdata.h:62
mbstate_t * mbstate
Multi-byte state.
Definition wdata.h:67
enum HistoryClass hclass
History to use, e.g. HC_NEO_COMMAND.
Definition wdata.h:56
An event such as a keypress.
Definition get.h:50
int op
Function opcode, e.g. OP_HELP.
Definition get.h:52
int ch
Raw key pressed.
Definition get.h:51
const struct Mapping * help_data
Data for the Help Bar.
const struct MenuDefinition * help_md
Menu Definition for key bindings.
int(* repaint)(struct MuttWindow *win)
void * wdata
Private data.
int(* recalc)(struct MuttWindow *win)
void(* wdata_free)(struct MuttWindow *win, void **ptr)
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
bool(* recursor)(struct MuttWindow *win)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ replace_part()

void replace_part ( struct EnterState * es,
size_t from,
const char * buf )

Search and replace on a buffer.

Parameters
esCurrent state of the input buffer
fromStarting point for the replacement
bufReplacement string

Definition at line 151 of file functions.c.

152{
153 /* Save the suffix */
154 size_t savelen = es->lastchar - es->curpos;
155 wchar_t *savebuf = NULL;
156
157 if (savelen)
158 {
159 savebuf = MUTT_MEM_CALLOC(savelen, wchar_t);
160 wmemcpy(savebuf, es->wbuf + es->curpos, savelen);
161 }
162
163 /* Convert to wide characters */
164 es->curpos = mutt_mb_mbstowcs(&es->wbuf, &es->wbuflen, from, buf);
165
166 if (savelen)
167 {
168 /* Make space for suffix */
169 if (es->curpos + savelen > es->wbuflen)
170 {
171 es->wbuflen = es->curpos + savelen;
172 MUTT_MEM_REALLOC(&es->wbuf, es->wbuflen, wchar_t);
173 }
174
175 /* Restore suffix */
176 wmemcpy(es->wbuf + es->curpos, savebuf, savelen);
177 FREE(&savebuf);
178 }
179
180 es->lastchar = es->curpos + savelen;
181}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition memory.h:55
size_t curpos
Position of the cursor.
Definition state.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function: