LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2016, 12:44 PM   #1
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311
Blog Entries: 1

Rep: Reputation: 59
Usingi Indent


I write a lot of C code. I almost always use indent to reformat my code, using the -gnu and some tweaks.

Does anyone know of a simple way to extend the gnu indent to put blank lines around do while and while loops. Here is one code fragment that I have formatted with indent. Notice that the loops with while are cuddled.
Code:
    
    pivot = first;
    pivotHash = d->hash[pivot];
    i = first;
    j = last;
    while (i < j)
    {
      /* for items less than pivot, move index to right */
      while (d->hash[i] <= pivotHash && i < last)
        i++;
      /* if the items are greater than pivot
         working from array end
       */
      while (d->hash[j] > pivotHash)
        j--;
      if (i < j)
        doSwap (d, i, j);
    }
I would like to have an indent option to yield

Code:
   
    pivot = first;
    pivotHash = d->hash[pivot];
    i = first;
    j = last;

    while (i < j)
    {
      /* for items less than pivot, move index to right */

      while (d->hash[i] <= pivotHash && i < last)
        i++;

      /* if the items are greater than pivot
         working from array end
       */

      while (d->hash[j] > pivotHash)
        j--;

      if (i < j)
        doSwap (d, i, j);
    }
When the while loop is a few lines, as above, it matters not, but if the while loop has 25+ lines, it helps to have a blank line before and after as visual clue to where the while starts
and ends.

I am more keen to see the blank line above and after if there are braces as single while statements are easy to follow. Consider

while ( ) {

}

or

while ( )
{

}

It would be great if that same logic can apply the same rule to
Code:
do {
  blah blah
} while (dddd);
x=y;
or
Code:

do 
{
  blah blah
} 
while (dddd);

x=y;
Thanks

PS, I currently use indent and then manually add the blank lines. Surely there must be an extension to indent to allow me to avoid the manual post processing.

Last edited by Lsatenstein; 12-04-2016 at 12:55 PM.
 
Old 12-08-2016, 06:59 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
I use astyle to clean up my c code it has a number of indenting/brackets options.
 
Old 12-13-2016, 10:15 PM   #3
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311

Original Poster
Blog Entries: 1

Rep: Reputation: 59
astyle works for me too.

Quote:
Originally Posted by Keith Hedger View Post
I use astyle to clean up my c code it has a number of indenting/brackets options.
I setup a ${HOME}/.astylerc
-A1 -S -f -e

Its the style I like mostly.


Thanks for pointing it out.

I tend to do indent first against the file, then follow up with astyle
indent handles x=+ and && where astyle claims to be inconsistent in this area of formatting. The following is my
${home}/.indent.pro
-bad -bl0 -blf -bli0 -bls -bs -c48 -cbi2 -cd48 -cli4 -cp48 -psl -l120

Imagine if instead of switches as above, we have a template system (model)

where we indicate the format we wish
EXAMPLES FOLLOW
Code:
if()
    xxxxx

if()
{
   xxxxx
}
else
   if()

do
{
   xxxx
} while ()

Last edited by Lsatenstein; 12-17-2016 at 11:34 AM.
 
Old 12-14-2016, 05:44 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
In your Original Post you were able use [code] and [/code] tags... Keep using them.
 
Old 12-17-2016, 11:40 AM   #5
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311

Original Poster
Blog Entries: 1

Rep: Reputation: 59
Instead of using indent or astyle (I follow astyle with indent

If we had templates to describe the formatting we desire, we could fine tune the formatted code to our preferences.
I like a blank line preceding and following for, while, and do loops. I do not need a blank line after if/else braces. The latter is overkill.

As for switch statement, the case statements inline or indented are fine for me, as long as there is a blank line before each case statement (but the first case statement).

Indent allows me to put /* */ comments indented to a given column (eg. 56 or 64 )
 
Old 12-17-2016, 12:39 PM   #6
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Although AFAIK it does not add "optional blank lines", the indent option -nsob causes indent not to "swallow" optional blank lines that are already present.

It should be fairly simple to use something such as sed/awk/perl/etc...to add optional blank lines automatically.

So creating a shell script which accepts one or more source code file names, then runs indent, astyle, and some little sed/awk/perl/etc... to add optional blank lines automatically, on each named file, should overall, accomplish the formatting you describe, without the manual editing step.


Quote:
Originally Posted by Lsatenstein View Post
[...]
If we had templates to describe the formatting we desire, we could fine tune the formatted code to our preferences.
[...]
Some IDE's have something like that. Other's have at least a visualization of what the formatted code will look like, as they let you adjust code formatting options within a GUI.
 
Old 12-18-2016, 04:11 PM   #7
Lsatenstein
Member
 
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 31and Tumbleweed) Gnome versions
Posts: 311

Original Poster
Blog Entries: 1

Rep: Reputation: 59
actually, I use indent for 2 options
Place same line comments indentebd to column 56
x+= becomes x +=

Astyle first, indent second
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which indent variable gives perfect indent in vim ? techie_san778 Linux - Software 1 07-25-2014 03:16 AM
indent phoenix7 Linux - Software 6 08-30-2005 12:14 PM
A more sensible editor with indent Haraldsh Linux - Software 2 11-12-2004 06:49 AM
no indent jesus_edu Linux - Newbie 0 04-13-2004 10:45 AM
indent chrismiceli Linux - General 1 05-12-2003 07:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration