update at 2025-11-04 18:05:12

This commit is contained in:
douboer
2025-11-04 18:05:12 +08:00
parent bb62226ef6
commit fe4daf0469
7 changed files with 1374 additions and 524 deletions

View File

@@ -92,19 +92,19 @@
#### 简化日志输出 #### 简化日志输出
- **改进前**:每个事件都有详细的调试日志,包括: - **改进前**:每个事件都有详细的调试日志,包括:
``` ```
🔍 Current IM: xxx Current IM: xxx
📖 Entering insert mode... Entering insert mode...
🎯 Vim mode: xxx Vim mode: xxx
💾 Saved IM status: xxx Saved IM status: xxx
🔑 Detected ESC key press Detected ESC key press
🔑 Insert key pressed: i Insert key pressed: i
``` ```
- **改进后**:只保留关键的状态转换日志: - **改进后**:只保留关键的状态转换日志:
``` ```
🚀 Loading plugin... Loading plugin...
🔤 ESC → English (saved Chinese) ESC → English (saved Chinese)
🈳 → Chinese → Chinese
Error: xxx Error: xxx
``` ```
- **优势** - **优势**
- 减少了 70% 的控制台输出 - 减少了 70% 的控制台输出

View File

@@ -92,19 +92,19 @@ This is a major update that introduces input method state memory and fixes sever
#### Simplified Logging #### Simplified Logging
- **Before**: Every event had detailed debug logs including: - **Before**: Every event had detailed debug logs including:
``` ```
🔍 Current IM: xxx Current IM: xxx
📖 Entering insert mode... Entering insert mode...
🎯 Vim mode: xxx Vim mode: xxx
💾 Saved IM status: xxx Saved IM status: xxx
🔑 Detected ESC key press Detected ESC key press
🔑 Insert key pressed: i Insert key pressed: i
``` ```
- **After**: Only key state transition logs remain: - **After**: Only key state transition logs remain:
``` ```
🚀 Loading plugin... Loading plugin...
🔤 ESC → English (saved Chinese) ESC → English (saved Chinese)
🈳 → Chinese → Chinese
Error: xxx Error: xxx
``` ```
- **Benefits**: - **Benefits**:
- 70% less console output - 70% less console output

583
README.md
View File

@@ -1,455 +1,47 @@
# Obsidian Vim 输入法自动切换插件# Obsidian Vim 输入法自动切换插件 # Obsidian Vim 输入法自动切换插件
[English](./README_en.md) | 中文
这是一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。
[English](./README_en.md) | 中文[English](#english) | [中文](#中文) ## 功能简介
- 自动切换输入法:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法
- 智能记忆:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
- 无缝体验:在 Insert 模式下正常输入文本时不会触发输入法切换
## 核心特性
### 1. 模式切换自动化
- 进入 Normal 模式(按 ESC 或其他命令)→ 自动切换到英文输入法
- 进入 Insert 模式(按 i, a, o 等)→ 自动恢复上次的输入法状态
一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。--- ### 2. 输入法状态记忆
- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)
- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态
## 功能简介## 中文
- **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法### 功能简介
- **智能记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
- **无缝体验**:在 Insert 模式下正常输入文本时不会触发输入法切换这是一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。它能够:
## 核心特性- **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法
- **智能记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
### 1. 模式切换自动化- **无缝体验**:在 Insert 模式下正常输入文本时不会触发输入法切换
- **进入 Normal 模式**(按 ESC 或其他命令)→ 自动切换到英文输入法
- **进入 Insert 模式**(按 i, a, o 等)→ 自动恢复上次的输入法状态### 核心特性
### 2. 输入法状态记忆#### 1. 模式切换自动化
- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)- **进入 Normal 模式**(按 ESC 或其他命令)→ 自动切换到英文输入法
- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态- **进入 Insert 模式**(按 i, a, o 等)→ 自动恢复上次的输入法状态
- 支持中英文混合输入场景 - 支持中英文混合输入场景
#### 2. 输入法状态记忆 ### 3. 智能检测机制
### 3. 智能检测机制- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)
插件使用多重检测机制确保可靠性:- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态
- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键- 支持中英文混合输入场景
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
- **定时轮询**兜底100ms 轮询检测模式变化#### 3. 智能检测机制
插件使用多重检测机制确保可靠性: 插件使用多重检测机制确保可靠性:
- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键
## 快速开始- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件 - **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
- **定时轮询**兜底100ms 轮询检测模式变化
### 安装要求- **定时轮询**兜底100ms 轮询检测模式变化 ## 安装要求
#### macOS### 安装要求
```bash
brew install fcitx-remote-for-osx#### macOS
```1. 安装 [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)
```bash
#### Linux brew install fcitx-remote-for-osx
```bash ```
# Ubuntu/Debian
sudo apt-get install fcitx2. 确认安装成功:
```bash
# Fedora fcitx-remote -n
sudo dnf install fcitx # 应该输出当前输入法的名称com.apple.keylayout.ABC
```
# Arch Linux
sudo pacman -S fcitx#### Linux
```通过系统包管理器安装 `fcitx` 输入法:
```bash
#### Windows# Ubuntu/Debian
下载 [fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe) 并放到系统 PATH 路径中sudo apt-get install fcitx
### 安装插件# Fedora
sudo dnf install fcitx
1. 下载插件到 Obsidian 插件目录:
```bash# Arch Linux
cd /path/to/your/vault/.obsidian/plugins/sudo pacman -S fcitx
git clone https://github.com/yourusername/vim-im-switch.git```
```
#### Windows
2. 在 Obsidian 中启用插件:下载并安装 AutoHotKey 版本的 fcitx-remote
- 打开设置 → 社区插件- [下载 fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- 找到 "Vim Input Method Switch" 并启用- 将 exe 文件放到系统 PATH 路径中
3. 配置输入法(可选):### 安装插件
- 设置英文输入法(默认:`com.apple.keylayout.ABC`
- 设置中文输入法默认自动检测1. 下载插件文件到 Obsidian 插件目录:
```bash
## 使用方法 cd /path/to/your/vault/.obsidian/plugins/
git clone https://github.com/yourusername/vim-im-switch.git
### 基本使用场景 ```
**中文输入**2. 在 Obsidian 中启用插件:
``` - 打开设置 → 社区插件 → 浏览
按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文) - 找到 "Vim Input Method Switch"
输入中文内容 - 点击启用
按 ESC → 退出到 Normal 模式 → 输入法切换到英文
```3. 配置输入法:
- 打开插件设置
**英文输入** - 设置英文输入法(默认:`com.apple.keylayout.ABC`
``` - 设置中文输入法(默认:自动检测)
按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
输入英文内容### 使用方法
按 ESC → 退出到 Normal 模式 → 输入法保持英文
```#### 基本使用
**中英混合**1. **中文输入**
``` ```
按 i → 自动恢复上次的输入法 按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文)
输入中文,然后手动切换到英文继续输入 输入中文内容
按 ESC → 保存当前输入法状态(英文) 按 ESC → 退出到 Normal 模式 → 输入法切换到英文
按 i → 自动恢复英文输入法 ```
```
2. **英文输入**
### 支持的 Vim 命令 ```
按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
- **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C` 输入英文内容
- **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令 按 ESC → 退出到 Normal 模式 → 输入法保持英文
```
## 工作原理
3. **中英混合**
```mermaid ```
graph TD 按 i → 自动恢复上次的输入法
A[Normal 模式<br/>英文输入法] -->|按 i/a/o 等| B[检测模式切换] 输入中文,然后手动切换到英文继续输入
B --> C[恢复上次保存的<br/>输入法状态] 按 ESC → 保存当前输入法状态(英文)
C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文] 按 i → 自动恢复英文输入法
D -->|按 ESC| E[保存当前输入法<br/>状态 中/英] ```
E --> F[切换到英文输入法]
F --> A#### 支持的 Vim 命令
style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px- **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px- **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令
style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px### 工作原理
```
```mermaid
### 技术细节graph TD
A[Normal 模式<br/>英文输入法] -->|按 i/a/o 等| B[检测模式切换]
**输入法检测** B --> C[恢复上次保存的<br/>输入法状态]
- 使用 `fcitx-remote -n` 获取当前输入法名称 C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文]
- 对比设置中的中英文输入法名称来判断状态 D -->|按 ESC| E[保存当前输入法<br/>状态 中/英]
E --> F[切换到英文输入法]
**事件处理** F --> A
- **键盘事件**:使用 `capture` 模式尽早捕获按键
- **防抖机制**100ms 内的重复事件会被忽略
- **异步处理**:所有输入法切换操作都是异步的,不阻塞编辑 style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px
**状态管理** style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
- `currentVimMode`:当前 Vim 模式normal/insert/visual style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
- `lastInsertModeIMStatus`:上次 Insert 模式的输入法状态```
- `imStatus`当前输入法状态Activate=中文Deactivate=英文)
### 技术细节
## 故障排除
#### 输入法检测
### 问题:插件没有效果- 使用 `fcitx-remote -n` 获取当前输入法名称
1. 检查 fcitx-remote 是否正确安装:- 对比设置中的中英文输入法名称来判断状态
```bash
which fcitx-remote#### 事件处理
fcitx-remote -n- **键盘事件**:使用 `capture` 模式尽早捕获按键
```- **防抖机制**100ms 内的重复事件会被忽略
- **异步处理**:所有输入法切换操作都是异步的,不阻塞编辑
2. 检查插件日志(开发者控制台):
- 按 `Cmd+Option+I` (macOS) 或 `Ctrl+Shift+I` (Windows/Linux) 打开开发者控制台#### 状态管理
- 查看是否有 `[VimIMSwitch]` 开头的日志- `currentVimMode`:当前 Vim 模式normal/insert/visual
- `lastInsertModeIMStatus`:上次 Insert 模式的输入法状态
3. 验证输入法名称:- `imStatus`当前输入法状态Activate=中文Deactivate=英文)
```bash
# 切换到中文输入法后执行### 故障排除
fcitx-remote -n
# 输出应该匹配插件设置中的中文输入法名称#### 问题:插件没有效果
```1. 检查 fcitx-remote 是否正确安装:
```bash
### 问题:输入法切换不正确 which fcitx-remote
1. 确认设置中的输入法名称正确 fcitx-remote -n
2. 手动切换输入法后,在开发者控制台查看日志 ```
3. 检查是否有其他插件冲突
2. 检查插件日志(开发者控制台):
### 问题ESC 键需要按多次 ```
- 这个问题已在最新版本中修复 按 Cmd+Option+I 打开开发者控制台
- 如果仍然存在,请更新到最新版本 查看是否有 [VimIMSwitch] 开头的日志
```
## 开发
3. 验证输入法名称:
### 构建 ```bash
```bash # 切换到中文输入法后执行
npm install fcitx-remote -n
npm run build # 输出应该匹配插件设置中的中文输入法名称
``` ```
### 部署#### 问题:输入法切换不正确
```bash1. 确认设置中的输入法名称正确
./deploy.sh2. 手动切换输入法后,在开发者控制台查看日志
```3. 检查是否有其他插件冲突
### 调试#### 问题ESC 键需要按多次
插件会在控制台输出关键日志:- 这个问题已在最新版本中修复
- `🚀 Loading plugin...` - 插件加载- 如果仍然存在,请更新到最新版本
- `🔤 ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - 切换到中文### 开发
- `❌ Error...` - 错误信息
#### 构建
## 更新日志```bash
npm install
查看 [CHANGELOG.md](./CHANGELOG.md) 获取详细的版本更新历史。npm run build
```
## 许可证
#### 部署
MIT License```bash
./deploy.sh
## 相关链接```
- [更新日志](./CHANGELOG.md)#### 调试
- [English Documentation](./README_en.md)插件会在控制台输出关键日志:
- [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)- `🚀 Loading plugin...` - 插件加载
- `🔤 ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - 切换到中文
- `❌ Error...` - 错误信息
### 更新日志
#### v1.0.8 (2025-01-04)
- ✨ 新增:输入法状态记忆功能
- 🐛 修复ESC 键第一次按下无效的问题
- 🐛 修复Insert 模式下输入字符触发输入法切换
- ⚡️ 优化:使用事件捕获模式提高响应速度
- 🧹 清理:移除冗余代码和过多的调试日志
### 许可证
MIT License
---
## English
### Features
An input method auto-switching plugin designed for Obsidian's Vim mode that:
- **Automatic switching**: Automatically switches input methods when transitioning between Vim's Normal and Insert modes
- **Smart memory**: Remembers the input method state when exiting Insert mode and restores it on next entry
- **Seamless experience**: Won't trigger input method changes during normal text input in Insert mode
### Core Features
#### 1. Mode Switch Automation
- **Enter Normal mode** (press ESC or other commands) → Auto switch to English input method
- **Enter Insert mode** (press i, a, o, etc.) → Auto restore previous input method state
#### 2. Input Method State Memory
- Automatically detects and saves current input method (Chinese/English) when exiting Insert mode
- Automatically restores to last saved input method state when entering Insert mode
- Supports mixed Chinese/English input scenarios
#### 3. Smart Detection Mechanism
The plugin uses multiple detection mechanisms for reliability:
- **Keyboard event listening** (primary): Uses capture mode to listen for ESC and Insert keys
- **CodeMirror events** (auxiliary): Monitors vim-mode-change events
- **Polling** (fallback): 100ms polling to detect mode changes
### Requirements
#### macOS #### macOS
1. Install [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx): 1. 安装 [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx):
```bash ```bash
brew install fcitx-remote-for-osx brew install fcitx-remote-for-osx
``` ```
2. Verify installation: 2. 验证安装:
```bash ```bash
fcitx-remote -n fcitx-remote -n
# Should output current input method name, e.g.: com.apple.keylayout.ABC # 应该输出当前输入法的名称,如:com.apple.keylayout.ABC
``` ```
#### Linux #### Linux
Install `fcitx` via your package manager: 通过你的包管理器安装 `fcitx`
```bash ```bash
# Ubuntu/Debian # Ubuntu/Debian
sudo apt-get install fcitx sudo apt-get install fcitx
@@ -462,119 +54,114 @@ sudo pacman -S fcitx
``` ```
#### Windows #### Windows
Download and install the AutoHotKey version of fcitx-remote: 下载并安装 AutoHotKey 版本的 fcitx-remote
- [Download fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe) - [下载 fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- Place the exe file in your system PATH - 将 exe 文件放到系统 PATH 路径中
### Installation ## 安装插件
1. Download plugin files to Obsidian plugins directory: 1. 下载插件文件到 Obsidian 插件目录:
```bash ```bash
cd /path/to/your/vault/.obsidian/plugins/ cd /path/to/your/vault/.obsidian/plugins/
git clone https://github.com/yourusername/vim-im-switch.git git clone https://github.com/yourusername/vim-im-switch.git
``` ```
2. Enable plugin in Obsidian: 2. Obsidian 中启用插件:
- Open Settings → Community Plugins → Browse - 打开设置 → 社区插件 → 浏览
- Find "Vim Input Method Switch" - 找到 "Vim Input Method Switch"
- Click Enable - 点击启用
3. Configure input methods: 3. 配置输入法(可选):
- Open plugin settings - 打开插件设置
- Set English input method (default: `com.apple.keylayout.ABC`) - 设置英文输入法(默认:`com.apple.keylayout.ABC`
- Set Chinese input method (default: auto-detect) - 设置中文输入法(默认:自动检测)
### Usage ## 使用方法
#### Basic Usage ### 基本使用场景
1. **Chinese Input**: 1. **中文输入**
``` ```
Press i → Enter Insert mode → IM switches to Chinese (if last time was Chinese) 按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文)
Type Chinese content 输入中文内容
Press ESC → Exit to Normal mode → IM switches to English ESC → 退出到 Normal 模式 → 输入法切换到英文
``` ```
2. **English Input**: 2. **英文输入**
``` ```
Press i → Enter Insert mode → IM stays English (if last time was English) 按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
Type English content 输入英文内容
Press ESC → Exit to Normal mode → IM stays English ESC → 退出到 Normal 模式 → 输入法保持英文
``` ```
3. **Mixed Input**: 3. **中英混合**
``` ```
Press i → Auto restore last IM 按 i → 自动恢复上次的输入法
Type Chinese, then manually switch to English 输入中文,然后手动切换到英文继续输入
Press ESC → Save current IM state (English) ESC → 保存当前输入法状态(英文)
Press i → Auto restore English IM 按 i → 自动恢复英文输入法
``` ```
#### Supported Vim Commands ### 支持的 Vim 命令
- **Enter Insert mode**: `i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C` - **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`
- **Exit Insert mode**: `ESC`, and other commands that trigger Normal mode - **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令
### How It Works ## 工作原理
```mermaid ```mermaid
graph TD graph TD
A[Normal Mode<br/>English IM] -->|Press i/a/o etc| B[Detect Mode Change] A[Normal 模式<br/>英文输入法] -->| i/a/o | B[检测模式切换]
B --> C[Restore Last Saved<br/>IM State] B --> C[恢复上次保存的<br/>输入法状态]
C --> D[Insert Mode<br/>Restored IM:<br/>Chinese/English] C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文]
D -->|Press ESC| E[Save Current IM<br/>State CN/EN] D -->| ESC| E[保存当前输入法<br/>状态 中/英]
E --> F[Switch to English IM] E --> F[切换到英文输入法]
F --> A F --> A
style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px
style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
``` ```
The plugin maintains a state machine that tracks Vim modes and input method states, automatically switching input methods when mode transitions occur while preserving user preferences. 插件维护一个状态机,跟踪 Vim 模式和输入法状态,在模式转换时自动切换输入法,同时保留用户偏好设置。
### Troubleshooting ## 故障排除
#### Issue: Plugin not working ### 问题:插件没有效果
1. Check if fcitx-remote is properly installed 1. 检查 fcitx-remote 是否正确安装
2. Check plugin logs in developer console 2. 检查插件日志(开发者控制台)
3. Verify input method names match settings 3. 验证输入法名称是否与设置匹配
#### Issue: Incorrect input method switching ### 问题:输入法切换不正确
1. Confirm input method names in settings are correct 1. 确认设置中的输入法名称正确
2. Check logs in developer console after manually switching IM 2. 手动切换输入法后,在开发者控制台查看日志
3. Check for conflicts with other plugins 3. 检查是否有其他插件冲突
### Development ## 开发
#### Build ### 构建
```bash ```bash
npm install npm install
npm run build npm run build
``` ```
#### Deploy ### 部署
```bash ```bash
./deploy.sh ./deploy.sh
``` ```
#### Debug ### 调试
The plugin outputs key logs in the console: 插件会在控制台输出关键日志:
- `🚀 Loading plugin...` - Plugin loaded - `Loading plugin...` - 插件加载
- `🔤 ESC → English` - ESC switches to English - `ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - Switch to Chinese - `→ Chinese` - 切换到中文
- `Error...` - Error messages - `Error...` - 错误信息
### Changelog ## 更新日志
查看 [CHANGELOG.md](./CHANGELOG.md) 获取详细的版本更新历史。
#### v1.0.8 (2025-01-04) ## 相关链接
- ✨ Added: Input method state memory feature - [更新日志](./CHANGELOG.md)
- <EFBFBD><EFBFBD> Fixed: ESC key not working on first press - [English Documentation](./README_en.md)
- 🐛 Fixed: Character input in Insert mode triggering IM switch - [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)
- ⚡️ Optimized: Use event capture mode for better responsiveness
- 🧹 Cleanup: Remove redundant code and excessive debug logs
### License
MIT License

580
README.md.bak2 Normal file
View File

@@ -0,0 +1,580 @@
# Obsidian Vim 输入法自动切换插件# Obsidian Vim 输入法自动切换插件
[English](./README_en.md) | 中文[English](#english) | [中文](#中文)
一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。---
## 功能简介## 中文
- **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法### 功能简介
- **智能记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
- **无缝体验**:在 Insert 模式下正常输入文本时不会触发输入法切换这是一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。它能够:
## 核心特性- **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法
- **智能记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
### 1. 模式切换自动化- **无缝体验**:在 Insert 模式下正常输入文本时不会触发输入法切换
- **进入 Normal 模式**(按 ESC 或其他命令)→ 自动切换到英文输入法
- **进入 Insert 模式**(按 i, a, o 等)→ 自动恢复上次的输入法状态### 核心特性
### 2. 输入法状态记忆#### 1. 模式切换自动化
- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)- **进入 Normal 模式**(按 ESC 或其他命令)→ 自动切换到英文输入法
- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态- **进入 Insert 模式**(按 i, a, o 等)→ 自动恢复上次的输入法状态
- 支持中英文混合输入场景
#### 2. 输入法状态记忆
### 3. 智能检测机制- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)
插件使用多重检测机制确保可靠性:- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态
- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键- 支持中英文混合输入场景
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
- **定时轮询**兜底100ms 轮询检测模式变化#### 3. 智能检测机制
插件使用多重检测机制确保可靠性:
## 快速开始- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
### 安装要求- **定时轮询**兜底100ms 轮询检测模式变化
#### macOS### 安装要求
```bash
brew install fcitx-remote-for-osx#### macOS
```1. 安装 [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)
```bash
#### Linux brew install fcitx-remote-for-osx
```bash ```
# Ubuntu/Debian
sudo apt-get install fcitx2. 确认安装成功:
```bash
# Fedora fcitx-remote -n
sudo dnf install fcitx # 应该输出当前输入法的名称com.apple.keylayout.ABC
```
# Arch Linux
sudo pacman -S fcitx#### Linux
```通过系统包管理器安装 `fcitx` 输入法:
```bash
#### Windows# Ubuntu/Debian
下载 [fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe) 并放到系统 PATH 路径中sudo apt-get install fcitx
### 安装插件# Fedora
sudo dnf install fcitx
1. 下载插件到 Obsidian 插件目录:
```bash# Arch Linux
cd /path/to/your/vault/.obsidian/plugins/sudo pacman -S fcitx
git clone https://github.com/yourusername/vim-im-switch.git```
```
#### Windows
2. 在 Obsidian 中启用插件:下载并安装 AutoHotKey 版本的 fcitx-remote
- 打开设置 → 社区插件- [下载 fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- 找到 "Vim Input Method Switch" 并启用- 将 exe 文件放到系统 PATH 路径中
3. 配置输入法(可选):### 安装插件
- 设置英文输入法(默认:`com.apple.keylayout.ABC`
- 设置中文输入法默认自动检测1. 下载插件文件到 Obsidian 插件目录:
```bash
## 使用方法 cd /path/to/your/vault/.obsidian/plugins/
git clone https://github.com/yourusername/vim-im-switch.git
### 基本使用场景 ```
**中文输入**2. 在 Obsidian 中启用插件:
``` - 打开设置 → 社区插件 → 浏览
按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文) - 找到 "Vim Input Method Switch"
输入中文内容 - 点击启用
按 ESC → 退出到 Normal 模式 → 输入法切换到英文
```3. 配置输入法:
- 打开插件设置
**英文输入** - 设置英文输入法(默认:`com.apple.keylayout.ABC`
``` - 设置中文输入法(默认:自动检测)
按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
输入英文内容### 使用方法
按 ESC → 退出到 Normal 模式 → 输入法保持英文
```#### 基本使用
**中英混合**1. **中文输入**
``` ```
按 i → 自动恢复上次的输入法 按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文)
输入中文,然后手动切换到英文继续输入 输入中文内容
按 ESC → 保存当前输入法状态(英文) 按 ESC → 退出到 Normal 模式 → 输入法切换到英文
按 i → 自动恢复英文输入法 ```
```
2. **英文输入**
### 支持的 Vim 命令 ```
按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
- **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C` 输入英文内容
- **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令 按 ESC → 退出到 Normal 模式 → 输入法保持英文
```
## 工作原理
3. **中英混合**
```mermaid ```
graph TD 按 i → 自动恢复上次的输入法
A[Normal 模式<br/>英文输入法] -->|按 i/a/o 等| B[检测模式切换] 输入中文,然后手动切换到英文继续输入
B --> C[恢复上次保存的<br/>输入法状态] 按 ESC → 保存当前输入法状态(英文)
C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文] 按 i → 自动恢复英文输入法
D -->|按 ESC| E[保存当前输入法<br/>状态 中/英] ```
E --> F[切换到英文输入法]
F --> A#### 支持的 Vim 命令
style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px- **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px- **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令
style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px### 工作原理
```
```mermaid
### 技术细节graph TD
A[Normal 模式<br/>英文输入法] -->|按 i/a/o 等| B[检测模式切换]
**输入法检测** B --> C[恢复上次保存的<br/>输入法状态]
- 使用 `fcitx-remote -n` 获取当前输入法名称 C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文]
- 对比设置中的中英文输入法名称来判断状态 D -->|按 ESC| E[保存当前输入法<br/>状态 中/英]
E --> F[切换到英文输入法]
**事件处理** F --> A
- **键盘事件**:使用 `capture` 模式尽早捕获按键
- **防抖机制**100ms 内的重复事件会被忽略
- **异步处理**:所有输入法切换操作都是异步的,不阻塞编辑 style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px
**状态管理** style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
- `currentVimMode`:当前 Vim 模式normal/insert/visual style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
- `lastInsertModeIMStatus`:上次 Insert 模式的输入法状态```
- `imStatus`当前输入法状态Activate=中文Deactivate=英文)
### 技术细节
## 故障排除
#### 输入法检测
### 问题:插件没有效果- 使用 `fcitx-remote -n` 获取当前输入法名称
1. 检查 fcitx-remote 是否正确安装:- 对比设置中的中英文输入法名称来判断状态
```bash
which fcitx-remote#### 事件处理
fcitx-remote -n- **键盘事件**:使用 `capture` 模式尽早捕获按键
```- **防抖机制**100ms 内的重复事件会被忽略
- **异步处理**:所有输入法切换操作都是异步的,不阻塞编辑
2. 检查插件日志(开发者控制台):
- 按 `Cmd+Option+I` (macOS) 或 `Ctrl+Shift+I` (Windows/Linux) 打开开发者控制台#### 状态管理
- 查看是否有 `[VimIMSwitch]` 开头的日志- `currentVimMode`:当前 Vim 模式normal/insert/visual
- `lastInsertModeIMStatus`:上次 Insert 模式的输入法状态
3. 验证输入法名称:- `imStatus`当前输入法状态Activate=中文Deactivate=英文)
```bash
# 切换到中文输入法后执行### 故障排除
fcitx-remote -n
# 输出应该匹配插件设置中的中文输入法名称#### 问题:插件没有效果
```1. 检查 fcitx-remote 是否正确安装:
```bash
### 问题:输入法切换不正确 which fcitx-remote
1. 确认设置中的输入法名称正确 fcitx-remote -n
2. 手动切换输入法后,在开发者控制台查看日志 ```
3. 检查是否有其他插件冲突
2. 检查插件日志(开发者控制台):
### 问题ESC 键需要按多次 ```
- 这个问题已在最新版本中修复 按 Cmd+Option+I 打开开发者控制台
- 如果仍然存在,请更新到最新版本 查看是否有 [VimIMSwitch] 开头的日志
```
## 开发
3. 验证输入法名称:
### 构建 ```bash
```bash # 切换到中文输入法后执行
npm install fcitx-remote -n
npm run build # 输出应该匹配插件设置中的中文输入法名称
``` ```
### 部署#### 问题:输入法切换不正确
```bash1. 确认设置中的输入法名称正确
./deploy.sh2. 手动切换输入法后,在开发者控制台查看日志
```3. 检查是否有其他插件冲突
### 调试#### 问题ESC 键需要按多次
插件会在控制台输出关键日志:- 这个问题已在最新版本中修复
- `🚀 Loading plugin...` - 插件加载- 如果仍然存在,请更新到最新版本
- `🔤 ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - 切换到中文### 开发
- `❌ Error...` - 错误信息
#### 构建
## 更新日志```bash
npm install
查看 [CHANGELOG.md](./CHANGELOG.md) 获取详细的版本更新历史。npm run build
```
## 许可证
#### 部署
MIT License```bash
./deploy.sh
## 相关链接```
- [更新日志](./CHANGELOG.md)#### 调试
- [English Documentation](./README_en.md)插件会在控制台输出关键日志:
- [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)- `🚀 Loading plugin...` - 插件加载
- `🔤 ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - 切换到中文
- `❌ Error...` - 错误信息
### 更新日志
#### v1.0.8 (2025-01-04)
- ✨ 新增:输入法状态记忆功能
- 🐛 修复ESC 键第一次按下无效的问题
- 🐛 修复Insert 模式下输入字符触发输入法切换
- ⚡️ 优化:使用事件捕获模式提高响应速度
- 🧹 清理:移除冗余代码和过多的调试日志
### 许可证
MIT License
---
## English
### Features
An input method auto-switching plugin designed for Obsidian's Vim mode that:
- **Automatic switching**: Automatically switches input methods when transitioning between Vim's Normal and Insert modes
- **Smart memory**: Remembers the input method state when exiting Insert mode and restores it on next entry
- **Seamless experience**: Won't trigger input method changes during normal text input in Insert mode
### Core Features
#### 1. Mode Switch Automation
- **Enter Normal mode** (press ESC or other commands) → Auto switch to English input method
- **Enter Insert mode** (press i, a, o, etc.) → Auto restore previous input method state
#### 2. Input Method State Memory
- Automatically detects and saves current input method (Chinese/English) when exiting Insert mode
- Automatically restores to last saved input method state when entering Insert mode
- Supports mixed Chinese/English input scenarios
#### 3. Smart Detection Mechanism
The plugin uses multiple detection mechanisms for reliability:
- **Keyboard event listening** (primary): Uses capture mode to listen for ESC and Insert keys
- **CodeMirror events** (auxiliary): Monitors vim-mode-change events
- **Polling** (fallback): 100ms polling to detect mode changes
### Requirements
#### macOS
1. Install [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx):
```bash
brew install fcitx-remote-for-osx
```
2. Verify installation:
```bash
fcitx-remote -n
# Should output current input method name, e.g.: com.apple.keylayout.ABC
```
#### Linux
Install `fcitx` via your package manager:
```bash
# Ubuntu/Debian
sudo apt-get install fcitx
# Fedora
sudo dnf install fcitx
# Arch Linux
sudo pacman -S fcitx
```
#### Windows
Download and install the AutoHotKey version of fcitx-remote:
- [Download fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- Place the exe file in your system PATH
### Installation
1. Download plugin files to Obsidian plugins directory:
```bash
cd /path/to/your/vault/.obsidian/plugins/
git clone https://github.com/yourusername/vim-im-switch.git
```
2. Enable plugin in Obsidian:
- Open Settings → Community Plugins → Browse
- Find "Vim Input Method Switch"
- Click Enable
3. Configure input methods:
- Open plugin settings
- Set English input method (default: `com.apple.keylayout.ABC`)
- Set Chinese input method (default: auto-detect)
### Usage
#### Basic Usage
1. **Chinese Input**:
```
Press i → Enter Insert mode → IM switches to Chinese (if last time was Chinese)
Type Chinese content
Press ESC → Exit to Normal mode → IM switches to English
```
2. **English Input**:
```
Press i → Enter Insert mode → IM stays English (if last time was English)
Type English content
Press ESC → Exit to Normal mode → IM stays English
```
3. **Mixed Input**:
```
Press i → Auto restore last IM
Type Chinese, then manually switch to English
Press ESC → Save current IM state (English)
Press i → Auto restore English IM
```
#### Supported Vim Commands
- **Enter Insert mode**: `i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`
- **Exit Insert mode**: `ESC`, and other commands that trigger Normal mode
### How It Works
```mermaid
graph TD
A[Normal Mode<br/>English IM] -->|Press i/a/o etc| B[Detect Mode Change]
B --> C[Restore Last Saved<br/>IM State]
C --> D[Insert Mode<br/>Restored IM:<br/>Chinese/English]
D -->|Press ESC| E[Save Current IM<br/>State CN/EN]
E --> F[Switch to English IM]
F --> A
style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px
style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
```
The plugin maintains a state machine that tracks Vim modes and input method states, automatically switching input methods when mode transitions occur while preserving user preferences.
### Troubleshooting
#### Issue: Plugin not working
1. Check if fcitx-remote is properly installed
2. Check plugin logs in developer console
3. Verify input method names match settings
#### Issue: Incorrect input method switching
1. Confirm input method names in settings are correct
2. Check logs in developer console after manually switching IM
3. Check for conflicts with other plugins
### Development
#### Build
```bash
npm install
npm run build
```
#### Deploy
```bash
./deploy.sh
```
#### Debug
The plugin outputs key logs in the console:
- `🚀 Loading plugin...` - Plugin loaded
- `🔤 ESC → English` - ESC switches to English
- `🈳 → Chinese` - Switch to Chinese
- `❌ Error...` - Error messages
### Changelog
#### v1.0.8 (2025-01-04)
- ✨ Added: Input method state memory feature
- <20><> Fixed: ESC key not working on first press
- 🐛 Fixed: Character input in Insert mode triggering IM switch
- ⚡️ Optimized: Use event capture mode for better responsiveness
- 🧹 Cleanup: Remove redundant code and excessive debug logs
### License
MIT License

580
README.md.broken Normal file
View File

@@ -0,0 +1,580 @@
# Obsidian Vim 输入法自动切换插件# Obsidian Vim 输入法自动切换插件
[English](./README_en.md) | 中文[English](#english) | [中文](#中文)
一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。---
## 功能简介## 中文
- **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法### 功能简介
- **智能记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
- **无缝体验**:在 Insert 模式下正常输入文本时不会触发输入法切换这是一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。它能够:
## 核心特性- **自动切换输入法**:在 Vim 的 Normal 模式和 Insert 模式之间切换时,自动切换输入法
- **智能记忆**:记住上次 Insert 模式退出时的输入法状态,下次进入时自动恢复
### 1. 模式切换自动化- **无缝体验**:在 Insert 模式下正常输入文本时不会触发输入法切换
- **进入 Normal 模式**(按 ESC 或其他命令)→ 自动切换到英文输入法
- **进入 Insert 模式**(按 i, a, o 等)→ 自动恢复上次的输入法状态### 核心特性
### 2. 输入法状态记忆#### 1. 模式切换自动化
- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)- **进入 Normal 模式**(按 ESC 或其他命令)→ 自动切换到英文输入法
- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态- **进入 Insert 模式**(按 i, a, o 等)→ 自动恢复上次的输入法状态
- 支持中英文混合输入场景
#### 2. 输入法状态记忆
### 3. 智能检测机制- 退出 Insert 模式时,自动检测并保存当前的输入法(中文/英文)
插件使用多重检测机制确保可靠性:- 下次进入 Insert 模式时,自动恢复到上次保存的输入法状态
- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键- 支持中英文混合输入场景
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
- **定时轮询**兜底100ms 轮询检测模式变化#### 3. 智能检测机制
插件使用多重检测机制确保可靠性:
## 快速开始- **键盘事件监听**(主要):使用事件捕获模式监听 ESC 和 Insert 按键
- **CodeMirror 事件**(辅助):监听 vim-mode-change 事件
### 安装要求- **定时轮询**兜底100ms 轮询检测模式变化
#### macOS### 安装要求
```bash
brew install fcitx-remote-for-osx#### macOS
```1. 安装 [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)
```bash
#### Linux brew install fcitx-remote-for-osx
```bash ```
# Ubuntu/Debian
sudo apt-get install fcitx2. 确认安装成功:
```bash
# Fedora fcitx-remote -n
sudo dnf install fcitx # 应该输出当前输入法的名称com.apple.keylayout.ABC
```
# Arch Linux
sudo pacman -S fcitx#### Linux
```通过系统包管理器安装 `fcitx` 输入法:
```bash
#### Windows# Ubuntu/Debian
下载 [fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe) 并放到系统 PATH 路径中sudo apt-get install fcitx
### 安装插件# Fedora
sudo dnf install fcitx
1. 下载插件到 Obsidian 插件目录:
```bash# Arch Linux
cd /path/to/your/vault/.obsidian/plugins/sudo pacman -S fcitx
git clone https://github.com/yourusername/vim-im-switch.git```
```
#### Windows
2. 在 Obsidian 中启用插件:下载并安装 AutoHotKey 版本的 fcitx-remote
- 打开设置 → 社区插件- [下载 fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- 找到 "Vim Input Method Switch" 并启用- 将 exe 文件放到系统 PATH 路径中
3. 配置输入法(可选):### 安装插件
- 设置英文输入法(默认:`com.apple.keylayout.ABC`
- 设置中文输入法默认自动检测1. 下载插件文件到 Obsidian 插件目录:
```bash
## 使用方法 cd /path/to/your/vault/.obsidian/plugins/
git clone https://github.com/yourusername/vim-im-switch.git
### 基本使用场景 ```
**中文输入**2. 在 Obsidian 中启用插件:
``` - 打开设置 → 社区插件 → 浏览
按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文) - 找到 "Vim Input Method Switch"
输入中文内容 - 点击启用
按 ESC → 退出到 Normal 模式 → 输入法切换到英文
```3. 配置输入法:
- 打开插件设置
**英文输入** - 设置英文输入法(默认:`com.apple.keylayout.ABC`
``` - 设置中文输入法(默认:自动检测)
按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
输入英文内容### 使用方法
按 ESC → 退出到 Normal 模式 → 输入法保持英文
```#### 基本使用
**中英混合**1. **中文输入**
``` ```
按 i → 自动恢复上次的输入法 按 i → 进入 Insert 模式 → 输入法切换到中文(如果上次是中文)
输入中文,然后手动切换到英文继续输入 输入中文内容
按 ESC → 保存当前输入法状态(英文) 按 ESC → 退出到 Normal 模式 → 输入法切换到英文
按 i → 自动恢复英文输入法 ```
```
2. **英文输入**
### 支持的 Vim 命令 ```
按 i → 进入 Insert 模式 → 输入法保持英文(如果上次是英文)
- **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C` 输入英文内容
- **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令 按 ESC → 退出到 Normal 模式 → 输入法保持英文
```
## 工作原理
3. **中英混合**
```mermaid ```
graph TD 按 i → 自动恢复上次的输入法
A[Normal 模式<br/>英文输入法] -->|按 i/a/o 等| B[检测模式切换] 输入中文,然后手动切换到英文继续输入
B --> C[恢复上次保存的<br/>输入法状态] 按 ESC → 保存当前输入法状态(英文)
C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文] 按 i → 自动恢复英文输入法
D -->|按 ESC| E[保存当前输入法<br/>状态 中/英] ```
E --> F[切换到英文输入法]
F --> A#### 支持的 Vim 命令
style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px- **进入 Insert 模式**`i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px- **退出 Insert 模式**`ESC`, 以及其他触发 Normal 模式的命令
style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px### 工作原理
```
```mermaid
### 技术细节graph TD
A[Normal 模式<br/>英文输入法] -->|按 i/a/o 等| B[检测模式切换]
**输入法检测** B --> C[恢复上次保存的<br/>输入法状态]
- 使用 `fcitx-remote -n` 获取当前输入法名称 C --> D[Insert 模式<br/>自动恢复的输入法:<br/>中文/英文]
- 对比设置中的中英文输入法名称来判断状态 D -->|按 ESC| E[保存当前输入法<br/>状态 中/英]
E --> F[切换到英文输入法]
**事件处理** F --> A
- **键盘事件**:使用 `capture` 模式尽早捕获按键
- **防抖机制**100ms 内的重复事件会被忽略
- **异步处理**:所有输入法切换操作都是异步的,不阻塞编辑 style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px
**状态管理** style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
- `currentVimMode`:当前 Vim 模式normal/insert/visual style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
- `lastInsertModeIMStatus`:上次 Insert 模式的输入法状态```
- `imStatus`当前输入法状态Activate=中文Deactivate=英文)
### 技术细节
## 故障排除
#### 输入法检测
### 问题:插件没有效果- 使用 `fcitx-remote -n` 获取当前输入法名称
1. 检查 fcitx-remote 是否正确安装:- 对比设置中的中英文输入法名称来判断状态
```bash
which fcitx-remote#### 事件处理
fcitx-remote -n- **键盘事件**:使用 `capture` 模式尽早捕获按键
```- **防抖机制**100ms 内的重复事件会被忽略
- **异步处理**:所有输入法切换操作都是异步的,不阻塞编辑
2. 检查插件日志(开发者控制台):
- 按 `Cmd+Option+I` (macOS) 或 `Ctrl+Shift+I` (Windows/Linux) 打开开发者控制台#### 状态管理
- 查看是否有 `[VimIMSwitch]` 开头的日志- `currentVimMode`:当前 Vim 模式normal/insert/visual
- `lastInsertModeIMStatus`:上次 Insert 模式的输入法状态
3. 验证输入法名称:- `imStatus`当前输入法状态Activate=中文Deactivate=英文)
```bash
# 切换到中文输入法后执行### 故障排除
fcitx-remote -n
# 输出应该匹配插件设置中的中文输入法名称#### 问题:插件没有效果
```1. 检查 fcitx-remote 是否正确安装:
```bash
### 问题:输入法切换不正确 which fcitx-remote
1. 确认设置中的输入法名称正确 fcitx-remote -n
2. 手动切换输入法后,在开发者控制台查看日志 ```
3. 检查是否有其他插件冲突
2. 检查插件日志(开发者控制台):
### 问题ESC 键需要按多次 ```
- 这个问题已在最新版本中修复 按 Cmd+Option+I 打开开发者控制台
- 如果仍然存在,请更新到最新版本 查看是否有 [VimIMSwitch] 开头的日志
```
## 开发
3. 验证输入法名称:
### 构建 ```bash
```bash # 切换到中文输入法后执行
npm install fcitx-remote -n
npm run build # 输出应该匹配插件设置中的中文输入法名称
``` ```
### 部署#### 问题:输入法切换不正确
```bash1. 确认设置中的输入法名称正确
./deploy.sh2. 手动切换输入法后,在开发者控制台查看日志
```3. 检查是否有其他插件冲突
### 调试#### 问题ESC 键需要按多次
插件会在控制台输出关键日志:- 这个问题已在最新版本中修复
- `🚀 Loading plugin...` - 插件加载- 如果仍然存在,请更新到最新版本
- `🔤 ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - 切换到中文### 开发
- `❌ Error...` - 错误信息
#### 构建
## 更新日志```bash
npm install
查看 [CHANGELOG.md](./CHANGELOG.md) 获取详细的版本更新历史。npm run build
```
## 许可证
#### 部署
MIT License```bash
./deploy.sh
## 相关链接```
- [更新日志](./CHANGELOG.md)#### 调试
- [English Documentation](./README_en.md)插件会在控制台输出关键日志:
- [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx)- `🚀 Loading plugin...` - 插件加载
- `🔤 ESC → English` - ESC 切换到英文
- `🈳 → Chinese` - 切换到中文
- `❌ Error...` - 错误信息
### 更新日志
#### v1.0.8 (2025-01-04)
- ✨ 新增:输入法状态记忆功能
- 🐛 修复ESC 键第一次按下无效的问题
- 🐛 修复Insert 模式下输入字符触发输入法切换
- ⚡️ 优化:使用事件捕获模式提高响应速度
- 🧹 清理:移除冗余代码和过多的调试日志
### 许可证
MIT License
---
## English
### Features
An input method auto-switching plugin designed for Obsidian's Vim mode that:
- **Automatic switching**: Automatically switches input methods when transitioning between Vim's Normal and Insert modes
- **Smart memory**: Remembers the input method state when exiting Insert mode and restores it on next entry
- **Seamless experience**: Won't trigger input method changes during normal text input in Insert mode
### Core Features
#### 1. Mode Switch Automation
- **Enter Normal mode** (press ESC or other commands) → Auto switch to English input method
- **Enter Insert mode** (press i, a, o, etc.) → Auto restore previous input method state
#### 2. Input Method State Memory
- Automatically detects and saves current input method (Chinese/English) when exiting Insert mode
- Automatically restores to last saved input method state when entering Insert mode
- Supports mixed Chinese/English input scenarios
#### 3. Smart Detection Mechanism
The plugin uses multiple detection mechanisms for reliability:
- **Keyboard event listening** (primary): Uses capture mode to listen for ESC and Insert keys
- **CodeMirror events** (auxiliary): Monitors vim-mode-change events
- **Polling** (fallback): 100ms polling to detect mode changes
### Requirements
#### macOS
1. Install [fcitx-remote-for-osx](https://github.com/xcodebuild/fcitx-remote-for-osx):
```bash
brew install fcitx-remote-for-osx
```
2. Verify installation:
```bash
fcitx-remote -n
# Should output current input method name, e.g.: com.apple.keylayout.ABC
```
#### Linux
Install `fcitx` via your package manager:
```bash
# Ubuntu/Debian
sudo apt-get install fcitx
# Fedora
sudo dnf install fcitx
# Arch Linux
sudo pacman -S fcitx
```
#### Windows
Download and install the AutoHotKey version of fcitx-remote:
- [Download fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-plugin/releases/download/1.0.3/fcitx-remote.exe)
- Place the exe file in your system PATH
### Installation
1. Download plugin files to Obsidian plugins directory:
```bash
cd /path/to/your/vault/.obsidian/plugins/
git clone https://github.com/yourusername/vim-im-switch.git
```
2. Enable plugin in Obsidian:
- Open Settings → Community Plugins → Browse
- Find "Vim Input Method Switch"
- Click Enable
3. Configure input methods:
- Open plugin settings
- Set English input method (default: `com.apple.keylayout.ABC`)
- Set Chinese input method (default: auto-detect)
### Usage
#### Basic Usage
1. **Chinese Input**:
```
Press i → Enter Insert mode → IM switches to Chinese (if last time was Chinese)
Type Chinese content
Press ESC → Exit to Normal mode → IM switches to English
```
2. **English Input**:
```
Press i → Enter Insert mode → IM stays English (if last time was English)
Type English content
Press ESC → Exit to Normal mode → IM stays English
```
3. **Mixed Input**:
```
Press i → Auto restore last IM
Type Chinese, then manually switch to English
Press ESC → Save current IM state (English)
Press i → Auto restore English IM
```
#### Supported Vim Commands
- **Enter Insert mode**: `i`, `I`, `a`, `A`, `o`, `O`, `s`, `S`, `c`, `C`
- **Exit Insert mode**: `ESC`, and other commands that trigger Normal mode
### How It Works
```mermaid
graph TD
A[Normal Mode<br/>English IM] -->|Press i/a/o etc| B[Detect Mode Change]
B --> C[Restore Last Saved<br/>IM State]
C --> D[Insert Mode<br/>Restored IM:<br/>Chinese/English]
D -->|Press ESC| E[Save Current IM<br/>State CN/EN]
E --> F[Switch to English IM]
F --> A
style A fill:#e1f5ff,stroke:#01579b,stroke-width:2px
style D fill:#fff9c4,stroke:#f57f17,stroke-width:2px
style E fill:#f3e5f5,stroke:#4a148c,stroke-width:2px
style F fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px
```
The plugin maintains a state machine that tracks Vim modes and input method states, automatically switching input methods when mode transitions occur while preserving user preferences.
### Troubleshooting
#### Issue: Plugin not working
1. Check if fcitx-remote is properly installed
2. Check plugin logs in developer console
3. Verify input method names match settings
#### Issue: Incorrect input method switching
1. Confirm input method names in settings are correct
2. Check logs in developer console after manually switching IM
3. Check for conflicts with other plugins
### Development
#### Build
```bash
npm install
npm run build
```
#### Deploy
```bash
./deploy.sh
```
#### Debug
The plugin outputs key logs in the console:
- `🚀 Loading plugin...` - Plugin loaded
- `🔤 ESC → English` - ESC switches to English
- `🈳 → Chinese` - Switch to Chinese
- `❌ Error...` - Error messages
### Changelog
#### v1.0.8 (2025-01-04)
- ✨ Added: Input method state memory feature
- <20><> Fixed: ESC key not working on first press
- 🐛 Fixed: Character input in Insert mode triggering IM switch
- ⚡️ Optimized: Use event capture mode for better responsiveness
- 🧹 Cleanup: Remove redundant code and excessive debug logs
### License
MIT License

View File

@@ -173,14 +173,14 @@ npm run build
./deploy.sh ./deploy.sh
``` ```
### Debug #### Debug
The plugin outputs key logs in the console: The plugin outputs key logs in the console:
- `🚀 Loading plugin...` - Plugin loaded - `Loading plugin...` - Plugin loaded
- `🔤 ESC → English` - ESC switches to English - `ESC → English` - ESC switches to English
- `🈳 → Chinese` - Switch to Chinese - `→ Chinese` - Switch to Chinese
- `Error...` - Error messages - `Error...` - Error messages
## Changelog ### Changelog
See [CHANGELOG_en.md](./CHANGELOG_en.md) for detailed version history. See [CHANGELOG_en.md](./CHANGELOG_en.md) for detailed version history.

103
fcitx-osx.vim Normal file
View File

@@ -0,0 +1,103 @@
" fcitx.vim 记住插入模式小企鹅输入法的状态
" Author: lilydjwg
" Maintainer: lilydjwg
" Modified by: codefalling
" Note: 另有使用 Python3 接口的新版本
" 此修改版用于 OS X 下的 https://github.com/CodeFalling/fcitx-remote-for-osx
" ---------------------------------------------------------------------
" Load Once:
if exists('g:fcitx_remote')
finish
endif
if &ttimeoutlen <= 0 || &ttimeoutlen > 50
set ttimeoutlen=50
endif
if (has("win32") || has("win95") || has("win64") || has("win16"))
" Windows 下不要载入
finish
endif
if exists('$SSH_TTY')
finish
endif
if !executable("fcitx-remote")
finish
endif
let s:keepcpo = &cpo
let g:loaded_fcitx = 1
set cpo&vim
" ---------------------------------------------------------------------
" Functions:
function Fcitx2en()
let inputstatus = system("fcitx-remote")
if inputstatus == 2
let b:inputtoggle = 1
let t = system("fcitx-remote -c")
endif
endfunction
function Fcitx2zh()
try
if b:inputtoggle == 1
let t = system("fcitx-remote -o")
let b:inputtoggle = 0
endif
catch /inputtoggle/
let b:inputtoggle = 0
endtry
endfunction
" ---------------------------------------------------------------------
" Autocmds:
function Fcitx2zhOnce()
call Fcitx2zh()
call UnBindAu()
endfunction
function BindAu2zhOnce()
augroup Fcitx
au InsertEnter * call Fcitx2zhOnce()
augroup END
endfunction
function BindAu()
augroup Fcitx
au InsertLeave * call Fcitx2en()
au InsertEnter * call Fcitx2zh()
au VimEnter * call Fcitx2en()
augroup END
endfunction
function UnBindAu()
au! Fcitx InsertLeave *
au! Fcitx InsertEnter *
endfunction
"call once when enter insert mode instead of vim startup
let g:called_bind = 0
function EchoBind()
if (g:called_bind==0)
call BindAu()
endif
let g:called_bind =1
endfunction
autocmd InsertEnter * call EchoBind()
"Called once right before you start selecting multiple cursors
function! Multiple_cursors_before()
call UnBindAu()
call BindAu2zhOnce()
endfunction
function! Multiple_cursors_after()
call Fcitx2en()
call BindAu()
endfunction
" ---------------------------------------------------------------------
" Restoration And Modelines:
let &cpo=s:keepcpo
unlet s:keepcpo
"vim:fdm=expr:fde=getline(v\:lnum-1)=~'\\v"\\s*-{20,}'?'>1'\:1
let g:fcitx_remote = 1