efm_perl.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/perl -w
  2. # vimparse.pl - Reformats the error messages of the Perl interpreter for use
  3. # with the quickfix mode of Vim
  4. #
  5. # Copyright (c) 2001 by Joerg Ziefle <joerg.ziefle@gmx.de>
  6. # You may use and distribute this software under the same terms as Perl itself.
  7. #
  8. # Usage: put one of the two configurations below in your ~/.vimrc (without the
  9. # description and '# ') and enjoy (be sure to adjust the paths to vimparse.pl
  10. # before):
  11. #
  12. # Program is run interactively with 'perl -w':
  13. #
  14. # set makeprg=$HOME/bin/vimparse.pl\ %\ $*
  15. # set errorformat=%f:%l:%m
  16. #
  17. # Program is only compiled with 'perl -wc':
  18. #
  19. # set makeprg=$HOME/bin/vimparse.pl\ -c\ %\ $*
  20. # set errorformat=%f:%l:%m
  21. #
  22. # Usage:
  23. # vimparse.pl [-c] [-f <errorfile>] <programfile> [programargs]
  24. #
  25. # -c compile only, don't run (perl -wc)
  26. # -f write errors to <errorfile>
  27. #
  28. # Example usages:
  29. # * From the command line:
  30. # vimparse.pl program.pl
  31. #
  32. # vimparse.pl -c -f errorfile program.pl
  33. # Then run vim -q errorfile to edit the errors with Vim.
  34. #
  35. # * From Vim:
  36. # Edit in Vim (and save, if you don't have autowrite on), then
  37. # type ':mak' or ':mak args' (args being the program arguments)
  38. # to error check.
  39. #
  40. # Version history:
  41. # 0.2 (04/12/2001):
  42. # * First public version (sent to Bram)
  43. # * -c command line option for compiling only
  44. # * grammatical fix: 'There was 1 error.'
  45. # * bug fix for multiple arguments
  46. # * more error checks
  47. # * documentation (top of file, &usage)
  48. # * minor code clean ups
  49. # 0.1 (02/02/2001):
  50. # * Initial version
  51. # * Basic functionality
  52. #
  53. # Todo:
  54. # * test on more systems
  55. # * use portable way to determine the location of perl ('use Config')
  56. # * include option that shows perldiag messages for each error
  57. # * allow to pass in program by STDIN
  58. # * more intuitive behaviour if no error is found (show message)
  59. #
  60. # Tested under SunOS 5.7 with Perl 5.6.0. Let me know if it's not working for
  61. # you.
  62. use strict;
  63. use Getopt::Std;
  64. use vars qw/$opt_c $opt_f $opt_h/; # needed for Getopt in combination with use strict 'vars'
  65. use constant VERSION => 0.2;
  66. getopts('cf:h');
  67. &usage if $opt_h; # not necessarily needed, but good for further extension
  68. if (defined $opt_f) {
  69. open FILE, "> $opt_f" or do {
  70. warn "Couldn't open $opt_f: $!. Using STDOUT instead.\n";
  71. undef $opt_f;
  72. };
  73. };
  74. my $handle = (defined $opt_f ? \*FILE : \*STDOUT);
  75. (my $file = shift) or &usage; # display usage if no filename is supplied
  76. my $args = (@ARGV ? ' ' . join ' ', @ARGV : '');
  77. my @lines = `perl @{[defined $opt_c ? '-c ' : '' ]} -w "$file$args" 2>&1`;
  78. my $errors = 0;
  79. foreach my $line (@lines) {
  80. chomp($line);
  81. my ($file, $lineno, $message, $rest);
  82. if ($line =~ /^(.*)\sat\s(.*)\sline\s(\d+)(\.|,\snear\s\".*\")$/) {
  83. ($message, $file, $lineno, $rest) = ($1, $2, $3, $4);
  84. $errors++;
  85. $message .= $rest if ($rest =~ s/^,//);
  86. print $handle "$file:$lineno:$message\n";
  87. } else { next };
  88. }
  89. if (defined $opt_f) {
  90. my $msg;
  91. if ($errors == 1) {
  92. $msg = "There was 1 error.\n";
  93. } else {
  94. $msg = "There were $errors errors.\n";
  95. };
  96. print STDOUT $msg;
  97. close FILE;
  98. unlink $opt_f unless $errors;
  99. };
  100. sub usage {
  101. (local $0 = $0) =~ s/^.*\/([^\/]+)$/$1/; # remove path from name of program
  102. print<<EOT;
  103. Usage:
  104. $0 [-c] [-f <errorfile>] <programfile> [programargs]
  105. -c compile only, don't run (executes 'perl -wc')
  106. -f write errors to <errorfile>
  107. Examples:
  108. * At the command line:
  109. $0 program.pl
  110. Displays output on STDOUT.
  111. $0 -c -f errorfile program.pl
  112. Then run 'vim -q errorfile' to edit the errors with Vim.
  113. * In Vim:
  114. Edit in Vim (and save, if you don't have autowrite on), then
  115. type ':mak' or ':mak args' (args being the program arguments)
  116. to error check.
  117. EOT
  118. exit 0;
  119. };