Skip to content

Commit

Permalink
feat: add keepMode option to keep mode at disable
Browse files Browse the repository at this point in the history
  • Loading branch information
kuuote committed Dec 4, 2023
1 parent 04d0a95 commit 324fe04
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 15 deletions.
2 changes: 2 additions & 0 deletions denops/skkeleton/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const config: ConfigOptions = {
immediatelyJisyoRW: true,
immediatelyOkuriConvert: true,
kanaTable: "rom",
keepMode: false,
keepState: false,
markerHenkan: "▽",
markerHenkanSelect: "▼",
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 8 additions & 0 deletions denops/skkeleton/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export type Func = (
char: string,
) => void | Promise<void>;

export const modeFunctions = new Cell<Record<string, Func>>(() => ({
abbrev: abbrev,
hankata: hankatakana,
hira: hirakana,
kata: katakana,
zenkaku: zenkaku,
}));

export const functions = new Cell<Record<string, Func>>(() => ({
// common
kakutei,
Expand Down
26 changes: 11 additions & 15 deletions denops/skkeleton/main.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -140,41 +139,38 @@ async function init(denops: Denops) {
}

async function enable(opts?: unknown, vimStatus?: unknown): Promise<string> {
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 <nomodeline> 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 <nomodeline> User skkeleton-enable-post");
} catch (e) {
Expand Down
5 changes: 5 additions & 0 deletions denops/skkeleton/mode.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +17,9 @@ export async function modeChange(context: Context, mode: string) {
// ignore
}
}
if (config.keepMode) {
variables.lastMode = mode;
}
}

export async function initializeStateWithAbbrev(
Expand Down
4 changes: 4 additions & 0 deletions denops/skkeleton/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
1 change: 1 addition & 0 deletions denops/skkeleton/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type ConfigOptions = {
immediatelyJisyoRW: boolean;
immediatelyOkuriConvert: boolean;
kanaTable: string;
keepMode: boolean;
keepState: boolean;
markerHenkan: string;
markerHenkanSelect: string;
Expand Down
5 changes: 5 additions & 0 deletions doc/skkeleton.jax
Original file line number Diff line number Diff line change
Expand Up @@ -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)
このオプションを有効にすると
Expand Down

0 comments on commit 324fe04

Please sign in to comment.