Insert Next List Item

285 views
Skip to first unread message

oliver

unread,
Aug 7, 2011, 10:35:51 PM8/7/11
to BBEdit Talk
I'm coming from TextMate, and I'm loving BBEdit so far, but there's
one thing I sorely miss. Let's say you're writing a list of items like
this: (^ = insertion point)

1. Milk
2. Eggs
3. Bacon^

At this point I want to hit a shortcut and have "4. " inserted on the
next line. Would this be possible with AppleScript?

It would be great if the script would look for bulleted lists and do
the right thing as well.

* Milk
* Eggs
* Bacon

I'm thinking it might have to be an AppleScript to select the line and
then call a Text Filter to handle finding the number/star and
appending the next one... but my kung fu is not strong enough to
figure it out.

Any help is appreciated. Thanks!

Christopher Stone

unread,
Aug 8, 2011, 12:04:04 AM8/8/11
to bbe...@googlegroups.com
On Aug 07, 2011, at 21:35, oliver wrote:
I'm thinking it might have to be an AppleScript to select the line and then call a Text Filter to handle finding the number/star and appending the next one... but my kung fu is not strong enough to figure it out.
______________________________________________________________________

Hey Oliver,

This is very quick and dirty, but it'll work with the numerical increment and the asterisk bullet.

It's expecting the insertion point (cursor) to be at the end of the line, but it could easily be adjusted so that anywhere in the line would do.

You can put it in the script menu and give it a keyboard shortcut and go to town.

The logic is pretty straightforward, so you can add more characters like '•' to the mix if desired.

--
Best Regards,
Chris

______________________________________________________________________


tell application "BBEdit"
  try
    tell text of front text window
      set lineOfInsertionPoint to line (startLine of selection)
      set findReco to find "^\\d+\\." searching in lineOfInsertionPoint options {search mode:grep}
      if found of findReco = true then
        set leadingNumber to text 1 thru -2 of (found text of findReco)
        set text of selection to return & (leadingNumber + 1) & ". "
        select insertion point after selection
      else if found of findReco = false then
        set findReco to find "^\\* " searching in lineOfInsertionPoint options {search mode:grep}
        if found of findReco = true then
          set text of selection to return & "* "
          select insertion point after selection
        end if
      end if
    end tell
  on error errMsg number errNum
    set sep to "=============================="
    set e to sep & return & "Error: " & errMsg & return & sep & return ¬
      & "Error Number: " & errNum & return & sep
    beep
    display dialog e
  end try
end tell

oliver

unread,
Aug 8, 2011, 2:41:20 AM8/8/11
to BBEdit Talk
Perfect! Thank you!

KG

unread,
Mar 26, 2012, 4:45:59 PM3/26/12
to bbe...@googlegroups.com
Hey Christopher,

Just a minor request to your existing script. This works fantastically if you line starts with the bullet. Assuming you have some indentation like 4 tab stops and then the bullet, the script stops. I made a couple of mods to the script as follows:

---------------------------------------------------------------------------------------
    tell application "BBEdit"
        try
            tell text of front text window
                set lineOfInsertionPoint to line (startLine of selection)
                set findReco to find "^\\d+\\." searching in lineOfInsertionPoint options {search mode:grep}
                if found of findReco = true then
                    set leadingNumber to text 1 thru -2 of (found text of findReco)
                    set text of selection to return & (leadingNumber + 1) & ". "
                    select insertion point after selection
                else if found of findReco = false then
                    set findReco to find "^\\* " searching in lineOfInsertionPoint options {search mode:grep}
                    if found of findReco = true then
                        set text of selection to return & "* "
                        select insertion point after selection
                    else
                        set findReco to find "^\\s*\\+" searching in lineOfInsertionPoint options {search mode:grep}
                        if found of findReco = true then
                            
                            set text of selection to return & tab & "+ "
                            select insertion point after selection
                        end if
                    end if
                end if
            end tell
        on error errMsg number errNum
            set sep to "=============================="
            set e to sep & return & "Error: " & errMsg & return & sep & return ¬
                & "Error Number: " & errNum & return & sep
            beep
            display dialog e
        end try
    end tell
---------------------------------------------------------------------------------------


Essentially the \s* helps ignore white space at the beginning. There's just one problem with my script. It inserts the new bullet point ignoring the previous number of tab stops. Is there a way in applescript to get hold of the number of tab stops and insert that at the beginning as well?

Thanks again.

Watts Martin

unread,
Mar 26, 2012, 5:42:35 PM3/26/12
to bbe...@googlegroups.com
While I don't have the script available at the moment, the "Smart New Line" script in the BBEdit Editor Actions Package I linked to a little earlier (http://www.ranea.org/bbedit_editoractions.html) may be something to look at. It handles bullets that are "-" or "*" at the start of a line and can continue the list, and handles the current indentation and expand tabs settings correctly.

It doesn't try to number lists as the one below does, but that might be something I could add for Markdown.

-- 
Watts Martin
Sent with Sparrow

--
You received this message because you are subscribed to the
"BBEdit Talk" discussion group on Google Groups.
To post to this group, send email to bbe...@googlegroups.com
To unsubscribe from this group, send email to
bbedit+un...@googlegroups.com
For more options, visit this group at
<http://groups.google.com/group/bbedit?hl=en>
If you have a feature request or would like to report a problem,
please email "sup...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

Kendall Conrad

unread,
Mar 26, 2012, 10:14:31 PM3/26/12
to BBEdit Talk
My Smart Newline script can achieve this. You can examine the code
near the top to see how I capture to white space at the start of the
line if that's all you care about. I also updated the script this
evening to also handle numbered lists, which was part of the original
discussion question, though this is a little late for that I suppose,
but others might find it useful.

http://www.angelwatt.com/words/2011/04/11/bbedit-smart-newline-open-line/

-Kendall

KG

unread,
Mar 27, 2012, 2:06:35 AM3/27/12
to bbe...@googlegroups.com
@Kendall : neat script. thanks again. 

@Kendall+Watts: One additional request-can the scripts be modified to pick the first character on the line and then use that as a bullet? for e.g any of the following could be bullets in my MD document ("+","-","*"). I have currently modified your scripts to take care of this, but rather than an if,else,if,else I thought it would be really cool to have the script automatically pick up the bullet character and insert.

Cheers and thanks for the great scripts.

KG

unread,
Mar 27, 2012, 2:18:19 AM3/27/12
to bbe...@googlegroups.com
@Kendall : just one more thing, can you provide a link to the latest source. from the url you provided i don't see the numbered list edit that you said you made

Thanks


On Tuesday, March 27, 2012 5:14:31 AM UTC+3, Kendall Conrad wrote:

Kendall Conrad

unread,
Mar 27, 2012, 9:50:05 AM3/27/12
to bbe...@googlegroups.com
The link I provided does have the latest code. If you visited the page somewhat recently you may need to do a refresh to see the latest changes. The code will state that is was last updated March 26 at the top of the code listing.

The updated code does handle doing lists that start with *, #, and -, but not +. I thought about it, but couldn't think of anytime I've seen it used. It would be very easy to add though to the code. Just edit the section "Check for lists" and add \\+ to the two regex finds. It has to be escaped since it's a special character for regex. I'll make an update when I get a chance, probably within the next 20 hours.

-Kendall

Kendall Conrad

unread,
Mar 28, 2012, 6:56:11 AM3/28/12
to bbe...@googlegroups.com
For anyone that was waiting, I did update the script to include handling of + listed items. I also added >. I made some other enhancements too while I was at it. The numerical listing handles both going 3. to 4., but also 2.4. to 2.5. so it handles decimal numbering or sub sections. Lastly, I added HTML list item support; it creates a <li> tags, copies the <li> tag's attributes from the current line, and duplicates them and also places the cursor inside the tag.

If you have visited the page before you'll likely need to hit refresh to ensure you're seeing the latest page updates. I haven't had a chance to do thorough testing so if you see issues or have other suggestions let me know.

http://www.angelwatt.com/words/2011/04/11/bbedit-smart-newline-open-line/

-Kendall

KG

unread,
Mar 30, 2012, 4:05:54 AM3/30/12
to bbe...@googlegroups.com
Rock on Sir !

Oliver Taylor

unread,
Apr 29, 2012, 10:44:50 PM4/29/12
to bbe...@googlegroups.com
This is fantastic, thank you Sir.
Reply all
Reply to author
Forward
0 new messages