Labs/Bespin/DesignDocs/ConfigIntegration

From MozillaWiki
< Labs‎ | Bespin‎ | DesignDocs
Jump to: navigation, search

How can you integrate your own configuration with the Bespin editor?

When you login to Bespin you should see a project named "BespinSettings". This is your special project that contains configuration, custom commands, a scratchpad, and anything else you want to put in there. It is like a .bespin home directory.

You can use the "config.js" file to put any JavaScript to tweak your Bespin experience. NOTE: Danger, danger. You can do bad things here!

The moving pieces:

editconfig

If you type 'editconfig' from the command line, it will open up the config.js file for you, ready for editing. Save this puppy.

runconfig

Takes the contents of the saved config.js and eval()'s it. The eval has a scope that is setup that gives you access to the following objects:

  • Bespin: The global config object
  • commandLine: Access to the command line. Here you can run things like:
    • commandLine.showInfo("pop me up")
    • commandLine.executeCommand("version")
  • editor: The core editor object. Here you can play with the "actions" to tweak the experience
  • editSession: Info on the session (project name, etc). Should be rare to use this.
  • files: Access to the FileSystem to open/save/remove/... files
  • server: Access to the Server API to talk back
  • toolbar: Access to the editor toolbar (the buttons etc)

set autoconfig on

If you run 'set autoconfig on' then when the editor is loaded, it will automatically run 'runconfig' for you.

By default this is set to be false, and thus the config.js is NOT loaded until you actively run a 'runconfig' yourself.

Hooking into events

You may want to hook in your own code to events, such as when a file is loaded. Simply start observing them yourself!

E.g.

 document.observe("editor:openfile:opensuccess", function(event) {
   var file = event.memo.file;
 
   commandLine.showInfo('Loaded file: ' + file.name, true);
 });

How do I load custom commands from my config.js?

Simply publish a command load event:

 // -- Load up custom commands
 publish("command:load", { commandname: "calculate" });

Can I load up other files to keep processing?

Absolutely, you can use the include() function to read in files to do more work. For example, if you want to break out your aliases into another file you would do something like this:

 // -- Load in my aliases
 include("aliases.js");

How do I set aliases in my config?

Now you have an aliases.js, or you just want to use your config.js, you can add aliases using the execute() shortcut function:

  // place to put aliases 
  execute("alias gimmebespin 'import bespin http://hg.mozilla.org/labs/bespin/archive/tip.zip'");

How can I load Bespin subsystems that are not loaded by default

Some pieces in Bespin are not autoloaded. To load up this functionality, you simply require it via the require() shortcut function:

 // -- Load up sub systems of Bespin that aren't loaded by default
 require("bespin.syntax.arduino");