Setting up Emacs as a modern Objective-C editor

Your .emacs.d/ only requires a few software packages and custom Lisp functions to function as a modern Objective-C editor. These instructions are primarily aimed at people developing on GNUstep but could probably be adapted easily for an Apple macOS environment.

First and foremost, emacs-ccls provides IDE features such as code completion, code outlining, and integration into your text completion framework of choice such as company, ivy, or helm.

This technology is powered by the Language Server Protocol which provides these IDE features to text editors through a separate server process.

The setup is quick and easy:

(require 'lsp-mode)

(require 'ccls)

(setq ccls-executable "/var/lib/snapd/snap/bin/ccls")

(add-hook 'objc-mode-hook #'lsp)

Add a .ccls file to the root directory of your Objective-C project with your compiler settings such as the locations of header files. For clang on NEXTSPACE this looks like:

clang

-I/Developer/Headers

-I/usr/NextSpace/include

-fobjc-nonfragile-abi

-F/usr/NextSpace/Frameworks

Now the next time you open a file within that project, ccls will be activated.

To get started, I find the three most helpful functions are finding references, finding definitions, and finding documentation, so I bind those in Objective-C mode to three easy keystrokes: C-x C-d(ocumentation) , C-x C-r(eferences) , and C-x C-w(here is function defined) .

Unfortunately, these packages do not provide rich documentation for Cocoa, so I had to write a function called cocoa-open-documentation to open Apple’s documentation in a web browser when over a specific symbol.

One keybinding that is helpful and already exists is C-x c(ompile) for compiling and running a project. If you’re on macOS, you’ll have to write a shell script to do this for you, but for GNUstep the compile command just looks like make && openapp ./App.app .

With all that setup, you now have a pleasantly modern environment for writing Objective-C in.

Note: I am noticing Emacs.app on GNUstep is having trouble opening a web browser. I’ll hopefully submit a patch shortly.