vidir 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings FATAL => 'all';
  4. use utf8;
  5. use open qw(:std :utf8);
  6. #use encoding 'utf8';
  7. use vars qw($VERSION);
  8. $VERSION = '0.040-woldrich';
  9. use File::Basename;
  10. use File::Spec;
  11. use File::Temp;
  12. use File::Copy;
  13. use File::Path ();
  14. use Getopt::Long;
  15. my($error, $verbose) = (0, 0);
  16. if (! GetOptions("verbose|v" => \$verbose)) {
  17. die "Usage: $0 [--verbose] [directory|file|-]\n";
  18. }
  19. my(@files, @directories, @sorted);
  20. @ARGV = @ARGV > 0 ? @ARGV : './';
  21. for my $item(@ARGV) {
  22. if($item eq '-') {
  23. push(@files, map { chomp; $_ } <STDIN>);
  24. close STDIN or die "Cant close STDIN: $!\n";
  25. open(STDIN, '/dev/tty') or die "Cant reopen STDIN: $!\n";
  26. next;
  27. }
  28. if(-d $item) {
  29. opendir(my $dh, $item) or die "Cant opendir $item: $!\n";
  30. for(grep { ! /\A[.]{1,2}\Z/ } readdir($dh)) {
  31. $item =~ s{/$}{};
  32. if(-d "$item/$_") {
  33. push(@directories, "$item/$_/");
  34. }
  35. else {
  36. push(@files, "$item/$_");
  37. }
  38. }
  39. closedir $dh or die "Cant close dirhandle $item: $!\n";
  40. }
  41. }
  42. @sorted = (sort(@directories), sort(@files));
  43. if (grep(/[[:cntrl:]]/, @sorted)) {
  44. die "$0: control characters in filenames are not supported\n";
  45. }
  46. my $tmp=File::Temp->new(TEMPLATE => "dirXXXXX", DIR => File::Spec->tmpdir);
  47. open (OUT, ">".$tmp->filename) || die "$0: cannot create ".$tmp->filename.": $!\n";
  48. my %item;
  49. my %done;
  50. my $c=0;
  51. foreach (@sorted) {
  52. $item{++$c}=$_;
  53. print OUT sprintf("%5d %s\n", $c, $_);
  54. }
  55. @sorted=();
  56. close OUT || die "$0: cannot write ".$tmp->filename.": $!\n";
  57. my @editor = 'vi';
  58. if(exists($ENV{VIDIR_EDITOR})) {
  59. @editor = $ENV{VIDIR_EDITOR};
  60. }
  61. elsif(exists($ENV{EDITOR})) {
  62. @editor = split(' ', $ENV{EDITOR});
  63. }
  64. elsif(-x '/usr/bin/editor') {
  65. @editor = '/usr/bin/editor';
  66. }
  67. elsif(exists($ENV{VISUAL})) {
  68. @editor = split(' ', $ENV{VISUAL});
  69. }
  70. if( (exists($ENV{VIDIR_EDITOR_ARGS})) && ($ENV{VIDIR_EDITOR_ARGS} ne '') ) {
  71. system(@editor, $ENV{VIDIR_EDITOR_ARGS}, $tmp);
  72. }
  73. else {
  74. system(@editor, $tmp);
  75. }
  76. open (IN, $tmp->filename) || die "$0: cannot read ".$tmp->filename.": $!\n";
  77. while (<IN>) {
  78. chomp;
  79. if(/^\s*(\d+) {0,1}(.*)/) {
  80. my $num=int($1);
  81. my $name=$2;
  82. my $iscopy=exists $done{$num};
  83. #$name =~ s{/+$}{}g;
  84. if (! exists $item{$num} && ! $iscopy) {
  85. die "$0: unknown item number $num\n";
  86. }
  87. elsif ($iscopy || $name ne $item{$num}) {
  88. next unless length $name;
  89. my $src=$iscopy ? $done{$num} : $item{$num};
  90. if (! (-e $src || -l $src) ) {
  91. print STDERR "$0: $src does not exist\n";
  92. delete $item{$num};
  93. next;
  94. }
  95. # deal with swaps
  96. if (-e $name || -l $name) {
  97. my $tmp=$name."~";
  98. my $c=0;
  99. while (-e $tmp || -l $tmp) {
  100. $c++;
  101. $tmp=$name."~$c";
  102. }
  103. if (! rename($name, $tmp)) {
  104. print STDERR "$0: failed to rename $name to $tmp: $!\n";
  105. $error=1;
  106. }
  107. elsif ($verbose) {
  108. print "'$name' -> '$tmp'\n";
  109. }
  110. foreach my $item (keys %item) {
  111. if ($item{$item} eq $name) {
  112. $item{$item}=$tmp;
  113. }
  114. }
  115. }
  116. File::Path::make_path(dirname($name));
  117. my $result=$iscopy ? copy($src, $name) : move($src, $name);
  118. if (! $result) {
  119. print STDERR "$0: failed to ".($iscopy ? "copy" : "rename")." $src to $name: $!\n";
  120. $error=1;
  121. }
  122. if (-d $name && ! $iscopy) {
  123. foreach (values %item) {
  124. s/^\Q$src\E/$name/;
  125. }
  126. }
  127. if ($verbose) {
  128. print "'$src' => '$name'\n" unless $iscopy;
  129. print "'$src' ~> '$name'\n" if $iscopy;
  130. }
  131. }
  132. $done{$num}=$name;
  133. delete $item{$num};
  134. }
  135. elsif (/^\s*$/) {
  136. # skip empty line
  137. }
  138. else {
  139. die "$0: unable to parse line \"$_\", aborting\n";
  140. }
  141. }
  142. close IN || die "$0: cannot read ".$tmp->filename.": $!\n";
  143. unlink($tmp.'~') if -e $tmp.'~';
  144. sub rm {
  145. my $file = shift;
  146. if (-d $file && ! -l $file) {
  147. return File::Path::rmtree($file);
  148. }
  149. else {
  150. return unlink $file;
  151. }
  152. }
  153. foreach my $item (reverse sort values %item) {
  154. if (! rm($item)) {
  155. print STDERR "$0: failed to remove $item: $!\n";
  156. $error=1;
  157. }
  158. if ($verbose) {
  159. print "removed '$item'\n";
  160. }
  161. }
  162. exit $error;
  163. __END__
  164. =head1 NAME
  165. vidir - edit directory
  166. =head1 SYNOPSIS
  167. B<vidir> [--verbose] [directory|file|-] ...
  168. =head1 DESCRIPTION
  169. vidir allows editing of the contents of a directory in a text editor. If no
  170. directory is specified, the current directory is edited.
  171. When editing a directory, each item in the directory will appear on its own
  172. numbered line. These numbers are how vidir keeps track of what items are
  173. changed. Delete lines to remove files from the directory, or
  174. edit filenames to rename files. You can also switch pairs of numbers to
  175. swap filenames.
  176. Note that if "-" is specified as the directory to edit, it reads a list of
  177. filenames from stdin and displays those for editing. Alternatively, a list
  178. of files can be specified on the command line.
  179. =head1 OPTIONS
  180. =over 4
  181. =item -v, --verbose
  182. Verbosely display the actions taken by the program.
  183. =back
  184. =head1 EXAMPLES
  185. =over 4
  186. =item vidir
  187. =item vidir *.jpeg
  188. Typical uses.
  189. =item find | vidir -
  190. Edit subdirectory contents too. To delete subdirectories,
  191. delete all their contents and the subdirectory itself in the editor.
  192. =item find -type f | vidir -
  193. Edit all files under the current directory and subdirectories.
  194. =back
  195. =head1 ENVIRONMENT VARIABLES
  196. =over 4
  197. =item EDITOR
  198. Editor to use.
  199. =item VISUAL
  200. Also supported to determine what editor to use.
  201. =item VIDIR_EDITOR_ARGS
  202. Optional args for editor
  203. =back
  204. =head1 AUTHOR
  205. Joey Hess <joey@kitenet.net> 2006-2010
  206. Modifications by Magnus Woldrich <m@japh.se> 2011
  207. =head1 COPYRIGHT
  208. Copyright 2006-2011 the B<vidir> L</AUTHOR>s as listed above.
  209. Licensed under the GNU GPL.
  210. =cut