[Tex/LaTex] TeXShop: Force spaces instead of tabs

sourcecodetexshop

Is there a way to make TeXShop insert an arbitrary number of spaces when I press the tab key instead of inserting a tab? This functionality is in TeXnicle and I really like it, but TeXShop is my favorite editor.

A related question is how to easily indent multiple lines of code with spaces (not tabs) in TeXShop. When I think about it, I don't even know how to indent multiple lines of code with tabs!

The setting in TeXnicle is shown in the following picture:

TeXnicle Functionality: Replace Tab with Spaces

Update

I have put together some code which will add a single space (or more) in front of an arbitary number of selected lines in TeXShop. I need some help here, however. After I run the script, TeXShop deselects the original selection. Why is this bad? You cannot quickly rerun the script on the same selection. It wouldn't be efficient to make a key binding for this script.

(*BASIC STRUCTURE OF THIS SCRIPT*)
--repeat with each line in theselection
--do shell script "sed 's/^/ /'   --THIS THING JUST ADDS A SPACE IN FRONT OF EACH LINE OF INPUT

tell application "TeXShop"
    --get the front document
    set thisDoc to the front document
    set mySel to selection of thisDoc
    set selContent to content of selection of thisDoc
    set outcome to do shell script "echo " & (quoted form of selContent) & "|sed 's/^/ /' "
    set content of selection of thisDoc to outcome
end tell

Before:

Selection Before Script

After:

Result

Is there a way to fix this script so that it leaves the selection selected after running?

Best Answer

Here is a script that inserts a single space in front of each selected line, and leaves the lines selected.

It works by saving the location of the selection, adding a space to each line, and then using reselecting the original selection.

tell application "TeXShop"
    --get the front document
    set thisDoc to the front document
    --get the current selection text, offset, and length
    set selContent to content of selection of thisDoc
    set selOffset to offset of selection of thisDoc
    set selLength to length of selection of thisDoc
end tell

set outcome to ""
set spaceAdded to 0
-- add a space to each line, except for blank lines
repeat with oneLine in (paragraphs of selContent)
    if length of oneLine is equal to 0 then
        set outcome to outcome & return
    else
        set outcome to outcome & " " & oneLine & return
        set spaceAdded to spaceAdded + 1
    end if
end repeat

tell application "TeXShop"
    try
        -- this will avoid the error caused if there is no text selected
        set content of selection of thisDoc to (text 1 thru -2 of outcome)
        set offset of selection of thisDoc to selOffset
        set length of selection of thisDoc to selLength + spaceAdded
    end try
end tell