NeoMutt  2025-01-09-41-g086358
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
history.c File Reference

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

#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "lib.h"
#include "functions.h"
+ Include dependency graph for history.c:

Go to the source code of this file.

Data Structures

struct  History
 Saved list of user-entered commands/searches. More...
 

Macros

#define HC_FIRST   HC_EXT_COMMAND
 

Functions

static struct Historyget_history (enum HistoryClass hclass)
 Get a particular history.
 
static void init_history (struct History *h)
 Set up a new History ring buffer.
 
static int dup_hash_dec (struct HashTable *dup_hash, char *str)
 Decrease the refcount of a history string.
 
static int dup_hash_inc (struct HashTable *dup_hash, char *str)
 Increase the refcount of a history string.
 
static void shrink_histfile (void)
 Read, de-dupe and write the history file.
 
static void save_history (enum HistoryClass hclass, const char *str)
 Save one history string to a file.
 
static void remove_history_dups (enum HistoryClass hclass, const char *str)
 De-dupe the history.
 
int mutt_hist_search (const char *find, enum HistoryClass hclass, struct HistoryArray *matches)
 Find matches in a history list.
 
void mutt_hist_cleanup (void)
 Free all the history lists.
 
void mutt_hist_init (void)
 Create a set of empty History ring buffers.
 
void mutt_hist_add (enum HistoryClass hclass, const char *str, bool save)
 Add a string to a history.
 
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_reset_state (enum HistoryClass hclass)
 Move the 'current' position to the end of the History.
 
void mutt_hist_read_file (void)
 Read the History from a file.
 
bool mutt_hist_at_scratch (enum HistoryClass hclass)
 Is the current History position at the 'scratch' place?
 
void mutt_hist_save_scratch (enum HistoryClass hclass, const char *str)
 Save a temporary string to the History.
 
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 -.
 

Variables

static struct History Histories [HC_MAX]
 Command histories, one for each HistoryClass.
 
static int OldSize = 0
 The previous number of history entries to save.
 

Detailed Description

Read/write command history from/to a file.

Authors
  • Richard Russon
  • Pietro Cerutti
  • Dennis Schön

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 history.c.

Macro Definition Documentation

◆ HC_FIRST

#define HC_FIRST   HC_EXT_COMMAND

Definition at line 84 of file history.c.

Function Documentation

◆ get_history()

static struct History * get_history ( enum HistoryClass  hclass)
static

Get a particular history.

Parameters
hclassType of history to find
Return values
ptrHistory ring buffer

Definition at line 111 of file history.c.

112{
113 const short c_history = cs_subset_number(NeoMutt->sub, "history");
114 if ((hclass >= HC_MAX) || (c_history == 0))
115 return NULL;
116
117 struct History *hist = &Histories[hclass];
118 return hist->hist ? hist : NULL;
119}
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:143
@ HC_MAX
Definition: lib.h:60
static struct History Histories[HC_MAX]
Command histories, one for each HistoryClass.
Definition: history.c:101
Saved list of user-entered commands/searches.
Definition: history.c:92
char ** hist
Array of history items.
Definition: history.c:93
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init_history()

static void init_history ( struct History h)
static

Set up a new History ring buffer.

Parameters
hHistory to populate

If the History already has entries, they will be freed.

Definition at line 127 of file history.c.

128{
129 if (OldSize != 0)
130 {
131 if (h->hist)
132 {
133 for (int i = 0; i <= OldSize; i++)
134 FREE(&h->hist[i]);
135 FREE(&h->hist);
136 }
137 }
138
139 const short c_history = cs_subset_number(NeoMutt->sub, "history");
140 if (c_history != 0)
141 h->hist = MUTT_MEM_CALLOC(c_history + 1, char *);
142
143 h->cur = 0;
144 h->last = 0;
145}
static int OldSize
The previous number of history entries to save.
Definition: history.c:104
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
short cur
Current history item.
Definition: history.c:94
short last
Last history item.
Definition: history.c:95
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dup_hash_dec()

static int dup_hash_dec ( struct HashTable dup_hash,
char *  str 
)
static

Decrease the refcount of a history string.

Parameters
dup_hashHash Table containing unique history strings
strString to find
Return values
0String was deleted from the Hash Table
>0Refcount of string
-1Error, string not found

If the string's refcount is 1, then the string will be deleted.

Definition at line 157 of file history.c.

158{
159 struct HashElem *he = mutt_hash_find_elem(dup_hash, str);
160 if (!he)
161 return -1;
162
163 uintptr_t count = (uintptr_t) he->data;
164 if (count <= 1)
165 {
166 mutt_hash_delete(dup_hash, str, NULL);
167 return 0;
168 }
169
170 count--;
171 he->data = (void *) count;
172 return count;
173}
void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:427
struct HashElem * mutt_hash_find_elem(const struct HashTable *table, const char *strkey)
Find the HashElem in a Hash Table element using a key.
Definition: hash.c:377
The item stored in a Hash Table.
Definition: hash.h:43
void * data
User-supplied data.
Definition: hash.h:46
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dup_hash_inc()

static int dup_hash_inc ( struct HashTable dup_hash,
char *  str 
)
static

Increase the refcount of a history string.

Parameters
dup_hashHash Table containing unique history strings
strString to find
Return values
numRefcount of string

If the string isn't found it will be added to the Hash Table.

Definition at line 183 of file history.c.

184{
185 uintptr_t count;
186
187 struct HashElem *he = mutt_hash_find_elem(dup_hash, str);
188 if (!he)
189 {
190 count = 1;
191 mutt_hash_insert(dup_hash, str, (void *) count);
192 return count;
193 }
194
195 count = (uintptr_t) he->data;
196 count++;
197 he->data = (void *) count;
198 return count;
199}
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition: hash.c:335
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ shrink_histfile()

static void shrink_histfile ( void  )
static

Read, de-dupe and write the history file.

Definition at line 204 of file history.c.

205{
206 FILE *fp_tmp = NULL;
207 int n[HC_MAX] = { 0 };
208 int line, hclass = 0, read = 0;
209 char *linebuf = NULL, *p = NULL;
210 size_t buflen;
211 bool regen_file = false;
212 struct HashTable *dup_hashes[HC_MAX] = { 0 };
213
214 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
215 FILE *fp = mutt_file_fopen(c_history_file, "r");
216 if (!fp)
217 return;
218
219 const bool c_history_remove_dups = cs_subset_bool(NeoMutt->sub, "history_remove_dups");
220 const short c_save_history = cs_subset_number(NeoMutt->sub, "save_history");
221 if (c_history_remove_dups)
222 {
223 for (hclass = 0; hclass < HC_MAX; hclass++)
224 dup_hashes[hclass] = mutt_hash_new(MAX(10, c_save_history * 2), MUTT_HASH_STRDUP_KEYS);
225 }
226
227 line = 0;
228 while ((linebuf = mutt_file_read_line(linebuf, &buflen, fp, &line, MUTT_RL_NO_FLAGS)))
229 {
230 if ((sscanf(linebuf, "%d:%n", &hclass, &read) < 1) || (read == 0) ||
231 (*(p = linebuf + strlen(linebuf) - 1) != '|') || (hclass < 0))
232 {
233 mutt_error(_("%s:%d: Bad history file format"), c_history_file, line);
234 regen_file = true;
235 continue;
236 }
237 /* silently ignore too high class (probably newer neomutt) */
238 if (hclass >= HC_MAX)
239 continue;
240 *p = '\0';
241 if (c_history_remove_dups && (dup_hash_inc(dup_hashes[hclass], linebuf + read) > 1))
242 {
243 regen_file = true;
244 continue;
245 }
246 n[hclass]++;
247 }
248
249 if (!regen_file)
250 {
251 for (hclass = HC_FIRST; hclass < HC_MAX; hclass++)
252 {
253 if (n[hclass] > c_save_history)
254 {
255 regen_file = true;
256 break;
257 }
258 }
259 }
260
261 if (regen_file)
262 {
263 fp_tmp = mutt_file_mkstemp();
264 if (!fp_tmp)
265 {
266 mutt_perror(_("Can't create temporary file"));
267 goto cleanup;
268 }
269 rewind(fp);
270 line = 0;
271 while ((linebuf = mutt_file_read_line(linebuf, &buflen, fp, &line, MUTT_RL_NO_FLAGS)))
272 {
273 if ((sscanf(linebuf, "%d:%n", &hclass, &read) < 1) || (read == 0) ||
274 (*(p = linebuf + strlen(linebuf) - 1) != '|') || (hclass < 0))
275 {
276 continue;
277 }
278 if (hclass >= HC_MAX)
279 continue;
280 *p = '\0';
281 if (c_history_remove_dups && (dup_hash_dec(dup_hashes[hclass], linebuf + read) > 0))
282 {
283 continue;
284 }
285 *p = '|';
286 if (n[hclass]-- <= c_save_history)
287 fprintf(fp_tmp, "%s\n", linebuf);
288 }
289 }
290
291cleanup:
292 mutt_file_fclose(&fp);
293 FREE(&linebuf);
294 if (fp_tmp)
295 {
296 if (fflush(fp_tmp) == 0)
297 {
298 fp = mutt_file_fopen(c_history_file, "w");
299 if (fp)
300 {
301 rewind(fp_tmp);
302 mutt_file_copy_stream(fp_tmp, fp);
303 mutt_file_fclose(&fp);
304 }
305 }
306 mutt_file_fclose(&fp_tmp);
307 }
308 if (c_history_remove_dups)
309 for (hclass = 0; hclass < HC_MAX; hclass++)
310 mutt_hash_free(&dup_hashes[hclass]);
311}
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
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition: file.c:225
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:685
#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:92
#define mutt_perror(...)
Definition: logging2.h:93
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:259
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
#define MUTT_HASH_STRDUP_KEYS
make a copy of the keys
Definition: hash.h:111
static int dup_hash_inc(struct HashTable *dup_hash, char *str)
Increase the refcount of a history string.
Definition: history.c:183
#define HC_FIRST
Definition: history.c:84
static int dup_hash_dec(struct HashTable *dup_hash, char *str)
Decrease the refcount of a history string.
Definition: history.c:157
#define MAX(a, b)
Definition: memory.h:31
#define _(a)
Definition: message.h:28
A Hash Table.
Definition: hash.h:97
#define mutt_file_mkstemp()
Definition: tmp.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ save_history()

static void save_history ( enum HistoryClass  hclass,
const char *  str 
)
static

Save one history string to a file.

Parameters
hclassHistory type
strString to save

Definition at line 318 of file history.c.

319{
320 static int n = 0;
321
322 if (!str || (*str == '\0')) // This shouldn't happen, but it's safer
323 return;
324
325 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
326 FILE *fp = mutt_file_fopen(c_history_file, "a");
327 if (!fp)
328 return;
329
330 char *tmp = mutt_str_dup(str);
332
333 // If tmp contains '\n' terminate it there.
334 char *nl = strchr(tmp, '\n');
335 if (nl)
336 *nl = '\0';
337
338 /* Format of a history item (1 line): "<histclass>:<string>|".
339 * We add a '|' in order to avoid lines ending with '\'. */
340 fprintf(fp, "%d:%s|\n", (int) hclass, tmp);
341
342 mutt_file_fclose(&fp);
343 FREE(&tmp);
344
345 if (--n < 0)
346 {
347 const short c_save_history = cs_subset_number(NeoMutt->sub, "save_history");
348 n = c_save_history;
350 }
351}
const char * cc_charset(void)
Get the cached value of $charset.
Definition: config_cache.c:116
static void shrink_histfile(void)
Read, de-dupe and write the history file.
Definition: history.c:204
int mutt_ch_convert_string(char **ps, const char *from, const char *to, uint8_t flags)
Convert a string between encodings.
Definition: charset.c:831
#define MUTT_ICONV_NO_FLAGS
No flags are set.
Definition: charset.h:64
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ remove_history_dups()

static void remove_history_dups ( enum HistoryClass  hclass,
const char *  str 
)
static

De-dupe the history.

Parameters
hclassHistory to de-dupe
strString to find

If the string is found, it is removed from the history.

When removing dups, we want the created "blanks" to be right below the resulting h->last position. See the comment section above 'struct History'.

Definition at line 363 of file history.c.

364{
365 struct History *h = get_history(hclass);
366 if (!h)
367 return; /* disabled */
368
369 /* Remove dups from 0..last-1 compacting up. */
370 int source = 0;
371 int dest = 0;
372 while (source < h->last)
373 {
374 if (mutt_str_equal(h->hist[source], str))
375 FREE(&h->hist[source++]);
376 else
377 h->hist[dest++] = h->hist[source++];
378 }
379
380 /* Move 'last' entry up. */
381 h->hist[dest] = h->hist[source];
382 int old_last = h->last;
383 h->last = dest;
384
385 /* Fill in moved entries with NULL */
386 while (source > h->last)
387 h->hist[source--] = NULL;
388
389 /* Remove dups from last+1 .. `$history` compacting down. */
390 const short c_history = cs_subset_number(NeoMutt->sub, "history");
391 source = c_history;
392 dest = c_history;
393 while (source > old_last)
394 {
395 if (mutt_str_equal(h->hist[source], str))
396 FREE(&h->hist[source--]);
397 else
398 h->hist[dest--] = h->hist[source--];
399 }
400
401 /* Fill in moved entries with NULL */
402 while (dest > old_last)
403 h->hist[dest--] = NULL;
404}
static struct History * get_history(enum HistoryClass hclass)
Get a particular history.
Definition: history.c:111
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
+ 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 HistoryArray *  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 413 of file history.c.

414{
415 if (!find || !matches)
416 return 0;
417
418 struct History *h = get_history(hclass);
419 if (!h)
420 return 0;
421
422 int cur = h->last;
423 const short c_history = cs_subset_number(NeoMutt->sub, "history");
424
425 do
426 {
427 cur--;
428 if (cur < 0)
429 cur = c_history;
430
431 if (cur == h->last)
432 break;
433
434 if (mutt_istr_find(h->hist[cur], find))
435 ARRAY_ADD(matches, h->hist[cur]);
436
437 } while (ARRAY_SIZE(matches) < c_history);
438
439 return ARRAY_SIZE(matches);
440}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#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:521
+ 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 445 of file history.c.

446{
447 if (!NeoMutt)
448 return;
449
450 const short c_history = cs_subset_number(NeoMutt->sub, "history");
451 for (enum HistoryClass hclass = HC_FIRST; hclass < HC_MAX; hclass++)
452 {
453 struct History *h = &Histories[hclass];
454 if (!h->hist)
455 continue;
456
457 /* The array has (`$history`+1) elements */
458 for (int i = 0; i <= c_history; i++)
459 {
460 FREE(&h->hist[i]);
461 }
462 FREE(&h->hist);
463 }
464}
HistoryClass
Type to differentiate different histories.
Definition: lib.h:52
+ 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 472 of file history.c.

473{
474 const short c_history = cs_subset_number(NeoMutt->sub, "history");
475 if (c_history == OldSize)
476 return;
477
478 for (enum HistoryClass hclass = HC_FIRST; hclass < HC_MAX; hclass++)
479 init_history(&Histories[hclass]);
480
481 OldSize = c_history;
482}
static void init_history(struct History *h)
Set up a new History ring buffer.
Definition: history.c:127
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

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

491{
492 struct History *h = get_history(hclass);
493 if (!h)
494 return; /* disabled */
495
496 if (*str)
497 {
498 int prev = h->last - 1;
499 const short c_history = cs_subset_number(NeoMutt->sub, "history");
500 if (prev < 0)
501 prev = c_history;
502
503 /* don't add to prompt history:
504 * - lines beginning by a space
505 * - repeated lines */
506 if ((*str != ' ') && (!h->hist[prev] || !mutt_str_equal(h->hist[prev], str)))
507 {
508 const bool c_history_remove_dups = cs_subset_bool(NeoMutt->sub, "history_remove_dups");
509 if (c_history_remove_dups)
510 remove_history_dups(hclass, str);
511 const short c_save_history = cs_subset_number(NeoMutt->sub, "save_history");
512 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
513 if (save && (c_save_history != 0) && c_history_file)
514 save_history(hclass, str);
515 mutt_str_replace(&h->hist[h->last++], str);
516 if (h->last > c_history)
517 h->last = 0;
518 }
519 }
520 h->cur = h->last; /* reset to the last entry */
521}
static void remove_history_dups(enum HistoryClass hclass, const char *str)
De-dupe the history.
Definition: history.c:363
static void save_history(enum HistoryClass hclass, const char *str)
Save one history string to a file.
Definition: history.c:318
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:280
+ 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 530 of file history.c.

531{
532 struct History *h = get_history(hclass);
533 if (!h)
534 return ""; /* disabled */
535
536 int next = h->cur;
537 const short c_history = cs_subset_number(NeoMutt->sub, "history");
538 do
539 {
540 next++;
541 if (next > c_history)
542 next = 0;
543 if (next == h->last)
544 break;
545 } while (!h->hist[next]);
546
547 h->cur = next;
548 return NONULL(h->hist[h->cur]);
549}
#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 558 of file history.c.

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

587{
588 struct History *h = get_history(hclass);
589 if (!h)
590 return; /* disabled */
591
592 h->cur = h->last;
593}
+ 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 600 of file history.c.

601{
602 const char *const c_history_file = cs_subset_path(NeoMutt->sub, "history_file");
603 if (!c_history_file)
604 return;
605
606 FILE *fp = mutt_file_fopen(c_history_file, "r");
607 if (!fp)
608 return;
609
610 int line = 0, hclass = 0, read = 0;
611 char *linebuf = NULL, *p = NULL;
612 size_t buflen;
613
614 const char *const c_charset = cc_charset();
615 while ((linebuf = mutt_file_read_line(linebuf, &buflen, fp, &line, MUTT_RL_NO_FLAGS)))
616 {
617 read = 0;
618 if ((sscanf(linebuf, "%d:%n", &hclass, &read) < 1) || (read == 0) ||
619 (*(p = linebuf + strlen(linebuf) - 1) != '|') || (hclass < 0))
620 {
621 mutt_error(_("%s:%d: Bad history file format"), c_history_file, line);
622 continue;
623 }
624 /* silently ignore too high class (probably newer neomutt) */
625 if (hclass >= HC_MAX)
626 continue;
627 *p = '\0';
628 p = mutt_str_dup(linebuf + read);
629 if (p)
630 {
631 mutt_ch_convert_string(&p, "utf-8", c_charset, MUTT_ICONV_NO_FLAGS);
632 mutt_hist_add(hclass, p, false);
633 FREE(&p);
634 }
635 }
636
637 mutt_file_fclose(&fp);
638 FREE(&linebuf);
639}
void mutt_hist_add(enum HistoryClass hclass, const char *str, bool save)
Add a string to a history.
Definition: history.c:490
+ 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 652 of file history.c.

653{
654 struct History *h = get_history(hclass);
655 if (!h)
656 return false; /* disabled */
657
658 return h->cur == h->last;
659}
+ 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 669 of file history.c.

670{
671 struct History *h = get_history(hclass);
672 if (!h)
673 return; /* disabled */
674
675 /* Don't check if str has a value because the scratch buffer may contain
676 * an old garbage value that should be overwritten */
677 mutt_str_replace(&h->hist[h->last], str);
678}
+ 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 685 of file history.c.

686{
687 struct HistoryArray matches = ARRAY_HEAD_INITIALIZER;
688
689 int match_count = mutt_hist_search(buf_string(buf), hclass, &matches);
690 if (match_count != 0)
691 {
692 if (match_count == 1)
693 {
694 const char **pstr = ARRAY_GET(&matches, 0);
695 buf_strcpy(buf, *pstr);
696 }
697 else
698 {
699 dlg_history(buf, &matches);
700 }
701 }
702
703 ARRAY_FREE(&matches);
704}
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
#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 HistoryArray *matches)
Select an item from a history list -.
Definition: dlg_history.c:119
int mutt_hist_search(const char *find, enum HistoryClass hclass, struct HistoryArray *matches)
Find matches in a history list.
Definition: history.c:413
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ Histories

struct History Histories[HC_MAX]
static

Command histories, one for each HistoryClass.

Definition at line 101 of file history.c.

◆ OldSize

int OldSize = 0
static

The previous number of history entries to save.

See also
$history

Definition at line 104 of file history.c.