Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating text in XCode, moving one line

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?

like image 323
Jeffrey Harris Avatar asked Sep 04 '09 16:09

Jeffrey Harris


2 Answers

Xcode 4 has a new set of command for moving the line where the cursor is or the selected text with command + option + [ or ]

⌥⌘[ or ⌥⌘]

http://developer.apple.com/library/mac/#documentation/IDEs/Conceptual/xcode_help-command_shortcuts/MenuCommands/MenuCommands014.html

like image 131
cescofry Avatar answered Sep 27 '22 22:09

cescofry


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!

like image 31
adurdin Avatar answered Sep 27 '22 21:09

adurdin