Skip to content

Commit

Permalink
introduce option L in vimcolor to display supported file types
Browse files Browse the repository at this point in the history
  • Loading branch information
wofr06 committed May 8, 2024
1 parent fef75b3 commit 20e2849
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
****************************************************************************
# ChangeLog for lesspipe.sh #
****************************************************************************
- respect color scheme setting of vim in vimcolor
- respect color scheme setting of vim in vimcolor, add listing of file types
- improve xml (and html) display using the xmq binary
- fix color detection (-R) again
- support for cpio archives
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ batcat --list-languages
pygmentize -L lexers
source-highlight --lang-list
code2color -h
vim -c "echo getcompletion('', 'filetype')" -c quit
vimcolor -L
```

### 5.2 Colored Directory listing
Expand Down
46 changes: 33 additions & 13 deletions vimcolor
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ use Term::ANSIColor qw(color colorvalid);
use IPC::Open3;
use Getopt::Std;

our ($opt_c, $opt_h, $opt_l);
&getopts('chl:') || usage();
our ($opt_c, $opt_h, $opt_l, $opt_L);
&getopts('chl:L') || usage();
usage() if $opt_h;

my ($file) = shift;
my $filetype = $opt_l;
my $report_colors = $opt_c;

# do a clean up if we get a CTRL-C
our ($tdir,$script_fh, $markup_fh);
$SIG{INT} = sub { if ($tdir) {
Expand All @@ -38,6 +34,11 @@ $SIG{INT} = sub { if ($tdir) {
unlink $tdir; print "\n"; exit 1 }};

$tdir = File::Temp->newdir('/tmp/vimcolorXXXX');
languages(@ARGV) if $opt_L;

my ($file) = shift;
my $filetype = $opt_l;
my $report_colors = $opt_c;

if (! $file or $file eq '-') {
$file = "$tdir/inputfile";
Expand All @@ -59,14 +60,14 @@ my %ANSI_COLORS = (
);

# get colors from the active color scheme
my $colorcmds = "$tdir/commands";
my $cmds = "$tdir/commands";
my $colorfile = "$tdir/colors";
open F, ">$colorcmds" or die "$!";
print F "redir! >$colorfile\ncolorscheme\n";
print F "hi $_\n" for keys %ANSI_COLORS;
print F "redir END\nq!\n";
open F, ">$cmds" or die "$!";
print F "redir! >$colorfile|colorscheme|";
print F "hi $_|" for keys %ANSI_COLORS;
print F "q!";
close F;
run('vim', qw(--not-a-term -RXZi NONE -S), $colorcmds, $colorfile);
run('vim', qw(--not-a-term -RXZi NONE -S), $cmds, $colorfile);
open F, $colorfile or die $!;
my ($key, %colattrs);
my %t = map {$_, $_} qw(bold underline reverse italic blink undercurl standout);
Expand Down Expand Up @@ -288,9 +289,27 @@ sub run {
}
}

sub languages {
my ($pat) = @_;
my $cmds = "$tdir/commands";
my $typefile = "$tdir/types";
open F, ">$cmds" or die "$!";
print F "redir! >$typefile|echow getcompletion(\'$pat\', 'filetype')|q!";
close F;
run('vim', qw(--not-a-term -RXZi NONE -S), $cmds, $typefile);
open F, $typefile or die $!;
$_ = <F>; $_ = <F>;
s/[\[\]',]//g;
print "$_\n";
close F;
exit;
}

sub usage {
print <<EOF;
usage: vimcolor [-c] [-h] [-l language] [filename]
usage: vimcolor [-c] [-l language] [filename]
usage: vimcolor -h
usage: vimcolor -L [pattern]
This program works by running the Vim text editor and getting it to apply its
excellent syntax highlighting (aka 'font-locking') to an input file, and mark
Expand All @@ -303,6 +322,7 @@ OPTIONS:
-c report color settings at the end of the program
-h print this help and exit
-l specify the type of file Vim should expect, if not recognized correctly
-L list supported languages (file types) [that match pattern] and exit
ARGUMENT
filename name of the file to colorize. If not given or - then STDIN is used
Expand Down

0 comments on commit 20e2849

Please sign in to comment.