new feature: option -z lists the names of available syntaxes

When one has installed additional syntaxes, one tends to forget
what exactly is there.  So it's nice to be able to list them.

The syntaxes are listed in the reverse order in which they were
read: the most recent first.

The long form of the option is, of course, --listsyntaxes,
which can be abbreviated to --list.

This fulfills https://savannah.gnu.org/bugs/?65779.
The feature was suggested by `davidhcefx`.
This commit is contained in:
Benno Schulenberg 2024-05-25 09:22:11 +02:00
parent b408147f48
commit 49c2f5dea9

View File

@ -642,6 +642,9 @@ void usage(void)
#ifndef NANO_TINY
print_opt("-y", "--afterends", N_("Make Ctrl+Right stop at word ends"));
#endif
#ifdef ENABLE_COLOR
print_opt("-z", "--listsyntaxes", N_("List the names of available syntaxes"));
#endif
#ifdef HAVE_LIBMAGIC
print_opt("-!", "--magic", N_("Also try magic to determine syntax"));
#endif
@ -796,6 +799,27 @@ void version(void)
printf("\n");
}
#ifdef ENABLE_COLOR
/* List the names of the available syntaxes. */
void list_syntax_names(void)
{
int width = 0;
printf(_("Available syntaxes:\n"));
for (syntaxtype *sntx = syntaxes; sntx != NULL; sntx = sntx->next) {
if (width > 45) {
printf("\n");
width = 0;
}
printf(" %s", sntx->name);
width += wideness(sntx->name, 45 * 4);
}
printf("\n");
}
#endif
/* Register that Ctrl+C was pressed during some system call. */
void make_a_note(int signal)
{
@ -1773,6 +1797,9 @@ int main(int argc, char **argv)
{"nowrap", 0, NULL, 'w'},
#endif
{"nohelp", 0, NULL, 'x'},
#ifdef ENABLE_COLOR
{"listsyntaxes", 0, NULL, 'z'},
#endif
{"modernbindings", 0, NULL, '/'},
#ifndef NANO_TINY
{"smarthome", 0, NULL, 'A'},
@ -1852,7 +1879,7 @@ int main(int argc, char **argv)
SET(MODERN_BINDINGS);
while ((optchr = getopt_long(argc, argv, "ABC:DEFGHIJ:KLMNOPQ:RS$T:UVWX:Y:Z"
"abcdef:ghijklmno:pqr:s:tuvwxy!@%_0/", long_options, NULL)) != -1) {
"abcdef:ghijklmno:pqr:s:tuvwxyz!@%_0/", long_options, NULL)) > 0) {
switch (optchr) {
#ifndef NANO_TINY
case 'A':
@ -2085,6 +2112,14 @@ int main(int argc, char **argv)
SET(AFTER_ENDS);
break;
#endif
#ifdef ENABLE_COLOR
case 'z':
if (!ignore_rcfiles)
do_rcfiles();
if (syntaxes)
list_syntax_names();
exit(0);
#endif
#ifdef HAVE_LIBMAGIC
case '!':
SET(USE_MAGIC);