1% Public functions:
2%
3%   rline_load_history (file);
4%   rline_save_history (file);
5%
6
7variable RLine_Hist_Max_Lines = 50;
8variable RLine_History_File = NULL;
9#ifdef UNIX
10%if (getenv ("HOME") != NULL) RLine_History_File = "$HOME/.${name}_slhist";
11#endif
12
13autoload ("rline_up_hist_search", "rline/histsrch.sl");
14autoload ("rline_down_hist_search", "rline/histsrch.sl");
15define rline_load_history ()
16{
17   variable file = RLine_History_File;
18   if (_NARGS == 1)
19     file = ();
20   if (file == NULL)
21     return;
22
23   variable fp = fopen (file, "r");
24   if (fp == NULL)
25     return;
26
27   variable lines = fgetslines (fp);
28   () = fclose (fp);
29   lines = strtrim_end (lines, "\n");
30   rline_set_history (lines);
31}
32
33define rline_save_history ()
34{
35   variable file = RLine_History_File;
36   if (_NARGS == 1)
37     file = ();
38   if (file == NULL)
39     return;
40
41   variable max_lines = qualifier ("max", RLine_Hist_Max_Lines);
42   variable h = rline_get_history ();
43   variable n = length (h);
44   if (n > max_lines)
45     h = h[[n-max_lines:]];
46
47   variable fd = open (file, O_WRONLY|O_CREAT|O_TRUNC|O_TEXT, S_IRUSR|S_IWUSR);
48   if (fd == NULL)
49     return;
50
51   variable fp = fdopen (fd, "w");
52   if (fp == NULL)
53     return;
54
55   foreach (h)
56     {
57	variable line = ();
58	() = fputs (line, fp);
59	() = fputs ("\n", fp);
60     }
61   () = fflush (fp);
62   () = close (fd);
63}
64
65define rline_edit_history ()
66{
67   variable lines = rline_get_history ();
68   lines = rline_call_editor (lines, "histedit", "tmp");
69   if ((lines == NULL)
70       || (length (lines) == 0))
71     return;
72
73   rline_set_history (strtrim_end(lines, "\n"));
74}
75