nano

nano with my custom patches
git clone git://bsandro.tech/nano
Log | Files | Refs | README | LICENSE

commit 7a7405571c8f7c34bbbd2142a1efae715454d163
parent 1270657602a9061e586e58ee5f6521a8c4910e8b
Author: Benno Schulenberg <bensberg@telfort.nl>
Date:   Wed, 21 May 2025 12:04:10 +0200

tweaks: rename 'use_utf8' to 'using_utf8', and make it a global variable

This elides a function call in some parts of the code.

Diffstat:
Msrc/chars.c | 40++++++++++++----------------------------
Msrc/global.c | 10++++++----
Msrc/nano.c | 6+++---
Msrc/prompt.c | 2+-
Msrc/prototypes.h | 5+----
Msrc/rcfile.c | 2+-
Msrc/winio.c | 21+++++++++------------
7 files changed, 33 insertions(+), 53 deletions(-)

diff --git a/src/chars.c b/src/chars.c @@ -23,27 +23,11 @@ #include <ctype.h> #include <string.h> - -static bool use_utf8 = FALSE; - /* Whether we've enabled UTF-8 support. */ - #ifdef ENABLE_UTF8 #include <wchar.h> #include <wctype.h> - -/* Enable UTF-8 support. */ -void utf8_init(void) -{ - use_utf8 = TRUE; -} #endif -/* Is UTF-8 support enabled? */ -bool using_utf8(void) -{ - return use_utf8; -} - #ifdef ENABLE_SPELLER /* Return TRUE when the given character is some kind of letter. */ bool is_alpha_char(const char *c) @@ -98,7 +82,7 @@ bool is_blank_char(const char *c) bool is_cntrl_char(const char *c) { #ifdef ENABLE_UTF8 - if (use_utf8) + if (using_utf8) return ((c[0] & 0xE0) == 0 || c[0] == DEL_CODE || ((signed char)c[0] == -62 && (signed char)c[1] < -96)); else @@ -165,7 +149,7 @@ char control_mbrep(const char *c, bool isdata) return '@'; #ifdef ENABLE_UTF8 - if (use_utf8) { + if (using_utf8) { if ((unsigned char)c[0] < 128) return control_rep(c[0]); else @@ -180,7 +164,7 @@ char control_mbrep(const char *c, bool isdata) * the number of bytes in the sequence, or -1 for an invalid sequence. */ int mbtowide(wchar_t *wc, const char *c) { - if ((signed char)*c < 0 && use_utf8) { + if ((signed char)*c < 0 && using_utf8) { unsigned char v1 = c[0]; unsigned char v2 = c[1] ^ 0x80; @@ -229,7 +213,7 @@ bool is_doublewidth(const char *ch) wchar_t wc; /* Only from U+1100 can code points have double width. */ - if ((unsigned char)*ch < 0xE1 || !use_utf8) + if ((unsigned char)*ch < 0xE1 || !using_utf8) return FALSE; if (mbtowide(&wc, ch) < 0) @@ -244,7 +228,7 @@ bool is_zerowidth(const char *ch) wchar_t wc; /* Only from U+0300 can code points have zero width. */ - if ((unsigned char)*ch < 0xCC || !use_utf8) + if ((unsigned char)*ch < 0xCC || !using_utf8) return FALSE; if (mbtowide(&wc, ch) < 0) @@ -264,7 +248,7 @@ bool is_zerowidth(const char *ch) int char_length(const char *pointer) { #ifdef ENABLE_UTF8 - if ((unsigned char)*pointer > 0xC1 && use_utf8) { + if ((unsigned char)*pointer > 0xC1 && using_utf8) { unsigned char c1 = pointer[0]; unsigned char c2 = pointer[1]; @@ -327,7 +311,7 @@ int collect_char(const char *string, char *thechar) int advance_over(const char *string, size_t *column) { #ifdef ENABLE_UTF8 - if ((signed char)*string < 0 && use_utf8) { + if ((signed char)*string < 0 && using_utf8) { /* A UTF-8 upper control code has two bytes and takes two columns. */ if (((unsigned char)string[0] == 0xC2 && (signed char)string[1] < -96)) { *column += 2; @@ -371,7 +355,7 @@ int advance_over(const char *string, size_t *column) size_t step_left(const char *buf, size_t pos) { #ifdef ENABLE_UTF8 - if (use_utf8) { + if (using_utf8) { size_t before, charlen = 0; if (pos < 4) @@ -422,7 +406,7 @@ int mbstrcasecmp(const char *s1, const char *s2) int mbstrncasecmp(const char *s1, const char *s2, size_t n) { #ifdef ENABLE_UTF8 - if (use_utf8) { + if (using_utf8) { wchar_t wc1, wc2; while (*s1 != '\0' && *s2 != '\0' && n > 0) { @@ -473,7 +457,7 @@ int mbstrncasecmp(const char *s1, const char *s2, size_t n) char *mbstrcasestr(const char *haystack, const char *needle) { #ifdef ENABLE_UTF8 - if (use_utf8) { + if (using_utf8) { size_t needle_len = mbstrlen(needle); while (*haystack != '\0') { @@ -535,7 +519,7 @@ char *mbrevstrcasestr(const char *haystack, const char *needle, const char *pointer) { #ifdef ENABLE_UTF8 - if (use_utf8) { + if (using_utf8) { size_t needle_len = mbstrlen(needle); size_t tail_len = mbstrlen(pointer); @@ -564,7 +548,7 @@ char *mbrevstrcasestr(const char *haystack, const char *needle, char *mbstrchr(const char *string, const char *chr) { #ifdef ENABLE_UTF8 - if (use_utf8) { + if (using_utf8) { bool bad_s = FALSE, bad_c = FALSE; wchar_t ws, wc; diff --git a/src/global.c b/src/global.c @@ -34,6 +34,8 @@ volatile sig_atomic_t the_window_resized = FALSE; bool on_a_vt = FALSE; /* Whether we're running on a Linux console (a VT). */ +bool using_utf8 = FALSE; + /* Whether we're in a UTF-8 locale. */ bool shifted_metas = FALSE; /* Whether any Sh-M-<letter> combo has been bound. */ @@ -1354,7 +1356,7 @@ void shortcut_init(void) add_to_sclist(MMOST & ~MMAIN, "^B", 0, do_left, 0); add_to_sclist(MMOST & ~MMAIN, "^F", 0, do_right, 0); #ifdef ENABLE_UTF8 - if (using_utf8()) { + if (using_utf8) { add_to_sclist(MMOST|MBROWSER|MHELP, "\xE2\x97\x82", KEY_LEFT, do_left, 0); add_to_sclist(MMOST|MBROWSER|MHELP, "\xE2\x96\xb8", KEY_RIGHT, do_right, 0); add_to_sclist(MSOME, "^\xE2\x97\x82", CONTROL_LEFT, to_prev_word, 0); @@ -1384,7 +1386,7 @@ void shortcut_init(void) add_to_sclist(MMOST, "Home", KEY_HOME, do_home, 0); add_to_sclist(MMOST, "End", KEY_END, do_end, 0); #ifdef ENABLE_UTF8 - if (using_utf8()) { + if (using_utf8) { add_to_sclist(MMAIN|MBROWSER|MHELP, "\xE2\x96\xb4", KEY_UP, do_up, 0); add_to_sclist(MMAIN|MBROWSER|MHELP, "\xE2\x96\xbe", KEY_DOWN, do_down, 0); add_to_sclist(MMAIN|MBROWSER|MLINTER, "^\xE2\x96\xb4", CONTROL_UP, to_prev_block, 0); @@ -1407,7 +1409,7 @@ void shortcut_init(void) #endif #ifndef NANO_TINY #ifdef ENABLE_UTF8 - if (using_utf8()) { + if (using_utf8) { add_to_sclist(MMAIN|MHELP, "M-\xE2\x96\xb4", ALT_UP, do_scroll_up, 0); add_to_sclist(MMAIN|MHELP, "M-\xE2\x96\xbe", ALT_DOWN, do_scroll_down, 0); } else @@ -1492,7 +1494,7 @@ void shortcut_init(void) add_to_sclist(MWHEREIS|MREPLACE|MREPLACEWITH|MWHEREISFILE|MFINDINHELP|MEXECUTE, "^P", 0, get_older_item, 0); add_to_sclist(MWHEREIS|MREPLACE|MREPLACEWITH|MWHEREISFILE|MFINDINHELP|MEXECUTE, "^N", 0, get_newer_item, 0); #ifdef ENABLE_UTF8 - if (using_utf8()) { + if (using_utf8) { add_to_sclist(MWHEREIS|MREPLACE|MREPLACEWITH|MWHEREISFILE|MFINDINHELP|MEXECUTE, "\xE2\x96\xb4", KEY_UP, get_older_item, 0); add_to_sclist(MWHEREIS|MREPLACE|MREPLACEWITH|MWHEREISFILE|MFINDINHELP|MEXECUTE, "\xE2\x96\xbe", KEY_DOWN, get_newer_item, 0); } else diff --git a/src/nano.c b/src/nano.c @@ -1865,7 +1865,7 @@ int main(int argc, char **argv) /* If setting the locale is successful and it uses UTF-8, we will * need to use the multibyte functions for text processing. */ if (setlocale(LC_ALL, "") && strcmp(nl_langinfo(CODESET), "UTF-8") == 0) - utf8_init(); + using_utf8 = TRUE; #else setlocale(LC_ALL, ""); #endif @@ -2374,7 +2374,7 @@ int main(int argc, char **argv) /* If the whitespace option wasn't specified, set its default value. */ if (whitespace == NULL) { #ifdef ENABLE_UTF8 - if (using_utf8()) { + if (using_utf8) { /* A tab is shown as a Right-Pointing Double Angle Quotation Mark * (U+00BB), and a space as a Middle Dot (U+00B7). */ whitespace = copy_of("\xC2\xBB\xC2\xB7"); @@ -2687,7 +2687,7 @@ int main(int argc, char **argv) #define byte(n) (unsigned char)openfile->current->data[n] /* Tell the user when the cursor sits on a BOM. */ if (openfile->current_x == 0 && byte(0) == 0xEF && byte(1) == 0xBB && - byte(2) == 0xBF && using_utf8()) { + byte(2) == 0xBF && using_utf8) { statusline(NOTICE, _("Byte Order Mark")); set_blankdelay_to_one(); } diff --git a/src/prompt.c b/src/prompt.c @@ -741,7 +741,7 @@ int ask_user(bool withall, const char *question) #ifdef ENABLE_UTF8 /* If the received code is a UTF-8 starter byte, get also the * continuation bytes and assemble them into one letter. */ - if (using_utf8() && 0xC0 <= kbinput && kbinput <= 0xF7) { + if (0xC0 <= kbinput && kbinput <= 0xF7 && using_utf8) { int extras = (kbinput / 16) % 4 + (kbinput <= 0xCF ? 1 : 0); while (extras <= waiting_keycodes() && extras-- > 0) diff --git a/src/prototypes.h b/src/prototypes.h @@ -27,6 +27,7 @@ extern volatile sig_atomic_t the_window_resized; #endif extern bool on_a_vt; +extern bool using_utf8; extern bool shifted_metas; extern bool meta_key; @@ -206,10 +207,6 @@ void to_last_file(void); #endif /* Most functions in chars.c. */ -#ifdef ENABLE_UTF8 -void utf8_init(void); -#endif -bool using_utf8(void); bool is_alpha_char(const char *c); bool is_blank_char(const char *c); bool is_cntrl_char(const char *c); diff --git a/src/rcfile.c b/src/rcfile.c @@ -1585,7 +1585,7 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only) #ifdef ENABLE_UTF8 /* When in a UTF-8 locale, ignore arguments with invalid sequences. */ - if (using_utf8() && mbstowcs(NULL, argument, 0) == (size_t)-1) { + if (using_utf8 && mbstowcs(NULL, argument, 0) == (size_t)-1) { jot_error(N_("Argument is not a valid multibyte string")); continue; } diff --git a/src/winio.c b/src/winio.c @@ -1036,7 +1036,7 @@ int parse_kbinput(WINDOW *frame) return CONTROL_SHIFT_DELETE; #endif #ifdef ENABLE_UTF8 - else if (0xC0 <= keycode && keycode <= 0xFF && using_utf8()) { + else if (0xC0 <= keycode && keycode <= 0xFF && using_utf8) { while (waiting_codes && 0x80 <= nextcodes[0] && nextcodes[0] <= 0xBF) get_input(NULL); return FOREIGN_SEQUENCE; @@ -1085,7 +1085,7 @@ int parse_kbinput(WINDOW *frame) return ERR; } #ifdef ENABLE_UTF8 - else if (byte > 0x7F && using_utf8()) { + else if (byte > 0x7F && using_utf8) { /* Convert the code to the corresponding Unicode, and * put the second byte back into the keyboard buffer. */ if (byte < 0xC0) { @@ -1442,7 +1442,7 @@ int *parse_verbatim_kbinput(WINDOW *frame, size_t *count) #ifdef ENABLE_UTF8 /* If the key code is a hexadecimal digit, commence Unicode input. */ - if (using_utf8() && isxdigit(keycode)) { + if (using_utf8 && isxdigit(keycode)) { long unicode = assemble_unicode(keycode); char multibyte[MB_CUR_MAX]; @@ -2230,16 +2230,13 @@ void minibar(void) if (*this_position == '\0') sprintf(hexadecimal, openfile->current->next ? -#ifdef ENABLE_UTF8 - using_utf8() ? "U+000A" : -#endif - " 0x0A" : " ----"); + (using_utf8 ? "U+000A" : " 0x0A") : " ----"); else if (*this_position == '\n') sprintf(hexadecimal, " 0x00"); #ifdef ENABLE_UTF8 - else if ((unsigned char)*this_position < 0x80 && using_utf8()) + else if ((unsigned char)*this_position < 0x80 && using_utf8) sprintf(hexadecimal, "U+%04X", (unsigned char)*this_position); - else if (using_utf8() && mbtowide(&widecode, this_position) > 0) + else if (using_utf8 && mbtowide(&widecode, this_position) > 0) sprintf(hexadecimal, "U+%04X", (int)widecode); #endif else @@ -2274,7 +2271,7 @@ void minibar(void) /* Indicate it when the line has an anchor. */ if (openfile->current->has_anchor && namewidth + 7 < COLS) - mvwaddstr(footwin, 0, COLS - 5 - padding, using_utf8() ? "\xE2\x80\xA0" : "+"); + mvwaddstr(footwin, 0, COLS - 5 - padding, using_utf8 ? "\xE2\x80\xA0" : "+"); /* Display how many percent the current line is into the file. */ if (namewidth + 6 < COLS) { @@ -2560,7 +2557,7 @@ void draw_row(int row, const char *converted, linestruct *line, size_t from_col) wattroff(midwin, interface_color_pair[LINE_NUMBER]); #ifndef NANO_TINY if (line->has_anchor && (from_col == 0 || !ISSET(SOFTWRAP))) - wprintw(midwin, using_utf8() ? "\xE2\x80\xA0" : "+"); + wprintw(midwin, using_utf8 ? "\xE2\x80\xA0" : "+"); else #endif wprintw(midwin, " "); @@ -2756,7 +2753,7 @@ void draw_row(int row, const char *converted, linestruct *line, size_t from_col) } else if (target_column + 1 == editwincols) { /* Defeat a VTE bug -- see https://sv.gnu.org/bugs/?55896. */ #ifdef ENABLE_UTF8 - if (using_utf8()) { + if (using_utf8) { striped_char[0] = '\xC2'; striped_char[1] = '\xA0'; charlen = 2;