3 # $Id: autocomplete.pl 482 2008-01-12 23:07:32Z fletcher $
5 # Find possible autocompletions in a MultiMarkdown file
6 # ( Designed to be called from TextMate, but the concept can be used elsewhere. )
9 # Copyright (c) 2006-2008 Fletcher T. Penney
10 # <http://fletcherpenney.net/>
12 # MultiMarkdown Version 2.0.b5
16 $word_to_match = $ENV{'TM_CURRENT_WORD'};
19 $multimarkdown_file = $ENV{'TM_FILEPATH'};
22 open(MULTI, "<$multimarkdown_file");
24 $multi_source = <MULTI>;
26 if ( $word_to_match =~ s/^\^/\\^/) {
29 # the '^' makes things tricky
31 $multi_source =~ s/^(?:\[($word_to_match.+?)\]:)?.*$/$1/img;
32 $word_to_match =~ s/\\\^/^/g;
34 } elsif ($word_to_match =~ /^\#/) {
36 # Match MultiMarkdown Citations
38 $multi_source =~ s/^(?:\[($word_to_match.+?)\]:)?.*$/$1/img;
42 # Slurp any .bib files
43 open (BIB, "cd $ENV{'TM_DIRECTORY'}; cat *.bib |");
46 $word_only = $word_to_match;
49 ^(?:@.*?\{($word_only.+?)\,)?.*$
52 $match =~ s/(...)/#$1/;
55 $multi_source .= $bibtex;
59 # Match regular anchor (link or image)
64 ($word_to_match.+?) # Match Heading
67 (?:.*\[($word_to_match.+?)\]>>)? # Match Equation label
68 (?:\[.*?\]\[($word_to_match.+?)\])? # Match Table label (or at least a label
69 .*?$ # at the beginning of a line)
76 $multi_source =~ s/\n\s*\n/\n/gs;
78 # Fix case (TextMate won't autocomplete different cases)
79 if ($word_to_match =~ /^\^/) {
80 $multi_source =~ s/\^+$word_to_match/$word_to_match/igm;
82 $multi_source =~ s/^$word_to_match/$word_to_match/igm;