2 This is part of the VimFx documentation.
3 Copyright Simon Lydell 2015.
4 See the file README.md for copying conditions.
9 VimFx has a public API. It is intended to be used by users who would like to
10 write a so-called [config file].
12 Some parts of the API is also intended to be used by authors who would like to
19 let {classes: Cc, interfaces: Ci, utils: Cu} = Components
20 Cu.import('resource://gre/modules/Services.jsm')
21 let apiPref = 'extensions.VimFx.api_url'
22 let apiUrl = Services.prefs.getComplexValue(apiPref, Ci.nsISupportsString).data
23 Cu.import(apiUrl, {}).getAPI(vimfx => {
25 // Do things with the `vimfx` object here.
30 You might also want to take a look at the [config file bootstrap.js
31 example][bootstrap.js].
33 Note that the callback passed to `getAPI` is called once every time VimFx starts
34 up, not once per Firefox session! This means that if you update VimFx (or
35 disable and then enable it), the callback is re-run with the new version.
40 The following sub-sections assume that you store VimFx’s public API in a
41 variable called `vimfx`.
43 ### `vimfx.get(pref)`, `vimfx.getDefault(pref)` and `vimfx.set(pref, value)`
45 Gets or sets the (default) value of the VimFx pref `pref`.
47 You can see all prefs in [defaults.coffee], or by opening [about:config] and
48 filtering by `extensions.vimfx`. Note that you can also access the [special
49 options], which may not be accessed in [about:config], using `vimfx.get()` and
50 `vimfx.set()`—in fact, this is the _only_ way of accessing those options.
52 #### `vimfx.get(pref)`
54 Gets the value of the VimFx pref `pref`.
57 // Get the value of the Hint chars option:
58 vimfx.get('hint_chars')
59 // Get all keyboard shortcuts (as a string) for the `f` command:
60 vimfx.get('mode.normal.follow')
63 #### `vimfx.getDefault(pref)`
65 Gets the default value of the VimFx pref `pref`.
67 Useful when you wish to extend a default, rather than replacing it. See below.
69 #### `vimfx.set(pref, value)`
71 Sets the value of the VimFx pref `pref` to `value`.
74 // Set the value of the Hint chars option:
75 vimfx.set('hint_chars', 'abcdefghijklmnopqrstuvwxyz')
76 // Add yet a keyboard shortcut for the `f` command:
77 vimfx.set('mode.normal.follow', vimfx.getDefault('mode.normal.follow') + ' e')
80 When extending a pref (as in the second example above), be sure to use
81 `vimfx.getDefault` rather than `vimfx.get`. Otherwise you get a multiplying
82 effect. In the above example, after starting Firefox a few times the pref would
83 be `f e e e e`. Also, if you find that example very verbose: Remember that
84 you’re using a programming language! Write a small helper function that suits
87 Note: If you produce conflicting keyboard shortcuts, the order of your code does
88 not matter. The command that comes first in VimFx’s settings page in the Add-ons
89 Manager (and in the help dialog) gets the shortcut; the other one(s) do(es) not.
90 See the notes about order in [mode object], [category object] and [command
91 object] for more information about order.
94 // Even though we set the shortcut for focusing the search bar last, the command
95 // for focusing the location bar “wins”, because it comes first in VimFx’s
96 // settings page in the Add-ons Manager.
97 vimfx.set('mode.normal.focus_location_bar', 'ö')
98 vimfx.set('mode.normal.focus_search_bar', 'ö')
100 // Swapping their orders also swaps the “winner”.
101 let {commands} = vimfx.modes.normal
102 ;[commands.focus_location_bar.order, commands.focus_search_bar.order] =
103 [commands.focus_search_bar.order, commands.focus_location_bar.order]
106 ### `vimfx.addCommand(options, fn)`
108 Creates a new command.
110 **Note:** This should only be used by config file users, not by extension
111 authors who wish to extend VimFx. They should add commands manually to
112 [`vimfx.modes`] instead.
116 - name: `String`. The name used when accessing the command via
117 `vimfx.modes[options.mode].commands[options.name]`. It is also used for the
118 pref used to store the shortcuts for the command:
119 `` `custom.mode.${options.mode}.${options.name}` ``.
120 - description: `String`. Shown in the help dialog and VimFx’s settings page in
122 - mode: `String`. Defaults to `'normal'`. The mode to add the command to. The
123 value has to be one of the keys of [`vimfx.modes`].
124 - category: `String`. Defaults to `'misc'` for Normal mode and `''`
125 (uncategorized) otherwise. The category to add the command to. The
126 value has to be one of the keys of [`vimfx.get('categories')`][categories].
127 - order: `Number`. Defaults to putting the command at the end of the category.
128 The first of the default commands has the order `100` and then they increase
129 by `100` per command. This allows to put new commands between two already
132 `fn` is called when the command is activated. See the [onInput] documentation
133 below for more information.
135 Note that you have to give the new command a shortcut in VimFx’s settings page
136 in the Add-ons Manager or set one using `vimfx.set()` to able to use the new
142 description: 'Log Hello World',
144 console.log('Hello World!')
147 vimfx.set('custom.mode.normal.hello', 'gö')
150 ### `vimfx.addOptionOverrides(...rules)` and `vimfx.addKeyOverrides(...rules)`
152 These methods take any number of arguments. Each argument is a rule. The rules
153 are added in order. The methods may be run multiple times.
155 A rule is an `Array` of length 2:
157 1. The first item is a function that returns `true` if the rule should be
158 applied and `false` if not. This is called the matching function.
159 2. The second item is the value that should be used if the rule is applied. This
160 is called the override.
162 The rules are tried in the same order they were added. When a matching rule is
163 found it is applied. No more rules will be applied.
165 #### `vimfx.addOptionOverrides(...rules)`
167 The rules are matched any time the value of a VimFx pref is needed.
169 The matching function receives a [location object].
171 The override is an object whose keys are VimFx pref names and whose values
172 override the pref in question. The values should be formatted as in an [options
176 vimfx.addOptionOverrides(
177 [ ({hostname, pathname, hash}) =>
178 `${hostname}${pathname}${hash}` === 'google.com/',
179 {prevent_autofocus: false}
184 #### `vimfx.addKeyOverrides(...rules)`
186 The rules are matched any time you press a key that is not part of the tail of a
189 The matching function receives a [location object] as well as the current
190 mode name (one of the keys of [`vimfx.modes`]).
192 The override is an array of keys which should not activate VimFx commands but be
195 This allows to disable commands on specific sites. To _add_ commands on specific
196 sites, add them globally and then disable them on all _other_ sites.
199 vimfx.addKeyOverrides(
200 [ location => location.hostname === 'facebook.com',
206 ### `vimfx.on(eventName, listener)`
208 Runs `listener(data)` when `eventName` is fired.
210 #### The `locationChange` event
212 Occurs when opening a new tab, navigating to a new URL or refreshing the page,
213 causing a full page load. The data passed to listeners is an object with the
214 following properties:
216 - vim: The current [vim object].
217 - location: A [location object].
219 This can be used to enter a different mode by default on some pages (which can
220 be used to replace the blacklist option).
223 vimfx.on('locationChange', ({vim, location}) => {
224 if (location.hostname === 'example.com') {
225 vim.enterMode('ignore')
230 #### The `notification` and `hideNotification` events
232 The `notification` event occurs when `vim.notify(message)` is called, and means
233 that `message` should be displayed to the user.
235 The `hideNotification` event occurs when the `vim.hideNotification()` is called,
236 and means that the current notification is requested to be hidden.
238 The data passed to listeners is an object with the following properties:
240 - vim: The current [vim object].
241 - message: The message that should be notified. Only for the `notification`
244 Both of these events are emitted even if the [`notifications_enabled`] option is
245 disabled, allowing you to display notifications in any way you want.
247 #### The `modeChange` event
249 Occurs whenever the current mode in any tab changes. The initial entering of the
250 default mode in new tabs also counts as a mode change. The data passed to
251 listeners is the current [vim object].
254 vimfx.on('modeChange', vim => {
255 let mode = vimfx.modes[vim.mode].name()
256 vim.notify(`Entering mode: ${mode}`)
260 #### The `TabSelect` event
262 Occurs whenever any tab in any window is selected. This is also fired when
263 Firefox starts for the currently selected tab. The data passed to listeners is
264 the `event` object passed to the standard Firefox [TabSelect] event.
266 ### The `modeDisplayChange` event
268 This is basically a combination of the `modeChange` and the `TabSelect` events.
269 The event is useful for knowing when to update UI showing the current mode. The
270 data passed to listeners is the current [vim object].
272 (VimFx itself uses this event to update the toolbar [button], by setting
273 `#main-window[vimfx-mode]` to the current mode. You may use this with custom
276 #### The `focusTypeChange` event
278 Occurs when focusing or blurring any element. The data passed to listeners is an
279 object with the following properties:
281 - vim: The current [vim object].
282 - focusType: A string similar to `match.focus` of a [match object], with the
283 following differences:
285 - The current pressed key is _not_ taken into account, because focus and blur
286 events have no current key.
287 - The value is never `null` or `'other'`, but `'none'` instead.
289 (VimFx itself uses this event to update the toolbar [button], by setting
290 `#main-window[vimfx-focus-type]` to the current focus type. You may use this
291 with custom [styling].)
297 An object whose keys are mode names and whose values are [mode object]s.
299 This is a very low-level part of the API. It allows to:
301 - Access all commands and run them. This is the only thing that a config file
305 let {commands} = vimfx.modes.normal
306 // Inside a custom command:
307 commands.tab_new.run(args)
310 - Adding new commands. This is intended to be used by extension authors who wish
311 to extend VimFx, not config file users. They should use the
312 `vimfx.addCommand()` helper instead.
315 vimfx.modes.normal.commands.new_command = {
316 pref: 'extensions.my_extension.mode.normal.new_command',
319 description: () => translate('mode.normal.new_command'),
320 run: args => console.log('New command! args:', args)
324 - Adding new modes. This is intended to be used by extension authors who wish to
325 extend VimFx, not config file users.
328 vimfx.modes.new_mode = {
329 name: () => translate('mode.new_mode'),
334 onInput(args, match) {
335 if (match.type === 'full') {
336 match.command.run(args)
338 return (match.type !== 'none')
343 Have a look at [modes.coffee] and [commands.coffee] for more information.
345 ### `vimfx.get('categories')`
347 An object whose keys are category names and whose values are [category object]s.
350 let categories = vimfx.get('categories')
352 // Add a new category.
353 categories.custom = {
354 name: () => 'Custom commands',
358 // Swap the order of the Location and Tabs categories.
359 ;[commands.focus_location_bar.order, categories.tabs.order] =
360 [categories.tabs.order, commands.focus_location_bar.order]
365 A mode is an object with the following properties:
367 - name(): `Function`. Returns a human readable name of the mode used in the help
368 dialog and VimFx’s settings page in the Add-ons Manager.
369 - order: `Number`. The first of the default modes has the order `100` and then
370 they increase by `100` per mode. This allows to put new modes between two
371 already existing ones.
372 - commands: `Object`. The keys are command names and the values are [command
374 - onEnter(data, ...args): `Function`. Called when the mode is entered.
375 - onLeave(data): `Function`. Called when the mode is left.
376 - onInput(data, match): `Function`. Called when a key is pressed.
378 #### onEnter, onLeave and onInput
380 These methods are called with an object (called `data` above) with the following
383 - vim: The current [vim object].
384 - storage: An object unique to the current [vim object] and to the current mode.
385 Allows to share things between commands of the same mode by getting and
390 This method is called with an object as mentioned above, and after that there
391 may be any number of arguments (`args` in `vim.enterMode(modeName, ...args)`)
392 that the mode is free to do whatever it wants with.
394 Whatever is returned from `onEnter` will be returned from
395 `vim.enterMode(modeName, ...args)`.
399 The object passed to this method (see above) also has the following properties:
401 - uiEvent: `Event` or `false`. The keydown event object if the event occurred in
402 the browser UI, `false` otherwise (if the event occurred in web page content).
403 - count: `Number`. The count for the command. `undefined` if no count. (This is
404 simply a copy of `match.count`. `match` is defined below.)
406 The above object should be passed to commands when running them. The mode is
407 free to do whatever it wants with the return value (if any) of the commands it
410 It also receives a [match object] as the second argument.
412 `onInput` should return `true` if the current keypress should not be passed on
413 to the browser and web pages, and `false` otherwise.
417 A category is an object with the following properties:
419 - name(): `Function`. Returns a human readable name of the category used in the
420 help dialog and VimFx’s settings page in the Add-ons Manager. Config file
421 users adding custom categories could simply return a string; extension authors
422 are encouraged to look up the name from a locale file.
423 - order: `Number`. The first of the default categories is the “uncategorized”
424 category. It has the order `100` and then they increase by `100` per category.
425 This allows to put new categories between two already existing ones.
429 A command is an object with the following properties:
431 - pref: `String`. The pref used to store the shortcuts for the command.
432 - run(args): `Function`. Called when the command is activated.
433 - description(): `Function`. Returns a description of the command (as a string),
434 shown in the help dialog and VimFx’s settings page in the Add-ons Manager.
435 - category: `String`. The category to add the command to. The value has to be
436 one of the keys of [`vimfx.get('categories')`][categories].
437 - order: `Number`. The first of the default commands has the order `100` and
438 then they increase by `100` per command. This allows to put new commands
439 between two already existing ones.
443 A `match` object has the following properties:
445 - type: `String`. It has one of the following values:
447 - `'full'`: The current keypress, together with previous keypresses, fully
448 matches a command shortcut.
449 - `'partial'`: The current keypress, together with previous keypresses,
450 partially matches a command shortcut.
451 - `'count'`: The current keypress is not part of a command shortcut, but is a
452 digit and contributes to the count of a future matched command.
453 - `'none'`: The current keypress is not part of a command shortcut and does
454 not contribute to a count.
456 - focus: `String` or `null`. The type of currently focused _element_ plus
457 current pressed _key_ combo. You might not want to run commands and suppress
458 the event if this value is anything other than null. It has one of the
459 following values, depending on what kind of _element_ is focused and which
462 - `'editable'`: element: some kind of text input, a `<select>` element or a
463 `contenteditable` element. key: any pressed key.
464 - `'activatable'`: element: an “activatable” element (link or button).
465 key: see the [`activatable_element_keys`] option.
466 - `'adjustable'`: element: an “adjustable” element (form control or video
467 player). key: see the [`adjustable_element_keys`] option.
468 - `'other'`: element: some other kind of element that can receive keystrokes,
469 for example an element in fullscreen mode. key: any pressed key.
471 If none of the above criteria is met, the value is `null`, which means that
472 the currently focused element does not appear to respond to keystrokes in any
475 - command: `null` unless `type` is `'full'`. Then it is the matched command (a
478 The matched command should usually be run at this point. It is suitable to
479 pass on the object passed to [onInput] to the command. Some modes might choose
480 to add extra properties to the object first. (That is favored over passing
481 several arguments, since it makes it easier for the command to in turn pass
482 the same data it got on to another command, if needed.)
484 Usually the return value of the command isn’t used, but that’s up to the mode.
486 - count: `Number`. The count for the command. `undefined` if no count.
488 - specialKeys: `Object`. The keys may be any of the following:
493 If a key exists, its value is always `true`. The keys that exist indicate the
494 [special keys] for the sequence used for the matched command (if any).
496 - keyStr: `String`. The current keypress represented as a string.
498 - unmodifiedKey: `String`. `keyStr` without modifiers.
500 - toplevel: `Boolean`. Whether or not the match was a toplevel match in the
501 shortcut key tree. This is `true` unless the match is part of the tail of a
504 - discard(): `Function`. Discards keys pressed so far: If `type` is `'partial'`
505 or `'count'`. For example, if you have typed `12g`, run `match.discard()` and
506 then press `$`, the `$` command will be run instead of `12g$`.
510 There is one `vim` object per tab.
512 A `vim` object has the following properties:
514 - window: [`Window`]. The current Firefox window object. Most commands
515 interacting with Firefox’s UI use this.
517 - browser: [`Browser`]. The `browser` that this vim object handles.
519 - options: `Object`. Provides access to all of VimFx’s options. It is an
522 - mode: `String`. The current mode name.
524 - enterMode(modeName, ...args): `Function`. Enter mode `modeName`, passing
525 `...args` to the mode. It is up to every mode to do whatever it wants to with
526 `...args`. If `modeName` was already the current mode, nothing is done and
527 `undefined` is returned. Otherwise it us up to the mode to return whatever it
530 - isUIEvent(event): `Function`. Returns `true` if `event` occurred in the
531 browser UI, and `false` otherwise (if it occurred in web page content).
533 - notify(message): `Function`. Display a notification with the text `message`.
535 - hideNotification(): `Function`. Hide the current notification (if any).
537 - markPageInteraction(): `Function`. Marks that the user has interacted with the
538 page. After that [autofocus prevention] is not done anymore. Commands
539 interacting with web page content might want to do this.
541 **Warning:** There are also properties starting with an underscore on `vim`
542 objects. They are private, and not supposed to be used outside of VimFx’s own
543 source code. They may change at any time.
547 An `options` object provides access to all of VimFx’s options. It is an object
548 whose keys are VimFx pref names.
550 Note that the values are not just simply `vimfx.get(pref)` for the `pref` in
551 question; they are _parsed_ (`parse(vimfx.get(pref))`):
553 - Space-separated prefs are parsed into arrays of strings.
555 - `black_list` and `{prev,next}_patterns` are parsed into arrays of regular
558 (See [parse-prefs.coffee] for all details.)
560 Any [option overrides] are automatically taken into account when getting an
563 The [special options] are also available on this object.
568 A location object is very similar to [`window.location`] in web pages.
569 Technically, it is a [`URL`] instance. You can experiment with the current
570 location object by opening the [web console] and entering `location`.
575 In frame scripts, the API consists of assigning global variables prefixed with
576 `VimFx`. VimFx then uses these when needed.
579 this.VimFxSomething = ...
582 ### `VimFxHintMatcher(...)`
584 **Note:** This should only be used by config file users, not by extension
585 authors who wish to extend VimFx.
587 If available, it is used to let you customize which elements do and don’t get
588 hints. It might help to read about [the `f` commands] first.
591 this.VimFxHintMatcher = (id, element, {type, semantic}) => {
592 // Inspect `element` and change `type` and `semantic` if needed.
593 return {type, semantic}
597 The arguments passed to this function are:
599 - id: `String`. A string identifying which command is used:
601 - `'normal'`: `f` or `af`.
602 - `'tab'`: `F`, `gf` or `gF`.
606 - element: `Element`. One out of all elements currently inside the viewport.
608 - info: `Object`. It has the following properties:
610 - type: `String` or `null`. If a string, it means that `element` should get a
611 hint. If `null`, it won’t. See the available strings below. When a marker
612 is matched, `type` decides what happens to `element`.
613 - semantic: `Boolean`. Indicates whether or not the element is “semantic.”
614 Semantic elements get better hints.
616 This object contains information on how VimFx has matched `element`. You have
617 the opportunity to change this.
619 The available type strings depend on `id`:
623 - link: A “proper” link (not used as a button with the help of JavaScript),
624 with an `href` attribute.
625 - text: An element that can you can type in, such as text inputs.
626 - clickable: Some clickable element not falling into another category.
627 - clickable-special: Like “clickable,” but uses a different technique to
628 simulate a click on the element. If “clickable” doesn’t work, try this one.
629 - scrollable: A scrollable element.
633 - link: Like “link” when `id` is “normal” (see above).
637 - link: Like “link” when `id` is “normal” (see above).
638 - text: Like “text” when `id` is “normal” (see above), except
639 contenteditable elements.
640 - contenteditable: Elements with “contenteditable” turned on.
644 - focusable: Any focusable element not falling into another category.
645 - scrollable: Like “scrollable” when `id` is “normal” (see above).
647 The function must return an object with just like the `info` parameter (with
648 `type` and `semantic`).
653 The public API is currently **experimental** and therefore **unstable.** Things
654 might break with new VimFx versions. However, no breaking changes are planned,
655 and will be avoided if feasible.
657 As soon as VimFx 1.0.0 (which does not seem to be too far away) is released
658 backwards compatibility will be a priority and won’t be broken until VimFx
661 [option overrides]: #vimfxaddoptionoverridesrules
662 [categories]: #vimfxgetcategories
663 [`vimfx.modes`]: #vimfxmodes
665 [mode object]: #mode-object
666 [category object]: #category-object
667 [command object]: #command-object
668 [match object]: #match-object
669 [vim object]: #vim-object
670 [options object]: #options-object
671 [location object]: #location-object
673 [blacklisted]: options.md#blacklist
674 [special options]: options.md#special-options
675 [config file]: config-file.md
676 [bootstrap.js]: config-file.md#bootstrapjs
677 [autofocus prevention]: options.md#prevent-autofocus
678 [`activatable_element_keys`]: options.md#activatable_element_keys
679 [`adjustable_element_keys`]: options.md#adjustable_element_keys
680 [`notifications_enabled`]: options.md#notifications_enabled
683 [the `f` commands]: commands.md#the-f-commands-1
684 [special keys]: shortcuts.md#special-keys
685 [styling]: styling.md
687 [defaults.coffee]: ../extension/lib/defaults.coffee
688 [parse-prefs.coffee]: ../extension/lib/parse-prefs.coffee
689 [modes.coffee]: ../extension/lib/modes.coffee
690 [commands.coffee]: ../extension/lib/commands.coffee
691 [vim.coffee]: ../extension/lib/vim.coffee
693 [`Window`]: https://developer.mozilla.org/en-US/docs/Web/API/Window
694 [`Browser`]: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/browser
695 [`window.location`]: https://developer.mozilla.org/en-US/docs/Web/API/Location
696 [`URL`]: https://developer.mozilla.org/en-US/docs/Web/API/URL
697 [TabSelect]: https://developer.mozilla.org/en-US/docs/Web/Events/TabSelect
698 [web console]: https://developer.mozilla.org/en-US/docs/Tools/Web_Console
699 [about:config]: http://kb.mozillazine.org/About:config