[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Nano-devel] [PATCH] new feature: the option --wordchars, to set extra w
From: |
Benno Schulenberg |
Subject: |
[Nano-devel] [PATCH] new feature: the option --wordchars, to set extra word characters |
Date: |
Thu, 30 Jun 2016 18:10:01 +0200 |
This allows the user to specify which other characters, besides the
default alphanumeric ones, should be considered as part of a word, so
that word operations like Ctrl+Left and Ctrl+Right will pass them by.
Using this option overrides the option --wordbounds.
This fulfills https://savannah.gnu.org/bugs/?47283.
---
src/chars.c | 15 +++++++++++++--
src/global.c | 4 ++++
src/nano.c | 20 ++++++++++++++++++--
src/proto.h | 3 +++
src/rcfile.c | 4 ++++
src/utils.c | 4 ++--
6 files changed, 44 insertions(+), 6 deletions(-)
diff --git a/src/chars.c b/src/chars.c
index d36faae..f66d62e 100644
--- a/src/chars.c
+++ b/src/chars.c
@@ -192,8 +192,19 @@ bool is_word_mbchar(const char *c, bool allow_punct)
{
assert(c != NULL);
- return is_alnum_mbchar(c) || (allow_punct ? is_punct_mbchar(c) :
- FALSE);
+ if (is_alnum_mbchar(c))
+ return TRUE;
+
+ if (word_chars != NULL) {
+ char *symbol = charalloc(MB_CUR_MAX + 1);
+ int symlen = parse_mbchar(c, symbol, NULL);
+
+ symbol[symlen] = '\0';
+
+ return (strstr(word_chars, symbol) != NULL);
+ }
+
+ return (allow_punct ? is_punct_mbchar(c) : FALSE);
}
/* Return the visible representation of control character c. */
diff --git a/src/global.c b/src/global.c
index fb8cce2..2ab9188 100644
--- a/src/global.c
+++ b/src/global.c
@@ -124,6 +124,9 @@ size_t quotelen;
#endif
#endif
+char *word_chars = NULL;
+ /* Nonalphanumeric characters that also form words. */
+
bool nodelay_mode = FALSE;
/* Are we checking for a cancel wile doing something? */
@@ -1669,6 +1672,7 @@ void thanks_for_all_the_fish(void)
delwin(edit);
delwin(bottomwin);
+ free(word_chars);
#ifndef DISABLE_JUSTIFY
free(quotestr);
#ifdef HAVE_REGEX_H
diff --git a/src/nano.c b/src/nano.c
index 367d3f3..978ded0 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -860,6 +860,8 @@ void usage(void)
#ifndef NANO_TINY
print_opt("-W", "--wordbounds",
N_("Detect word boundaries more accurately"));
+ print_opt("-X", "--wordchars",
+ N_("Which other characters are word parts"));
#endif
#ifndef DISABLE_COLOR
if (!ISSET(RESTRICTED))
@@ -1995,6 +1997,7 @@ int main(int argc, char **argv)
{"smooth", 0, NULL, 'S'},
{"quickblank", 0, NULL, 'U'},
{"wordbounds", 0, NULL, 'W'},
+ {"wordchars", 1, NULL, 'X'},
{"autoindent", 0, NULL, 'i'},
{"cut", 0, NULL, 'k'},
{"unix", 0, NULL, 'u'},
@@ -2040,11 +2043,11 @@ int main(int argc, char **argv)
while ((optchr =
#ifdef HAVE_GETOPT_LONG
getopt_long(argc, argv,
- "ABC:DEFGHIKLNOPQ:RST:UVWY:abcdefghijklmno:pqr:s:tuvwxz$",
+ "ABC:DEFGHIKLNOPQ:RST:UVWX:Y:abcdefghijklmno:pqr:s:tuvwxz$",
long_options, NULL)
#else
getopt(argc, argv,
- "ABC:DEFGHIKLNOPQ:RST:UVWY:abcdefghijklmno:pqr:s:tuvwxz$")
+ "ABC:DEFGHIKLNOPQ:RST:UVWX:Y:abcdefghijklmno:pqr:s:tuvwxz$")
#endif
) != -1) {
switch (optchr) {
@@ -2146,6 +2149,9 @@ int main(int argc, char **argv)
case 'W':
SET(WORD_BOUNDS);
break;
+ case 'X':
+ word_chars = mallocstrcpy(word_chars, optarg);
+ break;
#endif
#ifndef DISABLE_COLOR
case 'Y':
@@ -2279,6 +2285,7 @@ int main(int argc, char **argv)
#endif
#ifndef NANO_TINY
char *backup_dir_cpy = backup_dir;
+ char *word_chars_cpy = word_chars;
#endif
#ifndef DISABLE_JUSTIFY
char *quotestr_cpy = quotestr;
@@ -2297,6 +2304,7 @@ int main(int argc, char **argv)
#endif
#ifndef NANO_TINY
backup_dir = NULL;
+ word_chars = NULL;
#endif
#ifndef DISABLE_JUSTIFY
quotestr = NULL;
@@ -2327,6 +2335,10 @@ int main(int argc, char **argv)
free(backup_dir);
backup_dir = backup_dir_cpy;
}
+ if (word_chars_cpy != NULL) {
+ free(word_chars);
+ word_chars = word_chars_cpy;
+ }
#endif
#ifndef DISABLE_JUSTIFY
if (quotestr_cpy != NULL) {
@@ -2391,6 +2403,10 @@ int main(int argc, char **argv)
* that backup files will be saved there. */
if (!ISSET(RESTRICTED))
init_backup_dir();
+
+ /* By default, nothing but alphanumeric characters form words. */
+ if (word_chars == NULL)
+ word_chars = mallocstrcpy(NULL, "");
#endif
#ifndef DISABLE_OPERATINGDIR
diff --git a/src/proto.h b/src/proto.h
index c3386af..e959b13 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -91,7 +91,10 @@ extern size_t quotelen;
#endif
#endif /* !DISABLE_JUSTIFY */
+extern char *word_chars;
+
extern bool nodelay_mode;
+
extern char *answer;
extern ssize_t tabsize;
diff --git a/src/rcfile.c b/src/rcfile.c
index 8face04..f35a3d8 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -102,6 +102,7 @@ static const rcoption rcopts[] = {
{"unix", MAKE_IT_UNIX},
{"whitespace", 0},
{"wordbounds", WORD_BOUNDS},
+ {"wordchars", 0},
#endif
#ifndef DISABLE_COLOR
{"titlecolor", 0},
@@ -1176,6 +1177,9 @@ void parse_rcfile(FILE *rcstream
if (strcasecmp(rcopts[i].name, "backupdir") == 0)
backup_dir = option;
else
+ if (strcasecmp(rcopts[i].name, "wordchars") == 0)
+ word_chars = option;
+ else
#endif
#ifndef DISABLE_SPELLER
if (strcasecmp(rcopts[i].name, "speller") == 0)
diff --git a/src/utils.c b/src/utils.c
index 4411499..25294ff 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -296,8 +296,8 @@ bool is_separate_word(size_t position, size_t length, const
char *buf)
* word isn't a non-punctuation "word" character, and if we're at
* the end of the line or the character after the word isn't a
* non-punctuation "word" character, we have a whole word. */
- retval = (position == 0 || !is_word_mbchar(before, FALSE)) &&
- (word_end == strlen(buf) || !is_word_mbchar(after, FALSE));
+ retval = (position == 0 || !is_alnum_mbchar(before)) &&
+ (word_end == strlen(buf) || !is_alnum_mbchar(after));
free(before);
free(after);
--
2.8.4
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Nano-devel] [PATCH] new feature: the option --wordchars, to set extra word characters,
Benno Schulenberg <=