Created
June 19, 2012 01:24
-
-
Save gingi/2951789 to your computer and use it in GitHub Desktop.
BBEdit Menu Script to Toggle Indented Comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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