Skip to content

Commit 02e3638

Browse files
committed
mode() and visualmode() (mostly)
1 parent 07267ba commit 02e3638

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/vimscript/expression/evaluate.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { Pattern, SearchDirection } from '../pattern';
3434
import { escapeRegExp, isInteger } from 'lodash';
3535
import { VimState } from '../../state/vimState';
3636
import { Position } from 'vscode';
37+
import { Mode } from '../../mode/mode';
3738

3839
// ID of next lambda; incremented each time one is created
3940
let lambdaNumber = 1;
@@ -710,6 +711,16 @@ export class EvaluationContext {
710711
};
711712
};
712713

714+
// See `:help non-zero-arg`
715+
const nonZeroArg = (arg: Value): boolean => {
716+
if (arg.type === 'number' && arg.value !== 0) {
717+
return true;
718+
} else if (arg.type === 'string' && arg.value.length !== 0) {
719+
return true;
720+
}
721+
return false;
722+
};
723+
713724
const getArgs = (min: number, max?: number) => {
714725
if (max === undefined) {
715726
max = min;
@@ -1322,7 +1333,29 @@ export class EvaluationContext {
13221333
}
13231334
return int(values.length === 0 ? 0 : Math.min(...values.map(toInt)));
13241335
}
1325-
// TODO: mode()
1336+
case 'mode': {
1337+
const [arg] = getArgs(1);
1338+
switch (this.vimState!.currentModeIncludingPseudoModes) {
1339+
case Mode.Normal:
1340+
return str('n');
1341+
case Mode.OperatorPendingMode:
1342+
return nonZeroArg(arg!) ? str('n') : str('no');
1343+
case Mode.Visual:
1344+
return str('v');
1345+
case Mode.VisualLine:
1346+
return str('V');
1347+
case Mode.VisualBlock:
1348+
return str('\x16');
1349+
case Mode.Insert:
1350+
return str('i');
1351+
case Mode.Replace:
1352+
return str('R');
1353+
case Mode.CommandlineInProgress:
1354+
return str('c');
1355+
default:
1356+
return str(''); // TODO: Other modes
1357+
}
1358+
}
13261359
case 'or': {
13271360
const [x, y] = getArgs(2);
13281361
// eslint-disable-next-line no-bitwise
@@ -1626,7 +1659,14 @@ export class EvaluationContext {
16261659
const [d] = getArgs(1);
16271660
return list([...toDict(d!).items.values()]);
16281661
}
1629-
// TODO: visualmode()
1662+
case 'visualmode': {
1663+
const [arg] = getArgs(1); // TODO: Use arg
1664+
const mode = this.vimState?.lastVisualSelection?.mode;
1665+
if (mode === undefined) {
1666+
return str('');
1667+
}
1668+
return mode === Mode.Visual ? str('v') : mode === Mode.VisualLine ? str('V') : str('\x16');
1669+
}
16301670
// TODO: wordcount()
16311671
case 'xor': {
16321672
const [x, y] = getArgs(2);

0 commit comments

Comments
 (0)