Numbering the code

hi to all ,, can some one tell me how to give the code numbers like this

  1. #define SigPin 7
  2. #define Node1Pin 6
  3. #define Node2Pin 8
  4. #define Transducer1 13
  5. #define Transducer2 9
  6. #define PacketPin 2
  7. #define LEDC 3
  8. #define LEDA 4
  9. int beaconNumber;
  10. void findBeaconNumber() {
  11. pinMode(SigPin,OUTPUT);
  12. pinMode(Node1Pin,INPUT);
  13. pinMode(Node2Pin,INPUT);
  14. digitalWrite(Node1Pin,HIGH); // activate pullup
  15. digitalWrite(Node2Pin,HIGH); // activate pullup
  16. digitalWrite(SigPin,LOW);

awk '{printf "%d.\t%s\n", NR, $0}' filename

Where filename is the name of the file you want to see line numbers on.

--- bill

Or in Arduinish style

//
// linenumbering Arduino 
// 
#define MAXLENGTH 80

void setup()
{
  Serial.begin(115200);
}

unsigned int lineNr = 0;
char buffer[100];
int idx = 0;

void loop()
{
  if (Serial.available() >0 )
  {
    char c = Serial.read();
    if (c == '\n')
    {
      printline();
    } 
    else 
    {
      buffer[idx++] = c;
      buffer[idx] = '\0';
      if (idx == MAXLENGTH ) 
      {
        printline();
        buffer[idx++] = '\t';
        buffer[idx] = '\0';
      }
    }
  }
}

void printline()
{
  Serial.print(lineNr);
  Serial.print(".\t");
  Serial.println(buffer);
  idx = 0;
  lineNr++;
}

bperrybap:
awk '{printf "%d.\t%s\n", NR, $0}' filename

Or even less typing...

awk '{ print NR,$0 }' filename

:slight_smile:

maniacbug:

bperrybap:
awk '{printf "%d.\t%s\n", NR, $0}' filename

Or even less typing...

awk '{ print NR,$0 }' filename

:slight_smile:

But that won't give you the period '.' just after the number as was requested in the OP.
If you don't need the period, you could also use

cat -n filename

which is even easier.

--- bill

sorry to dumb this thread down.. =(

but where is this done?

Is this an edit in the preference or something? (ie: this is an IDE wide edit/change?) or done in each 'sketch'?

All of these suggestions are done from the command line. The simplest one, "cat -n", is only available on Linux & Mac, as far as I know. The awk-based solutions work on all platforms. For windows it means downloading 'gawk' for GnuWin32.

thunderconan was not very clear on what was the real intent of the line numbers or what
OS he was using.
Without this information it is impossible to know what to recommend as a solution.
My suggestions were the easiest ways to get line numbers output from the existing source code module.
If you then need the output in a file you can then redirect the output to a file.

But if the intent was just to see the line numbers from the IDE, then the IDE already
does this. Look at the bottom left corner of the IDE window and it displays the current
line number where the cursor is located.

That said, the way the IDE builds the code, it screws up the line numbers reported by
the compiler when errors are detected.
Since the IDE chose to never correct this by telling the compiler about the proper line numbers
to offset the code it inserts in the users sketch when sketches are built,
(which is what breaks the reported line numbers), the line
numbers reported by the compiler are often incorrect and so the IDE will often
highlight the incorrect line when there are errors.

Yes, Windows is a very crippled platform when it comes to software development.
One lucky thing is that the Windows Arduino IDE package includes the WINAVR AVR development
package which includes a core set of unix tools.
These are located in {Installdir}\hardware\tools\avr\utils\bin
Down in that directory you will find
both cat.exe and gawk.exe as well as a slew of other very useful unix commands.

So if you want to run the examples above and you are stuck using a Windows environment,
you can run the cat.exe or gawk.exe from the Dos command prompt using the
commands in Arduino directory mentioned above.

--- bill

But if the intent was just to see the line numbers from the IDE, then the IDE already
does this. Look at the bottom left corner of the IDE window and it displays the current
line number where the cursor is located.

Holy cow - i did not know that!

Thank you, no more laborious counting & sticking in line # comments which are always wrong!

also did not know about gawk - thanks for that too.

just keep in mind that the line number the IDE reports is the real line number in the file
and won't necessarily match up with the reported line numbers reported by the
compiler on errors, for the reasons previously mentioned.

You can also configure the IDE to use an external editor, which is what I do, since
I want to use a real text editor vs live with the wimpy reduced editing capabilities of the IDE.
Most real editors have the ability to tell you what line you are one or to jump to a particular line number.

And if you want, you can add the Arduino {Installdir}\hardware\tools\avr\utils\bin
to your DOS/Windows path and will then have lots of the unix tools available from the command line.
make, date, sh, grep, gzip, gunzip, bc, find, etc... (lots more)

--- bill

You can copy your code, paste in a word processor like ms word or open office write, then select all, press numbered bullet point button. Then copy the entire thing and paste where you want with the numbers. No messing with *nux.

liudr:
You can copy your code, paste in a word processor like ms word or open office write, then select all, press numbered bullet point button. Then copy the entire thing and paste where you want with the numbers. No messing with *nux.

Isn’t it amazing that something so simple is so complicated to do in Windows? :astonished:

It would be much simpler to use a real text editor instead of the IDE and just turn on line numbers.
If you use “vi” or “vim” simply use the command
set number

and viola all lines will have their number on the left while you are editing the file.

I’m sure most decent text editors have this capability.
programmers notepad supports this:
http://www.pnotepad.org/
There are even scheme files to provide arduino keyword highlighting.
You can google around for them, but here is an old link to one from the arduino forum:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1194541276

— bill