NeoMutt  2023-05-17-16-g61469c
Teaching an old dog new tricks
DOXYGEN
lib.h File Reference

Read/write command history from/to a file. More...

#include <stdbool.h>
#include <stdlib.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.

Enumerations

enum  HistoryClass {
  HC_CMD , HC_ALIAS , HC_COMMAND , HC_FILE ,
  HC_PATTERN , HC_OTHER , HC_MBOX , HC_MAX
}
 Type to differentiate different histories. More...
 

Functions

void mutt_hist_add (enum HistoryClass hclass, const char *str, bool save)
 Add a string to a history. More...
 
bool mutt_hist_at_scratch (enum HistoryClass hclass)
 Is the current History position at the 'scratch' place? More...
 
void mutt_hist_free (void)
 Free all the history lists. More...
 
void mutt_hist_init (void)
 Create a set of empty History ring buffers. More...
 
char * mutt_hist_next (enum HistoryClass hclass)
 Get the next string in a History. More...
 
char * mutt_hist_prev (enum HistoryClass hclass)
 Get the previous string in a History. More...
 
void mutt_hist_read_file (void)
 Read the History from a file. More...
 
void mutt_hist_reset_state (enum HistoryClass hclass)
 Move the 'current' position to the end of the History. More...
 
void mutt_hist_save_scratch (enum HistoryClass hclass, const char *str)
 Save a temporary string to the History. More...
 
int mutt_hist_search (const char *search_buf, enum HistoryClass hclass, char **matches)
 Find matches in a history list. More...
 
void dlg_select_history (char *buf, size_t buflen, char **matches, int match_count)
 Select an item from a history list. More...
 

Detailed Description

Read/write command history from/to a file.

Authors
  • Michael R. Elkins

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.

Enumeration Type Documentation

◆ HistoryClass

Type to differentiate different histories.

Saved lists of recently-used:

Enumerator
HC_CMD 

External commands.

HC_ALIAS 

Aliases.

HC_COMMAND 

NeoMutt commands.

HC_FILE 

Files.

HC_PATTERN 

Patterns.

HC_OTHER 

Miscellaneous strings.

HC_MBOX 

Mailboxes.

HC_MAX 

Definition at line 47 of file lib.h.

48{
49 HC_CMD,
50 HC_ALIAS,
52 HC_FILE,
54 HC_OTHER,
55 HC_MBOX,
56 HC_MAX,
57};
@ HC_MBOX
Mailboxes.
Definition: lib.h:55
@ HC_FILE
Files.
Definition: lib.h:52
@ HC_COMMAND
NeoMutt commands.
Definition: lib.h:51
@ HC_ALIAS
Aliases.
Definition: lib.h:50
@ HC_MAX
Definition: lib.h:56
@ HC_PATTERN
Patterns.
Definition: lib.h:53
@ HC_OTHER
Miscellaneous strings.
Definition: lib.h:54
@ HC_CMD
External commands.
Definition: lib.h:49

Function Documentation

◆ mutt_hist_add()

void mutt_hist_add ( enum HistoryClass  hclass,
const char *  str,
bool  save 
)

Add a string to a history.

Parameters
hclassHistory to add to
strString to add
saveShould the changes be saved to file immediately?

Definition at line 485 of file history.c.

486{
487 struct History *h = get_history(hclass);
488 if (!h)
489 return; /* disabled */
490
491 if (*str)
492 {
493 int prev = h->last - 1;
494 const short c_history = cs_subset_number(NeoMutt->sub, "history");
495 if (prev < 0)
496 prev = c_history;
497
498 /* don't add to prompt history:
499 * - lines beginning by a space
500 * - repeated lines */
501 if ((*str != ' ') && (!h->hist[prev] || !mutt_str_equal(h->hist[prev], str)))
502 {
503 const bool c_history_remove_dups = cs_subset_bool(NeoMutt->sub, "history_remove_dups");
504 if (c_history_remove_dups)
505 remove_history_dups(hclass, str);
506 const short c_save_history = cs_subset_number(NeoMutt->sub, "save_history");
507 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
508 if (save && (c_save_history != 0) && c_history_file)
509 save_history(hclass, str);
510 mutt_str_replace(&h->hist[h->last++], str);
511 if (h->last > c_history)
512 h->last = 0;
513 }
514 }
515 h->cur = h->last; /* reset to the last entry */
516}
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
static void remove_history_dups(enum HistoryClass hclass, const char *str)
De-dupe the history.
Definition: history.c:361
static struct History * get_history(enum HistoryClass hclass)
Get a particular history.
Definition: history.c:108
static void save_history(enum HistoryClass hclass, const char *str)
Save one history string to a file.
Definition: history.c:311
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:327
Saved list of user-entered commands/searches.
Definition: history.c:89
short cur
Current history item.
Definition: history.c:91
short last
Last history item.
Definition: history.c:92
char ** hist
Array of history items.
Definition: history.c:90
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_hist_at_scratch()

bool mutt_hist_at_scratch ( enum HistoryClass  hclass)

Is the current History position at the 'scratch' place?

Parameters
hclassHistory to use
Return values
trueHistory is at 'scratch' place

The last entry in the history is used as a 'scratch' area. It can be overwritten as the user types and edits.

To get (back) to the scratch area, call mutt_hist_next(), mutt_hist_prev() or mutt_hist_reset_state().

Definition at line 647 of file history.c.

648{
649 struct History *h = get_history(hclass);
650 if (!h)
651 return false; /* disabled */
652
653 return h->cur == h->last;
654}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_hist_free()

void mutt_hist_free ( void  )

Free all the history lists.

Definition at line 440 of file history.c.

441{
442 if (!NeoMutt)
443 return;
444
445 const short c_history = cs_subset_number(NeoMutt->sub, "history");
446 for (enum HistoryClass hclass = HC_FIRST; hclass < HC_MAX; hclass++)
447 {
448 struct History *h = &Histories[hclass];
449 if (!h->hist)
450 continue;
451
452 /* The array has (`$history`+1) elements */
453 for (int i = 0; i <= c_history; i++)
454 {
455 FREE(&h->hist[i]);
456 }
457 FREE(&h->hist);
458 }
459}
HistoryClass
Type to differentiate different histories.
Definition: lib.h:48
static struct History Histories[HC_MAX]
Command histories, one for each HistoryClass.
Definition: history.c:98
#define HC_FIRST
Definition: history.c:81
#define FREE(x)
Definition: memory.h:43
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_hist_init()

void mutt_hist_init ( void  )

Create a set of empty History ring buffers.

This just creates empty histories. To fill them, call mutt_hist_read_file().

Definition at line 467 of file history.c.

468{
469 const short c_history = cs_subset_number(NeoMutt->sub, "history");
470 if (c_history == OldSize)
471 return;
472
473 for (enum HistoryClass hclass = HC_FIRST; hclass < HC_MAX; hclass++)
474 init_history(&Histories[hclass]);
475
476 OldSize = c_history;
477}
static int OldSize
The previous number of history entries to save.
Definition: history.c:101
static void init_history(struct History *h)
Set up a new History ring buffer.
Definition: history.c:124
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_hist_next()

char * mutt_hist_next ( enum HistoryClass  hclass)

Get the next string in a History.

Parameters
hclassHistory to choose
Return values
ptrNext string

If there is no next string, and empty string will be returned.

Definition at line 525 of file history.c.

526{
527 struct History *h = get_history(hclass);
528 if (!h)
529 return ""; /* disabled */
530
531 int next = h->cur;
532 const short c_history = cs_subset_number(NeoMutt->sub, "history");
533 do
534 {
535 next++;
536 if (next > c_history)
537 next = 0;
538 if (next == h->last)
539 break;
540 } while (!h->hist[next]);
541
542 h->cur = next;
543 return NONULL(h->hist[h->cur]);
544}
#define NONULL(x)
Definition: string2.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_hist_prev()

char * mutt_hist_prev ( enum HistoryClass  hclass)

Get the previous string in a History.

Parameters
hclassHistory to choose
Return values
ptrPrevious string

If there is no previous string, and empty string will be returned.

Definition at line 553 of file history.c.

554{
555 struct History *h = get_history(hclass);
556 if (!h)
557 return ""; /* disabled */
558
559 int prev = h->cur;
560 const short c_history = cs_subset_number(NeoMutt->sub, "history");
561 do
562 {
563 prev--;
564 if (prev < 0)
565 prev = c_history;
566 if (prev == h->last)
567 break;
568 } while (!h->hist[prev]);
569
570 h->cur = prev;
571 return NONULL(h->hist[h->cur]);
572}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_hist_read_file()

void mutt_hist_read_file ( void  )

Read the History from a file.

The file $history_file is read and parsed into separate History ring buffers.

Definition at line 595 of file history.c.

596{
597 int line = 0, hclass, read;
598 char *linebuf = NULL, *p = NULL;
599 size_t buflen;
600
601 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
602 if (!c_history_file)
603 return;
604
605 FILE *fp = mutt_file_fopen(c_history_file, "r");
606 if (!fp)
607 return;
608
609 const char *const c_charset = cc_charset();
610 while ((linebuf = mutt_file_read_line(linebuf, &buflen, fp, &line, MUTT_RL_NO_FLAGS)))
611 {
612 read = 0;
613 if ((sscanf(linebuf, "%d:%n", &hclass, &read) < 1) || (read == 0) ||
614 (*(p = linebuf + strlen(linebuf) - 1) != '|') || (hclass < 0))
615 {
616 mutt_error(_("Bad history file format (line %d)"), line);
617 break;
618 }
619 /* silently ignore too high class (probably newer neomutt) */
620 if (hclass >= HC_MAX)
621 continue;
622 *p = '\0';
623 p = mutt_str_dup(linebuf + read);
624 if (p)
625 {
626 mutt_ch_convert_string(&p, "utf-8", c_charset, MUTT_ICONV_NO_FLAGS);
627 mutt_hist_add(hclass, p, false);
628 FREE(&p);
629 }
630 }
631
632 mutt_file_fclose(&fp);
633 FREE(&linebuf);
634}
const char * cc_charset(void)
Get the cached value of $charset.
Definition: cache.c:106
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition: file.c:738
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:634
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:150
#define MUTT_RL_NO_FLAGS
No flags are set.
Definition: file.h:39
#define mutt_error(...)
Definition: logging2.h:87
void mutt_hist_add(enum HistoryClass hclass, const char *str, bool save)
Add a string to a history.
Definition: history.c:485
int mutt_ch_convert_string(char **ps, const char *from, const char *to, uint8_t flags)
Convert a string between encodings.
Definition: charset.c:822
#define MUTT_ICONV_NO_FLAGS
No flags are set.
Definition: charset.h:71
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_hist_reset_state()

void mutt_hist_reset_state ( enum HistoryClass  hclass)

Move the 'current' position to the end of the History.

Parameters
hclassHistory to reset

After calling mutt_hist_next() and mutt_hist_prev(), this function resets the current position ('cur' pointer).

Definition at line 581 of file history.c.

582{
583 struct History *h = get_history(hclass);
584 if (!h)
585 return; /* disabled */
586
587 h->cur = h->last;
588}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_hist_save_scratch()

void mutt_hist_save_scratch ( enum HistoryClass  hclass,
const char *  str 
)

Save a temporary string to the History.

Parameters
hclassHistory to alter
strString to set

Write a 'scratch' string into the History's current position. This is useful to preserver a user's edits.

Definition at line 664 of file history.c.

665{
666 struct History *h = get_history(hclass);
667 if (!h)
668 return; /* disabled */
669
670 /* Don't check if str has a value because the scratch buffer may contain
671 * an old garbage value that should be overwritten */
672 mutt_str_replace(&h->hist[h->last], str);
673}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_hist_search()

int mutt_hist_search ( const char *  search_buf,
enum HistoryClass  hclass,
char **  matches 
)

Find matches in a history list.

Parameters
[in]search_bufString to find
[in]hclassHistory list
[out]matchesAll the matching lines
Return values
numMatches found

Definition at line 411 of file history.c.

412{
413 if (!search_buf || !matches)
414 return 0;
415
416 struct History *h = get_history(hclass);
417 if (!h)
418 return 0;
419
420 int match_count = 0;
421 int cur = h->last;
422 const short c_history = cs_subset_number(NeoMutt->sub, "history");
423 do
424 {
425 cur--;
426 if (cur < 0)
427 cur = c_history;
428 if (cur == h->last)
429 break;
430 if (mutt_istr_find(h->hist[cur], search_buf))
431 matches[match_count++] = h->hist[cur];
432 } while (match_count < c_history);
433
434 return match_count;
435}
const char * mutt_istr_find(const char *haystack, const char *needle)
Find first occurrence of string (ignoring case)
Definition: string.c:593
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dlg_select_history()

void dlg_select_history ( char *  buf,
size_t  buflen,
char **  matches,
int  match_count 
)

Select an item from a history list.

Parameters
[in]bufBuffer in which to save string
[in]buflenBuffer length
[out]matchesItems to choose from
[in]match_countNumber of items

Definition at line 127 of file dlg_history.c.

128{
130
131 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
132 char title[256] = { 0 };
133 snprintf(title, sizeof(title), _("History '%s'"), buf);
134 sbar_set_title(sbar, title);
135
136 struct Menu *menu = dlg->wdata;
138 menu->max = match_count;
139 menu->mdata = matches;
140 menu->mdata_free = NULL; // Menu doesn't own the data
141
142 struct HistoryData hd = { false, false, buf, buflen,
144 dlg->wdata = &hd;
145
146 // ---------------------------------------------------------------------------
147 // Event Loop
148 int op = OP_NULL;
149 do
150 {
152 window_redraw(NULL);
153
154 struct KeyEvent event = km_dokey_event(MENU_GENERIC);
155 if (event.ch == 'q')
156 op = OP_EXIT;
157 else
158 op = event.op;
159
160 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
161 if (op < 0)
162 continue;
163 if (op == OP_NULL)
164 {
166 continue;
167 }
169
170 int rc = history_function_dispatcher(dlg, op);
171
172 if (rc == FR_UNKNOWN)
173 rc = menu_function_dispatcher(menu->win, op);
174 if (rc == FR_UNKNOWN)
175 rc = global_function_dispatcher(NULL, op);
176 } while (!hd.done);
177 // ---------------------------------------------------------------------------
178
179 simple_dialog_free(&dlg);
180}
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
static const struct Mapping HistoryHelp[]
Help Bar for the History Selection dialog.
Definition: dlg_history.c:75
int menu_tagging_dispatcher(struct MuttWindow *win, int op)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition: tagging.c:223
int global_function_dispatcher(struct MuttWindow *win, int op)
Perform a Global function - Implements function_dispatcher_t -.
Definition: global.c:164
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:317
int history_function_dispatcher(struct MuttWindow *win, int op)
Perform a History function - Implements function_dispatcher_t -.
Definition: functions.c:76
#define mutt_debug(LEVEL,...)
Definition: logging2.h:84
static void history_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
Format a menu item for the history list - Implements Menu::make_entry() -.
Definition: dlg_history.c:112
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:166
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:129
struct KeyEvent km_dokey_event(enum MenuType mtype)
Determine what a keypress should do.
Definition: keymap.c:643
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: keymap.c:1077
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:40
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:605
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:523
@ WT_DLG_HISTORY
History Dialog, dlg_select_history()
Definition: mutt_window.h:85
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:48
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:224
Data to pass to the History Functions.
Definition: functions.h:35
size_t buflen
Length of the results buffer.
Definition: functions.h:39
struct Menu * menu
History Menu.
Definition: functions.h:40
char ** matches
History entries.
Definition: functions.h:41
bool done
Should we close the Dialog?
Definition: functions.h:36
char * buf
Buffer for the results.
Definition: functions.h:38
int match_count
Number of history entries.
Definition: functions.h:42
An event such as a keypress.
Definition: keymap.h:65
int op
Function opcode, e.g. OP_HELP.
Definition: keymap.h:67
int ch
Raw key pressed.
Definition: keymap.h:66
Definition: lib.h:70
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:77
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:97
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:152
void * mdata
Private data.
Definition: lib.h:138
int max
Number of entries in the menu.
Definition: lib.h:72
void * wdata
Private data.
Definition: mutt_window.h:145
@ MENU_GENERIC
Generic selection list.
Definition: type.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function: