Friday, September 21, 2012

How to format/highlight code/commands in mails

I hate to see mails which are not formatted, do not use monospaced font for code/command/logs. What is the point of writing a mail, without proper formatting, spell check, emphasis, punctuation!? I will leave the spell-check and punctuation for now. As a programmer, let me rant on the fonts and colors! ;-)

Monospace it!

How difficult is it to change the font of code snippets and commands, or logs to monospaced font!? I used to think, maybe its quite a bit of work, but... NO! Its really simple, I started using a simple macro with Outlook and Word, to change selected text to monospace (and I like Courier New) its really easy, I wont go into the details, there are lots of blogs and MS articles explaining how. Here is the code in VB:

Sub Change2MonoSpace()
' Macro - For MS WOrd
    Selection.Font.Name = "Courier New"
    Selection.Font.Size = 10
End Sub

Sub OLChange2Mono()
' Macro - For MS Outlook
    Dim objOL As Application
    Dim objDoc As Object
    Dim objSel As Object
    Set objOL = Application
    Set objDoc = objOL.ActiveInspector.WordEditor
    Set objSel = objDoc.Windows(1).Selection
    objSel.Font.Name = "Courier New"
    objSel.Font.Size = 10
End Sub


vim: TOhtml of the C classic.
Colorize

For code snippets, just making it monospaced, isn't sufficient IMO, its better to syntax highlight it, like your editor does. Again, this isn't difficult, I can think of more than one way to do it, which one is easy/simple - is a matter of personal taste.

1 - with PuTTY
    PuTTY has an option in Windows->Selection, "Paste to clipboard in RTF and plain text", enabling this, the selection, when pasted in MS Word or Outlook retains the colors.
 
2 - Vim
    Vim does too many things, and one of which is, the ex-mode command, :TOhtml, with which, a window appears, which has the HTML format of the current file/buffer - this can be saved, and later opened in IE to copy (the syntax highlight and background is as seen in the editor, the color scheme can be changed before conversion to suit needs)

3 - A special editor
    ScITE - Scintilla text editor has a copy option, to copy in RTF (but the highlight is only for supported languages, adding a new one is not simple, requires change in the code not like vim - i.e, users cant write a config/syntax-file)

4 - A utility
    Andre Simon's page has 2 awesome utilities:
    Highlight - syntax highlight source files
    Ansifilter - to colorize the console dumps which contain ANSI escape sequences

In these days where plain text mails are seldom used, the rich-text/HTML mails that we send/receive, can, and should be made to look much more beautiful!

No comments: