Commit 5f4fb8e5 authored by Benno Schulenberg's avatar Benno Schulenberg
Browse files

tweaks: frob some comments and rename a few variables and a function

parent 7a9d0101
Showing with 47 additions and 43 deletions
+47 -43
...@@ -26,9 +26,10 @@ ...@@ -26,9 +26,10 @@
#ifndef DISABLE_HISTORIES #ifndef DISABLE_HISTORIES
static bool history_changed = FALSE; static bool history_changed = FALSE;
/* Have any of the history lists changed? */ /* Whether any of the history lists has changed. */
/* Initialize the search and replace history lists. */ /* Initialize the lists of historical search and replace strings
* and the list of historical executed commands. */
void history_init(void) void history_init(void)
{ {
search_history = make_new_node(NULL); search_history = make_new_node(NULL);
...@@ -47,52 +48,52 @@ void history_init(void) ...@@ -47,52 +48,52 @@ void history_init(void)
executebot = execute_history; executebot = execute_history;
} }
/* Set the current position in the history list h to the bottom. */ /* Set the current position in the given history list to the bottom. */
void history_reset(const filestruct *h) void history_reset(const filestruct *list)
{ {
if (h == search_history) if (list == search_history)
search_history = searchbot; search_history = searchbot;
else if (h == replace_history) else if (list == replace_history)
replace_history = replacebot; replace_history = replacebot;
else if (h == execute_history) else if (list == execute_history)
execute_history = executebot; execute_history = executebot;
} }
/* Return the first node containing the first len characters of the /* Return from the history list that starts at start and ends at end
* string s in the history list, starting at h_start and ending at * the first node that contains the first len characters of the given
* h_end, or NULL if there isn't one. */ * text, or NULL if there is no such node. */
filestruct *find_history(const filestruct *h_start, const filestruct filestruct *find_history(const filestruct *start, const filestruct *end,
*h_end, const char *s, size_t len) const char *text, size_t len)
{ {
const filestruct *p; const filestruct *item;
for (p = h_start; p != h_end->prev && p != NULL; p = p->prev) { for (item = start; item != end->prev && item != NULL; item = item->prev) {
if (strncmp(s, p->data, len) == 0) if (strncmp(item->data, text, len) == 0)
return (filestruct *)p; return (filestruct *)item;
} }
return NULL; return NULL;
} }
/* Update a history list (the one in which h is the current position) /* Update a history list (the one in which item is the current position)
* with a fresh string s. That is: add s, or move it to the end. */ * with a fresh string text. That is: add text, or move it to the end. */
void update_history(filestruct **h, const char *s) void update_history(filestruct **item, const char *text)
{ {
filestruct **htop = NULL, **hbot = NULL, *thesame; filestruct **htop = NULL, **hbot = NULL, *thesame;
if (*h == search_history) { if (*item == search_history) {
htop = &searchtop; htop = &searchtop;
hbot = &searchbot; hbot = &searchbot;
} else if (*h == replace_history) { } else if (*item == replace_history) {
htop = &replacetop; htop = &replacetop;
hbot = &replacebot; hbot = &replacebot;
} else if (*h == execute_history) { } else if (*item == execute_history) {
htop = &executetop; htop = &executetop;
hbot = &executebot; hbot = &executebot;
} }
/* See if the string is already in the history. */ /* See if the string is already in the history. */
thesame = find_history(*hbot, *htop, s, HIGHEST_POSITIVE); thesame = find_history(*hbot, *htop, text, HIGHEST_POSITIVE);
/* If an identical string was found, delete that item. */ /* If an identical string was found, delete that item. */
if (thesame != NULL) { if (thesame != NULL) {
...@@ -117,7 +118,7 @@ void update_history(filestruct **h, const char *s) ...@@ -117,7 +118,7 @@ void update_history(filestruct **h, const char *s)
} }
/* Store the fresh string in the last item, then create a new item. */ /* Store the fresh string in the last item, then create a new item. */
(*hbot)->data = mallocstrcpy((*hbot)->data, s); (*hbot)->data = mallocstrcpy((*hbot)->data, text);
splice_node(*hbot, make_new_node(*hbot)); splice_node(*hbot, make_new_node(*hbot));
*hbot = (*hbot)->next; *hbot = (*hbot)->next;
(*hbot)->data = mallocstrcpy(NULL, ""); (*hbot)->data = mallocstrcpy(NULL, "");
...@@ -126,7 +127,7 @@ void update_history(filestruct **h, const char *s) ...@@ -126,7 +127,7 @@ void update_history(filestruct **h, const char *s)
history_changed = TRUE; history_changed = TRUE;
/* Set the current position in the list to the bottom. */ /* Set the current position in the list to the bottom. */
*h = *hbot; *item = *hbot;
} }
/* Move h to the string in the history list just before it, and return /* Move h to the string in the history list just before it, and return
...@@ -262,12 +263,11 @@ void history_error(const char *msg, ...) ...@@ -262,12 +263,11 @@ void history_error(const char *msg, ...)
; ;
} }
/* Now that we have more than one history file, let's just rely on a /* Check whether the ~/.nano subdirectory for history files exists. Return
* .nano dir for this stuff. Return 1 if the dir exists or was * TRUE if it exists or was successfully created, and FALSE otherwise. */
* successfully created, and return 0 otherwise. */ bool have_dotnano(void)
int check_dotnano(void)
{ {
int ret = 1; bool retval = TRUE;
struct stat dirstat; struct stat dirstat;
char *nanodir = construct_filename("/.nano"); char *nanodir = construct_filename("/.nano");
...@@ -277,21 +277,21 @@ int check_dotnano(void) ...@@ -277,21 +277,21 @@ int check_dotnano(void)
"It is required for saving/loading " "It is required for saving/loading "
"search history or cursor positions.\n"), "search history or cursor positions.\n"),
nanodir, strerror(errno)); nanodir, strerror(errno));
ret = 0; retval = FALSE;
} }
} else if (!S_ISDIR(dirstat.st_mode)) { } else if (!S_ISDIR(dirstat.st_mode)) {
history_error(N_("Path %s is not a directory and needs to be.\n" history_error(N_("Path %s is not a directory and needs to be.\n"
"Nano will be unable to load or save " "Nano will be unable to load or save "
"search history or cursor positions.\n"), "search history or cursor positions.\n"),
nanodir); nanodir);
ret = 0; retval = FALSE;
} }
free(nanodir); free(nanodir);
return ret; return retval;
} }
/* Load the search and replace histories from ~/.nano/search_history. */ /* Load the histories for Search and Replace and Execute Command. */
void load_history(void) void load_history(void)
{ {
char *histname = histfilename(); char *histname = histfilename();
...@@ -379,7 +379,7 @@ bool write_list(const filestruct *head, FILE *histfile) ...@@ -379,7 +379,7 @@ bool write_list(const filestruct *head, FILE *histfile)
return TRUE; return TRUE;
} }
/* Save the search and replace histories to ~/.nano/search_history. */ /* Save the histories for Search and Replace and Execute Command. */
void save_history(void) void save_history(void)
{ {
char *histname; char *histname;
...@@ -414,7 +414,7 @@ void save_history(void) ...@@ -414,7 +414,7 @@ void save_history(void)
free(histname); free(histname);
} }
/* Load the recorded file positions from ~/.nano/filepos_history. */ /* Load the recorded cursor positions for files that were edited. */
void load_poshistory(void) void load_poshistory(void)
{ {
char *poshist = poshistfilename(); char *poshist = poshistfilename();
...@@ -486,7 +486,7 @@ void load_poshistory(void) ...@@ -486,7 +486,7 @@ void load_poshistory(void)
free(poshist); free(poshist);
} }
/* Save the recorded last file positions to ~/.nano/filepos_history. */ /* Save the recorded cursor positions for files that were edited. */
void save_poshistory(void) void save_poshistory(void)
{ {
char *poshist = poshistfilename(); char *poshist = poshistfilename();
......
...@@ -565,6 +565,7 @@ void finish(void) ...@@ -565,6 +565,7 @@ void finish(void)
tcsetattr(0, TCSANOW, &oldterm); tcsetattr(0, TCSANOW, &oldterm);
#ifndef DISABLE_HISTORIES #ifndef DISABLE_HISTORIES
/* If the user wants history persistence, write the relevant files. */
if (ISSET(HISTORYLOG)) if (ISSET(HISTORYLOG))
save_history(); save_history();
if (ISSET(POS_HISTORY)) { if (ISSET(POS_HISTORY)) {
...@@ -2364,22 +2365,25 @@ int main(int argc, char **argv) ...@@ -2364,22 +2365,25 @@ int main(int argc, char **argv)
UNSET(NO_WRAP); UNSET(NO_WRAP);
#endif #endif
/* If we're using bold text instead of reverse video text, set it up /* If the user wants bold instead of reverse video for hilited text... */
* now. */
if (ISSET(BOLD_TEXT)) if (ISSET(BOLD_TEXT))
hilite_attribute = A_BOLD; hilite_attribute = A_BOLD;
#ifndef DISABLE_HISTORIES #ifndef DISABLE_HISTORIES
/* Set up the search/replace history. */ /* Initialize the pointers for the Search/Replace/Execute histories. */
history_init(); history_init();
/* Verify that the home directory and ~/.nano subdir exist. */
/* If we need any of the history files, verify that the user's home
* directory and its .nano subdirctory exist. */
if (ISSET(HISTORYLOG) || ISSET(POS_HISTORY)) { if (ISSET(HISTORYLOG) || ISSET(POS_HISTORY)) {
get_homedir(); get_homedir();
if (homedir == NULL || check_dotnano() == 0) { if (homedir == NULL || !have_dotnano()) {
UNSET(HISTORYLOG); UNSET(HISTORYLOG);
UNSET(POS_HISTORY); UNSET(POS_HISTORY);
} }
} }
/* If the user wants history persistence, read the relevant files. */
if (ISSET(HISTORYLOG)) if (ISSET(HISTORYLOG))
load_history(); load_history();
if (ISSET(POS_HISTORY)) if (ISSET(POS_HISTORY))
......
...@@ -366,7 +366,7 @@ void get_history_newer_void(void); ...@@ -366,7 +366,7 @@ void get_history_newer_void(void);
#ifdef ENABLE_TABCOMP #ifdef ENABLE_TABCOMP
char *get_history_completion(filestruct **h, char *s, size_t len); char *get_history_completion(filestruct **h, char *s, size_t len);
#endif #endif
int check_dotnano(void); bool have_dotnano(void);
void load_history(void); void load_history(void);
void save_history(void); void save_history(void);
void load_poshistory(void); void load_poshistory(void);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment