Setup Emacs for XCode+iOS development
The current document has been written after being tested on Emacs 24 with XCode 4.4 and iOS SDK 5.1 with Mac OS X Mountain Lion. All github repositories have been forked for modifications and also so that this document remains valid even after they have changed, but the credits go to the respective authors.
For building from command line you have to use xcodebuild. You can get info about that and other tools from the Apple developer documentation. So it has not been discussed here. Also for any command line implementations run the Emacs shell by pressing Ctrl-x m or run a shell using M-x shell.
Emacs as a development tool is excellent. The abstract and global approach of Emacs towards programming languages, has helped me to finally setup up Objective-C for iOS inside it, in such a way, that I am free to ditch XCode. Except perhaps some mundane tasks like file additions, etc. which Apple is so clumsy to provide to developers from the command line.
Moving on, Emacs already provides an objc-mode. But its very limited in its functionality. You will need much more than just objc-mode to get near the XCode functionalities. I tried implementing some of them and here’s the things which I have suceeded to implement so far:
YASnippet is a template system for Emacs. It allows you to type an abbreviation and automatically expand it into function templates. Now for iOS/Obj-C we have functions like
Memorizing such functions is a pain, and almost never really works out. With YASnippet you can just start typing and a choice of possible function templates are presented to you.
To do this you need to have yasobjc.rb. Get this from https://github.com/roupam/yasobjc
Then run the yasobjc.rb script as:
A really long command, let me abbreviate that:
Here’s a sample yasnippet config for the Emacs init file Change the parameters according to your emacs installation of YASnippets.
1 2 3 4 5 6 7 8 9
Auto-complete is a very very useful functionality in Emacs and there’s a ton of discussion of it on the web, so I won’t discuss how to set it up right here. I’ll just post my config here. Note the yasnippet mentioned in the auto-complete config.
1 2 3 4 5 6 7 8 9 10 11 12 13
Here’s a sample screenshot showing webView methods using YASnippet and auto-complete
Pragma marks are XCode’s way of browsing through large Objective-C source code files, showing the functionality of a set of methods.
For example check this screenshot:
It shows a set of methods associated with TextView, another set with TableView and so on, and the methods themselves. This is very convenient, if you are trying to read and understand the code.
Here’s my Emacs config for setting up pragma-marks:
1 2 3 4 5 6 7 8 9 10 11 12 13
And I have bound the function to the key Ctrl-x p
So whenever I am editing a source code file and I press Ctrl-x p here’s what I see in a separate buffer:
Selecting a method and pressing enter takes you to that particular method.
ETAGS or Emacs Tags is a facility for recording names and their definitions and later looking up the definitions. Here’s how to record the names and definitions of any project that your are using. You should have a binary called etags in your Mac OS X installation. Go to the root of your project directory and do.
This will generate a TAGS file in that directory. Visit that file using M-x visit-tags-table and it’s done.
Now you can browse through class definitions and your source code functions. Just go to a class name in your source code like say NSUserDefaults, and press M-. and you will be presented with the complete class definition, methods, etc. that can be found in the Framework headers.
Press M-* to go back to where you were before.
Check out more about Emacs TAGS here http://emacswiki.org/emacs/EmacsTags
If you have w3m installed for use from within your Emacs, then you can view and search XCode docsets. There’s nifty little tool that you can get from here https://github.com/roupam/emacs-xcode-document-viewer
1 2 3
You will also need to modify the xcode-document-viewer.el and point to the right docsetutil command
1 2 3 4
To search you can just do M-x xcdoc:search. For example a search on NSString leads to this.
Get fruitstrap from here https://github.com/roupam/fruitstrap
Clone this git://github.com/roupam/fruitstrap.git Get into the cloned directory and do
Get the Mac OS X 10.8 version with XCode 4.4 support from here https://github.com/phonegap/ios-sim After building just make the executable aviablable in your path
Open an Emacs shell. Press Ctrl-x m To run on device go to your project root and do
There’s a tool called xcs, but somewhat limited in its functionality, but still gets the job done. But still I think its an awesome piece of code, considering the fact that Apple is clumsy when it comes to developer support outside of Xcode.
You can get xcs from here https://github.com/gonzoua/xcs
Add this to you emacs init file if you would like to jump quickly between .h and .m files. Use Ctrl-C t
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Thanks to Kiran Kulkarni for this switching code.
Hope you enjoyed this post, and believe me Emacs is more than you could expect, much much more.
Please feel free to email me at roupam dot ghosh at gmail dot com.
That’s all folks.