releasing version 0.18
[moreutils.git] / vidir
blobf67f74d9b0ecde1657ca5ae63331f6f228cc054c
1 #!/usr/bin/perl
3 =head1 NAME
5 vidir - edit directory
7 =head1 SYNOPSIS
9 B<vidir> [--verbose] [directory|file|-] ...
11 =head1 DESCRIPTION
13 vidir allows editing of the contents of a directory in a text editor. If no
14 directory is specified, the current directory is edited.
16 When editing a directory, each item in the directory will appear on its own
17 numbered line. These numbers are how vidir keeps track of what items are
18 changed. Delete lines to remove files from the directory, or
19 edit filenames to rename files. You can also switch pairs of numbers to
20 swap filenames.
22 Note that if "-" is specified as the directory to edit, it reads a list of
23 filenames from stdin and displays those for editing. Alternatively, a list
24 of files can be specified on the command line.
26 =head1 OPTIONS
28 =over 4
30 =item -v, --verbose
32 Verbosely display the actions taken by the program.
34 =back
36 =head1 ENVIRONMENT VARIABLES
38 =over 4
40 =item EDITOR
42 Editor to use.
44 =item VISUAL
46 Also supported to determine what editor to use.
48 =back
50 =head1 BUGS
52 Does not support deletion of directories. Does not support recursive
53 editing of contents of a directory.
55 =head1 AUTHOR
57 Copyright 2006 by Joey Hess <joey@kitenet.net>
59 Licensed under the GNU GPL.
61 =cut
63 use File::Temp;
64 use Getopt::Long;
66 my $error=0;
68 my $verbose=0;
69 if (! GetOptions("verbose|v" => \$verbose)) {
70 die "Usage: $0 [--verbose] [directory|file|-]\n";
73 my @dir;
74 if (! @ARGV) {
75 push @ARGV, "."
77 foreach my $item (@ARGV) {
78 if ($item eq "-") {
79 push @dir, map { chomp; $_ } <STDIN>;
80 close STDIN;
81 open(STDIN, "/dev/tty") || die "reopen: $!\n";
83 elsif (-d $item) {
84 opendir(DIR, $item) || die "$0: cannot read $item: $!\n";
85 push @dir, map { "$item/$_" } sort readdir(DIR);
86 closedir DIR;
88 else {
89 push @dir, $item;
93 my $tmp=File::Temp->new(template => "dirXXXXX");
94 open (OUT, ">".$tmp->filename) || die "$0: cannot write ".$tmp->filename.": $!\n";
96 my %item;
97 my $c=0;
98 foreach (@dir) {
99 next if /(.*\/)?\.$/ || /(.*\/)?\.\.$/;
100 $item{++$c}=$_;
101 print OUT "$c\t$_\n";
103 @dir=();
104 close OUT;
106 my @editor="vi";
107 if (-x "/usr/bin/editor") {
108 @editor="/usr/bin/editor";
110 if (exists $ENV{EDITOR}) {
111 @editor=split(' ', $ENV{EDITOR});
113 if (exists $ENV{VISUAL}) {
114 @editor=split(' ', $ENV{VISUAL});
116 $ret=system(@editor, $tmp);
117 if ($ret != 0) {
118 die "@editor exited nonzero, aborting\n";
121 open (IN, $tmp->filename) || die "$0: cannot read ".$tmp->filename.": $!\n";
122 while (<IN>) {
123 chomp;
124 if (/^(\d+)\t{0,1}(.*)/) {
125 my $num=$1;
126 my $name=$2;
127 if (! exists $item{$num}) {
128 print STDERR "$0: unknown item number $num\n";
129 $error=1;
131 elsif ($name ne $item{$num}) {
132 next unless length $name;
133 my $src=$item{$num};
135 # deal with swaps
136 if (-e $name || -l $name) {
137 my $tmp=$name."~";
138 my $c=0;
139 while (-e $tmp || -l $tmp) {
140 $c++;
141 $tmp=$name."~$c";
143 if (! rename($name, $tmp)) {
144 print STDERR "$0: failed to rename $name to $tmp: $!\n";
145 $error=1;
147 elsif ($verbose) {
148 print "'$name' -> '$tmp'\n";
150 foreach my $item (keys %item) {
151 if ($item{$item} eq $name) {
152 $item{$item}=$tmp;
157 if (! rename($src, $name)) {
158 print STDERR "$0: failed to rename $src to $name: $!\n";
159 $error=1;
161 elsif ($verbose) {
162 print "'$src' -> '$name'\n";
165 delete $item{$num};
167 elsif (/^\s*$/) {
168 # skip empty line
170 else {
171 die "$0: unable to parse line \"$_\", aborting\n";
174 close IN;
175 unlink($tmp.'~') if -e $tmp.'~';
177 foreach my $item (sort values %item) {
178 if (! unlink($item)) {
179 print STDERR "$0: failed to remove $item: $!\n";
180 $error=1;
182 if ($verbose) {
183 print "removed '$item'\n";
187 exit $error;