add gobble mode and quickmarks

This commit is contained in:
D.B 2017-11-19 13:45:18 +01:00 committed by Colin Caine
parent e8aae0b469
commit 0a0c31230e
6 changed files with 78 additions and 1 deletions

View file

@ -21,6 +21,7 @@ import * as controller from './controller'
import * as convert from './convert'
import * as dom from './dom'
import * as hinting_background from './hinting_background'
import * as gobble_mode from './parsers/gobblemode'
import * as itertools from './itertools'
import * as keyseq from './keyseq'
import * as msgsafe from './msgsafe'
@ -35,6 +36,7 @@ import * as webext from './lib/webext'
convert,
dom,
hinting_background,
gobble_mode,
itertools,
keydown_background,
keyseq,

View file

@ -7,6 +7,7 @@ import {parser as hintmode_parser} from './hinting_background'
import * as normalmode from "./parsers/normalmode"
import * as insertmode from "./parsers/insertmode"
import * as ignoremode from "./parsers/ignoremode"
import { parser as gobblemode_parser } from './parsers/gobblemode'
/** Accepts keyevents, resolves them to maps, maps to exstrs, executes exstrs */
@ -16,6 +17,7 @@ function *ParserController () {
insert: insertmode.parser,
ignore: ignoremode.parser,
hint: hintmode_parser,
gobble: gobblemode_parser,
}
while (true) {

View file

@ -727,6 +727,19 @@ export async function reset(key: string){
browser.storage.sync.set({nmaps})
}
/** Bind quickmark to a key. Available with gn[key] and go[key].*/
//#background
export async function quickmark(key: string) {
// ensure we're binding to a single key
if (key.length !== 1) {
return
}
let address = (await activeTab()).url
await bind("gn" + key, "tabopen", address)
bind("go" + key, "open", address)
}
// }}}
// {{{ HINTMODE
@ -744,5 +757,18 @@ export function hint(option?: "-b") {
// }}}
// {{{ GOBBLE mode
//#background_helper
import * as gobbleMode from './parsers/gobblemode'
/** Initialize gobble mode. It will read `nChars` input keys and append them to
`endCmd`. */
//#background
export async function gobble(nChars: number, endCmd: string) {
gobbleMode.init(nChars, endCmd)
}
// }}}
// vim: tabstop=4 shiftwidth=4 expandtab

46
src/parsers/gobblemode.ts Normal file
View file

@ -0,0 +1,46 @@
import state from '../state'
/** Simple container for the gobble state. */
class GobbleState {
public numChars = 0
public chars = ''
public endCommand = ''
}
let modeState: GobbleState = undefined
/** Init gobble mode. After parsing the defined number of input keys, execute
`endCmd` with attached parsed input. `Escape` cancels the mode and returns to
normal mode. */
export function init(numChars: number, endCommand: string) {
state.mode = 'gobble'
modeState = new GobbleState()
modeState.numChars = numChars;
modeState.endCommand = endCommand;
}
/** Reset state. */
function reset() {
modeState = undefined
state.mode = 'normal'
}
import { MsgSafeKeyboardEvent } from '../msgsafe'
/** Receive keypress. If applicable, execute a command. */
export function parser(keys: MsgSafeKeyboardEvent[]) {
const key = keys[0].key
if (key === 'Escape') {
reset()
} else if (key.length == 1) {
// Workaround to avoid modifier keys, mainly Shift.
modeState.chars += key
if (modeState.chars.length >= modeState.numChars) {
const ex_str = modeState.endCommand + ' ' + modeState.chars
reset()
return { keys: [], ex_str }
}
}
return { keys: [], ex_str: '' }
}

View file

@ -40,6 +40,7 @@ export const DEFAULTNMAPS = {
":": "fillcmdline",
"s": "fillcmdline open google",
"S": "fillcmdline tabopen google",
"M": "gobble 1 quickmark",
"xx": "something",
"b": "openbuffer",
"ZZ": "qall",

View file

@ -14,7 +14,7 @@
If this turns out to be expensive there are improvements available.
*/
export type ModeName = 'normal'|'insert'|'hint'|'ignore'
export type ModeName = 'normal' | 'insert' | 'hint' | 'ignore' | 'gobble'
class State {
mode: ModeName = 'normal'
cmdHistory: string[] = []