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:
11 - Users who prefer to configure things using text files.
12 - Users who would like to add custom commands.
13 - Users who would like to set [special options].
14 - Users who would like to make site-specific customizations.
15 - Extension authors who would like to extend VimFx.
17 VimFx users who use the public API should write a so-called [config file].
23 let {classes: Cc, interfaces: Ci, utils: Cu} = Components
24 Cu.import('resource://gre/modules/Services.jsm')
25 let apiPref = 'extensions.VimFx.api_url'
26 let apiUrl = Services.prefs.getComplexValue(apiPref, Ci.nsISupportsString).data
27 Cu.import(apiUrl, {}).getAPI(vimfx => {
29 // Do things with the `vimfx` object here.
34 You might also want to take a look at the [config file bootstrap.js
35 example][bootstrap.js].
37 Note that the callback passed to `getAPI` is called once every time VimFx starts
38 up, not once per Firefox session! This means that if you update VimFx (or
39 disable and then enable it), the callback is re-run with the new version.
44 The following sub-sections assume that you store VimFx’s public API in a
45 variable called `vimfx`.
47 ### `vimfx.get(pref)`, `vimfx.getDefault(pref)` and `vimfx.set(pref, value)`
49 Gets or sets the (default) value of the VimFx pref `pref`.
51 You can see all prefs in [defaults.coffee], or by opening [about:config] and
52 filtering by `extensions.vimfx`. Note that you can also access the [special
53 options], which may not be accessed in [about:config], using `vimfx.get()` and
54 `vimfx.set()`—in fact, this is the _only_ way of accessing those options.
56 #### `vimfx.get(pref)`
58 Gets the value of the VimFx pref `pref`.
61 // Get the value of the Hint chars option:
62 vimfx.get('hint_chars')
63 // Get all keyboard shortcuts (as a string) for the `f` command:
64 vimfx.get('modes.normal.follow')
67 #### `vimfx.getDefault(pref)`
69 Gets the default value of the VimFx pref `pref`.
71 Useful when you wish to extend a default, rather than replacing it. See below.
73 #### `vimfx.set(pref, value)`
75 Sets the value of the VimFx pref `pref` to `value`.
78 // Set the value of the Hint chars option:
79 vimfx.set('hint_chars', 'abcdefghijklmnopqrstuvwxyz')
80 // Add yet a keyboard shortcut for the `f` command:
81 vimfx.set('modes.normal.follow', vimfx.getDefault('modes.normal.follow') + ' e')
84 When extending a pref (as in the second example above), be sure to use
85 `vimfx.getDefault` rather than `vimfx.get`. Otherwise you get a multiplying
86 effect. In the above example, after starting Firefox a few times the pref would
87 be `f e e e e`. Also, if you find that example very verbose: Remember that
88 you’re using a programming language! Write a small helper function that suits
91 Note: If you produce conflicting keyboard shortcuts, the order of your code does
92 not matter. The command that comes first in VimFx’s settings page in the Add-ons
93 Manager (and in the help dialog) gets the shortcut; the other one(s) do(es) not.
94 See the notes about order in [mode object], [category object] and [command
95 object] for more information about order.
98 // Even though we set the shortcut for focusing the search bar last, the command
99 // for focusing the location bar “wins”, because it comes first in VimFx’s
100 // settings page in the Add-ons Manager.
101 vimfx.set('modes.normal.focus_location_bar', 'ö')
102 vimfx.set('modes.normal.focus_search_bar', 'ö')
104 // Swapping their orders also swaps the “winner”.
105 let {commands} = vimfx.modes.normal
106 ;[commands.focus_location_bar.order, commands.focus_search_bar.order] =
107 [commands.focus_search_bar.order, commands.focus_location_bar.order]
110 ### `vimfx.addCommand(options, fn)`
112 Creates a new command.
114 **Note:** This should only be used by config file users, not by extension
115 authors who wish to extend VimFx. They should add commands manually to
116 [`vimfx.modes`] instead.
120 - name: `String`. The name used when accessing the command via
121 `vimfx.modes[options.mode].commands[options.name]`. It is also used for the
122 pref used to store the shortcuts for the command:
123 `` `custom.mode.${options.mode}.${options.name}` ``.
124 - description: `String`. Shown in the help dialog and VimFx’s settings page in
126 - mode: `String`. Defaults to `'normal'`. The mode to add the command to. The
127 value has to be one of the keys of [`vimfx.modes`].
128 - category: `String`. Defaults to `'misc'` for Normal mode and `''`
129 (uncategorized) otherwise. The category to add the command to. The
130 value has to be one of the keys of [`vimfx.get('categories')`][categories].
131 - order: `Number`. Defaults to putting the command at the end of the category.
132 The first of the default commands has the order `100` and then they increase
133 by `100` per command. This allows to put new commands between two already
136 `fn` is called when the command is activated. See the [onInput] documentation
137 below for more information.
139 Note that you have to give the new command a shortcut in VimFx’s settings page
140 in the Add-ons Manager or set one using `vimfx.set()` to able to use the new
146 description: 'Log Hello World',
148 console.log('Hello World!')
151 vimfx.set('custom.mode.normal.hello', 'gö')
154 ### `vimfx.addOptionOverrides(...rules)` and `vimfx.addKeyOverrides(...rules)`
156 These methods take any number of arguments. Each argument is a rule. The rules
157 are added in order. The methods may be run multiple times.
159 A rule is an `Array` of length 2:
161 1. The first item is a function that returns `true` if the rule should be
162 applied and `false` if not. This is called the matching function.
163 2. The second item is the value that should be used if the rule is applied. This
164 is called the override.
166 The rules are tried in the same order they were added. When a matching rule is
167 found it is applied. No more rules will be applied.
169 #### `vimfx.addOptionOverrides(...rules)`
171 The rules are matched any time the value of a VimFx pref is needed.
173 The matching function receives a [location object].
175 The override is an object whose keys are VimFx pref names and whose values
176 override the pref in question. The values should be formatted as in an [options
180 vimfx.addOptionOverrides(
181 [ ({hostname, pathname, hash}) =>
182 `${hostname}${pathname}${hash}` === 'google.com/',
183 {prevent_autofocus: false}
188 #### `vimfx.addKeyOverrides(...rules)`
190 The rules are matched any time you press a key that is not part of the tail of a
193 The matching function receives a [location object] as well as the current
194 mode name (one of the keys of [`vimfx.modes`]).
196 The override is an array of keys which should not activate VimFx commands but be
199 This allows to disable commands on specific sites. To _add_ commands on specific
200 sites, add them globally and then disable them on all _other_ sites.
203 vimfx.addKeyOverrides(
204 [ location => location.hostname === 'facebook.com',
210 ### `vimfx.on(eventName, listener)`
212 Runs `listener(data)` when `eventName` is fired.
214 #### The `locationChange` event
216 Occurs when opening a new tab or navigating to a new URL causing a full page
217 load. The data passed to listeners is an object with the following properties:
219 - vim: The current [vim object].
220 - location: A [location object].
222 This can be used to enter a different mode by default on some pages (which can
223 be used to replace the blacklist option).
226 vimfx.on('load', ({vim, location}) => {
227 if (location.hostname === 'example.com') {
228 vim.enterMode('ignore')
233 #### The `modeChange` event
235 Occurs whenever the current mode in any tab changes. The initial entering of the
236 default mode in new tabs also counts as a mode change. The data passed to
237 listeners is the current [vim object].
240 vimfx.on('modeChange', vim => {
241 let mode = vimfx.modes[vim.mode].name()
242 vim.notify(`Entering mode: ${mode}`)
246 #### The `TabSelect` event
248 Occurs whenever any tab in any window is selected. This is also fired when
249 Firefox starts for the currently selected tab. The data passed to listeners is
250 the `event` object passed to the standard Firefox [TabSelect] event.
252 ### `vimfx.refresh()`
254 If you make changes to [`vimfx.modes`] directly you need to call
255 `vimfx.refresh()` for your changes to take effect.
259 An object whose keys are mode names and whose values are [mode object]s.
261 This is a very low-level part of the API. It allows to:
263 - Access all commands and run them. This is the only thing that a config file
267 let {commands} = vimfx.modes.normal
268 // Inside a custom command:
269 commands.tab_new.run(args)
272 - Adding new commands. This is intended to be used by extension authors who wish
273 to extend VimFx, not config file users. They should use the
274 `vimfx.addCommand()` helper instead.
277 vimfx.modes.normal.commands.new_command = {
278 pref: 'extensions.my_extension.mode.normal.new_command',
281 description: () => translate('mode.normal.new_command'),
282 run: args => console.log('New command! args:', args)
286 - Adding new modes. This is intended to be used by extension authors who wish to
287 extend VimFx, not config file users.
290 vimfx.modes.new_mode = {
291 name: () => translate('mode.new_mode'),
296 onInput(args, match) {
297 if (match.type === 'full') {
298 match.command.run(args)
300 return (match.type !== 'none')
305 When you’re done modifying `vimfx.modes` directly, you need to call
306 `vimfx.refresh()`. (That’s taken care of automatically in the
307 `vimfx.addCommand()` helper.)
309 Have a look at [modes.coffee] and [commands.coffee] for more information.
311 ### `vimfx.get('categories')`
313 An object whose keys are category names and whose values are [category object]s.
316 let categories = vimfx.get('categories')
318 // Add a new category.
319 categories.custom = {
320 name: () => 'Custom commands',
324 // Swap the order of the Location and Tabs categories.
325 ;[commands.focus_location_bar.order, categories.tabs.order] =
326 [categories.tabs.order, commands.focus_location_bar.order]
331 A mode is an object with the follwing properties:
333 - name(): `Function`. Returns a human readable name of the mode used in the help
334 dialog and VimFx’s settings page in the Add-ons Manager.
335 - order: `Number`. The first of the default modes has the order `100` and then
336 they increase by `100` per mode. This allows to put new modes between two
337 already existing ones.
338 - commands: `Object`. The keys are command names and the values are [command
340 - onEnter(data, ...args): `Function`. Called when the mode is entered.
341 - onLeave(data): `Function`. Called when the mode is left.
342 - onInput(data, match): `Function`. Called when a key is pressed.
344 #### onEnter, onLeave and onInput
346 These methods are called with an object (called `data` above) with the following
349 - vim: The current [vim object].
350 - storage: An object unique to the current [vim object] and to the current mode.
351 Allows to share things between commands of the same mode by getting and
356 This method is called with an object as mentioned above, and after that there
357 may be any number of arguments (`args` in `vim.enterMode(modeName, ...args)`)
358 that the mode is free to do whatever it wants with.
362 The object passed to this method (see above) also has the following properties:
364 - isFrameEvent: `Boolean`. `true` if the event occured in web page content,
365 `false` otherwise (if the event occured in the browser UI).
366 - count: `Number`. The count for the command. `undefined` if no count. (This is
367 simply a copy of `match.count`. `match` is defined below.)
369 The above object should be passed to commands when running them. The mode is
370 free to do whatever it wants with the return value (if any) of the commands it
373 It also receives a [match object] as the second argument.
375 `onInput` should return `true` if the current keypress should not be passed on
376 to the browser and web pages, and `false` otherwise.
380 A category is an object with the follwing properties:
382 - name(): `Function`. Returns a human readable name of the category used in the
383 help dialog and VimFx’s settings page in the Add-ons Manager. Config file
384 users adding custom categories could simply return a string; extension authors
385 are encouraged to look up the name from a locale file.
386 - order: `Number`. The first of the default categories is the “uncategorized”
387 category. It has the order `100` and then they increase by `100` per category.
388 This allows to put new categories between two already existing ones.
392 A command is an object with the following properties:
394 - pref: `String`. The pref used to store the shortcuts for the command.
395 - run(args): `Function`. Called when the command is activated.
396 - description(): `Function`. Returns a description of the command (as a string),
397 shown in the help dialog and VimFx’s settings page in the Add-ons Manager.
398 - category: `String`. The category to add the command to. The value has to be
399 one of the keys of [`vimfx.get('categories')`][categories].
400 - order: `Number`. The first of the default commands has the order `100` and
401 then they increase by `100` per command. This allows to put new commands
402 between two already existing ones.
406 A `match` object has the following properties:
408 - type: `String`. It has one of the following values:
410 - `'full'`: The current keypress, together with previous keypresses, fully
411 matches a command shortcut.
412 - `'partial'`: The current keypress, together with previous keypresses,
413 partially matches a command shortcut.
414 - `'count'`: The current keypress is not part of a command shortcut, but is a
415 digit and contributes to the count of a future matched command.
416 - `'none'`: The current keypress is not part of a command shortcut and does
417 not contribute to a count.
419 - focus: `String` or `null`. The type of currently focused _element_ plus
420 current pressed _key_ combo. You might not want to run commands and suppress
421 the event if this value is anything other than null. It has one of the
422 following values, depending on what kind of _element_ is focused and which
425 - `'editable'`: element: a text input or a `contenteditable` element.
426 key: any pressed key.
427 - `'activatable'`: element: an “activatable” element (link or button).
428 key: see the [`activatable_element_keys`] option.
429 - `'adjustable'`: element: an “adjustable” element (form control or video
430 player). key: see the [`adjustable_element_keys`] option.
431 - `'other'`: element: some other kind of element that can receive keystrokes,
432 for example an element in fullscreen mode. key: any pressed key.
434 If none of the above criteria is met, the value is `null`, which means that
435 the currently focused element does not appear to respond to keystrokes in any
438 - command: `null` unless `type` is `'full'`. Then it is the matched command (a
441 The matched command should usually be run at this point. It is suitable to
442 pass on the object passed to [onInput] to the command. Some modes might choose
443 to add extra properties to the object first. (That is favored over passing
444 several arguments, since it makes it easier for the command to in turn pass
445 the same data it got on to another command, if needed.)
447 Usually the return value of the command isn’t used, but that’s up to the mode.
449 - count: `Number`. The count for the command. `undefined` if no count.
451 - specialKeys: `Object`. The keys may be any of the following:
456 If a key exists, its value is always `true`. The keys that exist indicate the
457 [special keys] for the sequence used for the matched command (if any).
459 - keyStr: `String`. The current keypress represented as a string.
461 - unmodifiedKey: `String`. `keyStr` without modifiers.
463 - toplevel: `Boolean`. Whether or not the match was a toplevel match in the
464 shortcut key tree. This is `true` unless the match is part of the tail of a
469 There is one `vim` object per tab.
471 A `vim` object has the following properties:
473 - window: [`Window`]. The current Firefox window object. Most commands
474 interacting with Firefox’s UI use this.
476 - browser: [`Browser`]. The `browser` that this vim object handles.
478 - options: `Object`. Provides access to all of VimFx’s options. It is an
481 - mode: `String`. The current mode name.
483 - enterMode(modeName, ...args): `Function`. Enter mode `modeName`, passing
484 `...args` to the mode. It is up to every mode to do whatever it wants to with
487 - isFrameEvent(event): `Function`. Returns `true` if `event` occurred in web
488 page content, and `false` otherwise (if it occurred in Firefox’s UI).
490 - notify(title, options = {}): `Function`. Display a notification with the title
491 `title` (a `String`). If you need more text than a title, use `options.body`.
492 See [`Notification`] for more information.
494 - markPageInteraction(): `Function`. Marks that the user has interacted with the
495 page. After that [autofocus prevention] is not done anymore. Commands
496 interacting with web page content might want to do this.
498 **Warning:** There are also properties starting with an underscore on `vim`
499 objects. They are private, and not supposed to be used outside of VimFx’s own
500 source code. They may change at any time.
504 An `options` object provides access to all of VimFx’s options. It is an object
505 whose keys are VimFx pref names.
507 Note that the values are not just simply `vimfx.get(pref)` for the `pref` in
508 question; they are _parsed_ (`parse(vimfx.get(pref))`):
510 - Space-separated prefs are parsed into arrays of strings.
512 - `black_list` and `{prev,next}_patterns` are parsed into arrays of regular
515 (See [parse-prefs.coffee] for all details.)
517 Any [option overrides] are automatically taken into account when getting an
520 The [special options] are also available on this object.
525 A location object is very similar to [`window.location`] in web pages.
526 Technically, it is a [`URL`] instance. You can experient with the current
527 location object by opening the [web console] and entering `location`.
532 The public API is currently **experimental** and therefore **unstable.** Things
533 might break with new VimFx versions.
535 As soon as VimFx 1.0.0 is released backwards compatibility will be a priority
536 and won’t be broken until VimFx 2.0.0.
538 [option overrides]: #vimfxaddOptionOverridesrules
539 [categories]: #vimfxgetcategories
540 [`vimfx.modes`]: #vimfxmodes
542 [mode object]: #mode-object
543 [category object]: #category-object
544 [command object]: #command-object
545 [match object]: #match-object
546 [vim object]: #vim-object
547 [options object]: #options-object
548 [location object]: #location-object
550 [blacklisted]: options.md#blacklist
551 [special options]: options.md#special-options
552 [config file]: config-file.md
553 [bootstrap.js]: config-file.md#bootstrapjs
554 [autofocus prevention]: options.md#prevent-autofocus
555 [`activatable_element_keys`]: options.md#activatable_element_keys
556 [`adjustable_element_keys`]: options.md#adjustable_element_keys
558 [special keys]: shortcuts.md#special-keys
560 [defaults.coffee]: ../extension/lib/defaults.coffee
561 [parse-prefs.coffee]: ../extension/lib/parse-prefs.coffee
562 [modes.coffee]: ../extension/lib/modes.coffee
563 [commands.coffee]: ../extension/lib/commands.coffee
564 [vim.coffee]: ../extension/lib/vim.coffee
566 [`Window`]: https://developer.mozilla.org/en-US/docs/Web/API/Window
567 [`Browser`]: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/browser
568 [`Notification`]: https://developer.mozilla.org/en-US/docs/Web/API/Notification
569 [`window.location`]: https://developer.mozilla.org/en-US/docs/Web/API/Location
570 [`URL`]: https://developer.mozilla.org/en-US/docs/Web/API/URL
571 [TabSelect]: https://developer.mozilla.org/en-US/docs/Web/Events/TabSelect
572 [web console]: https://developer.mozilla.org/en-US/docs/Tools/Web_Console
573 [about:config]: http://kb.mozillazine.org/About:config