Copy code and preserve formatting

I often share parts of my code with colleagues via email. Is there a way to preserve text formatting when I paste code into an email?

This sounds more like a question for Stack Overflow or your email clients forums. The text in your clipboard that is passed to your email client should have all the formatting information from Komodo. Your email client may not be interpreting properly. Does it work from other programs?

There is an alternative though. We have a built in tool for this Highlight the snippet of code, right click > Share Code via Then just share the link with them.

Also, if you’re both using Komodo IDE you can share entire files through the Collaboration tool.

  • Carey

Thank you for your advice Carey, but the only option I have when pasting into Word or Outlook is to paste as unformatted text. Unfortunately my colleagues are not using Komodo IDE, so the Collaboration tool isn’t helpful to me in this particular situation.

Even when selecting “Paste Special” in MS Office, I only get the following options:
* Unformatted Text
* Unformatted Unicode Text

My current process is to paste the unformatted text into an email, then manually format the colors to match the stylers set in Komodo. I want a faster way…

This isn’t possible. You’re talking about syntax highlighting. There is a special process inside Komodo that scans the code and colors it in real time. You would need a pluggin for Outlook that does the same. I would start there.

Or I would use which already does this.

  • Carey

@mmaNate87 I always looked to the old activestate recipes, but there is here a snippet that does what you want.
Copy to clipboard code selection in HTML format

I’m using a adapted version for my self:

var view = ko.views.manager.currentView;

var tmpFileSvc = Components.classes[";1"]
fname = tmpFileSvc.makeTempName(".html");

var lang = view.koDoc.languageObj;
var forceColor = true;
var selectionOnly = view.selection != "";
var schemeService = Components.classes[';1'].getService();

var file = Components.classes[";1"]
file.URI = ko.uriparse.localPathToURI(fname);'rb');
var str = file.readfile();

str = str.replace(/\n|\r/g, "");
var m = str.match(/\s*[a-zA-Z0-9._-]*\s*{.*?}/g);
var styles = {};
for (var i in m) {
    var selector = m[i].match(/\s*([a-zA-Z0-9._-]*)\s*{(.*?)}/);
    if (selector && /^span\./.test(selector)) {
        styles[selector[1].substring("span.".length)] = selector[2].replace(/(^\s*|\s*$)/g, "");

m = str.match(/(<span.*?)<\/body>/);
var result = m[1];

for (i in styles) {
    var style = styles[i];
    if (i === 'linenumbers') {
        result = result.replace(new RegExp('<span class="' + i + '">', "g"), '<span style="display: none;">');
    } else {
        result = result.replace(new RegExp('<span class="' + i + '">', "g"), '<span style="' + style + '">');
result = result.replace(/<span style="display: none;">[^<]+<\/span>/g, '');
result = result.replace(/&nbsp;/g, ' '); // OpenOffice Writer ignores nbsp

result = '<pre style="width:2200px;background:#333; color: #fff; padding-left: 15px;" spellcheck="false"><span style="font-size:12px; width: 2200px;">' + result + '</span></pre>';

transferable = xtk.clipboard.addTextDataFlavor("text/html", result);

Its not perfect, but it does what you want

You select the text you want to copy and run this userscript, when you now paste in a email or word it will look something like as in the image.


WOW, nice find @babobski! I had no idea this exist! Great point to search the Komodo recipes. There is tons in there.

Since there’s “Print” thing, I was sure there’s a way to “render” scintilla. But yeah nice find :slight_smile: