Snippets code from my daily experience

January 25, 2009

XUL developers please add id attribute to elements (Sunday Thought)

Filed under: sunday_thought,xul — dafi @ 1:00 pm

The XUL overlay mechanism is great, it allows to extend virtually any UI widget.

Damn, why developers continue to forget to add id attributes to elements candidate to be re-used (ie all)?

Please dear developers follow these simple rules

  • Please don’t forget windows and dialogs or simply refer to this short list :P
  • Please don’t forget menu and menuitems (not generated dinamically)
  • Add id attribute to every element
  • Add id attribute to every element (#1)
  • Add id attribute to every element (#2)
  • Add id attribute to every element (#n)

iwilluseid

Based on my experience no application is fully compliant: Firefox, Thunderbird, SeaMonkey and also Komodo contain elements I needed to access without an associated id attribute

How to survive to bad practices

Sometimes (but not always) it is possible to use some workaround.
Consider that workarounds can be worst than initial problem

  • Find target element with getAttributesByName method, it is especially useful for menuitems passing the ‘value’ attribute
  • Find an element closer to the target using its id then navigate DOM with methods/attributes like firstChild & co.

January 20, 2009

Posting syntax highlighted code on wordpress.com using Komodo macro

This post is superseded, now it is possible to use native tag also on wordpress.com, more details can be found here

Soon or later any blogger talking about software development will post some snippet of code.

To make code more cool and attractive he/she will use syntax highlight.

There are many alternatives for many blog platforms like Pygments, GeSHI, CodeHighlighter, WP-Syntax and so on.

This blog is hosted on the free version of WordPress.com, it doesn’t allow to use any plugin or user-defined CSS so adding sexy code is very difficult.

Komodo has the ability to print to HTML files code with the syntax highlighted, this feature is fully accessible from APIs so I’ve written a macro that

  • make CSS style inline replacing HTML class with corresponding style attributes
  • copy to clipboard (using the new Komodo 5 clipboard helper library) in HTML format so user can paste directly on Visual WordPress mode

Below you find the macro source code obviously highlighted by itself :P

How to get the selected text (or the whole Komodo view) converted to HTML

The variable str at end contains the converted HTML

var view = ko.views.manager.currentView;

var tmpFileSvc = Components.classes["@activestate.com/koFileService;1"]
                 .getService(Components.interfaces.koIFileService)
fname = tmpFileSvc.makeTempName(".html");

var lang = view.document.languageObj;
var forceColor = true;
var selectionOnly = view.selection != "";
var schemeService = Components.classes['@activestate.com/koScintillaSchemeService;1'].getService();
schemeService.convertToHTMLFile(view.scimoz,
                                view.document.displayPath,
                                view.document.language,
                                lang.styleBits,
                                view.document.encoding.python_encoding_name,
                                fname,
                                selectionOnly,
                                forceColor);

var file = Components.classes["@activestate.com/koFileEx;1"]
        .createInstance(Components.interfaces.koIFileEx)
file.URI = ko.uriparse.localPathToURI(fname);
file.open('rb');
var str = file.readfile();
file.close();

Applying the replace to str

Nothing really interesting, simply the replace method is called many times to make CSS style inline inside span tags

Copy to clipboard in HTML format

Komodo 5 allows to interact with clipboard wrapping XPCOM services, these APIs allow to copy in the so called HTML flavor so content preserves styles and colors.

xtk.include("clipboard");

var transferable = xtk.clipboard.addTextDataFlavor("text/html", str);
transferable = xtk.clipboard.addTextDataFlavor("text/unicode", str, transferable);
xtk.clipboard.copyFromTransferable(transferable);

That’s all folks

After running this macro on a Komodo view you can paste directly on WordPress Visual editor and obtain your beautiful code.

Using HTML flavor you can paste into any application able to accept Special Formats like Microsoft Word or OpenOffice Writer.

The full macro source code can be found on Macro SVN repository

January 17, 2009

Rumbling about VisualDiffer

Filed under: komodo,xul — dafi @ 12:34 pm

After many months I’ve started again to work on VisualDiffer the most complex extension I ever realized.

Developing VisualDiffer is an exciting task at least for two reasons

  • Deeper level XUL usage to create complex user interfaces
  • Attention to code performances to make application more reactive

At this time the UI doesn’t present any special widget but I’ve some idea about line position sliders and comparison file results, they will be very fun to implement.

The implementation has a bottleneck in the Javascript sort function.

While VisualDiffer only runs on Komodo I’ve tried to run the directories sort routines on Firefox with TraceMonkey enabled and I noticed some speedup, nothing so amazing but with deep file trees  (~600 directories and ~2000 files) TraceMonkey completes in 10-20% less time (very good!!).

Due to the fact Komodo is based on Python, it should be easier to write core sort routines directly in Python, I will do it very soon.

VisualDiffer features list (in my mind)  is very long but the already implemented is a very short subset.

I think it is mandatory to adopt the Release Early, Release Often strategy otherwise I’ll release the next version in 2030.

A couple of new functionalities are complete

  • 2369618 quick comparing two open files selecting them from View Tab context menu (released)
  • 2515481 set two different base folders from Directory Panel (not released)

At this time I’ve abandoned the idea to add the three-file compare view I want to add many little time saver features like

  • copy/move folders from Folder sides
  • rename folders/files
  • reload changed files
  • syntax highlight (this should be easy within Komodo)
  • in place edit (no need to switch between Komodo and VisualDiffer)

January 15, 2009

7 Things You May (Or May Not) Know About Me

Filed under: meme — dafi @ 5:43 pm

Well, I’m proud to be invited by Paul

So, the rules:

  1. Link to your original tagger(s) and list these rules in your post.
  2. Share seven facts about yourself in the post.
  3. Tag seven people at the end of your post by leaving their names and the links to their blogs.
  4. Let them know they’ve been tagged.

The seven facts

  1. I don’t drive cars
  2. I continue to use my HP11C calculator (sometimes only to be sure it continues to turn on)
  3. The dark side of me loves Apple Computers and their closed source philosophy
  4. I’ve written 163 (the last yesterday) extensions for automating my repetitive daily jobs but I’m not sure this is a success
  5. I’m falling in love with Komodo editor
  6. I’m falling in love with Charlize Theron (or any “compatible” blonde girl)
  7. I don’t eat fish

The seven people

  1. Massimiliano Mirra because He is a great XUL developer
  2. Jeff Griffiths because He is very patience with me when I ask strange things about Komodo
  3. Mark Finkle because He is always ready to help me on #irc.mozilla.org
  4. Giorgio Maone because We live in same town but we never saw
  5. Antonio Cangiano because He is a very smart guy and a gentleman
  6. Pascal Chevrel because He realized my dream inviting me to FOSDEM 2009
  7. Goofy because He is a dear friend and a great French translator for XUL extensions

The Rubric Theme. Get a free blog at WordPress.com

Follow

Get every new post delivered to your Inbox.