| 1 | package Padre::Document::LaTeX; | ||
| 2 | BEGIN { | ||
| 3 | $Padre::Document::LaTeX::VERSION = '0.13'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: LaTeX document support for Padre | ||
| 7 | |||
| 8 | use 5.008; | ||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Padre::Document (); | ||
| 12 | |||
| 13 | our @ISA = 'Padre::Document'; | ||
| 14 | |||
| 15 | sub task_functions { | ||
| 16 | return ''; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub task_outline { | ||
| 20 | return 'Padre::Document::LaTeX::Outline'; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub task_syntax { | ||
| 24 | return 'Padre::Document::LaTeX::Syntax'; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub get_command { | ||
| 28 | my $self = shift; | ||
| 29 | |||
| 30 | my $arg_ref = shift || {}; | ||
| 31 | |||
| 32 | my $debug = exists $arg_ref->{debug} ? $arg_ref->{debug} : 0; | ||
| 33 | my $trace = exists $arg_ref->{trace} ? $arg_ref->{trace} : 0; | ||
| 34 | |||
| 35 | my $filename = $self->filename; | ||
| 36 | my $dir = File::Basename::dirname($filename); | ||
| 37 | chdir $dir; | ||
| 38 | $filename = $self->get_title; | ||
| 39 | |||
| 40 | my $command = "pdflatex -interaction nonstopmode -file-line-error $filename"; | ||
| 41 | return $command; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub comment_lines_str { | ||
| 45 | return '%'; | ||
| 46 | } | ||
| 47 | |||
| 48 | my @latex_star_commands = qw/ | ||
| 49 | chapter paragraph section subsection subsubsection | ||
| 50 | /; | ||
| 51 | |||
| 52 | my @latex_commands = qw/ | ||
| 53 | begin end | ||
| 54 | |||
| 55 | DeclareMathOperator pdfpageheight pdfpagewidth RequirePackage usepackage | ||
| 56 | |||
| 57 | addtocounter appendix author | ||
| 58 | bibliography bibliographystyle | ||
| 59 | caption cite | ||
| 60 | date documentclass dots | ||
| 61 | footnote | ||
| 62 | hline href hspace | ||
| 63 | include includegraphics insert institute item | ||
| 64 | label LaTeX | ||
| 65 | maketitle | ||
| 66 | newcommand newpage | ||
| 67 | pagebreak pagestyle | ||
| 68 | raggedleft raggedright ref renewcommand | ||
| 69 | subtitle | ||
| 70 | tableofcontents textbar textbf textcolor textgreater textit textless textsc | ||
| 71 | TeX texttt thepage title titlegraphic today | ||
| 72 | url | ||
| 73 | vspace | ||
| 74 | |||
| 75 | alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi omicron pi rho sigma tau upsilon phi chi psi omega | ||
| 76 | varepsilon vartheta varpi varrho varsigma varphi | ||
| 77 | Alpha Beta Gamma Delta Epsilon Zeta Eta Theta Iota Kappa Lambda Mu Nu Xi Omicron Pi Rho Sigma Tau Upsilon Phi Chi Psi Omega | ||
| 78 | amalg approx ast asymp | ||
| 79 | bigcirc bigcup bigtriangledown bigtriangleup bowtie bullet | ||
| 80 | cap cdot cdotp circ colon cong cup | ||
| 81 | dagger dashv ddagger diamond div doteq downarrow Downarrow | ||
| 82 | equiv exists | ||
| 83 | forall frac frown | ||
| 84 | ge geq | ||
| 85 | hat hookleftarrow hookrightarrow | ||
| 86 | in | ||
| 87 | Join | ||
| 88 | langle ldotp le leadsto left leftarrow Leftarrow leftharpoondown leftharpoonup leftrightarrow Leftrightarrow leq lhd ll | ||
| 89 | longleftarrow Longleftarrow longleftrightarrow Longleftrightarrow longmapsto longrightarrow Longrightarrow | ||
| 90 | mapsto mathbb mathcal mathrm mid models mp | ||
| 91 | nearrow neq ni nwarrow | ||
| 92 | odot ominus oplus oslash | ||
| 93 | parallel partial perp pm prec preceq propto | ||
| 94 | rangle rhd right rightarrow Rightarrow rightharpoondown rightharpoonup rightleftarrow Rightleftarrow rightleftharpoons | ||
| 95 | searrow seteq setminus sim simeq smile sqcap sqcup sqsubset sqsubseteq sqsupset sqsupseteq star subset subseteq substack | ||
| 96 | succ succeq sum supset supseteq swarrow | ||
| 97 | text tilde times triangleleft triangleright | ||
| 98 | unlhd unrhd uparrow Uparrow updownarrow Updownarrow uplus | ||
| 99 | vdash vee | ||
| 100 | wedge wr | ||
| 101 | |||
| 102 | bigskip DeclareOptionBeamer defbeamertemplate frame framesubtitle frametitle mode note | ||
| 103 | only pause ProcessOptionsBeamer | ||
| 104 | setbeamercolor setbeamersize setbeameroption setbeamertemplate usebeamerfont usetheme | ||
| 105 | |||
| 106 | fancyhead fancyfoot headheight headrulewidth footrulewidth | ||
| 107 | |||
| 108 | acro ac acs acf acl | ||
| 109 | |||
| 110 | For ForAll Procedure Repeat State Until While | ||
| 111 | /; | ||
| 112 | push @latex_commands, @latex_star_commands; | ||
| 113 | |||
| 114 | my @latex_star_environments = qw/ | ||
| 115 | equation eqnarray figure table | ||
| 116 | /; | ||
| 117 | |||
| 118 | my @latex_environments = qw/ | ||
| 119 | abstract acronym align | ||
| 120 | cases center | ||
| 121 | document | ||
| 122 | enumerate | ||
| 123 | flushleft flushright footnotesize | ||
| 124 | Huge | ||
| 125 | itemize Large | ||
| 126 | math | ||
| 127 | pmatrix | ||
| 128 | small | ||
| 129 | tabular tiny | ||
| 130 | verbatim | ||
| 131 | |||
| 132 | algorithm algorithmic | ||
| 133 | |||
| 134 | column columns | ||
| 135 | |||
| 136 | beamercolorbox block example frame overlayarea overprint proof theorem | ||
| 137 | /; | ||
| 138 | push @latex_environments, @latex_star_environments; | ||
| 139 | |||
| 140 | my @latex_packages = qw/ | ||
| 141 | a4wide acronym alg algorithm2e algorithmicx algpseudocode amsfonts amsmath amsopn amssymb | ||
| 142 | babel beamer | ||
| 143 | cite color colortbl | ||
| 144 | dcolumn | ||
| 145 | fancybox fontenc | ||
| 146 | graphics graphicx | ||
| 147 | hyperref | ||
| 148 | ifthen import inputenc | ||
| 149 | lastpage latexsym listings longtable | ||
| 150 | makeidx multicol multirow | ||
| 151 | pgf | ||
| 152 | tabularx times | ||
| 153 | url | ||
| 154 | verbatim | ||
| 155 | xcolor xy | ||
| 156 | /; | ||
| 157 | |||
| 158 | # method copied from the PHP plugin and a adapted a bit | ||
| 159 | # TODO know includegraphics etc. options (see CSS completion support for ideas) | ||
| 160 | # TODO units for height, width, vspace, etc. | ||
| 161 | # TODO for bibliography, insert, include, includegraphics, usepackage: check for available files ... | ||
| 162 | # TODO also offer \begin w/o \end | ||
| 163 | sub autocomplete { | ||
| 164 | my $self = shift; | ||
| 165 | my $event = shift; | ||
| 166 | |||
| 167 | my $config = Padre->ide->config; | ||
| 168 | my $min_chars = $config->lang_perl5_autocomplete_min_chars; # TODO rename this config option/have our own? | ||
| 169 | |||
| 170 | my $editor = $self->editor; | ||
| 171 | my $pos = $editor->GetCurrentPos; | ||
| 172 | my $line = $editor->LineFromPosition($pos); | ||
| 173 | my $first = $editor->PositionFromLine($line); | ||
| 174 | |||
| 175 | # This function is called very often, return asap | ||
| 176 | return if ( $pos - $first ) < ( $min_chars - 1 ); | ||
| 177 | |||
| 178 | # line from beginning to current position | ||
| 179 | my $prefix = $editor->GetTextRange( $first, $pos ); | ||
| 180 | |||
| 181 | # Remove any ident from the beginning of the prefix | ||
| 182 | $prefix =~ s/^[\r\t]+//; | ||
| 183 | return if length($prefix) == 0; | ||
| 184 | |||
| 185 | # One char may be added by the current event | ||
| 186 | return if length($prefix) < ( $min_chars - 1 ); | ||
| 187 | |||
| 188 | # The second parameter may be a reference to the current event or the next | ||
| 189 | # char which will be added to the editor: | ||
| 190 | my $nextchar = ''; # Use empty instead of undef | ||
| 191 | if ( defined($event) and ( ref($event) eq 'Wx::KeyEvent' ) ) { | ||
| 192 | my $key = $event->GetUnicodeKey; | ||
| 193 | $nextchar = chr($key); | ||
| 194 | } elsif ( defined($event) and ( !ref($event) ) ) { | ||
| 195 | $nextchar = $event; | ||
| 196 | } | ||
| 197 | return if ord($nextchar) == 27; # Close on escape | ||
| 198 | $nextchar = '' if ord($nextchar) < 32; | ||
| 199 | |||
| 200 | # abort as soon as possible | ||
| 201 | return if $prefix !~ /\\(\w+.*)$/; | ||
| 202 | $prefix = $1; | ||
| 203 | |||
| 204 | # check for environments | ||
| 205 | if ( $prefix =~ /(begin|end)(?:(\{)(\w*))?$/ ) { # TODO use extended regex | ||
| 206 | my $begin_or_end = $1; | ||
| 207 | my $add_bracket = defined $2 ? '' : '{'; | ||
| 208 | my $env_prefix = defined $3 ? $3 : ''; | ||
| 209 | |||
| 210 | # TODO end the currently open environment | ||
| 211 | |||
| 212 | my @candidates = $begin_or_end eq 'begin' | ||
| 213 | ? map { $add_bracket . $_ . "}\n\\end{$_}", $add_bracket . $_ . '}' } @latex_environments # TODO make configurable | ||
| 214 | : map { $add_bracket . $_ . '}' } @latex_environments; | ||
| 215 | return $self->find_completions( $env_prefix, $nextchar, @candidates ); | ||
| 216 | } | ||
| 217 | |||
| 218 | # check for packages | ||
| 219 | if ( $prefix =~ /usepackage(?:(\{)(\w*))?$/ ) { | ||
| 220 | my $add_bracket = defined $1 ? '' : '{'; | ||
| 221 | my $use_prefix = defined $2 ? $2 : ''; | ||
| 222 | |||
| 223 | my @candidates = map { $add_bracket . $_ . '}' } @latex_packages; | ||
| 224 | return $self->find_completions( $use_prefix, $nextchar, @candidates ); | ||
| 225 | } | ||
| 226 | |||
| 227 | # check for citations | ||
| 228 | if ( $prefix =~ /cite\w?(?:(\{)(\w*))?$/ ) { | ||
| 229 | my $add_bracket = defined $1 ? '' : '{'; | ||
| 230 | my $cite_prefix = defined $2 ? $2 : ''; | ||
| 231 | |||
| 232 | # search entire document for citations | ||
| 233 | my $text = $editor->GetText; | ||
| 234 | my @citations = (); | ||
| 235 | while ( $text =~ /cite\{([\w:]+)\}/g ) { | ||
| 236 | push @citations, $add_bracket . $1 . '}'; | ||
| 237 | } | ||
| 238 | |||
| 239 | # TODO look at the bibliography files | ||
| 240 | return $self->find_completions( $cite_prefix, $nextchar, @citations ); | ||
| 241 | } | ||
| 242 | |||
| 243 | # check for internal references | ||
| 244 | if ( $prefix =~ /ref(?:(\{)(\w*))?$/ ) { | ||
| 245 | my $add_bracket = defined $1 ? '' : '{'; | ||
| 246 | my $ref_prefix = defined $2 ? $2 : ''; | ||
| 247 | |||
| 248 | # search entire document for labels and references | ||
| 249 | my $text = $editor->GetText; | ||
| 250 | my @references = (); | ||
| 251 | while ( $text =~ /(?:label|ref)\{([\w:]+)\}/g ) { | ||
| 252 | push @references, $add_bracket . $1 . '}'; | ||
| 253 | } | ||
| 254 | |||
| 255 | return $self->find_completions( $ref_prefix, $nextchar, @references ); | ||
| 256 | } | ||
| 257 | |||
| 258 | # check for commands | ||
| 259 | return if $prefix !~ /(\w+)$/; | ||
| 260 | $prefix = $1; | ||
| 261 | |||
| 262 | # TODO distinguish math mode | ||
| 263 | # TODO check which packages are included? | ||
| 264 | return $self->find_completions( $prefix, $nextchar, '', @latex_commands ); | ||
| 265 | } | ||
| 266 | |||
| 267 | # TODO find a nicer name | ||
| 268 | # TODO think about how to handle post_text and pre_text (maybe use a switch?) | ||
| 269 | # TODO move to a place where all Padre document classes can use this ... | ||
| 270 | sub find_completions { | ||
| 271 | my $self = shift; | ||
| 272 | my $prefix = shift; | ||
| 273 | my $nextchar = shift; | ||
| 274 | my @candidates = @_; | ||
| 275 | |||
| 276 | my $editor = $self->editor; | ||
| 277 | my $pos = $editor->GetCurrentPos; | ||
| 278 | my $line = $editor->LineFromPosition($pos); | ||
| 279 | my $first = $editor->PositionFromLine($line); | ||
| 280 | |||
| 281 | my $last = $editor->GetLength(); | ||
| 282 | my $pre_text = $editor->GetTextRange( 0, $first + length($prefix) ); | ||
| 283 | my $post_text = $editor->GetTextRange( $first, $last ); | ||
| 284 | |||
| 285 | my $regex; | ||
| 286 | eval { $regex = qr{\b(\Q$prefix\E\w*)\b} }; | ||
| 287 | if ($@) { | ||
| 288 | warn "Cannot build regex for '$prefix'\n"; | ||
| 289 | return; | ||
| 290 | } | ||
| 291 | |||
| 292 | my %seen; | ||
| 293 | my @words; | ||
| 294 | push @words, grep { $_ =~ $regex and !$seen{$_}++ } @candidates; | ||
| 295 | push @words, grep { !$seen{$_}++ } reverse( $pre_text =~ /$regex/g ); | ||
| 296 | push @words, grep { !$seen{$_}++ } ( $post_text =~ /$regex/g ); | ||
| 297 | |||
| 298 | # TODO is 20 a good limit? | ||
| 299 | # TODO configurable? | ||
| 300 | if ( scalar @words > 20 ) { | ||
| 301 | @words = @words[ 0 .. 19 ]; | ||
| 302 | } | ||
| 303 | |||
| 304 | # Suggesting the current word as the only solution doesn't help | ||
| 305 | # anything, but your need to close the suggestions window before | ||
| 306 | # you may press ENTER/RETURN. | ||
| 307 | if ( ( $#words == 0 ) and ( $prefix eq $words[0] ) ) { | ||
| 308 | return; | ||
| 309 | } | ||
| 310 | |||
| 311 | my $suffix = $editor->GetTextRange( $pos, $pos + 15 ); | ||
| 312 | $suffix = $1 if $suffix =~ /^(\w*)/; # Cut away any non-word chars | ||
| 313 | |||
| 314 | # While typing within a word, the rest of the word shouldn't be | ||
| 315 | # inserted. | ||
| 316 | if ( defined($suffix) ) { | ||
| 317 | for ( 0 .. $#words ) { | ||
| 318 | $words[$_] =~ s/\Q$suffix\E$//; | ||
| 319 | } | ||
| 320 | } # TODO check this | ||
| 321 | |||
| 322 | # This is the final result if there is no char which hasn't been | ||
| 323 | # saved to the editor buffer until now | ||
| 324 | return ( length($prefix), @words ) if !defined($nextchar); | ||
| 325 | |||
| 326 | # Finally cut out all words which do not match the next char | ||
| 327 | # which will be inserted into the editor (by the current event) | ||
| 328 | my @final_words; | ||
| 329 | for (@words) { | ||
| 330 | |||
| 331 | # Accept everything which has prefix + next char + at least one other char | ||
| 332 | next if !/^\Q$prefix$nextchar\E./; | ||
| 333 | push @final_words, $_; | ||
| 334 | } | ||
| 335 | |||
| 336 | return ( length($prefix), @final_words ); | ||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | 1; | ||
| 341 | |||
| 342 | __END__ |