From 931c99a4d8dead787619e8fc0b2a2362f20c583a Mon Sep 17 00:00:00 2001 From: douboer Date: Tue, 4 Nov 2025 19:58:03 +0800 Subject: [PATCH] update at 2025-11-04 19:58:03 --- CHANGELOG.md | 48 +++++++++++++++++++++++- CHANGELOG_en.md | 48 +++++++++++++++++++++++- README.md | 44 +++++++++++++++++++++- README_en.md | 48 ++++++++++++++++++++++-- RELEASE.md | 44 ++++++++++++++++++++-- deploy.sh | 15 ++++++-- fcitx-osx.vim | 99 +++++++++++++++++++++++++++++++++---------------- 7 files changed, 301 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acaf2d3..a41e24b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # 更新日志 -本文档记录了 Obsidian Vim 输入法自动切换插件的所有重要变更。 +本文档记录了 Vim 输入法自动切换插件的所有重要变更。 + +包含 Obsidian 插件和 Vim 插件两个版本的更新历史。 格式基于 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.0.0/), 版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。 @@ -9,6 +11,50 @@ --- +## [2.0.0] - 2025-11-04 + +### 新增功能 + +#### Vim 原生插件支持 +- **新增 fcitx-osx.vim 插件**:为原生 Vim/NeoVim 编辑器提供输入法自动切换功能 +- **智能输入法状态记忆**: + - 记住退出 Insert 模式时的输入法(中文/英文) + - 下次进入 Insert 模式时自动恢复上次的输入法状态 + - 完全无感知切换,无任何 UI 闪烁 +- **使用 fcitx-remote -s 命令**: + - 通过指定输入法 ID 进行精确切换 + - 替代原有的 `-c`/`-o` 开关命令 + - 支持自定义英文和中文输入法 ID +- **异步执行优化**: + - 使用 Vim 8+ 的 `job_start()` 异步执行命令 + - 完全消除输入法切换时的 UI 闪烁和延迟 + - 向后兼容旧版本 Vim(使用后台进程) + +#### 部署脚本增强 +- **deploy.sh 更新**:一键部署 Obsidian 插件和 Vim 插件 +- 自动创建 `~/.vim/plugin/` 目录 +- 同时复制两个插件到各自的目标位置 + +### 改进 + +#### Vim 插件性能优化 +- 使用 `fcitx-remote -n` 获取当前输入法名称 +- 保存完整的输入法 ID 而不是简单的开关状态 +- 避免不必要的输入法切换(相同输入法时跳过) +- 所有输出重定向到 `/dev/null`,确保静默执行 + +### Bug 修复 +- 修复 Vim 插件中 `fcitx-remote -c` 命令不生效的问题 +- 修复标题栏显示 "fcitx-remote" 的闪烁问题 +- 修复字符串比较失败(`"2\n" == 2`)的问题 + +### 文档更新 +- 更新 README.md 和 README_en.md,添加 Vim 插件安装说明 +- 更新所有文档标题,从 "Obsidian" 改为包含两个插件的描述 +- 添加 Vim 插件配置说明和调试方法 + +--- + ## [1.0.8] - 2025-01-04 这是一个重大更新版本,引入了输入法状态记忆功能,并修复了多个关键问题。 diff --git a/CHANGELOG_en.md b/CHANGELOG_en.md index d6b5371..4296713 100644 --- a/CHANGELOG_en.md +++ b/CHANGELOG_en.md @@ -1,6 +1,8 @@ # Changelog -All notable changes to the Obsidian Vim Input Method Switch Plugin will be documented in this file. +All notable changes to the Vim Input Method Switch Plugin will be documented in this file. + +Includes update history for both Obsidian plugin and Vim plugin versions. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). @@ -9,6 +11,50 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 --- +## [2.0.0] - 2025-11-04 + +### Added + +#### Native Vim Plugin Support +- **New fcitx-osx.vim plugin**: Provides automatic input method switching for native Vim/NeoVim editors +- **Smart input method state memory**: + - Remembers input method (Chinese/English) when exiting Insert mode + - Automatically restores previous IM state when entering Insert mode again + - Completely seamless switching with no UI flicker +- **Using fcitx-remote -s command**: + - Precise switching by specifying input method ID + - Replaces old `-c`/`-o` toggle commands + - Supports custom English and Chinese IM IDs +- **Asynchronous execution optimization**: + - Uses Vim 8+ `job_start()` for async command execution + - Completely eliminates UI flicker and delay during IM switching + - Backward compatible with older Vim versions (using background process) + +#### Enhanced Deploy Script +- **deploy.sh update**: One-click deployment for both Obsidian and Vim plugins +- Automatically creates `~/.vim/plugin/` directory +- Copies both plugins to their respective target locations + +### Improved + +#### Vim Plugin Performance Optimization +- Uses `fcitx-remote -n` to get current input method name +- Saves complete IM ID instead of simple toggle state +- Avoids unnecessary IM switches (skips when same IM) +- All output redirected to `/dev/null` for silent execution + +### Fixed +- Fixed issue where `fcitx-remote -c` command doesn't work in Vim plugin +- Fixed title bar flashing "fcitx-remote" issue +- Fixed string comparison failure (`"2\n" == 2`) issue + +### Documentation +- Updated README.md and README_en.md with Vim plugin installation instructions +- Updated all document titles from "Obsidian" to include both plugins +- Added Vim plugin configuration and debugging instructions + +--- + ## [1.0.8] - 2025-01-04 This is a major update that introduces input method state memory and fixes several critical issues. diff --git a/README.md b/README.md index c43f917..dd5fe0a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ -# Obsidian Vim 输入法自动切换插件 +# Vim 输入法自动切换插件 [English](./README_en.md) | 中文 -这是一个为 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件。 +这是一个为 Vim 和 Obsidian 编辑器的 Vim 模式设计的输入法自动切换插件,包含: +- **Obsidian 插件**:适用于 Obsidian 编辑器的 Vim 模式 +- **Vim 插件**:适用于原生 Vim/NeoVim 编辑器 ## 功能简介 @@ -60,6 +62,8 @@ sudo pacman -S fcitx ## 安装插件 +### Obsidian 插件安装 + 1. 下载插件文件到 Obsidian 插件目录: ```bash cd /path/to/your/vault/.obsidian/plugins/ @@ -76,6 +80,33 @@ sudo pacman -S fcitx - 设置英文输入法(默认:`com.apple.keylayout.ABC`) - 设置中文输入法(默认:自动检测) +### Vim 插件安装 + +1. 复制插件文件到 Vim 配置目录: + ```bash + mkdir -p ~/.vim/plugin + cp fcitx-osx.vim ~/.vim/plugin/ + ``` + +2. 重启 Vim,插件会自动加载 + +3. 配置输入法(可选): + 在 `.vimrc` 中添加: + ```vim + " 英文输入法 ID(默认值) + let g:fcitx_english_im = 'com.apple.keylayout.ABC' + + " 中文输入法 ID(可选,插件会自动检测) + " let g:fcitx_chinese_im = 'com.tencent.inputmethod.wetype.pinyin' + ``` + +### 一键部署(推荐) + +使用部署脚本同时安装两个插件: +```bash +./deploy.sh +``` + ## 使用方法 ### 基本使用场景 @@ -147,17 +178,26 @@ npm run build ``` ### 部署 +一键部署 Obsidian 插件和 Vim 插件: ```bash ./deploy.sh ``` ### 调试 + +#### Obsidian 插件 插件会在控制台输出关键日志: - `Loading plugin...` - 插件加载 - `ESC → English` - ESC 切换到英文 - `→ Chinese` - 切换到中文 - `Error...` - 错误信息 +#### Vim 插件 +在 Vim 中查看消息: +```vim +:messages +``` + ## 更新日志 查看 [CHANGELOG.md](./CHANGELOG.md) 获取详细的版本更新历史。 diff --git a/README_en.md b/README_en.md index 37154f6..fd39725 100644 --- a/README_en.md +++ b/README_en.md @@ -1,8 +1,10 @@ -# Obsidian Vim Input Method Switch Plugin +# Vim Input Method Switch Plugin English | [中文](./README.md) -An input method auto-switching plugin designed for Obsidian's Vim mode. +An input method auto-switching plugin designed for Vim and Obsidian's Vim mode, including: +- **Obsidian Plugin**: For Obsidian editor's Vim mode +- **Vim Plugin**: For native Vim/NeoVim editors ## Features @@ -53,6 +55,8 @@ Download [fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-p ### Installation +#### Obsidian Plugin Installation + 1. Download plugin to Obsidian plugins directory: ```bash cd /path/to/your/vault/.obsidian/plugins/ @@ -67,6 +71,33 @@ Download [fcitx-remote.exe](https://github.com/yuanotes/obsidian-vim-im-switch-p - Set English input method (default: `com.apple.keylayout.ABC`) - Set Chinese input method (default: auto-detect) +#### Vim Plugin Installation + +1. Copy plugin file to Vim config directory: + ```bash + mkdir -p ~/.vim/plugin + cp fcitx-osx.vim ~/.vim/plugin/ + ``` + +2. Restart Vim, the plugin will load automatically + +3. Configure input methods (optional): + Add to `.vimrc`: + ```vim + " English input method ID (default) + let g:fcitx_english_im = 'com.apple.keylayout.ABC' + + " Chinese input method ID (optional, auto-detect by default) + " let g:fcitx_chinese_im = 'com.tencent.inputmethod.wetype.pinyin' + ``` + +#### One-Click Deploy (Recommended) + +Deploy both plugins with a single command: +```bash +./deploy.sh +``` + ## Usage ### Basic Usage Scenarios @@ -169,14 +200,25 @@ npm run build ``` ### Deploy +Deploy both Obsidian and Vim plugins with one command: ```bash ./deploy.sh ``` -#### Debug +### Debug + +#### Obsidian Plugin The plugin outputs key logs in the console: - `Loading plugin...` - Plugin loaded - `ESC → English` - ESC switches to English +- `→ Chinese` - Switches to Chinese +- `Error...` - Error messages + +#### Vim Plugin +View messages in Vim: +```vim +:messages +``` - `→ Chinese` - Switch to Chinese - `Error...` - Error messages diff --git a/RELEASE.md b/RELEASE.md index fa65f0f..773fb0d 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -8,13 +8,37 @@ ### 版本概述 -版本 2.0.0 是一个重要的里程碑版本,为 Obsidian Vim 输入法自动切换插件带来了重大改进。此版本包含输入法状态管理系统的完全重写、增强的可靠性以及全面的文档。 +版本 2.0.0 是一个重要的里程碑版本,为 Vim 输入法自动切换插件带来了重大改进。此版本包含: +- **新增原生 Vim 插件支持**:为 Vim/NeoVim 编辑器提供相同的输入法自动切换功能 +- **完善的输入法状态管理系统**:智能记忆和恢复输入法状态 +- **增强的可靠性和性能**:消除 UI 闪烁,优化切换速度 +- **全面的文档更新**:包含两个插件的完整使用说明 ### 新增内容 #### 主要功能 -##### 输入法状态记忆系统 +##### 原生 Vim 插件支持(全新) +**fcitx-osx.vim**:专为原生 Vim/NeoVim 编辑器设计的输入法自动切换插件 +- **智能状态记忆**:与 Obsidian 插件相同的输入法状态管理逻辑 +- **完全静默执行**:无任何 UI 闪烁或延迟 +- **异步执行优化**:使用 Vim 8+ 的 `job_start()` 实现后台切换 +- **精确切换**:使用 `fcitx-remote -s` 命令指定输入法 ID +- **向后兼容**:支持旧版本 Vim(使用后台进程) + +**安装方式**: +```bash +mkdir -p ~/.vim/plugin +cp fcitx-osx.vim ~/.vim/plugin/ +``` + +**配置选项**: +```vim +let g:fcitx_english_im = 'com.apple.keylayout.ABC' +let g:fcitx_chinese_im = 'com.tencent.inputmethod.wetype.pinyin' +``` + +##### 输入法状态记忆系统(两个插件共享) 插件现在具备智能状态记忆系统,能够记住您的输入法偏好: - **智能记忆**:自动记住退出 Insert 模式时使用的输入法(中文/英文) - **自动恢复**:再次进入 Insert 模式时,插件会恢复上次的输入法状态 @@ -121,12 +145,23 @@ npm run build #### 新用户 -**安装步骤**: +**Obsidian 插件安装步骤**: 1. 为您的平台安装 fcitx-remote 2. 将插件克隆到 Obsidian 插件目录 3. 在 Obsidian 设置中启用插件 4. 配置输入法名称(可选) +**Vim 插件安装步骤**: +1. 为您的平台安装 fcitx-remote +2. 复制 `fcitx-osx.vim` 到 `~/.vim/plugin/` +3. 重启 Vim,插件自动加载 +4. 在 `.vimrc` 中配置输入法 ID(可选) + +**一键部署(推荐)**: +```bash +./deploy.sh # 同时部署两个插件 +``` + 详细安装说明请参阅 [README.md](./README.md) 或 [README_en.md](./README_en.md)。 ### 已知问题 @@ -146,10 +181,11 @@ npm run build ### 未来规划 计划在未来版本中实现的功能: -- 支持更多输入法切换工具(im-select 等) +- 支持更多输入法切换工具(im-select、macism 等) - 可配置的快捷键绑定 - Visual 模式输入法处理改进 - 与 Obsidian 原生语言切换集成 +- NeoVim Lua 插件版本 ### 相关链接 diff --git a/deploy.sh b/deploy.sh index eea6441..3fef94e 100755 --- a/deploy.sh +++ b/deploy.sh @@ -55,15 +55,24 @@ cp -f main.js "$TARGET_DIR/" cp -f manifest.json "$TARGET_DIR/" cp -f styles.css "$TARGET_DIR/" -echo -e "${GREEN}✅ 文件复制完成:${NC}" +echo -e "${GREEN}✅ Obsidian 插件文件复制完成:${NC}" echo -e " • main.js" echo -e " • manifest.json" echo -e " • styles.css" +# 复制 Vim 插件 +echo -e "${YELLOW}📋 复制 Vim 插件...${NC}" +mkdir -p ~/.vim/plugin +cp -f fcitx-osx.vim ~/.vim/plugin/ +echo -e "${GREEN}✅ Vim 插件复制完成:${NC}" +echo -e " • fcitx-osx.vim → ~/.vim/plugin/" + # 验证文件 echo -e "${YELLOW}🔍 验证复制的文件...${NC}" ls -la "$TARGET_DIR" echo -e "${GREEN}🎉 部署完成!${NC}" -echo -e "${BLUE}📍 插件已部署到: $TARGET_DIR${NC}" -echo -e "${YELLOW}💡 请重启 Obsidian 或重新加载插件以使更改生效${NC}" \ No newline at end of file +echo -e "${BLUE}📍 Obsidian 插件已部署到: $TARGET_DIR${NC}" +echo -e "${BLUE}📍 Vim 插件已部署到: ~/.vim/plugin/${NC}" +echo -e "${YELLOW}💡 请重启 Obsidian 或重新加载插件以使更改生效${NC}" +echo -e "${YELLOW}💡 Vim 插件将在下次启动 Vim 时自动加载${NC}" \ No newline at end of file diff --git a/fcitx-osx.vim b/fcitx-osx.vim index 8ffbf99..3eb65cc 100644 --- a/fcitx-osx.vim +++ b/fcitx-osx.vim @@ -1,10 +1,9 @@ -" fcitx.vim 记住插入模式小企鹅输入法的状态 - " Author: lilydjwg -" Maintainer: lilydjwg -" Modified by: codefalling -" Note: 另有使用 Python3 接口的新版本 -" 此修改版用于 OS X 下的 https://github.com/CodeFalling/fcitx-remote-for-osx - " --------------------------------------------------------------------- +" fcitx-osx.vim - 智能记忆输入法状态 +" Author: Gavin Chan +" Modified by: codefalling, enhanced with smart IM state memory +" Version: 2.0.0 +" Description: 记住退出 Insert 模式时的输入法状态,下次进入时自动恢复 +" --------------------------------------------------------------------- " Load Once: if exists('g:fcitx_remote') finish @@ -27,63 +26,100 @@ endif let s:keepcpo = &cpo let g:loaded_fcitx = 1 set cpo&vim + +" --------------------------------------------------------------------- +" 全局变量:记住上次 Insert 模式的输入法名称 +let g:fcitx_last_insert_im_name = '' + +" 英文和中文输入法的 ID(可以通过 fcitx-remote -n 获取) +if !exists('g:fcitx_english_im') + let g:fcitx_english_im = 'com.apple.keylayout.ABC' +endif + +if !exists('g:fcitx_chinese_im') + let g:fcitx_chinese_im = 'auto-detect' " 将自动检测 +endif + " --------------------------------------------------------------------- " Functions: -function Fcitx2en() - let inputstatus = system("fcitx-remote") - if inputstatus == 2 - let b:inputtoggle = 1 - let t = system("fcitx-remote -c") + +" 离开 Insert 模式:保存当前输入法名称,然后切换到英文 +function! Fcitx2en() + " 保存当前输入法名称 + let current_im = substitute(system("fcitx-remote -n 2>/dev/null"), '\n', '', 'g') + let g:fcitx_last_insert_im_name = current_im + + " 切换到英文输入法(使用后台任务并重定向所有输出) + if has('job') + call job_start(['sh', '-c', 'fcitx-remote -s ' . shellescape(g:fcitx_english_im) . ' >/dev/null 2>&1']) + else + call system("fcitx-remote -s " . shellescape(g:fcitx_english_im) . " >/dev/null 2>&1 &") 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 + +" 进入 Insert 模式:根据保存的输入法名称恢复 +function! Fcitx2zh() + " 如果上次保存的输入法名称为空,说明是第一次进入,保持英文 + if g:fcitx_last_insert_im_name == '' + return + endif + + " 如果上次是英文输入法,保持英文(不需要做任何事) + if g:fcitx_last_insert_im_name == g:fcitx_english_im + return + endif + + " 上次是中文输入法,恢复到那个输入法(使用后台任务并重定向所有输出) + if has('job') + call job_start(['sh', '-c', 'fcitx-remote -s ' . shellescape(g:fcitx_last_insert_im_name) . ' >/dev/null 2>&1']) + else + call system("fcitx-remote -s " . shellescape(g:fcitx_last_insert_im_name) . " >/dev/null 2>&1 &") + endif endfunction " --------------------------------------------------------------------- " Autocmds: -function Fcitx2zhOnce() + +" 进入 Insert 模式一次后的处理 +function! Fcitx2zhOnce() call Fcitx2zh() call UnBindAu() endfunction -function BindAu2zhOnce() +function! BindAu2zhOnce() augroup Fcitx au InsertEnter * call Fcitx2zhOnce() augroup END endfunction -function BindAu() +" 绑定自动命令 +function! BindAu() augroup Fcitx + " 离开 Insert 模式:保存输入法状态并切换到英文 au InsertLeave * call Fcitx2en() + " 进入 Insert 模式:恢复上次的输入法状态 au InsertEnter * call Fcitx2zh() + " Vim 启动时:切换到英文 au VimEnter * call Fcitx2en() augroup END endfunction -function UnBindAu() +function! UnBindAu() au! Fcitx InsertLeave * au! Fcitx InsertEnter * endfunction -"call once when enter insert mode instead of vim startup +" 延迟初始化:首次进入 Insert 模式时才绑定自动命令 let g:called_bind = 0 -function EchoBind() +function! EchoBind() if (g:called_bind==0) call BindAu() endif - let g:called_bind =1 + 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() @@ -95,9 +131,10 @@ function! Multiple_cursors_after() endfunction " --------------------------------------------------------------------- -" Restoration And Modelines: +" 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 + +" vim:fdm=expr:fde=getline(v\:lnum-1)=~'\\v"\\s*-{20,}'?'>1'\:1