Skip to content

Instantly share code, notes, and snippets.

@gingi
Created June 19, 2012 01:24
Show Gist options
  • Save gingi/2951789 to your computer and use it in GitHub Desktop.
Save gingi/2951789 to your computer and use it in GitHub Desktop.
BBEdit Menu Script to Toggle Indented Comments
tell application "BBEdit"
tell front text window
set srcLang to source language of the selection
set commStr to ""
set closeStr to ""
if srcLang = "HTML" then
set commStr to "<!--"
set closeStr to "-->"
else if srcLang = "Perl" or srcLang = "Unix Shell Script" then
set commStr to "#"
else
set commStr to "//"
end if
if length of selection is 0 then
select line (startLine of selection)
end if
tell selection
set existingComments to find "^\\s*" & commStr options {search mode:grep}
if found of existingComments then
-- Remove existing comments
replace "^(\\s*)" & commStr & " ?" using ("\\1") options {search mode:grep, case sensitive:false}
if closeStr is not equal to "" then
replace "(\\s*" & closeStr & "\\s*)*$" using "" options {search mode:grep}
end if
else
-- Add comments
set commInd to length of found text of (find Â
"^(\\s*)" options {search mode:grep} searching in startLine of selection)
replace "^(\\s{0," & commInd & "})(.+)" using ("\\1" & commStr & " \\2") options {search mode:grep, case sensitive:false}
if closeStr is not equal to "" then
replace "(\\s*" & closeStr & "\\s*)*$" using "" options {search mode:grep}
replace "\\s*$" using (" " & closeStr) options {search mode:grep, extend selection:true}
end if
end if
end tell
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment