Syntax aware auto-indent?

1,370 views
Skip to first unread message

Robert Rollins

unread,
Apr 11, 2011, 2:24:47 PM4/11/11
to BBEdit Talk
Hi all! I recently started checking out BBEdit as an alternative
development platform to Eclipse and TextMate, and so far I like it a
lot. But there's one thing I'm really missing: syntax aware ("smart")
auto-indent.

When a PHP programmer types an open curly bracket (or a Python
programmer types a colon) then presses Return, the expectation is that
the cursor will appear indented one tab further to the right than the
above line. However, BBEdit's auto-indent brings the cursor to the
same level as the previous line, regardless of the contents of the
above line. This requires an additional TAB press to bring the code
into the proper indentation level when starting a deeper block.

Is there any way, via an addon to BBEdit, or other such device, to
make auto-indent more syntax aware?

Watts Martin

unread,
Apr 11, 2011, 3:22:24 PM4/11/11
to bbe...@googlegroups.com
Robert Rollins wrote:
> When a PHP programmer types an open curly bracket (or a Python
> programmer types a colon) then presses Return, the expectation is that
> the cursor will appear indented one tab further to the right than the
> above line. However, BBEdit's auto-indent brings the cursor to the
> same level as the previous line, regardless of the contents of the
> above line. This requires an additional TAB press to bring the code
> into the proper indentation level when starting a deeper block.

My impression is that this is something of a religious issue in these
parts. :) I'm with you--while I think programmers should have the option
of being able to turn this off, it's remarkable how much time this
little trick, and its corresponding trick of automatic de-indenting when
you start a line with a close bracket, ends up saving in practice.

The best I've done in BBEdit is create a combination AppleScript and
"Universal Item" clipping to let me do an indent with Ctrl-Return, which
not only isn't really syntax aware, it turned out to be less trivial
than I was originally expecting. The clipping file I called "Return &
Indent" and simply contains this, where "<r>" indicates a return character:

<r>
#INDENT##script maketab.scpt##INSERTION#<r>

(i.e., you need to have a blank line above the code.) The AppleScript is
named "maketab" and contains this:

tell application "BBEdit"
tell text window 1
if expand tabs is true then
set insertText to ""
repeat tab width times
set insertText to insertText & " "
end repeat
else
set insertText to tab
end if
insertText
end tell
end tell

Both of those need to be saved in the "~/Library/Application
Support/BBEdit/Clippings/Universal Items" folder. This is still not 100%
ideal, as BBEdit's autocompletion will try to execute this via any time
I type the word "return" in plain text; I'm not sure if there's a way to
turn that off short of just renaming it something sufficiently weird
that it's not likely to come up (or just turning off autocomplete).

At least for me, the hardest thing to adapt to in BBEdit is that
AppleScript is pretty much essential if you want to learn how to do
extensions. I know some people are very comfortable with AppleScript,
but I am not one of them.

--
Watts Martin <lay...@gmail.com>

Kendall Conrad

unread,
Apr 11, 2011, 4:51:48 PM4/11/11
to BBEdit Talk
Here's two AppleScript scripts that I wrote for this type of thing. I
think I made it after a discussion here actually. I have it set with
cmd+return / cmd+shift+return. It handles cases for JavaDoc (/**),
open curly, colon, and by default will just move down a line with the
correct indentation. One benefit is it allows you to execute it no
matter where you're at on the current line. It just looks at the end
of the current line to determine what to do.

The second script is similar except it will create a new line above
the current one with correct indentation and can be executed from
anywhere on the current line. I made these to mimic vim's o and O
commands, though I added extras for o to have some extra output.

Hope these help. They're not automatic, but I have found them very
helpful in coding.

-Kendall

(*
Author: Kendall Conrad of Angelwatt.com
Name: Smart Newline
Created: 2010-01-23
Description: Based on the current line it will generate different
text for the context. It knows about indentation, code doc syntax,
function starts
*)
tell application "BBEdit" to tell front window
activate
set lineNum to startLine of selection
set leng to length of line lineNum
-- Move cursor to end of line
if leng > 0 then
select insertion point after (character (leng) of line lineNum)
end if

-- Find leading whitespace
set theResult to find "(^[\\s]*)" options {search mode:grep}
searching in line (lineNum)
-- Set text to the white space found
set white to ""
if found of theResult then
set white to found text of theResult
end if
set wleng to length of white

-- Are we starting a code doc?
if (leng - wleng) ≥ 3 then
if (characters (wleng + 1) through (wleng + 3) of line lineNum as
string) is equal to "/**" then
set selection to return & white & " * " & return & white & " */"
select insertion point after line (lineNum + 1)
return
end if
end if

-- Check if inside a code doc
if (leng - wleng) ≥ 1 then
if (characters (wleng + 1) through (wleng + 2) of line lineNum as
string) is equal to "* " then
set selection to return & white & "* "
select insertion point after line (lineNum + 1)
return
end if
end if

-- Check for code block {}
if (leng - wleng) ≥ 1 then
if (character -1 of line lineNum as string) is equal to "{" then
set selection to return & white & tab & return & white & "}"
select insertion point after line (lineNum + 1)
return
end if
end if

-- Check for code block : (Python)
if (leng - wleng) ≥ 1 then
if (character -1 of line lineNum as string) is equal to ":" then
set selection to return & white & tab
select insertion point after line (lineNum + 1)
return
end if
end if

-- Default: Insert a return plus the white space
set selection to return & white
select insertion point after selection
end tell

-----------------------------

(*
Author: Kendall Conrad of Angelwatt.com
Name: OpenLineUp
Created: 2010-01-18
Description: Creates a new line above the line currently on at
appropriate indent level
*)
tell application "BBEdit" to tell front window
activate
set lineNum to startLine of selection
set leng to length of line lineNum
-- Move cursor to start of line
if leng > 0 then
select insertion point before (character 1 of line lineNum)
end if
-- Find leading whitespace
set theResult to find "(^[\\s]*)" options {search mode:grep}
searching in line (lineNum)
-- Set text to the white space found
set white to ""
if found of theResult then
set white to found text of theResult
end if
-- Insert a return plus the white space
set selection to white & return
select insertion point before (character -1 of selection)
end tell

Robert Rollins

unread,
Apr 14, 2011, 4:52:03 PM4/14/11
to BBEdit Talk
Cool, thanks for the solutions!

Rick Yentzer

unread,
Feb 16, 2012, 10:06:16 AM2/16/12
to bbe...@googlegroups.com
I'm still hoping that this would be a preference instead of an applescript. Most other editors I've tried out recently have this feature. It saves a lot of typing.

Watts Martin

unread,
Feb 18, 2012, 2:32:50 AM2/18/12
to bbe...@googlegroups.com
It does, but I stand by my feeling that it's somewhat of a religious
issue, like autocomplete, automatic pairing, etc. People who like such
things tend to think they're must-haves and people who don't tend to
think they're the spawn of the devil -- and you hear a lot of stuff that
boils down to "real programmers don't need to have their editors indent
for them, let alone type closing tags, quotes and brackets." I used to
be of that opinion, but learned during my years with TextMate that it's
remarkable how much time those little things end up saving once you
start getting used to them.

At any rate, in BBEdit I have a somewhat more advanced package I put
together that's a collection of Applescripts which includes a "smart
return" function that's not only a syntax-aware indent but does some of
the auto-pairing that TextMate refugees miss. I'll try to get it cleaned
up and online if people are interested. (I confess that against my own
advice of last year I've lately been trying out a competing editor; I'm
waiting for "Take Control of BBEdit" to come out and bring me back to
the fold.)

Rick Yentzer

unread,
Feb 18, 2012, 9:04:02 AM2/18/12
to bbe...@googlegroups.com
Count me in as interested in the scripts. I've set up a few clippings and dabbled in applescripts to get something similar but it is certainly basic at best.

There is another editor that offers this functionality in the preference settings. It seems that would be something BBEdit could offer this as an option in the preferences as well. I'm looking forward to the Take Control of BBedit, perhaps it will restore my faith in BBEdit.

Ryan Wilcox

unread,
Feb 22, 2012, 11:49:24 AM2/22/12
to BBEdit Talk
On Feb 18, 2:32 am, Watts Martin <lay...@gmail.com> wrote:

> At any rate, in BBEdit I have a somewhat more advanced package I put
> together that's a collection of Applescripts which includes a "smart
> return" function that's not only a syntax-aware indent but does some of
> the auto-pairing that TextMate refugees miss. I'll try to get it cleaned
> up and online if people are interested. (I confess that against my own
> advice of last year I've lately been trying out a competing editor; I'm
> waiting for "Take Control of BBEdit" to come out and bring me back to
> the fold.)

Watts,

I didn't see this last paragraph until now. I have a Source BBEdit
Package which might be a good place for those scripts. (https://
github.com/rwilcox/Source.bbpackage)

I look forward to seeing your scripts, no matter how they end up going
public.

Later,
_Ryan Wilcox
Reply all
Reply to author
Forward
0 new messages