Skip to content

Commit

Permalink
substitute: change usage of mixedSed
Browse files Browse the repository at this point in the history
* .sedKeys: if no valid context, then use the default context
* .sed (rules): ignore its context values and make it always work
  * even .context is still None
* .sed (rules): take effects before static rules
  • Loading branch information
gdh1995 committed Apr 3, 2021
1 parent cb0ca27 commit d868365
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
11 changes: 5 additions & 6 deletions background/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const SedActionMap: ReadonlySafeDict<SedAction> = {

let staticSeds_: readonly ClipSubItem[] | null = null

const parseSeds_ = (text: string): readonly ClipSubItem[] => {
const parseSeds_ = (text: string, fixedContexts: SedContext): readonly ClipSubItem[] => {
const result: ClipSubItem[] = []
for (let line of text.split("\n")) {
line = line.trim()
Expand Down Expand Up @@ -51,7 +51,7 @@ const parseSeds_ = (text: string): readonly ClipSubItem[] => {
}
const matchRe = BgUtils_.makeRegexp_(body[1], retainMatched ? flags.replace("g", "") : flags)
matchRe && result.push({
contexts_: parseSedKeys_(head),
contexts_: fixedContexts || parseSedKeys_(head),
host_: host,
match_: matchRe,
retainMatched_: retainMatched,
Expand Down Expand Up @@ -101,14 +101,13 @@ export const substitute_ = (text: string, context: SedContext, mixedSed?: MixedS
const notParsed = !mixedSed || typeof mixedSed !== "object"
let rules = notParsed ? mixedSed as Exclude<typeof mixedSed, ParsedSedOpts> : (mixedSed as ParsedSedOpts).r
if (rules === false) { return text }
let arr = staticSeds_ || (staticSeds_ = parseSeds_(settings.get_("clipSub")))
let arr = staticSeds_ || (staticSeds_ = parseSeds_(settings.get_("clipSub"), SedContext.NONE))
context = !notParsed && (mixedSed as ParsedSedOpts).k && parseSedKeys_((mixedSed as ParsedSedOpts).k!) || context
// note: `sed` may come from options of key mappings, so here always convert it to a string
if (rules && rules !== true) {
rules = (rules + "").replace(<RegExpG> /(?!\\) ([a-z]{1,6})(?![\x00- A-Za-z\\])/g, "\n$1")
arr = arr.concat(parseSeds_(rules))
arr = parseSeds_(rules, context || (context = SedContext.NO_STATIC)).concat(arr)
}
context = !notParsed && (mixedSed as ParsedSedOpts).k ? parseSedKeys_((mixedSed as ParsedSedOpts).k!)
: context
let parsedUrl: URL | null, host: string | null = "", hostType: number
for (const item of arr) {
if (item.contexts_ & context && (!item.host_
Expand Down
59 changes: 28 additions & 31 deletions background/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,32 +404,7 @@ var BgUtils_ = {
} }
if (workType === Urls.WorkType.ActIfNoSideEffects) { switch (cmd) {
case "e": case "exec": case "eval": case "expr": case "calc": case "m": case "math":
return a.require_("MathParser").catch(() => null
).then<Urls.MathEvalResult>(function (MathParser): Urls.MathEvalResult {
BgUtils_.quotedStringRe_.test(path) && (path = path.slice(1, -1));
path = path.replace(/\uff0c/g as RegExpG, " ");
const re2 = /([\u2070-\u2079\xb2\xb3\xb9]+)|[\xb0\uff0b\u2212\xd7\xf7]|''?/g
path = path.replace(<RegExpG> /deg\b/g, "\xb0")
.replace(<RegExpG & RegExpSearchable<0>> /[\xb0']\s*\d+(\s*)(?=\)|$)/g, (str, g1): string => {
str = str.trim()
return str + (str[0] === "'" ? "''" : "'") + g1
}).replace(<RegExpG & RegExpSearchable<1>> re2, (str, g1): string => {
let out = "", i: number
if (!g1) {
return str === "\xb0" ? "/180*PI+" : (i = "\uff0b\u2212\xd7\xf7".indexOf(str)) >= 0 ? "+-*/"[i]
: `/${str === "''" ? 3600 : 60}/180*PI+`
}
for (const ch of str) {
out += ch < "\xba" ? ch > "\xb3" ? 1 : ch < "\xb3" ? 2 : 3 : ch.charCodeAt(0) - 0x2070
}
return out && "**" + out
}).replace(<RegExpG>/\*PI\+(?!\s*\d)/g, "*PI").replace(<RegExpG> /([\d.])rad\b/g, "$1")
let nParenthesis = ([].reduce as (cb: (o: number, c: string) => number, o: number) => number
).call(path, (n, ch) => n + (ch === "(" ? 1 : ch === ")" ? -1 : 0), 0)
while (nParenthesis-- > 0) { path += ")" }
let result = BgUtils_.tryEvalMath_(path, MathParser) || "";
return [result, Urls.kEval.math, path];
});
return a.require_("MathParser").catch(a.blank_).then<Urls.MathEvalResult>(a.tryEvalMath_.bind(0, path))
case "error":
return [path, Urls.kEval.ERROR];
} }
Expand Down Expand Up @@ -527,21 +502,43 @@ var BgUtils_ = {
a._nestedEvalCounter = 0;
return <Urls.Url> res;
} as Urls.Executor,
tryEvalMath_ (expr: string, mathParser: any): string | null {
let result: any = null;
tryEvalMath_ (path: string, mathParser: any): Urls.MathEvalResult {
BgUtils_.quotedStringRe_.test(path) && (path = path.slice(1, -1));
path = path.replace(/\uff0c/g as RegExpG, " ");
const re2 = /([\u2070-\u2079\xb2\xb3\xb9]+)|[\xb0\uff0b\u2212\xd7\xf7]|''?/g
path = path.replace(<RegExpG> /deg\b/g, "\xb0")
.replace(<RegExpG & RegExpSearchable<0>> /[\xb0']\s*\d+(\s*)(?=\)|$)/g, (str, g1): string => {
str = str.trim()
return str + (str[0] === "'" ? "''" : "'") + g1
}).replace(<RegExpG & RegExpSearchable<1>> re2, (str, g1): string => {
let out = "", i: number
if (!g1) {
return str === "\xb0" ? "/180*PI+" : (i = "\uff0b\u2212\xd7\xf7".indexOf(str)) >= 0 ? "+-*/"[i]
: `/${str === "''" ? 3600 : 60}/180*PI+`
}
for (const ch of str) {
out += ch < "\xba" ? ch > "\xb3" ? 1 : ch < "\xb3" ? 2 : 3 : ch.charCodeAt(0) - 0x2070
}
return out && "**" + out
}).replace(<RegExpG>/\*PI\+(?!\s*\d)/g, "*PI").replace(<RegExpG> /([\d.])rad\b/g, "$1")
let nParenthesis = ([].reduce as (cb: (o: number, c: string) => number, o: number) => number
).call(path, (n, ch) => n + (ch === "(" ? 1 : ch === ")" ? -1 : 0), 0)
while (nParenthesis-- > 0) { path += ")" }

let result: any = ""
if ((mathParser = mathParser || window.MathParser || {}).evaluate) {
try {
result = mathParser.evaluate(expr);
result = mathParser.evaluate(path)
if (typeof result === "function") {
result = null;
result = ""
} else {
result = "" + result;
}
} catch {}
mathParser.clean();
mathParser.errormsg && (mathParser.errormsg = "")
}
return result;
return [result, Urls.kEval.math, path];
},
copy_: (() => "") as (text: string | any[], join?: FgReq[kFgReq.copy]["j"], sed?: MixedSedOpts | null) => string,
paste_: (() => "") as (this: void, sed?: MixedSedOpts | null, len?: number) => string | Promise<string | null> | null,
Expand Down
6 changes: 3 additions & 3 deletions content/key_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ let isPassKeysReversed = false
let mapKeyTypes = kMapKey.NONE
let mappedKeys: SafeDict<string> | null = null
let keyFSM: KeyFSM
let currentKeys = ""
let curKeyTimestamp = 0
let nextKeys: KeyFSM | ReadonlyChildKeyFSM & SafeObject | null = null
let currentKeys: string
let curKeyTimestamp: number
let nextKeys: KeyFSM | ReadonlyChildKeyFSM & SafeObject | null

let isWaitingAccessKey = false
let isCmdTriggered: kKeyCode = kKeyCode.None
Expand Down
1 change: 1 addition & 0 deletions typings/messages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ declare const enum SedContext {
/** `n` */ goNext = 1 << 13,
/** `o` */ omni = 1 << 14,
/** `r` */ goToRoot = 1 << 17,
NO_STATIC = 1 << 30,
}

interface FgRes {
Expand Down

0 comments on commit d868365

Please sign in to comment.