| 1 | package Padre::Plugin::LaTeX; | ||
| 2 | BEGIN { | ||
| 3 | $Padre::Plugin::LaTeX::VERSION = '0.13'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: LaTeX support for Padre | ||
| 7 | |||
| 8 | use warnings; | ||
| 9 | use strict; | ||
| 10 | |||
| 11 | use File::Spec::Functions qw{ catfile }; | ||
| 12 | |||
| 13 | use base 'Padre::Plugin'; | ||
| 14 | use Padre::Wx (); | ||
| 15 | |||
| 16 | sub plugin_name { | ||
| 17 | Wx::gettext('LaTeX'); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub padre_interfaces { | ||
| 21 | 'Padre::Plugin' => 0.89, | ||
| 22 | 'Padre::Document' => 0.89, | ||
| 23 | 'Padre::Wx::Main' => 0.89; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub registered_documents { | ||
| 27 | 'application/x-latex' => 'Padre::Document::LaTeX', | ||
| 28 | 'application/x-bibtex' => 'Padre::Document::BibTeX', | ||
| 29 | ; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub plugin_icon { | ||
| 33 | my $self = shift; | ||
| 34 | |||
| 35 | # find resource path | ||
| 36 | my $iconpath = catfile( $self->plugin_directory_share, 'icons', 'text-x-tex.png' ); | ||
| 37 | |||
| 38 | # create and return icon | ||
| 39 | return Wx::Bitmap->new( $iconpath, Wx::wxBITMAP_TYPE_PNG ); | ||
| 40 | |||
| 41 | # TODO: simplify | ||
| 42 | } | ||
| 43 | |||
| 44 | sub menu_plugins_simple { | ||
| 45 | my $self = shift; | ||
| 46 | return $self->plugin_name => [ | ||
| 47 | Wx::gettext('Create/Update PDF') => sub { $self->create_pdf }, | ||
| 48 | Wx::gettext('View PDF') => sub { $self->view_pdf }, | ||
| 49 | Wx::gettext('Run BibTeX') => sub { $self->run_bibtex }, | ||
| 50 | '---' => undef, | ||
| 51 | Wx::gettext('Find Symbol/Special Character') | ||
| 52 | => sub { Padre::Wx::launch_browser('http://detexify.kirelabs.org') }, | ||
| 53 | '---' => undef, | ||
| 54 | Wx::gettext('About') => sub { $self->show_about }, | ||
| 55 | ]; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub plugin_disable { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | if ( $self->{about_box} ) { | ||
| 62 | $self->{about_box}->Destroy; | ||
| 63 | $self->{about_box} = undef; | ||
| 64 | } | ||
| 65 | |||
| 66 | #require Class::Unload; | ||
| 67 | #Class::Unload->unload('Padre::Document::LaTeX'); | ||
| 68 | #Class::Unload->unload('Padre::Document::BibTeX'); | ||
| 69 | |||
| 70 | return 1; | ||
| 71 | } | ||
| 72 | |||
| 73 | ##################################################################### | ||
| 74 | # Custom Methods | ||
| 75 | |||
| 76 | sub show_about { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | # Generate the About dialog | ||
| 80 | my $self->{about} = Wx::AboutDialogInfo->new; | ||
| 81 | $self->{about}->SetName( Wx::gettext('LaTeX Plug-in') ); | ||
| 82 | my $authors = 'Zeno Gantner, Ahmad M. Zawawi'; | ||
| 83 | my $description = Wx::gettext( <<'END' ); | ||
| 84 | LaTeX support for Padre | ||
| 85 | |||
| 86 | For syntax highlighting of BibTeX files install the Kate plugin: Padre::Plugin::Kate | ||
| 87 | |||
| 88 | Copyright 2010, 2011 %s | ||
| 89 | This plug-in is free software; you can redistribute it and/or modify it under the same terms as Padre. | ||
| 90 | END | ||
| 91 | $self->{about}->SetDescription( sprintf( $description, $authors ) ); | ||
| 92 | |||
| 93 | # Show the About dialog | ||
| 94 | Wx::AboutBox($self->{about}); | ||
| 95 | |||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub create_pdf { | ||
| 100 | my $self = shift; | ||
| 101 | |||
| 102 | my $pdflatex = 'pdflatex -interaction nonstopmode -file-line-error'; | ||
| 103 | |||
| 104 | my $main = $self->main; | ||
| 105 | my $doc = $main->current->document; | ||
| 106 | my $tex_dir = $doc->dirname; | ||
| 107 | my $tex_file = $doc->get_title; | ||
| 108 | |||
| 109 | if ( !$doc->isa('Padre::Document::LaTeX') ) { | ||
| 110 | $main->message( Wx::gettext('Creating PDF files is only supported for LaTeX documents.') ); | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | |||
| 114 | # TODO autosave or ask or use temporary file | ||
| 115 | |||
| 116 | chdir $tex_dir; | ||
| 117 | my $output_text = `$pdflatex $tex_file`; | ||
| 118 | $self->_output($output_text); | ||
| 119 | |||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub run_bibtex { | ||
| 124 | my $self = shift; | ||
| 125 | |||
| 126 | my $bibtex = 'bibtex'; | ||
| 127 | |||
| 128 | my $main = $self->main; | ||
| 129 | my $doc = $main->current->document; | ||
| 130 | |||
| 131 | my $tex_dir = $doc->dirname; | ||
| 132 | my $aux_file = $doc->filename; | ||
| 133 | $aux_file =~ s/\.tex/.aux/; | ||
| 134 | |||
| 135 | if ( !$doc->isa('Padre::Document::LaTeX') ) { | ||
| 136 | $main->message( Wx::gettext('Running BibTeX is only supported for LaTeX documents.') ); | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | # TODO autosave (or ask) | ||
| 141 | |||
| 142 | chdir $tex_dir; # TODO does this have side effects? | ||
| 143 | my $output_text = `$bibtex $aux_file`; | ||
| 144 | $self->_output($output_text); | ||
| 145 | |||
| 146 | return; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub view_pdf { | ||
| 150 | my $self = shift; | ||
| 151 | |||
| 152 | my $main = $self->main; | ||
| 153 | my $doc = $main->current->document; | ||
| 154 | |||
| 155 | if ( !$doc->isa('Padre::Document::LaTeX') ) { | ||
| 156 | $main->message( Wx::gettext('Viewing PDF files is only supported for LaTeX documents.') ); | ||
| 157 | return; | ||
| 158 | } | ||
| 159 | |||
| 160 | my $pdf_file = $doc->filename; | ||
| 161 | $pdf_file =~ s/\.tex$/.pdf/; | ||
| 162 | |||
| 163 | if ( !-f $pdf_file ) { | ||
| 164 | main->error( sprintf( Wx::gettext("Could not find file '%s'."), $pdf_file ) ); | ||
| 165 | } | ||
| 166 | |||
| 167 | $self->launch_pdf_viewer($pdf_file); | ||
| 168 | |||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | sub launch_pdf_viewer { | ||
| 174 | my $self = shift; | ||
| 175 | my $pdf_file = shift; | ||
| 176 | |||
| 177 | # TODO find PDF viewer from system settings like Debian alternatives | ||
| 178 | # TODO get PDF viewer from configuration | ||
| 179 | |||
| 180 | require File::Which; | ||
| 181 | my @pdf_viewers = qw/evince okular xpdf gv acroread/; | ||
| 182 | my $pdf_viewer = ''; | ||
| 183 | foreach my $program (@pdf_viewers) { | ||
| 184 | last if defined( $pdf_viewer = File::Which::which($program) ); | ||
| 185 | } | ||
| 186 | |||
| 187 | system "$pdf_viewer $pdf_file &"; | ||
| 188 | |||
| 189 | return; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub editor_enable { | ||
| 193 | my $self = shift; | ||
| 194 | my $editor = shift; | ||
| 195 | my $document = shift; | ||
| 196 | |||
| 197 | if ( $document->isa('Padre::Document::LaTeX') ) { | ||
| 198 | |||
| 199 | # TODO | ||
| 200 | } | ||
| 201 | |||
| 202 | return 1; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub _output { | ||
| 206 | my ( $self, $text ) = @_; | ||
| 207 | my $main = $self->main; | ||
| 208 | |||
| 209 | $main->show_output(1); | ||
| 210 | $main->output->clear; | ||
| 211 | $main->output->AppendText($text); | ||
| 212 | |||
| 213 | return; | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | 1; | ||
| 218 | |||
| 219 | |||
| 220 | =pod | ||
| 221 | |||
| 222 | =head1 NAME | ||
| 223 | |||
| 224 | Padre::Plugin::LaTeX - LaTeX support for Padre | ||
| 225 | |||
| 226 | =head1 VERSION | ||
| 227 | |||
| 228 | version 0.13 | ||
| 229 | |||
| 230 | =head1 DESCRIPTION | ||
| 231 | |||
| 232 | LaTeX support for Padre, the Perl Application Development and Refactoring | ||
| 233 | Environment. | ||
| 234 | |||
| 235 | Syntax highlighting for LaTeX is supported by Padre out of the box. | ||
| 236 | This plug-in adds some more features to deal with LaTeX files. | ||
| 237 | If you also want syntax highlighting for BibTeX files, install the Kate | ||
| 238 | plugin. | ||
| 239 | |||
| 240 | =head1 AUTHORS | ||
| 241 | |||
| 242 | =over 4 | ||
| 243 | |||
| 244 | =item * | ||
| 245 | |||
| 246 | Zeno Gantner <zenog@cpan.org> | ||
| 247 | |||
| 248 | =item * | ||
| 249 | |||
| 250 | Ahmad M. Zawawi <ahmad.zawawi@gmail.com> | ||
| 251 | |||
| 252 | =back | ||
| 253 | |||
| 254 | =head1 COPYRIGHT AND LICENSE | ||
| 255 | |||
| 256 | This software is copyright (c) 2011 by Zeno Gantner, Ahmad M. Zawawi. | ||
| 257 | |||
| 258 | This is free software; you can redistribute it and/or modify it under | ||
| 259 | the same terms as the Perl 5 programming language system itself. | ||
| 260 | |||
| 261 | =cut | ||
| 262 | |||
| 263 | |||
| 264 | __END__ |