Formatting Your C++ Code the Easy Way

Everyone has his own coding style and dislikes everyone else’s. When you have to work with code written by other people, there’s a ton of it, and their coding style differs from yours in many ways, you have to do something.

As for Psi project, we in fact have some set of code style guidelines. But still sometimes we occasionally are looking at something that’s noticeably differs from our sense of perfection.

That’s where code beautifiers come into play.

Enter Artistic Style. Small, neat and cross-platform C, C++, C# and Java code beautifier.

Installing Artistic Style

UPDATE: As of astyle-1.20 release, building it on Mac OS X is simple with provided build/Makefile.

As there is Windows-binary available on the site and a Makefile is present in the source package, it doesn’t cleanly build itself on Mac OS X, because it doesn’t know about some of osx-specific compilation flags, so it just crashes on start. And here’s that flag, if you’re curious: -headerpad_max_install_names
You could either modify your Makefile with this knowledge, or could use a tool to produce the Makefile automatically:qmake -project && qmake. After that, it’s just a singlemakeaway from getting your hands on the binary. I have packaged both resultingMakefileandastyle.proin one package.

Using Artistic Style

Pipe a file to the executable, or specify file name as the argument: result is the same — code is reformatted, and is either written to the stdout, or to the file. And it also dumps an advertisement to the stderr, so don't forget to 2> /dev/null.

The following command line gives the result most closely resembling what we describe in the Psi Code Style Guidelines:

astyle --mode=c --indent=tab=4 --brackets=linux --brackets=break-closing --min-conditional-indent=0 --pad=oper --unpad=paren && echo ""

TextMate Command

And that’s not all! To reap all benefits without having to resort to fiddling with command line, I’ve prepared a super-special limited edition TextMate command. Grab it while it’s hot! Limited supply offer!

Qt Bundle for TextMate: Prerelease

UPDATE: I’ve put some stuff from my bundle to the official TextMate Bundle Repository. Now you could skip all this stuff and just use the GetBundle Bundle.

Long time, no posts, and truth to be said, I’ve been secretly working on Qt Bundle for TextMate all along, and now you can have your hands on it. Today I will discuss how to install it on your Mac. Description of what it actually does will follow, so explore it on your own and make suggestions.

Getting individual bundles together

As specified in official TextMate manual you can pull individual bundles from subversion repositories thought the world. In order to install Qt bundle, execute following commands:

mkdir -p /Library/Application\ Support/TextMate/Bundles cd /Library/Application\ Support/TextMate/Bundles svn co svn://dev.psi-im.org/mblsha/textmate/Qt.tmbundle

This way you’ll get only the bundles you want.

To update the bundle at a later time, create a simple shell script with these commands:

cd /Library/Application\ Support/TextMate/Bundles svn up *.tmbundle osascript -e 'tell application "TextMate" to reload bundles'

The more the merrier

If you absolutely want to have all the latest and greatest, you may opt to get all the bundles from TextMate’s repository, but be careful: there are so much, you’ll have to disable most of them by hand. This is simple.

mkdir -p /Library/Application\ Support/TextMate/ cd /Library/Application\ Support/TextMate/ svn --username anon --password anon co \ http://macromates.com/svn/Bundles/trunk/Bundles

Then you’ll have to add svn:extrenals property to Bundles directory in order to automatically update Qt bundle:

svn propset svn:externals 'Qt.tmbundle svn://dev.psi-im.org/mblsha/textmate/Qt.tmbundle' Bundles

But please be wary that if you already have some svn:externals set, you should use svn propedit command instead.

After this setup getting Qt bundle is just a simple matter of svn up Bundles.

Finishing touches

After initial check out you should perform a couple of small steps to enable quick documentation lookup:

  1. Start Qt Assistant.
  2. Open Index on the Sidebar and wait until the index is created.
  3. Quit Qt Assistant.
  4. Now the index is created and saved to disk.
  5. Open Terminal and execute following commands: cd /Library/Application\ Support/TextMate/Bundles/Qt.tmbundle/Support/lib/assistant_search qmake && make
  6. Now Documentation for Word command will actually work.

Time for Markdown

Phew! I just finished converting all my posts from HTML to Markdown format to get a grasp of how the things should be done and it was really inspiring.

What is Markdown? It’s yet another markup language for the web which has very nice syntax. With its help now I finally can read my blog’s posts’ text paragraphs with tons of links. Also, it’s possible to use a full power of HTML markup in Markdown — that’s very useful too. Now I like it even more than Textile.

Also, there’s SmartyPants — tool to enhance typography of web-published documents. Give it a try.

UPDATE: Just noticed a minor nuisance: if you’re using reference-style links, link labels for all links used prior to WordPress’ tag should also be placed prior that tag. By now I should’ve corrected all errors. BTW, because Markdown allows for seamless inclusion of HTML you don’t have to convert all old posts to Markdown format as I did — everything would just work.

If you’re interested how it looks like, here’s a screenshot of this post in TextMate. If you want a more complex example, look no further than Markdown’s Syntax in Markdown page by John Gruber himself.

If you’re using WordPress, there are plugins for both Markdown and SmartyPants: PHP Markdown and PHP SmartyPants. They’re easy and straightforward to install. There are also plugins for other blogging tools.

![This post in TextMate](http://mblsha.psi-im.org/images/mac/textmate/markdown_smartypants.png)

PS: I’m also interested in alternate markup solutions out there. How do you publish on the web?