Better copy-n-paste for source code in Trac browser

47 views
Skip to first unread message

benhoyt

unread,
Mar 21, 2008, 5:57:23 PM3/21/08
to Trac Users
Hi guys,

I just noticed that Google Code (and Google Code Search) display code
in such a way as to make copying and pasting source code really nice,
and this could be a great enhancement for the Trac source browser.

When you copy and paste code from the Trac source browser, it's a bit
of a hassle, because you get the line numbers selected as well, and
you always only want the code. Try it here and you'll see:
http://trac.edgewall.org/browser/trunk/cgi-bin/trac.cgi

However, Google Code [Search] generates the HTML in such a way that
when you copy and paste code, you only get the code -- the line
numbers are in a different table cell or something. Try it at these
two links:
http://google.decenturl.com/python-src-complexobject
http://code.google.decenturl.com/modwsgi-src-setup

When I do want to copy-n-paste code in Trac, I always find myself
scrolling to the bottom, clicking "Plain Text", finding my place again
in the plain text version, and *then* doing the copy-n-paste.

Any thoughts? Should I start a Trac ticket suggesting it? Or just
follow the open source rule and stop complaining until I'm ready to
implement it? :-)

I could also create a http://microPledge.com project for it -- I'd
pledge $20, for a start. :-)

Cheers,
Ben.

Matt Good

unread,
Mar 21, 2008, 10:01:25 PM3/21/08
to Trac Users
On Mar 21, 2:57 pm, benhoyt <benh...@gmail.com> wrote:
> I just noticed that Google Code (and Google Code Search) display code
> in such a way as to make copying and pasting source code really nice,
> and this could be a great enhancement for the Trac source browser.

This was considered a long time ago, but we opted in favor of making
the source browser handle line-wrapping. The two options seem to be
mutually exclusive. I've mentioned this trade-off to the Google Code
developers since people complained a lot more when the lines did not
wrap. You can always use the Plain Text link at the bottom to copy/
paste from.

-- Matt

benhoyt

unread,
Mar 21, 2008, 11:03:51 PM3/21/08
to Trac Users
> This was considered a long time ago, but we opted in
> favor of making the source browser handle line-wrapping.
> The two options seem to be mutually exclusive.

Gotcha. I must say I don't think line-wrapped source code is usually a
good idea, but I guess it's partly a matter of taste, and I can see
the advantages of Trac's way.

Thanks for the heads-up.

-Ben

Ted Gifford

unread,
Mar 24, 2008, 11:36:29 AM3/24/08
to trac-...@googlegroups.com

As a matter of taste, it would be nice to have it as a configuration item.
I might have some time in a month or two to look into the cost of
having two implementations.

Ted

benhoyt

unread,
Mar 24, 2008, 4:08:58 PM3/24/08
to Trac Users

> As a matter of taste, it would be nice to have it as a configuration item.
> I might have some time in a month or two to look into the cost of
> having two implementations.

Good idea -- thanks.

Oh, and I had another thought about why it might be more than a matter
of taste. Because, at least speaking for myself, I copy/paste code
much more often than I view line-wrapped code. Just my two cents. :-)

Cheers,
Ben.

Noah Kantrowitz

unread,
Mar 24, 2008, 6:06:01 PM3/24/08
to trac-...@googlegroups.com
A better idea is probably have a javascript button that hides the line
numbers.

--Noah

benhoyt

unread,
Mar 24, 2008, 7:42:27 PM3/24/08
to Trac Users

> A better idea is probably have a javascript button that hides the line
> numbers.

Aha -- excellent idea! No config option, no fancy server-side code,
just a simple <a> tag with a 15-line JavaScript function called by the
onclick event. You could put the "toggle line numbers" link nicely in
the table cell to the right of the "Line" heading cell.

(It'd have to actually remove and put back in the <th> elements, I
think, because at least in Firefox, if you tell them "display: none",
they'll still be copied in your selection.)

Cheers,
Ben.

Ted Gifford

unread,
Mar 25, 2008, 11:25:59 AM3/25/08
to trac-...@googlegroups.com
benhoyt wrote:
> Aha -- excellent idea! No config option, no fancy server-side code,
> just a simple <a> tag with a 15-line JavaScript function called by the
> onclick event. You could put the "toggle line numbers" link nicely in
> the table cell to the right of the "Line" heading cell.
>
It turns out to be two two-line functions (excuse the poor namespace
control):

var a,b;
function removeLineNumbers()
{
a = jQuery('#preview table.code th.lineno').remove();
b = jQuery('#preview table.code tbody th').remove();
}
function restoreLineNumbers()
{
var rows = jQuery('#preview table.code tbody tr'); // cache
value--doesn't count as line of code ; )
jQuery('#preview table.code thead tr').prepend(a[0]);
b.each(function(i){rows.eq(i).prepend(b[i]);});
}

benhoyt

unread,
Mar 26, 2008, 7:18:01 PM3/26/08
to Trac Users
So, should we add it as a Trac enhancement ticket? -Ben

> It turns out to be two two-line functions (excuse the poor namespace
> control):
>
> var a,b;
> function removeLineNumbers()
> {
> a = jQuery('#preview table.code th.lineno').remove();
> b = jQuery('#preview table.code tbody th').remove();}
>
> function restoreLineNumbers()
> {
> ...

Noah Kantrowitz

unread,
Mar 26, 2008, 11:24:19 PM3/26/08
to trac-...@googlegroups.com
benhoyt wrote:
> So, should we add it as a Trac enhancement ticket? -Ben

Please do, prevents things from getting lost.

--Noah

signature.asc

benhoyt

unread,
Mar 27, 2008, 4:56:07 PM3/27/08
to Trac Users

> > So, should we add it as a Trac enhancement ticket? -Ben
>
> Please do, prevents things from getting lost.

Done. Ticket 7055: 'Add "toggle line numbers" JavaScript to source
browser':
http://trac.edgewall.org/ticket/7055

If anyone here is more involved with Trac or an admin, I wasn't sure
about the milestone and version settings, so please update to
suit. :-)

Cheers!
Ben.

rupert thurner

unread,
Mar 27, 2008, 8:52:41 PM3/27/08
to Trac Users
which file should contain this code?
> }- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -
Reply all
Reply to author
Forward
0 new messages