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

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

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

Data Structures

struct  HistoryEntry
 A line in the History menu. More...
 

Enumerations

enum  HistoryClass {
  HC_EXT_COMMAND , HC_ALIAS , HC_NEO_COMMAND , HC_FILE ,
  HC_PATTERN , HC_OTHER , HC_MAILBOX , HC_MAX ,
  HC_NONE = HC_MAX
}
 Type to differentiate different histories. More...
 
enum  ExpandoDataHistory { ED_HIS_MATCH = 1 , ED_HIS_NUMBER }
 Expando UIDs for History. More...
 

Functions

void mutt_hist_add (enum HistoryClass hclass, const char *str, bool save)
 Add a string to a history.
 
bool mutt_hist_at_scratch (enum HistoryClass hclass)
 Is the current History position at the 'scratch' place?
 
void mutt_hist_cleanup (void)
 Free all the history lists.
 
void mutt_hist_init (void)
 Create a set of empty History ring buffers.
 
char * mutt_hist_next (enum HistoryClass hclass)
 Get the next string in a History.
 
char * mutt_hist_prev (enum HistoryClass hclass)
 Get the previous string in a History.
 
void mutt_hist_read_file (void)
 Read the History from a file.
 
void mutt_hist_reset_state (enum HistoryClass hclass)
 Move the 'current' position to the end of the History.
 
void mutt_hist_save_scratch (enum HistoryClass hclass, const char *str)
 Save a temporary string to the History.
 
int mutt_hist_search (const char *find, enum HistoryClass hclass, struct StringArray *matches)
 Find matches in a history list.
 
void mutt_hist_complete (struct Buffer *buf, enum HistoryClass hclass)
 Complete a string from a history list.
 
int main_hist_observer (struct NotifyCallback *nc)
 Notification that a Config Variable has change - Implements observer_t -.
 
void dlg_history (struct Buffer *buf, struct StringArray *matches)
 Select an item from a history list -.
 

Detailed Description

Read/write command history from/to a file.

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.

Enumeration Type Documentation

◆ HistoryClass

Type to differentiate different histories.

Saved lists of recently-used:

Enumerator
HC_EXT_COMMAND 

External commands.

HC_ALIAS 

Aliases.

HC_NEO_COMMAND 

NeoMutt commands.

HC_FILE 

Files.

HC_PATTERN 

Patterns.

HC_OTHER 

Miscellaneous strings.

HC_MAILBOX 

Mailboxes.

HC_MAX 
HC_NONE 

No History.

Definition at line 53 of file lib.h.

54{
56 HC_ALIAS,
58 HC_FILE,
60 HC_OTHER,
62 HC_MAX,
63 HC_NONE = HC_MAX,
64};
@ HC_FILE
Files.
Definition lib.h:58
@ HC_EXT_COMMAND
External commands.
Definition lib.h:55
@ HC_MAILBOX
Mailboxes.
Definition lib.h:61
@ HC_ALIAS
Aliases.
Definition lib.h:56
@ HC_NEO_COMMAND
NeoMutt commands.
Definition lib.h:57
@ HC_NONE
No History.
Definition lib.h:63
@ HC_MAX
Definition lib.h:62
@ HC_PATTERN
Patterns.
Definition lib.h:59
@ HC_OTHER
Miscellaneous strings.
Definition lib.h:60

◆ ExpandoDataHistory

Expando UIDs for History.

See also
ED_HISTORY, ExpandoDomain
Enumerator
ED_HIS_MATCH 

HistoryEntry.history.

ED_HIS_NUMBER 

HistoryEntry.num.

Definition at line 80 of file lib.h.

81{
82 ED_HIS_MATCH = 1,
84};
@ ED_HIS_MATCH
HistoryEntry.history.
Definition lib.h:82
@ ED_HIS_NUMBER
HistoryEntry.num.
Definition lib.h:83

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 484 of file history.c.

485{
486 struct History *h = get_history(hclass);
487 if (!h)
488 return; /* disabled */
489
490 if (str && *str)
491 {
492 int prev = h->last - 1;
493 const short c_history = cs_subset_number(NeoMutt->sub, "history");
494 if (prev < 0)
495 prev = c_history;
496
497 /* don't add to prompt history:
498 * - lines beginning by a space
499 * - repeated lines */
500 if ((*str != ' ') && (!h->hist[prev] || !mutt_str_equal(h->hist[prev], str)))
501 {
502 const bool c_history_remove_dups = cs_subset_bool(NeoMutt->sub, "history_remove_dups");
503 if (c_history_remove_dups)
504 remove_history_dups(hclass, str);
505 const short c_save_history = cs_subset_number(NeoMutt->sub, "save_history");
506 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
507 if (save && (c_save_history != 0) && c_history_file)
508 save_history(hclass, str);
509 mutt_str_replace(&h->hist[h->last++], str);
510 if (h->last > c_history)
511 h->last = 0;
512 }
513 }
514 h->cur = h->last; /* reset to the last entry */
515}
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition helpers.c:143
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition helpers.c:168
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
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:110
static void save_history(enum HistoryClass hclass, const char *str)
Save one history string to a file.
Definition history.c:323
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:665
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition string.c:284
Saved list of user-entered commands/searches.
Definition history.c:91
short cur
Current history item.
Definition history.c:93
short last
Last history item.
Definition history.c:94
char ** hist
Array of history items.
Definition history.c:92
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
+ 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 646 of file history.c.

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

◆ mutt_hist_cleanup()

void mutt_hist_cleanup ( void )

Free all the history lists.

Definition at line 443 of file history.c.

444{
445 for (enum HistoryClass hclass = HC_FIRST; hclass < HC_MAX; hclass++)
446 {
447 struct History *h = &Histories[hclass];
448 if (!h->hist)
449 continue;
450
451 /* The array has (OldSize+1) elements */
452 for (int i = 0; i <= OldSize; i++)
453 {
454 FREE(&h->hist[i]);
455 }
456 FREE(&h->hist);
457 }
458}
HistoryClass
Type to differentiate different histories.
Definition lib.h:54
static int OldSize
The previous number of history entries to save.
Definition history.c:103
static struct History Histories[HC_MAX]
Command histories, one for each HistoryClass.
Definition history.c:100
#define HC_FIRST
Definition history.c:83
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
+ 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 466 of file history.c.

467{
468 const short c_history = cs_subset_number(NeoMutt->sub, "history");
469 if (c_history == OldSize)
470 return;
471
472 for (enum HistoryClass hclass = HC_FIRST; hclass < HC_MAX; hclass++)
473 init_history(&Histories[hclass]);
474
475 OldSize = c_history;
476}
static void init_history(struct History *h)
Set up a new History ring buffer.
Definition history.c:126
+ 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 524 of file history.c.

525{
526 struct History *h = get_history(hclass);
527 if (!h)
528 return ""; /* disabled */
529
530 int next = h->cur;
531 const short c_history = cs_subset_number(NeoMutt->sub, "history");
532 do
533 {
534 next++;
535 if (next > c_history)
536 next = 0;
537 if (next == h->last)
538 break;
539 } while (!h->hist[next]);
540
541 h->cur = next;
542 return NONULL(h->hist[h->cur]);
543}
#define NONULL(x)
Definition string2.h:44
+ 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 552 of file history.c.

553{
554 struct History *h = get_history(hclass);
555 if (!h)
556 return ""; /* disabled */
557
558 int prev = h->cur;
559 const short c_history = cs_subset_number(NeoMutt->sub, "history");
560 do
561 {
562 prev--;
563 if (prev < 0)
564 prev = c_history;
565 if (prev == h->last)
566 break;
567 } while (!h->hist[prev]);
568
569 h->cur = prev;
570 return NONULL(h->hist[h->cur]);
571}
+ 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 594 of file history.c.

595{
596 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
597 if (!c_history_file)
598 return;
599
600 FILE *fp = mutt_file_fopen(c_history_file, "r");
601 if (!fp)
602 return;
603
604 int line = 0, hclass = 0, read = 0;
605 char *linebuf = NULL, *p = NULL;
606 size_t buflen;
607
608 const char *const c_charset = cc_charset();
609 while ((linebuf = mutt_file_read_line(linebuf, &buflen, fp, &line, MUTT_RL_NO_FLAGS)))
610 {
611 read = 0;
612 if ((sscanf(linebuf, "%d:%n", &hclass, &read) < 1) || (read == 0) ||
613 (*(p = linebuf + strlen(linebuf) - 1) != '|') || (hclass < 0))
614 {
615 mutt_error(_("%s:%d: Bad history file format"), c_history_file, line);
616 continue;
617 }
618 /* silently ignore too high class (probably newer neomutt) */
619 if (hclass >= HC_MAX)
620 continue;
621 *p = '\0';
622 p = mutt_str_dup(linebuf + read);
623 if (p)
624 {
625 mutt_ch_convert_string(&p, "utf-8", c_charset, MUTT_ICONV_NO_FLAGS);
626 mutt_hist_add(hclass, p, false);
627 FREE(&p);
628 }
629 }
630
631 mutt_file_fclose(&fp);
632 FREE(&linebuf);
633}
const char * cc_charset(void)
Get the cached value of $charset.
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:678
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
#define MUTT_RL_NO_FLAGS
No flags are set.
Definition file.h:40
#define mutt_error(...)
Definition logging2.h:94
void mutt_hist_add(enum HistoryClass hclass, const char *str, bool save)
Add a string to a history.
Definition history.c:484
int mutt_ch_convert_string(char **ps, const char *from, const char *to, uint8_t flags)
Convert a string between encodings.
Definition charset.c:817
#define MUTT_ICONV_NO_FLAGS
No flags are set.
Definition charset.h:66
#define _(a)
Definition message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
+ 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 580 of file history.c.

581{
582 struct History *h = get_history(hclass);
583 if (!h)
584 return; /* disabled */
585
586 h->cur = h->last;
587}
+ 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 663 of file history.c.

664{
665 struct History *h = get_history(hclass);
666 if (!h)
667 return; /* disabled */
668
669 /* Don't check if str has a value because the scratch buffer may contain
670 * an old garbage value that should be overwritten */
671 mutt_str_replace(&h->hist[h->last], str);
672}
+ 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 * find,
enum HistoryClass hclass,
struct StringArray * matches )

Find matches in a history list.

Parameters
[in]findString 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 (!find || !matches)
414 return 0;
415
416 struct History *h = get_history(hclass);
417 if (!h)
418 return 0;
419
420 int cur = h->last;
421 const short c_history = cs_subset_number(NeoMutt->sub, "history");
422
423 do
424 {
425 cur--;
426 if (cur < 0)
427 cur = c_history;
428
429 if (cur == h->last)
430 break;
431
432 if (mutt_istr_find(h->hist[cur], find))
433 ARRAY_ADD(matches, h->hist[cur]);
434
435 } while (ARRAY_SIZE(matches) < c_history);
436
437 return ARRAY_SIZE(matches);
438}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
const char * mutt_istr_find(const char *haystack, const char *needle)
Find first occurrence of string (ignoring case)
Definition string.c:528
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_hist_complete()

void mutt_hist_complete ( struct Buffer * buf,
enum HistoryClass hclass )

Complete a string from a history list.

Parameters
bufBuffer in which to save string
hclassHistory list to use

Definition at line 679 of file history.c.

680{
681 struct StringArray matches = ARRAY_HEAD_INITIALIZER;
682
683 int match_count = mutt_hist_search(buf_string(buf), hclass, &matches);
684 if (match_count != 0)
685 {
686 if (match_count == 1)
687 {
688 const char **pstr = ARRAY_GET(&matches, 0);
689 buf_strcpy(buf, *pstr);
690 }
691 else
692 {
693 dlg_history(buf, &matches);
694 }
695 }
696
697 ARRAY_FREE(&matches);
698}
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
#define ARRAY_GET(head, idx)
Return the element at index.
Definition array.h:109
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition array.h:58
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
void dlg_history(struct Buffer *buf, struct StringArray *matches)
Select an item from a history list -.
int mutt_hist_search(const char *find, enum HistoryClass hclass, struct StringArray *matches)
Find matches in a history list.
Definition history.c:411
+ Here is the call graph for this function:
+ Here is the caller graph for this function: