Advertisement
jonahbron

HTML Tidy Macro for KomodoEdit

Aug 18th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. HTML Tidy Macro for Komodo Edit
  3. Pipes tidy output through sed to convert space indentation to tabs (tidy doesn't support tab indentation).
  4. Requires that tidy and sed be installed.
  5. BSD License
  6. */
  7.  
  8. komodo.assertMacroVersion(3);
  9. if (komodo.view) { komodo.view.setFocus(); }
  10.  
  11. var formatter;
  12. var language = komodo.document.language;
  13. switch (language) {
  14.     case 'Perl':
  15.         formatter = 'perltidy -i=2 -pt=2 -l=0';
  16.         break;
  17.     case 'XML':
  18.     case 'XUL':
  19.     case 'XLST':
  20.         formatter = 'tidy -q -xml -i --indent-spaces 4 -w 80';
  21.         break;
  22.     case 'HTML':
  23.         formatter = 'tidy -q -asxhtml -i --indent-spaces 4 -w 120';
  24.         break;
  25.     default:
  26.         alert("I don't know how to tidy " + language);
  27.         return null;
  28. }
  29.  
  30. formatter += ' | sed \'s/    /\t/g\'';
  31.  
  32. //save current cursor position
  33. var currentPos = komodo.editor.currentPos;
  34.  
  35. try {
  36.     // Save the file.  After the operation you can check what changes where made by
  37.     // File -> Show Unsaved Changes
  38.     komodo.doCommand('cmd_save');
  39.    
  40.     // Group operations into a single undo
  41.     komodo.editor.beginUndoAction();
  42.    
  43.     // Select entire buffer & pipe it into formatter.
  44.     komodo.doCommand('cmd_selectAll');
  45.     Run_RunEncodedCommand(window, formatter + " {'insertOutput': True, 'operateOnSelection': True}");
  46.    
  47.     // Restore cursor.  It will be close to the where it started depending on how the text was modified.
  48.     komodo.editor.gotoPos(currentPos);
  49.    
  50.     // On windows, when the output of a command is inserted into an edit buffer it has unix line ends.
  51.     komodo.doCommand('cmd_cleanLineEndings');
  52. } catch (e) {
  53.     alert(e);
  54. } finally {
  55.     // Must end undo action or may corrupt edit buffer
  56.     komodo.editor.endUndoAction();
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement