new feature: allow specifying a custom nanorc file on the command line

This fulfills https://savannah.gnu.org/bugs/?57547.
Requested-by: Saagar Jha <saagar@saagarjha.com>
This commit is contained in:
Benno Schulenberg 2020-01-14 12:41:22 +01:00
parent 706f3e93f4
commit 343f97b3ac
4 changed files with 28 additions and 2 deletions

View File

@ -249,6 +249,9 @@ char *statedir = NULL;
char *startup_problem = NULL;
/* An error message (if any) about nanorc files or history files. */
#endif
#ifdef ENABLE_NANORC
char *custom_nanorc = NULL;
#endif
bool spotlighted = FALSE;
/* Whether any text is spotlighted. */

View File

@ -635,6 +635,10 @@ void usage(void)
print_opt("-d", "--rebinddelete",
N_("Fix Backspace/Delete confusion problem"));
print_opt("-e", "--emptyline", N_("Keep the line below the title bar empty"));
#ifdef ENABLE_NANORC
print_opt(_("-f <file>"), _("--rcfile=<file>"),
N_("Use only this file for configuring nano"));
#endif
#ifdef ENABLE_BROWSER
if (!ISSET(RESTRICTED))
print_opt("-g", "--showcursor", N_("Show cursor in file browser & help text"));
@ -1781,6 +1785,9 @@ int main(int argc, char **argv)
{"constantshow", 0, NULL, 'c'},
{"rebinddelete", 0, NULL, 'd'},
{"emptyline", 0, NULL, 'e'},
#ifdef ENABLE_NANORC
{"rcfile", 1, NULL, 'f'},
#endif
#ifdef ENABLE_BROWSER
{"showcursor", 0, NULL, 'g'},
#endif
@ -1889,7 +1896,7 @@ int main(int argc, char **argv)
while ((optchr =
getopt_long(argc, argv,
"ABC:DEFGHIJ:KLMNOPQ:RST:UVWX:Y:Zabcdeghijklmno:pr:s:tuvwxyz$",
"ABC:DEFGHIJ:KLMNOPQ:RST:UVWX:Y:Zabcdef:ghijklmno:pr:s:tuvwxyz$",
long_options, NULL)) != -1) {
switch (optchr) {
#ifndef NANO_TINY
@ -2029,6 +2036,11 @@ int main(int argc, char **argv)
case 'e':
SET(EMPTY_LINE);
break;
#ifdef ENABLE_NANORC
case 'f':
custom_nanorc = mallocstrcpy(custom_nanorc, optarg);
break;
#endif
case 'g':
SET(SHOW_CURSOR);
break;

View File

@ -175,6 +175,9 @@ extern char *statedir;
#if defined(ENABLE_NANORC) || defined(ENABLE_HISTORIES)
extern char *startup_problem;
#endif
#ifdef ENABLE_NANORC
extern char *custom_nanorc;
#endif
extern bool spotlighted;
extern size_t light_from_col;

View File

@ -1647,11 +1647,18 @@ void do_rcfiles(void)
{
const char *xdgconfdir;
if (custom_nanorc) {
nanorc = get_full_path(custom_nanorc);
if (access(nanorc, F_OK) != 0)
die(_("Specified rcfile does not exist\n"));
} else
nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc");
/* First process the system-wide nanorc, if it exists and is suitable. */
nanorc = mallocstrcpy(nanorc, SYSCONFDIR "/nanorc");
if (is_good_file(nanorc))
parse_one_nanorc();
if (custom_nanorc == NULL) {
get_homedir();
xdgconfdir = getenv("XDG_CONFIG_HOME");
@ -1663,6 +1670,7 @@ void do_rcfiles(void)
parse_one_nanorc();
else if (homedir == NULL && xdgconfdir == NULL)
jot_error(N_("I can't find my home directory! Wah!"));
}
check_vitals_mapped();