views:

567

answers:

2

I'm using Emacs to edit my Objective-C code. The default indentation looks like this:

    NSTimer *timer =
        [NSTimer timerWithTimeInterval:1.0
                 target:self
                 selector:@selector(callback:)
                 userInfo:nil 
                 repeats:YES];

I would like Emacs to indent the code like XCode, that is, to align with the colons:

    NSTimer *timer =
        [NSTimer timerWithTimeInterval:1.0
                                target:self
                              selector:@selector(callback:)
                              userInfo:nil 
                               repeats:YES];

Is there any hope to achieve this?

+6  A: 

I've been hacking on this for a bit and have gotten closer, but it's not fully functional yet. The solution I have is to add an entry to align-rules-list, so that a simple M-x align will do the trick. The problem is that you have to run align twice in order to get it to work.

(obj-c-colons
 (regexp . "^\\(\\s-*[^:]+\\):")
 (justify . t)
 (repeat . t)
 (modes obj-c-mode)) ;; <= Replace with actual name of major mode

I don't actually know what the name of the objective-c mode is, so you will have to replace obj-c-mode with whatever the mode is actually called. You can add this to align-rules-list with:

(add-to-list 'align-rules-list
             '(obj-c-colons
               (regexp . "^\\(\\s-*[^:]+\\):")
               (justify . t)
               (repeat . t)
               (modes obj-c-mode)))

For now, you have to execute align twice; the first time will only line it up as follows:

 NSTimer *timer =
        [NSTimer timerWithTimeInterval:1.0
                              target:self
                            selector:@selector(callback:)
                            userInfo:nil
                             repeats:YES];

Notice that the colons are two characters too far to the left. The second align will correct this. Don't ask me why.

There is a lot that goes on with the align command, so figuring it out is hard. Hopefully someone comes up with a way to improve upon this. Anyway, take a look at the relevant EmacsWiki page for more info on aligning.

haxney
Nicely done. I think my solutions failed because I missed the anchoring ^ at the beginning. Regarding running twice, if you just add the rule 2 times (with different names), it gets run twice automatically. Not perfect, but it avoids having to run `M-x align` two times.
Trey Jackson
+3  A: 

Is there something wrong with c-lineup-ObjC-method-call-colons for objc-method-call-cont in c-offsets-alist?

Ivan Andrus
I can't find any documentation regarding c-lineup-ObjC-method-call-colons, any hints?
Martin Cote
Try C-h v c-offsets-alist RET and C-h f c-lineup-ObjC-method-call-colons RETEasiest is M-x customize-option RET c-offsets-alist RET and then edit objc-method-call-cont and change it to (c-lineup-ObjC-method-call-colons c-lineup-ObjC-method-call +) (include the parentheses), set it for the current session and try it out. When you are done you can Save for future sessions. You can also create entire programming styles that define all sorts of things. That however is beyond the scope of this comment :) http://www.gnu.org/software/emacs/manual/html_node/ccmode/Sample-_002eemacs-File.html
Ivan Andrus
Gaa! I clearly don't know how to format for stack overflow.
Ivan Andrus
Hmmm, none of that seems to work on my side. I'm probably using a too old version of emacs (22.3.1).
Martin Cote
Ivan Andrus