[Tex/LaTex] Indentation and line wraps in TeXShop

indentationsourcetexshop

As pointed out in this question, in TeXShop there is no way to make source paragraphs (i.e. long lines with no hard wraps) follow source indents automatically, that is, while typing. The solution proposed in the accepted answer is unsatisfactory, in my opinion, as it requires manual insertion of line breaks at every single line… I believe you should not be forced to adopt such a behavior in year 2011, unless you are a big fan of old typewriters.

My ultimate goal is to obtain

readable, "programming language-style"
source code, with indented blocks for
environments,

and I would like to get that using TeXShop (I know that's easy using other editors, such as Emacs).
My question is:

is there at least a way to obtain
such a formatting after typing,
e.g. using
a macro which inserts line breaks
in the right spots?

The first thing I tried is the following:

  1. Write the code naturally, with no indents nor hard wraps;
  2. Select any block (or even the whole code) and then apply Source > Wrap Lines > Hard Wrap from TeXShop's menu;
  3. Select any block you want to indent, and indent it using the appropriate command in the Source menu.

However this does not work nicely, because after you indent the block some lines may become too long for the editor window's width.

Best Answer

I not sure what exactly is your required feature, but maybe the following will satisfy you. I make use of another program to do the work, namely Textwrangler, to properly align and insert hard line breaks.

Here is a macro that does the following:

  1. collects all marked text,
  2. passes it on to Textwrangler
  3. where the text is properly aligned and broken
  4. the result is pasted back in TexShop in place of the marked text.

I apply the macro using an assigned keyboard shortcut. I am no Applescript whizkid, so I guess the macro can be improved.

--Applescript
-- Script to transfer the current TeXShop document to TextWrangler
-- Ido Ben-Zvi
-- Based on Ramon Figueroa-Centeno's macros 

tell application "TeXShop"
    try
        activate
        set posPath to path of the front document
        --return posPath

        tell application "System Events" to keystroke "c" using {command down}

        tell application "TextWrangler"
            activate
            tell application "System Events"
                keystroke "a" using command down
                keystroke "v" using command down
                keystroke "a" using command down
            end tell
            hard wrap selection of text window 1 limit character width width 80 indentation none with paragraph fill and relative
            --copy selection of text window 1
            --set modified_text to selection of text window 1       
            tell application "System Events" to keystroke "c" using {command down}
            --close myTeXfile
        end tell


        -- back at TexShop      
        activate
        --set modified of front document to yes
        --paste 
        tell application "System Events"
            keystroke "v" using command down
        end tell
    on error
        beep
    end try
end tell




--tell application "TextWrangler"
--  close POSIX file FILEPATH
--end tell