Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.3 KiB

  1. #!/usr/bin/env perl
  2. # Execute shell commands and include their output in the current buffer.
  3. #
  4. # This is brittle and probably very subject to quoting problems, but in
  5. # practice it's been a useful hack for local notes and technical writing.
  6. #
  7. # See also:
  8. #
  9. # - filter-exec for formatted output including the original command
  10. #
  11. # - filter-exec-stdin for a version that passes the contained block to
  12. # stdin of the executed command
  13. use strict;
  14. use warnings;
  15. use 5.10.0;
  16. my $text;
  17. {
  18. # Line separator:
  19. local $/ = undef;
  20. $text = <>;
  21. }
  22. # HTML quote markers / commands:
  23. $text =~ s{
  24. <!--[ ]exec-raw[ ](.*?)[ ]-->
  25. .*?
  26. <!--[ ]end[ ]-->
  27. }{
  28. handle_block('<!-- ', $1, ' -->', '<!-- end -->');
  29. }egsx;
  30. # Vim/VimWiki-style markers:
  31. $text =~ s/
  32. [{]{3}exec-raw[ ](.*?)$
  33. .*?
  34. [}]{3}
  35. /
  36. handle_block('{{{', $1, '', '}}}');
  37. /egmsx;
  38. print $text;
  39. sub handle_block {
  40. my ($start, $command, $command_end, $end) = @_;
  41. my $result = `$command`;
  42. return "${start}exec-raw ${command}${command_end}\n${result}${end}";
  43. }
  44. __DATA__
  45. A self-test - use ,r in vim to check:
  46. {{{exec-raw date
  47. Thu 02 Apr 2020 11:49:22 AM MDT
  48. }}}
  49. <!-- exec-raw date -->
  50. Thu 02 Apr 2020 11:49:22 AM MDT
  51. <!-- end -->
  52. <!-- exec-raw pwd -->
  53. /home/brennen/notes
  54. <!-- end -->
  55. <!-- exec -->
  56. $ ls ~/bin/filter-exec-raw
  57. /home/brennen/bin/filter-exec-raw
  58. <!-- end -->