# [TxMt] idea: a better label completion technique

Piero D\'Ancona pierodancona at gmail.com
Sun Sep 24 10:54:21 UTC 2006

Hi list

I am using TextMate full time to write Latex and it's great,
and improving.
insert \refs: too slow and clumsy. I (almost) can stand it
for bibliography completion but \refs are too many, too
similar to each other, and too frequently needed, so the dialog
interrupts my workflow. So I started thinking and tinkering,
but my coding skills are too poor (I also sent a msg to this
list asking for help). Finally I think it is better to publish
the idea and let the code wizards code it, if they like it.
My feeling is that this could be the optimal way to insert
references, even better than auctex's.

The idea is very simple: parse the tex file, write
all labels (plus some surrounding text) into an auxiliary
.lbl file, convert this into html, and replace each
\label{...} command with an "a href" to a tiny javascript
command that tells TextMate to insert the label's name
into the source file. Finally, output to HTML. (Improvement:
just search the labels matching a few characters, as it
is now).

Do you see it? This produces an HTML window, with a list of
all my labels, followed by the beginning lines of the
equations (or section titles). More important, each
label is clickable, and when I click on it, the reference
gets inserted into the source file. I can keep the label window
open, so no need to invoke the command over and over.
When I add more equations, I just refresh the window
as necessary.

But I got stuck with some stupid coding problems.

1- Easy to grep the tex file for lines containing a \label
command plus some context. E.g. the following command
outputs line number, plus the \label line, plus two nonempty lines
following it:

sed -n -e "/\\label/{=;N;N;G;p;}" | sed '/^$/d' | sed 's/^$$[0-9]+$$$/\n\1/'

2- Easy to save all this info into a file fileName.lbl and
do some rudimentary html-ization (just add <p></p>'s
to separate the labels from each other- you can do much
better I know, but this is fast)

3- Not so difficult to replace each "\label{...}"  with the
javascript link. But I got stuck when writing the osascript.
Let me explain. Is is easy to create a link such that, when
I click on it, TeXniscope is activated (e.g.):

<a href="javascript:TextMate.system('open -a TeXniscope', null);">
TeXniscope</a>

Nice! So I said, I'll just insert an osascript telling TextMate to
insert the label reference:

<a href="javascript:TextMate.system("/ust/bin/osascript -e
'tell app \"TextMate\" to insert .......'", null);">\labe{...}</a>

(of course, "....." must be replaced in both places with the \label's
name, using some bash, and some string must be joined etc etc)
This doesn't work. Why?? even the simple

<a href="javascript:TextMate.system("/ust/bin/osascript -e
'tell app \"Finder\" to activate'", null);">Finder</a>

does not work. This must be some escaping trickery.
Could some pious wizard explain what's
wrong? and even better, like the idea so much to code it?
I do not think it should be hard (for you :).

Regards,
Piero