From 3ab8b34b1e9ff0bd15eb7afc9071dd043971664d Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 16 May 2023 14:09:30 -0500 Subject: [PATCH] Use Rust version of global fallback variables --- fish-rust/src/fallback.rs | 2 ++ src/env_dispatch.cpp | 16 ++++++++-------- src/fallback.cpp | 11 ++--------- src/fallback.h | 14 +++++++------- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/fish-rust/src/fallback.rs b/fish-rust/src/fallback.rs index f0ff63a31..01ddd900f 100644 --- a/fish-rust/src/fallback.rs +++ b/fish-rust/src/fallback.rs @@ -12,6 +12,7 @@ /// Width of ambiguous East Asian characters and, as of TR11, all private-use characters. /// 1 is the typical default, but we accept any non-negative override via `$fish_ambiguous_width`. +#[no_mangle] pub static FISH_AMBIGUOUS_WIDTH: AtomicI32 = AtomicI32::new(1); /// Width of emoji characters. @@ -24,6 +25,7 @@ /// Valid values are 1, and 2. 1 is the typical emoji width used in Unicode 8 while some newer /// terminals use a width of 2 since Unicode 9. // For some reason, this is declared here and exposed here, but is set in `env_dispatch`. +#[no_mangle] pub static FISH_EMOJI_WIDTH: AtomicI32 = AtomicI32::new(1); static WC_LOOKUP_TABLE: Lazy = Lazy::new(WcLookupTable::new); diff --git a/src/env_dispatch.cpp b/src/env_dispatch.cpp index 8ff4adfdb..58b3d3a23 100644 --- a/src/env_dispatch.cpp +++ b/src/env_dispatch.cpp @@ -149,13 +149,13 @@ static void handle_timezone(const wchar_t *env_var_name, const environment_t &va tzset(); } -/// Update the value of g_fish_emoji_width +/// Update the value of FISH_EMOJI_WIDTH static void guess_emoji_width(const environment_t &vars) { if (auto width_str = vars.get(L"fish_emoji_width")) { int new_width = fish_wcstol(width_str->as_string().c_str()); - g_fish_emoji_width = std::min(2, std::max(1, new_width)); + FISH_EMOJI_WIDTH = std::min(2, std::max(1, new_width)); FLOGF(term_support, "'fish_emoji_width' preference: %d, overwriting default", - g_fish_emoji_width); + FISH_EMOJI_WIDTH); return; } @@ -172,18 +172,18 @@ static void guess_emoji_width(const environment_t &vars) { if (term == L"Apple_Terminal" && version >= 400) { // Apple Terminal on High Sierra - g_fish_emoji_width = 2; + FISH_EMOJI_WIDTH = 2; FLOGF(term_support, "default emoji width: 2 for %ls", term.c_str()); } else if (term == L"iTerm.app") { // iTerm2 now defaults to Unicode 9 sizes for anything after macOS 10.12. - g_fish_emoji_width = 2; + FISH_EMOJI_WIDTH = 2; FLOGF(term_support, "default emoji width for iTerm: 2"); } else { // Default to whatever system wcwidth says to U+1F603, // but only if it's at least 1 and at most 2. int w = wcwidth(L'😃'); - g_fish_emoji_width = std::min(2, std::max(1, w)); - FLOGF(term_support, "default emoji width: %d", g_fish_emoji_width); + FISH_EMOJI_WIDTH = std::min(2, std::max(1, w)); + FLOGF(term_support, "default emoji width: %d", FISH_EMOJI_WIDTH); } } @@ -209,7 +209,7 @@ static void handle_change_ambiguous_width(const env_stack_t &vars) { if (auto width_str = vars.get(L"fish_ambiguous_width")) { new_width = fish_wcstol(width_str->as_string().c_str()); } - g_fish_ambiguous_width = std::max(0, new_width); + FISH_AMBIGUOUS_WIDTH = std::max(0, new_width); } static void handle_term_size_change(const env_stack_t &vars) { diff --git a/src/fallback.cpp b/src/fallback.cpp index 966bb28aa..e022f3cab 100644 --- a/src/fallback.cpp +++ b/src/fallback.cpp @@ -129,16 +129,9 @@ int killpg(int pgr, int sig) { } #endif -// Width of ambiguous characters. 1 is typical default. -int g_fish_ambiguous_width = 1; - -// Width of emoji characters. -// 1 is the typical emoji width in Unicode 8. -int g_fish_emoji_width = 1; - static int fish_get_emoji_width(wchar_t c) { (void)c; - return g_fish_emoji_width; + return FISH_EMOJI_WIDTH; } // Big hack to use our versions of wcswidth where we know them to be broken, which is @@ -179,7 +172,7 @@ int fish_wcwidth(wchar_t wc) { case widechar_ambiguous: case widechar_private_use: // TR11: "All private-use characters are by default classified as Ambiguous". - return g_fish_ambiguous_width; + return FISH_AMBIGUOUS_WIDTH; case widechar_widened_in_9: return fish_get_emoji_width(wc); default: diff --git a/src/fallback.h b/src/fallback.h index 79ed82812..b56caafe1 100644 --- a/src/fallback.h +++ b/src/fallback.h @@ -1,6 +1,7 @@ #ifndef FISH_FALLBACK_H #define FISH_FALLBACK_H +#include #include "config.h" // The following include must be kept despite what IWYU says. That's because of the interaction @@ -8,15 +9,14 @@ // in . At least on OS X if we don't do this we get compilation errors do to the macro // substitution if wchar.h is included after this header. #include // IWYU pragma: keep + // +// Width of ambiguous characters. 1 is typical default. +extern int32_t FISH_AMBIGUOUS_WIDTH; -/// The column width of ambiguous East Asian characters. -extern int g_fish_ambiguous_width; +// Width of emoji characters. +// 1 is the typical emoji width in Unicode 8. +extern int32_t FISH_EMOJI_WIDTH; -/// The column width of emoji characters. This must be configurable because the value changed -/// between Unicode 8 and Unicode 9, wcwidth() is emoji-ignorant, and terminal emulators do -/// different things. See issues like #4539 and https://github.com/neovim/neovim/issues/4976 for how -/// painful this is. A value of 0 means to use the guessed value. -extern int g_fish_emoji_width; /// fish's internal versions of wcwidth and wcswidth, which can use an internal implementation if /// the system one is busted.