953,971 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Code line numbers - causing paste problems

The new line numbers inserted when using syntax coloring code tags is causing me problems when copying and pasting code. Here are some examples:

http://www.daniweb.com/techtalkforums/post311344-32.html

I copy and paste the code to my editor. This is how it looks when it's selected in my browser:[INDENT][IMG]http://img522.imageshack.us/img522/7406/picture1ji6.png[/IMG]
[/INDENT]And this is how my code looks when it's in the editor:

1.
#include <stdio.h>
2.
#include <string.h>
3.

4.
int main(void) {
    5.
    6.
    char mystring[] = "this is a test.\n";
    7.
        char *p = strchr(mystring, '\n');
    8.
        if (p != NULL) *p = '\0';
    9.
        printf("%s no newlines should occur.\n", mystring);
    10.
        11.
}



Sometimes, instead of line numbers I get pound symbols (#) instead. I'm using Firefox 2.0.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

I don't have this problem at all. I tried copying/pasting from both Firefox and IE into Windows Notepad. Perhaps it's a problem with your editor?

In the meantime, a workaround would be to click the Reply w/ Quote button and then copy the post contents from the editor window.

Since the majority of the code posted within the forums is just for discussion purposes, I thought the line numbers would be a great aid towards talking about a particular piece of code. ie. "You have an error on line #5"

cscgal
The Queen of DaniWeb
Administrator
19,420 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 
Perhaps it's a problem with your editor?


Nah, before I was using Xcode, but using TextEdit didn't seem to make a difference. To me, it seems to be somewhat on the browser end of things.

Just tried Safari now - no difference.

I'll let you know how things are on Linux when I next boot onto Debian.In the meantime, a workaround would be to click the Reply w/ Quote button and then copy the post contents from the editor window. Thanks; it seems like I'll be needing to do this whenever I need to try out code in my compiler from now on.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

I have a mac as well so I am going to see if it works for me.

cscgal
The Queen of DaniWeb
Administrator
19,420 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 
Perhaps it's a problem with your editor?

Same issue with my editor. DevShed had a similar issue and a better workaround.

[imo]Even though the better solution was to leave it as it was.[/imo]

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Dave, are you in Linux?

cscgal
The Queen of DaniWeb
Administrator
19,420 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 
Dave, are you in Linux?

Nope.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Although it may first seem like a great idea...Since the majority of the code posted within the forums is just for discussion purposes, I thought the line numbers would be a great aid towards talking about a particular piece of code. ie. "You have an error on line #5" That actually happens about 0.5% of the time. IMO it's not worthwhile to inconvenience 99.5% of the rest.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

0.5% of the time you discuss the code snippets and 99.5% of the time you copy them into your editor and compile them? I suspect that's not the case with the vast majority of traffic to the programming forums.

cscgal
The Queen of DaniWeb
Administrator
19,420 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 
0.5% of the time you discuss the code snippets and 99.5% of the time you copy them into your editor and compile them? I suspect that's not the case with the vast majority of traffic to the programming forums.

I use colors and other tools available [when available] because that is far more effective than looking at a printout from 1972. So yes, that is the case.

BTW, I found this , FWIW.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Still investigating a solution. In the meantime, I've confirmed the problem is not with the web browser but rather with your editor. When using Firefox 2 and Windows Vista, I was able to copy/paste just fine into Notepad but I got line numbers when copy/pasting into Zend PHP Studio.

cscgal
The Queen of DaniWeb
Administrator
19,420 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

FireFox1.5 into VEdit -- line numbers.
FireFox1.5 into Notepad -- line numbers
Firefox1.5 into Wordpad -- line numbers

IE7 into VEdit -- one line, no formatting
IE7 into Notepad -- one line, no formatting
IE7 into Wordpad -- no formatting

Opera9 -- seems to work.

I see no reason for line numbers either. I doubt it's worth the trouble to track down the solution. And it would be too presumptuous to require a specific browser.

It would also be beneficial IMO to put back the label "code" and the lines above and below the code. Don't have to change the code format, just the head and tail.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,474 posts since May 2006
Reputation Points: 3,342
Solved Threads: 938
 

I went ahead and implemented the same workaround DevShed uses that Dave suggested. Of course it's not that elegant ... but let's see.

cscgal
The Queen of DaniWeb
Administrator
19,420 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

I like that change -- now we can turn line numbers on and off as we please. Hope you keep this feature. :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,038 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,340
 

Thanks Ancient!

cscgal
The Queen of DaniWeb
Administrator
19,420 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

I second that. It wasn't that the line numbers were bad, but if they interfere with my ease of copying and pasting, then it becomes more a nuisance. This solution is great. :)

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

I went ahead and implemented this in our code snippet library as well. Here is an example:

http://www.daniweb.com/code/snippet582.html

You'll notice that with the line numbers, when code is too long for the line, it very cleanly wraps around with the line number indicating it's just a wrap around. I think this is a lot nicer than the messy horizontal scrollbars, especially with very long snippets.

cscgal
The Queen of DaniWeb
Administrator
19,420 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 
I like that change -- now we can turn line numbers on and off as we please. Hope you keep this feature. :)


I think this will work. Can you tell how many line are in the code block? If so, could you please add the toggle to the top of the block also if the block is greater than, say, 30 lines? If not, just add the toggle to the top. Many times people post 2 or more (window) pages of code and if we see something we wish to comment on at the top, we have to scroll all the way down, then all the way back up. This would also delineate the block nicely.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,474 posts since May 2006
Reputation Points: 3,342
Solved Threads: 938
 

I actually went ahead and decided to allow the code to be as long as it needs to be. I found the scrollbars incredibly annoying. I prefer to read code the same way that I do everyday in my IDE ... by utilizing the full height of my monitor, as opposed to being forced to only see a couple of lines of code at a time. I think it has something to do with me not having a photographic memory.

cscgal
The Queen of DaniWeb
Administrator
19,420 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 
The new line numbers inserted when using syntax coloring code tags is causing me problems when copying and pasting code.


Click on the link "toggle plain text" which appears below the code snippet and then try copy-pasting. It works out to be fine.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 732
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You