Skip to content

Instantly share code, notes, and snippets.

@jonahbron
Created August 18, 2011 18:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonahbron/1154711 to your computer and use it in GitHub Desktop.
Save jonahbron/1154711 to your computer and use it in GitHub Desktop.
An HTMLTidy Macro for Komodo Edit
/*
HTML Tidy Macro for Komodo Edit
Pipes tidy output through sed to convert space indentation to tabs (tidy doesn't support tab indentation).
Requires that tidy and sed be installed.
BSD License
*/
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus(); }
var formatter;
var language = komodo.document.language;
switch (language) {
case 'Perl':
formatter = 'perltidy -i=2 -pt=2 -l=0';
break;
case 'XML':
case 'XUL':
case 'XLST':
formatter = 'tidy -q -xml -i --indent-spaces 4 -w 80';
break;
case 'HTML':
formatter = 'tidy -q -asxhtml -i --indent-spaces 4 -w 120';
break;
default:
alert("I don't know how to tidy " + language);
return null;
}
formatter += ' | sed \'s/ /\t/g\'';
//save current cursor position
var currentPos = komodo.editor.currentPos;
try {
// Save the file. After the operation you can check what changes where made by
// File -> Show Unsaved Changes
komodo.doCommand('cmd_save');
// Group operations into a single undo
komodo.editor.beginUndoAction();
// Select entire buffer & pipe it into formatter.
komodo.doCommand('cmd_selectAll');
Run_RunEncodedCommand(window, formatter + " {'insertOutput': True, 'operateOnSelection': True}");
// Restore cursor. It will be close to the where it started depending on how the text was modified.
komodo.editor.gotoPos(currentPos);
// On windows, when the output of a command is inserted into an edit buffer it has unix line ends.
komodo.doCommand('cmd_cleanLineEndings');
} catch (e) {
alert(e);
} finally {
// Must end undo action or may corrupt edit buffer
komodo.editor.endUndoAction();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment