Fortran block comments in xcode

Just in case someone like me wants to comment a whole selection in a fortran code in xcode editor. You can do that by edit an inbuilt user script.
Menu >> user scripts (the script icon) >> Edit User Scripts
Then under "Comments", you can edit the "Un/Comment Selectio" by changing "//" to "!"

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

You can make the script decide for you ...

Instead of changing "//" to "!" and back whenever you change languages, you can have that script detect the filename and behave accordingly. The following modified script does the job:


#! /usr/bin/perl -w
#
# un_commentLines.pl : comment or uncomment the selected lines
# use '# ' for Perl and shell scripts,
# '! ' for fortran files
# '-- ' for applescript files
# '// ' otherwise

my $outputString = "";
my $perlCmt = "#";
my $aCmt = "--";
my $cCmt = "//";
my $fCmt = "!";

# get the first few lines of the file
my $fileString = <<'HEADTEXT';
%%%{PBXHeadText}%%%
HEADTEXT

# get the file name
my $filePath = <<'FILEPATH';
%%%{PBXFilePath}%%%
FILEPATH

# determine the type of file
# for perl or shell scripts, look for the #! line at the top
# (careful, it might already be commented out);
# for other files, look at the file extension
my $commentString;
if ($fileString =~ m!^($perlCmt|$cCmt)?#\!\s*.*?/perl|^($perlCmt|$cCmt)?#\!\s*.*?/sh!) {
$commentString = $perlCmt;
} elsif ($filePath =~ m/(\.f|\.f90|\.f95)$/i) {
$commentString = $fCmt;
} elsif ($filePath =~ m/(\.applescript|\.scpt)$/) {
$commentString = $aCmt;
} else {
$commentString = $cCmt;
}

# read selection from standard input
my @selection = ;

# if no chars in selection, create an empty selection
if (!@selection) {
push @selection, "";
};

# use first line of selection to determine if we add or remove comments
my $firstLineOfSelection = $selection[0]; # get first line
my $addCommentString = 1;
if ($firstLineOfSelection =~ /^\s*$commentString/) { # selection starts with comment
$addCommentString = 0;
}
# also use first line of selection to determine indentation
# (if you want the comment character always at the start of a line,
# then ditch the next two lines, and delete the use of $indentString below)
$firstLineOfSelection =~ /^(\s*)/;
my $indentString = $1;

# do the work
foreach my $line (@selection) {
if ($addCommentString == 1) {
$line =~ s/$indentString//;
$outputString .= $indentString.$commentString.$line;
} else {
$line =~ s/^(\s*)$commentString/$1/;
$outputString .= $line;
}
}

print "%%%{PBXSelection}%%%";
print $outputString;
print "%%%{PBXSelection}%%%";

Useful script modification,

Useful script modification, but it gives a compilation error that I remedied changing this line from

my @selection = ;

to

my @selection = <STDIN>;

Cheers!