Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/include/wowtype_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 14 additions & 4 deletions core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 单按切换中英文。
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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}"));
Expand Down Expand Up @@ -2139,7 +2142,14 @@ impl<'e> Session<'e> {

/// c 在全角样式下换成的中文标点;半角样式、数字写法里的符号、没有中文标点的是 None。
fn punct_text(&mut self, c: char, prev: Option<char>) -> Option<String> {
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)。
Expand Down
18 changes: 17 additions & 1 deletion core/src/session_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
},
}
}
}
Expand Down Expand Up @@ -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,
Expand Down
117 changes: 105 additions & 12 deletions core/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> {
closing_punct_with(ascii, QuoteStyle::default())
}

/// 同 [`closing_punct`],引号按 `style` 取。
pub fn closing_punct_with(ascii: char, style: QuoteStyle) -> Option<char> {
Some(match ascii {
')' => ')',
']' => '】',
'>' => '》',
'}' => '」',
'"' => '”',
'\'' => '’',
'"' => match style {
QuoteStyle::Curly => '”',
QuoteStyle::Corner => '」',
},
'\'' => match style {
QuoteStyle::Curly => '’',
QuoteStyle::Corner => '』',
},
_ => return None,
})
}
Expand All @@ -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 字符。
///
/// - 半角样式:一律原样上屏
Expand Down Expand Up @@ -177,6 +212,17 @@ pub fn punct_with(
style: PunctStyle,
prev: Option<char>,
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<char>,
quotes: &mut [bool; 2],
) -> Option<&'static str> {
if style == PunctStyle::Halfwidth {
return None;
Expand All @@ -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)
}
Expand All @@ -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<char>, quotes: &mut [bool; 2]) -> &'static str {
let (slot, open, close) = if ascii == '"' {
(0, "“", "”")
} else {
(1, "‘", "’")
/// 引号左右交替:“……” ‘……’ 或 「……」『……』。光标前是空白或左括号时一定给左引号。
fn quote(ascii: char, style: QuoteStyle, prev: Option<char>, 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
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions fcitx5-addon/src/wowtype_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<uint32_t>(autoSpaceFlags_);
config.shift_toggle = shiftToggle_ == WowtypeShiftToggle::Left ? WOWTYPE_SHIFT_TOGGLE_LEFT
: shiftToggle_ == WowtypeShiftToggle::Right ? WOWTYPE_SHIFT_TOGGLE_RIGHT
Expand Down
17 changes: 17 additions & 0 deletions fcitx5-addon/src/wowtype_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -246,6 +259,9 @@ FCITX_CONFIGURATION(
fcitx::Option<WowtypePunctStyle, fcitx::NoConstrain<WowtypePunctStyle>,
fcitx::DefaultMarshaller<WowtypePunctStyle>, WowtypePunctStyleAnnotation>
punctStyle{this, "PunctuationStyle", "标点样式", WowtypePunctStyle::Fullwidth};
fcitx::Option<WowtypeQuoteStyle, fcitx::NoConstrain<WowtypeQuoteStyle>,
fcitx::DefaultMarshaller<WowtypeQuoteStyle>, WowtypeQuoteStyleAnnotation>
quoteStyle{this, "QuoteStyle", "引号样式", WowtypeQuoteStyle::Curly};
fcitx::Option<WowtypeSelectKeys, fcitx::NoConstrain<WowtypeSelectKeys>,
fcitx::DefaultMarshaller<WowtypeSelectKeys>, WowtypeSelectKeysAnnotation>
selectKeys{this, "SelectKeys", "分号、引号选词", WowtypeSelectKeys::Off};
Expand Down Expand Up @@ -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;
Expand Down
Loading