views:

599

answers:

3

In emacs I have various functions to manipulate text. Now that I'm using xcode, I suppose I could make emacs my default editor, but I want to browse obj-c objects and such, so I'd rather just implement my most used text manipulation commands for xcode.

First on my list, I'd like a command that moves the text of the current line up/down one line, keeping the cursor on the current line.

In emacs this is:

(defun move-one-line-downward ()
  "Move current line downward once."
  (interactive)
  (forward-line)
  (transpose-lines 1)
  (forward-line -1))

I'd be happiest if I could write a script in Python that would do the equivalent in XCode, but as far as I can tell, I need to talk to AppleScript to do this.

Can someone walk me through how to do this with XCode?

A: 

I think your question is more generic that Xcode/emacs. Most IDE can't do stuff that editors can do and vice versa.

What I do is to use Xcode for 'simple' stuff and compile/debug. When I want to do lot's of coding (hm ... text editing) I open multiple files in one Vim (no offense to emacs ... vim is my editor of choice) and use all the vim tricks available.

I follow the same process when I need to do lots of coding on Windows (Visual Studio). I have VS opened just for compiling/debugging but I do most coding in separate Vim window.

I don't need to do any of that on Unix because I just open file in Vim and run Makefile from within Vim, jumping directly to errors and so on (:mak) ...

I'm sure that there are some extremely clever things to get Xcode do stuff that are already in Vim and Vim to do stuff that Xcode can do but I'm focusing on coding so not much time to play around.

stefanB
Well, that's the PREFERRED way of working, but that wasn't the question.
guns
+2  A: 

What you’re wanting to do can be achieved through Xcode’s “User Scripts”—and it helpfully comes with a pair of scripts that almost do what you want. (I’m using Xcode 3.2 here, but I think these were also there in 3.1)

Look in /Developer/Library/Xcode/User Scripts/; there are two applescripts there, Move Line Up.scpt and Move Line Down.scpt. Here’s what’s in Move Line Up:

(*
To edit this script, choose Save As... and save it in your home directory, then re-add it to the User Scripts list.
*)
using terms from application "Xcode"
    tell first text document
        set {startLine, endLine} to selected paragraph range

        if startLine > 1 then
            set theText to (paragraphs startLine through endLine)
            set theText to (theText as string)
            delete (paragraphs startLine through endLine)
            make new paragraph at beginning of paragraph (startLine - 1) with data theText
            set selected paragraph range to {startLine - 1, endLine - 1}
        else
            beep 1
        end if
    end tell
end using terms from

These almost do what you want, except they select the whole line afterwards; I’m no applescript expert, but you probably want to store the selected character range. Have a look at the Xcode scripting dictionary (in AppleScript Editor, File -> Open Dictionary -> Xcode) to find out the types of objects you can manipulate.

You can add your own scripts to Xcode with the “Edit User Scripts”menu item in the script menu; and assign shortcut keys to the scripts in that window also, by double-clicking in the right-hand column beside the entry for the script menu item.

You can also use shell scripts (perl, python, bash, whatever) in the User Scripts menu, but these process only the selection or the whole file, so might be a bit heavyweight for moving a single line up or down.

See all the docuemntation on User Scripts here: http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/XcodeWorkspace/310-User_Scrips/user_scripts.html

I hope this helps!

adurdin
+1 nice ............
stefanB
A: 

Also, you can hook up emacs (or vi, or bbedit, or ed, I imagine) to xcode, so that they will talk to each other. When you tell xcode to open a file it will call emacsclient, etc. You can even get emacs to tell xcode where to put a breakpoint (I don't remember how, but I asked a question here a while back, and it works great)

Brian Postow