From 324fe04ea17a0db122e546ed8ad1bcda17de2515 Mon Sep 17 00:00:00 2001 From: kuuote Date: Fri, 1 Dec 2023 15:38:41 +0900 Subject: [PATCH] feat: add keepMode option to keep mode at disable --- denops/skkeleton/config.ts | 2 ++ denops/skkeleton/function.ts | 8 ++++++++ denops/skkeleton/main.ts | 26 +++++++++++--------------- denops/skkeleton/mode.ts | 5 +++++ denops/skkeleton/store.ts | 4 ++++ denops/skkeleton/types.ts | 1 + doc/skkeleton.jax | 5 +++++ 7 files changed, 36 insertions(+), 15 deletions(-) diff --git a/denops/skkeleton/config.ts b/denops/skkeleton/config.ts index 9c5a3f39..d6f4690d 100644 --- a/denops/skkeleton/config.ts +++ b/denops/skkeleton/config.ts @@ -17,6 +17,7 @@ export const config: ConfigOptions = { immediatelyJisyoRW: true, immediatelyOkuriConvert: true, kanaTable: "rom", + keepMode: false, keepState: false, markerHenkan: "▽", markerHenkanSelect: "▼", @@ -88,6 +89,7 @@ const validators: Validators = { } return name; }, + keepMode: (x) => ensure(x, is.Boolean), keepState: (x) => ensure(x, is.Boolean), markerHenkan: (x) => ensure(x, is.String), markerHenkanSelect: (x) => ensure(x, is.String), diff --git a/denops/skkeleton/function.ts b/denops/skkeleton/function.ts index 56d3a246..e9d3e3ee 100644 --- a/denops/skkeleton/function.ts +++ b/denops/skkeleton/function.ts @@ -22,6 +22,14 @@ export type Func = ( char: string, ) => void | Promise; +export const modeFunctions = new Cell>(() => ({ + abbrev: abbrev, + hankata: hankatakana, + hira: hirakana, + kata: katakana, + zenkaku: zenkaku, +})); + export const functions = new Cell>(() => ({ // common kakutei, diff --git a/denops/skkeleton/main.ts b/denops/skkeleton/main.ts index caf734b3..591c52b0 100644 --- a/denops/skkeleton/main.ts +++ b/denops/skkeleton/main.ts @@ -1,15 +1,14 @@ import { config, setConfig } from "./config.ts"; import { autocmd, Denops, fn, op, vars } from "./deps.ts"; import { assert, AssertError, is } from "./deps/unknownutil.ts"; -import { functions } from "./function.ts"; +import { functions, modeFunctions } from "./function.ts"; import { disable as disableFunc } from "./function/disable.ts"; -import { initializeStateWithAbbrev, modeChange } from "./mode.ts"; -import { hirakana } from "./function/mode.ts"; import { GoogleJapaneseInput, load as jisyoLoad, SkkServer } from "./jisyo.ts"; import { currentKanaTable, registerKanaTable } from "./kana.ts"; import { handleKey, registerKeyMap } from "./keymap.ts"; +import { initializeStateWithAbbrev } from "./mode.ts"; import { keyToNotation, notationToKey, receiveNotation } from "./notation.ts"; -import { currentContext, currentLibrary } from "./store.ts"; +import { currentContext, currentLibrary, variables } from "./store.ts"; import type { CompletionData, RankData, SkkServerOptions } from "./types.ts"; import { homeExpand } from "./util.ts"; @@ -140,41 +139,38 @@ async function init(denops: Denops) { } async function enable(opts?: unknown, vimStatus?: unknown): Promise { - const context = currentContext.get(); - const state = context.state; - const denops = context.denops!; + const oldContext = currentContext.get(); + const oldState = oldContext.state; + const denops = oldContext.denops!; if (await fn.mode(denops) === "R") { console.log("skkeleton doesn't allowed in replace mode"); return ""; } if ( - (state.type !== "input" || state.mode !== "direct") && isOpts(opts) && + (oldState.type !== "input" || oldState.mode !== "direct") && isOpts(opts) && vimStatus ) { return handle(opts, vimStatus); } // Note: must set before context initialization currentKanaTable.set(config.kanaTable); + const context = currentContext.init(); + context.denops = denops; - currentContext.init().denops = denops; try { await denops.cmd("doautocmd User skkeleton-enable-pre"); } catch (e) { console.log(e); } - if (context.mode === "zenkaku") { - hirakana(context); - } - // NOTE: Disable textwidth - currentContext.get().textwidth = await op.textwidth.getLocal(denops); + context.textwidth = await op.textwidth.getLocal(denops); await op.textwidth.setLocal(denops, 0); await denops.call("skkeleton#map"); await vars.b.set(denops, "keymap_name", "skkeleton"); await vars.g.set(denops, "skkeleton#enabled", true); - await modeChange(currentContext.get(), "hira"); + await modeFunctions.get()[variables.lastMode]?.(context, ""); try { await denops.cmd("doautocmd User skkeleton-enable-post"); } catch (e) { diff --git a/denops/skkeleton/mode.ts b/denops/skkeleton/mode.ts index 0a615fff..7efa5d1c 100644 --- a/denops/skkeleton/mode.ts +++ b/denops/skkeleton/mode.ts @@ -1,6 +1,8 @@ +import { config } from "./config.ts"; import { Context } from "./context.ts"; import { autocmd, vars } from "./deps.ts"; import { initializeState } from "./state.ts"; +import { variables } from "./store.ts"; export async function modeChange(context: Context, mode: string) { context.mode = mode; @@ -15,6 +17,9 @@ export async function modeChange(context: Context, mode: string) { // ignore } } + if (config.keepMode) { + variables.lastMode = mode; + } } export async function initializeStateWithAbbrev( diff --git a/denops/skkeleton/store.ts b/denops/skkeleton/store.ts index f567ec80..492a5aa6 100644 --- a/denops/skkeleton/store.ts +++ b/denops/skkeleton/store.ts @@ -4,3 +4,7 @@ import { Cell, LazyCell } from "./util.ts"; export const currentContext = new Cell(() => new Context()); export const currentLibrary = new LazyCell(() => new Library()); + +export const variables = { + lastMode: "hira", +}; diff --git a/denops/skkeleton/types.ts b/denops/skkeleton/types.ts index 146f7f2b..a9baff4e 100644 --- a/denops/skkeleton/types.ts +++ b/denops/skkeleton/types.ts @@ -50,6 +50,7 @@ export type ConfigOptions = { immediatelyJisyoRW: boolean; immediatelyOkuriConvert: boolean; kanaTable: string; + keepMode: boolean; keepState: boolean; markerHenkan: string; markerHenkanSelect: string; diff --git a/doc/skkeleton.jax b/doc/skkeleton.jax index 58032141..3a03ddbb 100644 --- a/doc/skkeleton.jax +++ b/doc/skkeleton.jax @@ -269,6 +269,11 @@ globalKanaTableFiles *skkeleton-config-globalKanaTableFiles* > 値の例: [["/usr/share/skk/azik_us.rule, "euc-jp"]] < +keepMode *skkeleton-config-keepMode* + (デフォルト v:false) + このオプションを有効にすると無効化しても前回のモード(カナ入力状態等) + を保持するようになります。 + keepState *skkeleton-config-keepState* (デフォルト v:false) このオプションを有効にすると