Integrating Emacs and XCode

I tend to use Emacs for all my editing, although when I am writing Mac OS/X GUIs, I couple it with XCode for managing the project build process. The main things I've needed to customize in my ".emacs" file is to get Emacs to automatically recognise Objective-C files when they are opened (including Objective-C ".h" files), and have the "compile" command in Emacs call xcodebuild to actually compile the program.

To have Emacs recognise Objective-C files, first add the following to your ".emacs" so that ".m" and ".mm" files get handled automatically:

  1.  
  2. (setq auto-mode-alist
  3. (cons '("\\.m$" . objc-mode) auto-mode-alist))
  4. (setq auto-mode-alist
  5. (cons '("\\.mm$" . objc-mode) auto-mode-alist))
  6.  

Next add this code so that header files get automatically handled.

Finally you can compile using xcodebuild with the following code:

  1.  
  2. (defun bh-compile ()
  3. (interactive)
  4. (let ((df (directory-files "."))
  5. (has-proj-file nil)
  6. )
  7. (while (and df (not has-proj-file))
  8. (let ((fn (car df)))
  9. (if (> (length fn) 10)
  10. (if (string-equal (substring fn -10) ".xcodeproj")
  11. (setq has-proj-file t)
  12. )
  13. )
  14. )
  15. (setq df (cdr df))
  16. )
  17. (if has-proj-file
  18. (compile "xcodebuild -configuration Debug")
  19. (compile "make")
  20. )
  21. )
  22. )
  23.  

4 thoughts on “Integrating Emacs and XCode

  1. Rakko

    This comment is about your site (or at least the WordPress layout), not this post; I couldn’t find any other way to contact you.

    What I wanted to let you know is that when you click the menus along the top, the tabs (e.g. All/Uncategorized/Fun/etc.) overlay it, making it impossible to see or click on everything in the menu.

    Reply
  2. Ward WIllats

    Great. I changed my alists a long time ago, though I still have to thump the mode for .h files in mixed C++/objc-proj manually. However I just discovered xcodebuild and your elisp gives me some good ideas. Thanks for this.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>