diff --git a/core/include/wowtype_core.h b/core/include/wowtype_core.h index 44298484..e4c2f07e 100644 --- a/core/include/wowtype_core.h +++ b/core/include/wowtype_core.h @@ -498,8 +498,15 @@ typedef struct { * 也算,这个键照切了以后的模式处理)、到了时间(tick)、第二下没算成单按时切,这几个 * frame 都可能带 mode_changed(和 commit:打到一半切英文)。按键没带 time_ms 时照旧立刻切。 * 默认 0:外壳要会这些才开 */ + int quote_style; /* 1.15 起:全角标点(punct_style 为 0)下双引号、单引号上屏成哪一套, + * WOWTYPE_QUOTE_STYLE_*。默认 CURLY(“”‘’);CORNER 是「」『』, + * 左右交替和数字后面保持半角的规则两套一样 */ } WowtypeSessionConfig; +/* quote_style 的取值(1.15 起)。 */ +#define WOWTYPE_QUOTE_STYLE_CURLY 0 /* “ ” ‘ ’(默认) */ +#define WOWTYPE_QUOTE_STYLE_CORNER 1 /* 「 」 『 』 */ + /* 一次调用之后外壳要做的事。归会话所有,和里面的字符串一起在同一个会话的下一次调用前有效。 * 读 size 之外的字段前先看 size(旧核心给的结构体短)。 */ typedef struct { diff --git a/core/src/session.rs b/core/src/session.rs index ef6ffd07..c786ce8e 100644 --- a/core/src/session.rs +++ b/core/src/session.rs @@ -21,7 +21,7 @@ //! [`Session::choose`],表情面板往编辑区加搜索词用 [`Session::append`],这些过渡用法挪完以后会删掉。 use crate::datetime::LocalTime; -pub use crate::symbols::PunctStyle; +pub use crate::symbols::{PunctStyle, QuoteStyle}; use crate::translate::JobState; use crate::Engine; @@ -523,6 +523,8 @@ pub struct Config { pub pick_char: bool, /// 标点样式:全角(中文标点)或半角(原样交给应用)。 pub punct_style: PunctStyle, + /// 全角样式下引号用哪一套:弯引号(“”‘’,默认)或直角引号(「」『』)。 + pub quote_style: QuoteStyle, /// 中英文之间自动补空格,[`crate::autospace`] 的 `LETTERS | DIGITS` 位掩码,0 是关。 pub auto_space: u32, /// 用哪边的 Shift 单按切换中英文。 @@ -577,6 +579,7 @@ impl Default for Config { select_keys: SelectKeys::Off, pick_char: true, punct_style: PunctStyle::Fullwidth, + quote_style: QuoteStyle::Curly, auto_space: 0, shift_toggle: ShiftToggle::Both, shift_tap_ms: 500, @@ -2039,7 +2042,7 @@ impl<'e> Session<'e> { return false; } if let Some(top) = self.pairs.last().copied() { - if crate::symbols::closing_punct(c) == Some(top.close) { + if crate::symbols::closing_punct_with(c, self.config.quote_style) == Some(top.close) { if self.hints & key_hints::NEXT_CHAR == 0 || self.next_char == Some(top.close) { self.skip_pair(top); return true; @@ -2051,7 +2054,7 @@ impl<'e> Session<'e> { if prev.is_some_and(|p| p.is_ascii_digit()) && crate::symbols::numeric_punct(c) { return false; } - let Some((open, close)) = crate::symbols::auto_pair(c) else { + let Some((open, close)) = crate::symbols::auto_pair_with(c, self.config.quote_style) else { return false; }; self.emit(&format!("{open}{close}")); @@ -2139,7 +2142,14 @@ impl<'e> Session<'e> { /// c 在全角样式下换成的中文标点;半角样式、数字写法里的符号、没有中文标点的是 None。 fn punct_text(&mut self, c: char, prev: Option) -> Option { - crate::symbols::punct_with(c, self.config.punct_style, prev, &mut self.quotes).map(str::to_owned) + crate::symbols::punct_with_quotes( + c, + self.config.punct_style, + self.config.quote_style, + prev, + &mut self.quotes, + ) + .map(str::to_owned) } /// 上屏 text 之前要不要补一个空格(汉字和字母、数字之间,设置里的 AutoSpace)。 diff --git a/core/src/session_ffi.rs b/core/src/session_ffi.rs index ae12d528..285b35cf 100644 --- a/core/src/session_ffi.rs +++ b/core/src/session_ffi.rs @@ -12,7 +12,7 @@ use std::panic; use std::ptr; use crate::ffi::{set_last_error, WowtypeEngineHandle}; -use crate::symbols::PunctStyle; +use crate::symbols::{PunctStyle, QuoteStyle}; use crate::session::{ ArrowKeys, Candidate, CandidateKind, Config, Frame, Key, KeyAction, Mode, Panel, SelectKeys, Session, ShiftToggle, Tool, @@ -196,6 +196,10 @@ pub const WOWTYPE_SELECT_KEYS_SEMICOLON_QUOTE: c_int = 2; pub const WOWTYPE_ARROW_KEYS_CANDIDATES: c_int = 0; pub const WOWTYPE_ARROW_KEYS_CARET: c_int = 1; +/// 全角标点下引号的样式(1.15 起)。 +pub const WOWTYPE_QUOTE_STYLE_CURLY: c_int = 0; +pub const WOWTYPE_QUOTE_STYLE_CORNER: c_int = 1; + /// 会话的设置(C 布局)。外壳没填的字段(size 比我们认识的短)用默认值。 #[repr(C)] #[derive(Clone, Copy)] @@ -247,6 +251,9 @@ pub struct WowtypeSessionConfig { /// 1.14 起:双击 Shift 不切换(单按先等一会儿再切,#253),默认 0(外壳要会按 `wake_after_ms` 调 tick、 /// 跟着 tick 和 OBSERVE 的 frame 里的 `mode_changed` 才开)。 pub shift_double_tap: c_int, + /// 1.15 起:全角样式下引号用哪一套,`WOWTYPE_QUOTE_STYLE_*`,默认弯引号(旧外壳给的结构体短, + /// 这一项保持 Default 的值,就是原来的行为)。 + pub quote_style: c_int, } impl Default for WowtypeSessionConfig { @@ -290,6 +297,10 @@ impl Default for WowtypeSessionConfig { association: c_int::from(config.association), auto_pair_punct: c_int::from(config.auto_pair_punct), shift_double_tap: c_int::from(config.shift_double_tap), + quote_style: match config.quote_style { + QuoteStyle::Curly => WOWTYPE_QUOTE_STYLE_CURLY, + QuoteStyle::Corner => WOWTYPE_QUOTE_STYLE_CORNER, + }, } } } @@ -318,6 +329,11 @@ impl WowtypeSessionConfig { } else { PunctStyle::Fullwidth }, + quote_style: if self.quote_style == WOWTYPE_QUOTE_STYLE_CORNER { + QuoteStyle::Corner + } else { + QuoteStyle::Curly + }, auto_space: self.auto_space, shift_toggle: match self.shift_toggle { WOWTYPE_SHIFT_TOGGLE_LEFT => ShiftToggle::Left, diff --git a/core/src/symbols.rs b/core/src/symbols.rs index 694e36a1..bad4cdfd 100644 --- a/core/src/symbols.rs +++ b/core/src/symbols.rs @@ -110,26 +110,48 @@ pub fn fullwidth_punct(ascii: char) -> Option<&'static str> { /// 自动配对:左标点的 ASCII 键给出的 `(左半边, 右半边)`,和 [`fullwidth_punct`] 一致。 pub fn auto_pair(ascii: char) -> Option<(char, char)> { + auto_pair_with(ascii, QuoteStyle::default()) +} + +/// 同 [`auto_pair`],引号按 `style` 取(括号两种样式一样)。 +pub fn auto_pair_with(ascii: char, style: QuoteStyle) -> Option<(char, char)> { Some(match ascii { '(' => ('(', ')'), '[' => ('【', '】'), '<' => ('《', '》'), '{' => ('「', '」'), - '"' => ('“', '”'), - '\'' => ('‘', '’'), + '"' => match style { + QuoteStyle::Curly => ('“', '”'), + QuoteStyle::Corner => ('「', '」'), + }, + '\'' => match style { + QuoteStyle::Curly => ('‘', '’'), + QuoteStyle::Corner => ('『', '』'), + }, _ => return None, }) } /// 自动配对:右标点的 ASCII 键对应的右半边(跳过补好的那个时比对)。引号左右是同一个键。 pub fn closing_punct(ascii: char) -> Option { + closing_punct_with(ascii, QuoteStyle::default()) +} + +/// 同 [`closing_punct`],引号按 `style` 取。 +pub fn closing_punct_with(ascii: char, style: QuoteStyle) -> Option { Some(match ascii { ')' => ')', ']' => '】', '>' => '》', '}' => '」', - '"' => '”', - '\'' => '’', + '"' => match style { + QuoteStyle::Curly => '”', + QuoteStyle::Corner => '」', + }, + '\'' => match style { + QuoteStyle::Curly => '’', + QuoteStyle::Corner => '』', + }, _ => return None, }) } @@ -141,6 +163,19 @@ pub enum PunctStyle { Halfwidth, } +/// 全角样式下双引号、单引号上屏成什么。 +/// +/// 弯引号(“”‘’)是 GB/T 15834 的写法;直角引号(「」『』)是繁体中文和部分 +/// 排版规范的习惯。两套都有人用,让用户自己选,默认弯引号保持原有行为。 +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum QuoteStyle { + /// “ ” ‘ ’(默认) + #[default] + Curly, + /// 「 」 『 』 + Corner, +} + /// 按样式和光标前一个字符决定标点。返回 `None` 表示原样上屏这个 ASCII 字符。 /// /// - 半角样式:一律原样上屏 @@ -177,6 +212,17 @@ pub fn punct_with( style: PunctStyle, prev: Option, quotes: &mut [bool; 2], +) -> Option<&'static str> { + punct_with_quotes(ascii, style, crate::symbols::QuoteStyle::default(), prev, quotes) +} + +/// 同 [`punct_with`],另指定引号样式。 +pub fn punct_with_quotes( + ascii: char, + style: PunctStyle, + quote_style: QuoteStyle, + prev: Option, + quotes: &mut [bool; 2], ) -> Option<&'static str> { if style == PunctStyle::Halfwidth { return None; @@ -185,7 +231,7 @@ pub fn punct_with( return None; } if ascii == '"' || ascii == '\'' { - return Some(quote(ascii, prev, quotes)); + return Some(quote(ascii, quote_style, prev, quotes)); } fullwidth_punct(ascii) } @@ -195,15 +241,18 @@ thread_local! { static QUOTE_OPEN: std::cell::Cell<[bool; 2]> = const { std::cell::Cell::new([false; 2]) }; } -/// 引号左右交替:“……” ‘……’。光标前是空白或左括号时一定给左引号。 -fn quote(ascii: char, prev: Option, quotes: &mut [bool; 2]) -> &'static str { - let (slot, open, close) = if ascii == '"' { - (0, "“", "”") - } else { - (1, "‘", "’") +/// 引号左右交替:“……” ‘……’ 或 「……」『……』。光标前是空白或左括号时一定给左引号。 +fn quote(ascii: char, style: QuoteStyle, prev: Option, quotes: &mut [bool; 2]) -> &'static str { + let (slot, open, close) = match (ascii, style) { + ('"', QuoteStyle::Curly) => (0, "“", "”"), + ('"', QuoteStyle::Corner) => (0, "「", "」"), + (_, QuoteStyle::Curly) => (1, "‘", "’"), + (_, QuoteStyle::Corner) => (1, "『", "』"), }; + // 列表里只放括号类字符,不放「『:它们是 Corner 样式下引号自己上屏的字符, + // 放进来会让连着按第二个引号键又被当成"前面跟的是左括号",一直给左引号。 let want_open = - prev.is_some_and(|c| c.is_whitespace() || "(([【<《{「『".contains(c)) || !quotes[slot]; + prev.is_some_and(|c| c.is_whitespace() || "(([【<《{".contains(c)) || !quotes[slot]; quotes[slot] = want_open; if want_open { open @@ -323,6 +372,50 @@ mod tests { assert_eq!(punct('"', Halfwidth, None), None); } + #[test] + fn corner_quote_style_uses_corner_brackets() { + use PunctStyle::*; + let mut q = [false; 2]; + // 双引号:先左后右 + assert_eq!( + punct_with_quotes('"', Fullwidth, QuoteStyle::Corner, None, &mut q), + Some("「") + ); + assert_eq!( + punct_with_quotes('"', Fullwidth, QuoteStyle::Corner, Some('好'), &mut q), + Some("」") + ); + // 单引号单独计数 + assert_eq!( + punct_with_quotes('\'', Fullwidth, QuoteStyle::Corner, Some('好'), &mut q), + Some("『") + ); + assert_eq!( + punct_with_quotes('\'', Fullwidth, QuoteStyle::Corner, Some('好'), &mut q), + Some("』") + ); + // 打完左引号紧跟着再按同一个键,仍然是右引号(「 不在"前面是左括号"那张表里) + let mut q = [false; 2]; + assert_eq!( + punct_with_quotes('"', Fullwidth, QuoteStyle::Corner, None, &mut q), + Some("「") + ); + assert_eq!( + punct_with_quotes('"', Fullwidth, QuoteStyle::Corner, Some('「'), &mut q), + Some("」") + ); + // 半角样式下不换成引号 + assert_eq!( + punct_with_quotes('"', Halfwidth, QuoteStyle::Corner, None, &mut q), + None + ); + // 默认仍给弯引号 + assert_eq!( + punct_with_quotes('"', Fullwidth, QuoteStyle::default(), None, &mut q), + Some("“") + ); + } + #[test] fn searches_by_pinyin_prefix() { let hits = search("juhao", 5); diff --git a/fcitx5-addon/src/wowtype_engine.cpp b/fcitx5-addon/src/wowtype_engine.cpp index af55d8cf..e8e9d9e3 100644 --- a/fcitx5-addon/src/wowtype_engine.cpp +++ b/fcitx5-addon/src/wowtype_engine.cpp @@ -1161,6 +1161,12 @@ void WowtypeEngine::applyConfig() { punctStyle_ = name == "half" || name == "halfwidth" ? WowtypePunctStyle::Halfwidth : WowtypePunctStyle::Fullwidth; } + quoteStyle_ = *config_.quoteStyle; + if (const char *env = std::getenv("WOWTYPE_QUOTE_STYLE"); env && *env) { + const std::string name = env; + quoteStyle_ = name == "corner" || name == "cornerBracket" ? WowtypeQuoteStyle::Corner + : WowtypeQuoteStyle::Curly; + } traditional_ = envOr("WOWTYPE_TRADITIONAL", *config_.traditional); emojiSuggest_ = envOr("WOWTYPE_EMOJI", *config_.emojiSuggest); emojiKey_ = *config_.emojiKey; @@ -1521,6 +1527,8 @@ void WowtypeEngine::syncSession(WowtypeState *state) { : WOWTYPE_SELECT_KEYS_OFF; config.pick_char = pickChar_ ? 1 : 0; config.punct_style = punctStyle_ == WowtypePunctStyle::Halfwidth ? 1 : 0; + config.quote_style = quoteStyle_ == WowtypeQuoteStyle::Corner ? WOWTYPE_QUOTE_STYLE_CORNER + : WOWTYPE_QUOTE_STYLE_CURLY; config.auto_space = static_cast(autoSpaceFlags_); config.shift_toggle = shiftToggle_ == WowtypeShiftToggle::Left ? WOWTYPE_SHIFT_TOGGLE_LEFT : shiftToggle_ == WowtypeShiftToggle::Right ? WOWTYPE_SHIFT_TOGGLE_RIGHT diff --git a/fcitx5-addon/src/wowtype_engine.h b/fcitx5-addon/src/wowtype_engine.h index e921413f..b8aa560e 100644 --- a/fcitx5-addon/src/wowtype_engine.h +++ b/fcitx5-addon/src/wowtype_engine.h @@ -126,6 +126,19 @@ struct WowtypePunctStyleAnnotation { } }; +/// 全角标点下引号用哪一套。 +enum class WowtypeQuoteStyle { Curly, Corner }; +FCITX_CONFIG_ENUM_NAME(WowtypeQuoteStyle, "Curly", "Corner"); + +struct WowtypeQuoteStyleAnnotation { + bool skipDescription() const { return false; } + bool skipSave() const { return false; } + void dumpDescription(fcitx::RawConfig &config) const { + config.setValueByPath("EnumI18n/0", "弯引号 “ ” ‘ ’"); + config.setValueByPath("EnumI18n/1", "直角引号 「 」 『 』"); + } +}; + /// 单按哪个 Shift 切换中英文。 enum class WowtypeShiftToggle { Both, Left, Right, Off }; FCITX_CONFIG_ENUM_NAME(WowtypeShiftToggle, "Both", "Left", "Right", "Off"); @@ -246,6 +259,9 @@ FCITX_CONFIGURATION( fcitx::Option, fcitx::DefaultMarshaller, WowtypePunctStyleAnnotation> punctStyle{this, "PunctuationStyle", "标点样式", WowtypePunctStyle::Fullwidth}; + fcitx::Option, + fcitx::DefaultMarshaller, WowtypeQuoteStyleAnnotation> + quoteStyle{this, "QuoteStyle", "引号样式", WowtypeQuoteStyle::Curly}; fcitx::Option, fcitx::DefaultMarshaller, WowtypeSelectKeysAnnotation> selectKeys{this, "SelectKeys", "分号、引号选词", WowtypeSelectKeys::Off}; @@ -661,6 +677,7 @@ class WowtypeEngine final : public fcitx::InputMethodEngineV2 { /// 启动后还没切到过 WowType:第一次切过来(包括 WowType 是默认输入法时第一次获得焦点)按 InitialMode 定模式 bool initialModeApplied_ = false; WowtypePunctStyle punctStyle_ = WowtypePunctStyle::Fullwidth; + WowtypeQuoteStyle quoteStyle_ = WowtypeQuoteStyle::Curly; /// 打开的翻页键组(Page Up/Down、方向键、fcitx 全局翻页键一直可用) bool pageMinus_ = true; bool pageBracket_ = false; diff --git a/fcitx5-addon/test/key_harness.cpp b/fcitx5-addon/test/key_harness.cpp index 481850fd..0e7e4e36 100644 --- a/fcitx5-addon/test/key_harness.cpp +++ b/fcitx5-addon/test/key_harness.cpp @@ -1606,6 +1606,41 @@ void runCases(Instance &instance) { join(page(ic.get()))); fresh(ic.get()); + // QuoteStyle:全角标点下引号用直角引号(#303) + { + std::ofstream(configDir + "/wowtype.conf") << "QuoteStyle=Corner\n"; + } + addon->reloadConfig(); + fresh(ic.get()); + type(ic.get(), "\"\""); + check(ic->committed == "「」", "QuoteStyle=Corner:两次引号键给「」,实际 " + ic->committed); + fresh(ic.get()); + type(ic.get(), "''"); + check(ic->committed == "『』", "QuoteStyle=Corner:两次单引号键给『』,实际 " + ic->committed); + fresh(ic.get()); + // 半角标点时不换成直角引号 + { + std::ofstream(configDir + "/wowtype.conf") << "QuoteStyle=Corner\nPunctuationStyle=Halfwidth\n"; + } + addon->reloadConfig(); + fresh(ic.get()); + type(ic.get(), "\""); + check(ic->committed.empty() && ic->passed == "\"", "半角标点下引号原样,实际 上屏「" + + ic->committed + "」交给应用「" + ic->passed + "」"); + // 默认(弯引号)不能变 + { + std::ofstream(configDir + "/wowtype.conf") << "QuoteStyle=Curly\n"; + } + addon->reloadConfig(); + fresh(ic.get()); + type(ic.get(), "\"\""); + check(ic->committed == "“”", "QuoteStyle=Curly:两次引号键给“”,实际 " + ic->committed); + { + std::ofstream(configDir + "/wowtype.conf") << ""; + } + addon->reloadConfig(); + fresh(ic.get()); + // #129:「方向键」设成左右键移动光标:左键把光标移进拼音,字母插在光标处,退格删光标前一个; // 右键到了末尾不去挑候选,上下键选词 { diff --git a/settings/wowtype-config b/settings/wowtype-config index b9508871..3e5e9c80 100755 --- a/settings/wowtype-config +++ b/settings/wowtype-config @@ -361,6 +361,7 @@ DEFAULTS = { "WindowMode": "Same", "Traditional": "False", "PunctuationStyle": "Fullwidth", + "QuoteStyle": "Curly", "SelectKeys": "Off", "PickChar": "True", "Association": "False", @@ -507,6 +508,11 @@ PUNCT_STYLES = [ ("半角 , .", "Halfwidth"), ] +QUOTE_STYLES = [ + ("弯引号 “ ” ‘ ’", "Curly"), + ("直角引号 「 」 『 』", "Corner"), +] + SHIFT_TOGGLES = [ ("左右 Shift 都行", "Both"), ("左 Shift", "Left"), @@ -560,6 +566,7 @@ ENUMS = { "InitialMode": tuple(value for _label, value in INITIAL_MODES), "WindowMode": tuple(value for _label, value in WINDOW_MODES), "PunctuationStyle": tuple(value for _label, value in PUNCT_STYLES), + "QuoteStyle": tuple(value for _label, value in QUOTE_STYLES), "SelectKeys": tuple(value for _label, value in SELECT_KEYS), "TranslateTarget": tuple(code for _label, code, _english in TRANSLATE_LANGUAGES), "SettingsTheme": tuple(value for _label, value in SETTINGS_THEMES), @@ -6223,6 +6230,19 @@ class SettingsWindow(Adw.ApplicationWindow): self.punct_row.connect("notify::selected", lambda row, _p: self.set_value( "PunctuationStyle", self.punct_names[row.get_selected()])) defaults.add(self.punct_row) + self.quote_names = [name for _, name in QUOTE_STYLES] + self.quote_row = Adw.ComboRow( + title="引号样式", + subtitle="全角标点下引号用哪一套", + model=Gtk.StringList.new([label for label, _ in QUOTE_STYLES])) + self.add_info(self.quote_row, "只影响全角标点,半角不受影响;" + "左右交替和数字后面保持半角的规则两套一样。") + current = self.values.get("QuoteStyle", "Curly") + self.quote_row.set_selected( + self.quote_names.index(current) if current in self.quote_names else 0) + self.quote_row.connect("notify::selected", lambda row, _p: self.set_value( + "QuoteStyle", self.quote_names[row.get_selected()])) + defaults.add(self.quote_row) page.add(defaults) switch = Adw.PreferencesGroup(title="中英文切换") diff --git a/spec/config.json b/spec/config.json index 82e04849..4f14b80e 100644 --- a/spec/config.json +++ b/spec/config.json @@ -163,6 +163,9 @@ "platforms": ["fcitx5", "settings", "macos", "windows"], "note": "输出繁体字"}, {"key": "PunctuationStyle", "type": "enum", "default": "Fullwidth", "values": ["Fullwidth", "Halfwidth"], "platforms": ["fcitx5", "settings", "macos", "windows"], "note": "标点:中文标点、英文标点"}, + {"key": "QuoteStyle", "type": "enum", "default": "Curly", "values": ["Curly", "Corner"], + "platforms": ["fcitx5", "settings"], + "note": "全角标点下双引号、单引号用哪一套:Curly 弯引号 “”‘’(默认),Corner 直角引号 「」『』。只影响全角标点,半角不受影响;左右交替、数字后面保持半角的规则两套一样。macOS、Windows 还没有这一项"}, {"key": "SelectKeys", "type": "enum", "default": "Off", "values": ["Off", "Semicolon", "SemicolonQuote"], "platforms": ["fcitx5", "settings", "macos", "windows"], "note": "分号选第 2 个、引号选第 3 个"}, {"key": "PickChar", "type": "bool", "default": true,