<commit_before>var VSettings: VSettingsTy, VHud: VHUDTy, VPort: VPortTy, VEvent: VEventModeTy
  , VimiumInjector: VimiumInjectorTy | undefined | null;

(function () {
  interface EscF {
    <T extends Exclude<HandlerResult, HandlerResult.Suppress>> (this: void, i: T): T;
    (this: void, i: HandlerResult.Suppress): HandlerResult.Prevent;
  }
  interface Port extends chrome.runtime.Port {
    postMessage<K extends keyof FgRes>(request: Req.fgWithRes<K>): 1;
    postMessage<K extends keyof FgReq>(request: Req.fg<K>): 1;
  }
  interface SpecialCommands {
    [kFgCmd.reset] (this: void): void;
    [kFgCmd.showHelp] (msg?: number | "e"): void;
  }

  let KeydownEvents: KeydownCacheArray, keyMap: KeyMap
    , currentKeys = "", isEnabled = false, isLocked = false
    , mappedKeys = null as SafeDict<string> | null, nextKeys = null as KeyMap | ReadonlyChildKeyMap | null
    , esc = function<T extends Exclude<HandlerResult, HandlerResult.Suppress>> (i: T): T {
      currentKeys = ""; nextKeys = null; return i;
    } as EscF
    , onKeyup2 = null as ((this: void, event: Pick<KeyboardEvent, "keyCode">) => void) | null
    , passKeys = null as SafeEnum | null | "", isPassKeysReverted = false
    , onWndFocus = function (this: void): void { /* empty */ }, onWndBlur2: ((this: void) => void) | null = null
    , OnOther: BrowserType = !(Build.BTypes & ~BrowserType.Chrome) || !(Build.BTypes & ~BrowserType.Firefox)
          || !(Build.BTypes & ~BrowserType.Edge)
        ? Build.BTypes as number : BrowserType.Chrome
    ;

  const injector = VimiumInjector,
  notChrome = !(Build.BTypes & ~BrowserType.Chrome) ? false : !(Build.BTypes & BrowserType.Chrome) ? true
    : typeof browser !== "undefined" && !!(
    browser && (browser as typeof chrome).runtime) && !((browser as typeof chrome | Element) instanceof Element),
  vPort = {
    _port: null as Port | null,
    _callbacks: Object.create(null) as { [msgId: number]: <K extends keyof FgRes>(this: void, res: FgRes[K]) => void },
    _id: 1,
    SafePost_<K extends keyof FgReq> (this: void, request: FgReq[K] & Req.baseFg<K>): void {
      try {
        if (!vPort._port) {
          vPort.Connect_();
          injector && setTimeout(vPort.TestAlive_, 50);
        } else if (Build.BTypes & BrowserType.Firefox && injector) {
          injector.$_run(InjectorTask.recheckLiving);
        }
        (vPort._port as Port).postMessage(request);
      } catch { // this extension is reloaded or disabled
        safeDestroy();
      }
    },
    Listener_<T extends keyof BgReq> (this: void, response: Req.bg<T>): void {
      type TypeToCheck = { [K in keyof BgReq]: (this: void, request: BgReq[K]) => void };
      type TypeChecked = { [K in keyof BgReq]: <T2 extends keyof BgReq>(this: void, request: BgReq[T2]) => void };
      (requestHandlers as TypeToCheck as TypeChecked)[response.N](response);
    },
    TestAlive_ (): void { vPort._port || safeDestroy(); },
    ClearPort_ (this: void): void {
      vPort._port = null;
      setTimeout(function (i): void {
        if (!i) {
          try { !safeDestroy || vPort._port || vPort.Connect_(); return; } catch {}
        }
        safeDestroy && safeDestroy();
      }, requestHandlers[kBgReq.init] ? 2000 : 5000);
    },
    Connect_: (function (this: void): void {
      const runtime: typeof chrome.runtime = (
        (!(Build.BTypes & ~BrowserType.Chrome) ? false : !(Build.BTypes & BrowserType.Chrome) ? true : notChrome)
        ? browser as typeof chrome : chrome).runtime,
      status = requestHandlers[0] ? PortType.initing
        : (isEnabled ? passKeys ? PortType.knownPartial : PortType.knownEnabled : PortType.knownDisabled)
        + (isLocked ? PortType.isLocked : 0) + (VDom.UI.styleIn_ ? PortType.hasCSS : 0),
      name = PortNameEnum.Prefix + (
        PortType.isTop * +(top === window) + PortType.hasFocus * +document.hasFocus() + status),
      data = { name: injector ? name + PortNameEnum.Delimiter + injector.versionHash : name },
      port = vPort._port = injector ? runtime.connect(injector.id, data) as Port
        : runtime.connect(data) as Port;
      port.onDisconnect.addListener(vPort.ClearPort_);
      port.onMessage.addListener(vPort.Listener_);
    })
  };
  function post<K extends keyof FgReq>(this: void, request: FgReq[K] & Req.baseFg<K>): 1 {
    return (vPort._port as Port).postMessage(request);
  }

  function onKeydown(event: KeyboardEvent): void {
    if (!isEnabled
        || (Build.MinCVer >= BrowserVer.Min$Event$$IsTrusted || !(Build.BTypes & BrowserType.Chrome) ? !event.isTrusted
            : event.isTrusted !== true && !(event.isTrusted == null && event instanceof KeyboardEvent))
        || !event.keyCode) { return; }
    if (VScroller.keyIsDown_ && VEvent.OnScrolls_[0](event)) { return; }
    if (Build.BTypes & BrowserType.Firefox
        && (!(Build.BTypes & ~BrowserType.Firefox) ? InsertMode.lock_
          : InsertMode.lock_ && OnOther === BrowserType.Firefox)
        && !VDom.isInDOM_(InsertMode.lock_ as LockableElement, document)) {
      InsertMode.lock_ = null;
    }
    let keyChar: string, key = event.keyCode, action: HandlerResult;
    if (action = VUtils.bubbleEvent_(event)) { /* empty */ }
    else if (InsertMode.isActive_()) {
      const g = InsertMode.global_;
      if (g ? !g.code ? VKeyboard.isEscape_(event) : key === g.code && VKeyboard.getKeyStat_(event) === g.stat
          : VKeyboard.isEscape_(event)
          ? !(passKeys && ((key === VKeyCodes.esc ? "<esc>" : "<c-[>") in passKeys) !== isPassKeysReverted)
          : (key > VKeyCodes.maxNotFn && (keyChar = VKeyboard.getKeyName_(event)) &&
              (action = checkValidKey(event, keyChar)), 0)
      ) {
        if (InsertMode.lock_ === document.body && InsertMode.lock_) {
          event.repeat && InsertMode.focusUpper_(key, true, event);
        } else {
          action = g && g.passExitKey ? HandlerResult.Nothing : HandlerResult.Prevent;
          InsertMode.exit_(event);
        }
      }
    }
    else if (key > VKeyCodes.maxNotPrintable || key === VKeyCodes.backspace || key === VKeyCodes.tab
        || key === VKeyCodes.enter) {
      if (keyChar = VKeyboard.char_(event)) {
        action = checkValidKey(event, keyChar);
        if (action === HandlerResult.Nothing && InsertMode.suppressType_ && keyChar.length === 1) {
          action = HandlerResult.Prevent;
        }
      }
    }
    else if (key !== VKeyCodes.esc || VKeyboard.getKeyStat_(event)
      || passKeys && ("<esc>" in passKeys) !== isPassKeysReverted) { /* empty */ }
    else if (nextKeys) {
      esc(HandlerResult.Suppress);
      action = HandlerResult.Prevent;
    } else if (!event.repeat && VDom.UI.removeSelection_()) {
      action = HandlerResult.Prevent;
    } else if (event.repeat && !KeydownEvents[VKeyCodes.esc] && document.activeElement !== document.body) {
      let c = document.activeElement; c && c.blur && c.blur();
    } else if (top !== window && document.activeElement === document.body) {
      InsertMode.focusUpper_(key, event.repeat, event);
    }
    if (action < HandlerResult.MinStopOrPreventEvents) { return; }
    if (action > HandlerResult.MaxNotPrevent) {
      event.preventDefault();
    }
    event.stopImmediatePropagation();
    KeydownEvents[key] = 1;
  }
  function onKeyup(event: KeyboardEvent): void {
    if (!isEnabled
        || (Build.MinCVer >= BrowserVer.Min$Event$$IsTrusted || !(Build.BTypes & BrowserType.Chrome) ? !event.isTrusted
            : event.isTrusted !== true && !(event.isTrusted == null && event instanceof KeyboardEvent))
        || !event.keyCode) { return; }
    VScroller.keyIsDown_ = 0;
    if (InsertMode.suppressType_ && getSelection().type !== InsertMode.suppressType_) {
      VEvent.setupSuppress_();
    }
    if (KeydownEvents[event.keyCode]) {
      KeydownEvents[event.keyCode] = 0;
      event.preventDefault();
      event.stopImmediatePropagation();
    } else if (onKeyup2) {
      onKeyup2(event);
    }
  }
  function onFocus(this: void, event: Event | FocusEvent): void {
    if (Build.MinCVer >= BrowserVer.Min$Event$$IsTrusted || !(Build.BTypes & BrowserType.Chrome)
        ? !event.isTrusted : event.isTrusted === false) { return; }
    // on Firefox, target may also be `document`<commit_msg><esc>: suppress the first repeated one if needed<commit_after>var VSettings: VSettingsTy, VHud: VHUDTy, VPort: VPortTy, VEvent: VEventModeTy
  , VimiumInjector: VimiumInjectorTy | undefined | null;

(function () {
  interface EscF {
    <T extends Exclude<HandlerResult, HandlerResult.Suppress>> (this: void, i: T): T;
    (this: void, i: HandlerResult.Suppress): HandlerResult.Prevent;
  }
  interface Port extends chrome.runtime.Port {
    postMessage<K extends keyof FgRes>(request: Req.fgWithRes<K>): 1;
    postMessage<K extends keyof FgReq>(request: Req.fg<K>): 1;
  }
  interface SpecialCommands {
    [kFgCmd.reset] (this: void): void;
    [kFgCmd.showHelp] (msg?: number | "e"): void;
  }

  let KeydownEvents: KeydownCacheArray, keyMap: KeyMap
    , currentKeys = "", isEnabled = false, isLocked = false
    , mappedKeys = null as SafeDict<string> | null, nextKeys = null as KeyMap | ReadonlyChildKeyMap | null
    , esc = function<T extends Exclude<HandlerResult, HandlerResult.Suppress>> (i: T): T {
      currentKeys = ""; nextKeys = null; return i;
    } as EscF
    , onKeyup2 = null as ((this: void, event: Pick<KeyboardEvent, "keyCode">) => void) | null
    , passKeys = null as SafeEnum | null | "", isPassKeysReverted = false
    , onWndFocus = function (this: void): void { /* empty */ }, onWndBlur2: ((this: void) => void) | null = null
    , OnOther: BrowserType = !(Build.BTypes & ~BrowserType.Chrome) || !(Build.BTypes & ~BrowserType.Firefox)
          || !(Build.BTypes & ~BrowserType.Edge)
        ? Build.BTypes as number : BrowserType.Chrome
    ;

  const injector = VimiumInjector,
  notChrome = !(Build.BTypes & ~BrowserType.Chrome) ? false : !(Build.BTypes & BrowserType.Chrome) ? true
    : typeof browser !== "undefined" && !!(
    browser && (browser as typeof chrome).runtime) && !((browser as typeof chrome | Element) instanceof Element),
  vPort = {
    _port: null as Port | null,
    _callbacks: Object.create(null) as { [msgId: number]: <K extends keyof FgRes>(this: void, res: FgRes[K]) => void },
    _id: 1,
    SafePost_<K extends keyof FgReq> (this: void, request: FgReq[K] & Req.baseFg<K>): void {
      try {
        if (!vPort._port) {
          vPort.Connect_();
          injector && setTimeout(vPort.TestAlive_, 50);
        } else if (Build.BTypes & BrowserType.Firefox && injector) {
          injector.$_run(InjectorTask.recheckLiving);
        }
        (vPort._port as Port).postMessage(request);
      } catch { // this extension is reloaded or disabled
        safeDestroy();
      }
    },
    Listener_<T extends keyof BgReq> (this: void, response: Req.bg<T>): void {
      type TypeToCheck = { [K in keyof BgReq]: (this: void, request: BgReq[K]) => void };
      type TypeChecked = { [K in keyof BgReq]: <T2 extends keyof BgReq>(this: void, request: BgReq[T2]) => void };
      (requestHandlers as TypeToCheck as TypeChecked)[response.N](response);
    },
    TestAlive_ (): void { vPort._port || safeDestroy(); },
    ClearPort_ (this: void): void {
      vPort._port = null;
      setTimeout(function (i): void {
        if (!i) {
          try { !safeDestroy || vPort._port || vPort.Connect_(); return; } catch {}
        }
        safeDestroy && safeDestroy();
      }, requestHandlers[kBgReq.init] ? 2000 : 5000);
    },
    Connect_: (function (this: void): void {
      const runtime: typeof chrome.runtime = (
        (!(Build.BTypes & ~BrowserType.Chrome) ? false : !(Build.BTypes & BrowserType.Chrome) ? true : notChrome)
        ? browser as typeof chrome : chrome).runtime,
      status = requestHandlers[0] ? PortType.initing
        : (isEnabled ? passKeys ? PortType.knownPartial : PortType.knownEnabled : PortType.knownDisabled)
        + (isLocked ? PortType.isLocked : 0) + (VDom.UI.styleIn_ ? PortType.hasCSS : 0),
      name = PortNameEnum.Prefix + (
        PortType.isTop * +(top === window) + PortType.hasFocus * +document.hasFocus() + status),
      data = { name: injector ? name + PortNameEnum.Delimiter + injector.versionHash : name },
      port = vPort._port = injector ? runtime.connect(injector.id, data) as Port
        : runtime.connect(data) as Port;
      port.onDisconnect.addListener(vPort.ClearPort_);
      port.onMessage.addListener(vPort.Listener_);
    })
  };
  function post<K extends keyof FgReq>(this: void, request: FgReq[K] & Req.baseFg<K>): 1 {
    return (vPort._port as Port).postMessage(request);
  }

  function onKeydown(event: KeyboardEvent): void {
    if (!isEnabled
        || (Build.MinCVer >= BrowserVer.Min$Event$$IsTrusted || !(Build.BTypes & BrowserType.Chrome) ? !event.isTrusted
            : event.isTrusted !== true && !(event.isTrusted == null && event instanceof KeyboardEvent))
        || !event.keyCode) { return; }
    if (VScroller.keyIsDown_ && VEvent.OnScrolls_[0](event)) { return; }
    if (Build.BTypes & BrowserType.Firefox
        && (!(Build.BTypes & ~BrowserType.Firefox) ? InsertMode.lock_
          : InsertMode.lock_ && OnOther === BrowserType.Firefox)
        && !VDom.isInDOM_(InsertMode.lock_ as LockableElement, document)) {
      InsertMode.lock_ = null;
    }
    let keyChar: string, key = event.keyCode, action: HandlerResult;
    if (action = VUtils.bubbleEvent_(event)) { /* empty */ }
    else if (InsertMode.isActive_()) {
      const g = InsertMode.global_;
      if (g ? !g.code ? VKeyboard.isEscape_(event) : key === g.code && VKeyboard.getKeyStat_(event) === g.stat
          : VKeyboard.isEscape_(event)
          ? !(passKeys && ((key === VKeyCodes.esc ? "<esc>" : "<c-[>") in passKeys) !== isPassKeysReverted)
          : (key > VKeyCodes.maxNotFn && (keyChar = VKeyboard.getKeyName_(event)) &&
              (action = checkValidKey(event, keyChar)), 0)
      ) {
        if (InsertMode.lock_ === document.body && InsertMode.lock_) {
          event.repeat && InsertMode.focusUpper_(key, true, event);
        } else {
          action = g && g.passExitKey ? HandlerResult.Nothing : HandlerResult.Prevent;
          InsertMode.exit_(event);
        }
      }
    }
    else if (key > VKeyCodes.maxNotPrintable || key === VKeyCodes.backspace || key === VKeyCodes.tab
        || key === VKeyCodes.enter) {
      if (keyChar = VKeyboard.char_(event)) {
        action = checkValidKey(event, keyChar);
        if (action === HandlerResult.Nothing && InsertMode.suppressType_ && keyChar.length === 1) {
          action = HandlerResult.Prevent;
        }
      }
    }
    else if (key !== VKeyCodes.esc || VKeyboard.getKeyStat_(event)
      || passKeys && ("<esc>" in passKeys) !== isPassKeysReverted) { /* empty */ }
    else if (nextKeys) {
      esc(HandlerResult.Suppress);
      action = HandlerResult.Prevent;
    } else if (!event.repeat && VDom.UI.removeSelection_()) {
      action = HandlerResult.Prevent;
    } else if (event.repeat && !KeydownEvents[VKeyCodes.esc] && document.activeElement !== document.body) {
      let c = document.activeElement; c && c.blur && c.blur();
      action = HandlerResult.Prevent;
    } else if (top !== window && document.activeElement === document.body) {
      InsertMode.focusUpper_(key, event.repeat, event);
    }
    if (action < HandlerResult.MinStopOrPreventEvents) { return; }
    if (action > HandlerResult.MaxNotPrevent) {
      event.preventDefault();
    }
    event.stopImmediatePropagation();
    KeydownEvents[key] = 1;
  }
  function onKeyup(event: KeyboardEvent): void {
    if (!isEnabled
        || (Build.MinCVer >= BrowserVer.Min$Event$$IsTrusted || !(Build.BTypes & BrowserType.Chrome) ? !event.isTrusted
            : event.isTrusted !== true && !(event.isTrusted == null && event instanceof KeyboardEvent))
        || !event.keyCode) { return; }
    VScroller.keyIsDown_ = 0;
    if (InsertMode.suppressType_ && getSelection().type !== InsertMode.suppressType_) {
      VEvent.setupSuppress_();
    }
    if (KeydownEvents[event.keyCode]) {
      KeydownEvents[event.keyCode] = 0;
      event.preventDefault();
      event.stopImmediatePropagation();
    } else if (onKeyup2) {
      onKeyup2(event);
    }
  }
  function onFocus(this: void, event: Event | FocusEvent): void {
    if (Build.MinCVer >= BrowserVer.Min$Event$$IsTrusted || !(Build.BTypes & BrowserType.Chrome)
        ? !event.isTrusted : event.isTrusted === false) { return; }
    // on Firefox, target may also be `document`
{'fraction_non_alphanumeric': 0.14507271739786373, 'fraction_numerical': 0.003500090519582403, 'mean_word_length': 3.118290258449304, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '467012', 'n_tokens_mistral': 5348, 'n_tokens_neox': 5352, 'n_words': 1188}