Skip to content

Emacs’ cc-mode’s built-in styles gallery

May 15, 2009

I, like a lot of programmers, have a preferred style of formatting my source code that makes me happy. So therefore, it was important to me that Emacs understood and indented my code correctly.

Whilst you can manually set each of the individual levels of offsets to apply, it’s usually much simpler to use one of the built-in styles that comes with CC mode. Even if you don’t find one that completely matches your personal style, it is easier to start with one that most closely matches and make slight adjustments, than to start completely from scratch.

What I’d have found useful at the time, would have been an example of what each of the built-in styles looked like. (Sure, it’d not have been too hard to manually set a buffer to each style and re-apply the indentation to use the new style.)

To this end, I present a gallery of examples of the built-in CC mode styles. At the very least, I hope it’ll be a useful reference to people new to Emacs.

To use the style in your current (cc-mode) buffer, use the command C-c . or M-x c-set-style, and then the name of the style. This will not automatically re-format your code. To do so, use the commands C-x h M-C-\ (this puts the region around the entire buffer, and then runs the indent-region command).

To set the style permanently, add the following to your .emacs file:

(add-hook 'c-mode-common-hook '(lambda () (c-set-style "STYLE-NAME")))

GNU

/**
 *This is a comment
 */
public class Example
{
  private static final int REPEAT_COUNT = 10;

  public static void main(String[] args)
  {
    // Another comment

    for (int i = 0; i < REPEAT_COUNT; i++)
      {
        if ((i % 2) == 0)
          {
            System.out.print("Hello ");
          }
        else
          {
            System.out.println("World!");
          }
      }

    switch(REPEAT_COUNT)
      {
      case 10:
        System.out.println("10");
        break;
      default:
        break;
      }
  }
}

K&R

/**
 *This is a comment
 */
public class Example
{
     private static final int REPEAT_COUNT = 10;

     public static void main(String[] args)
          {
               // Another comment

               for (int i = 0; i < REPEAT_COUNT; i++)
               {
                    if ((i % 2) == 0)
                    {
                         System.out.print("Hello ");
                    }
                    else
                    {
                         System.out.println("World!");
                    }
               }

               switch(REPEAT_COUNT)
               {
               case 10:
                    System.out.println("10");
                    break;
               default:
                    break;
               }
          }
}

BSD

/**
 *This is a comment
 */
public class Example
{
        private static final int REPEAT_COUNT = 10;

        public static void main(String[] args)
        {
                // Another comment

                for (int i = 0; i < REPEAT_COUNT; i++)
                {
                        if ((i % 2) == 0)
                        {
                                System.out.print("Hello ");
                        }
                        else
                        {
                                System.out.println("World!");
                        }
                }

                switch(REPEAT_COUNT)
                {
                case 10:
                        System.out.println("10");
                        break;
                default:
                        break;
                }
        }
}

Stroustrup

/**
 *This is a comment
 */
public class Example
{
    private static final int REPEAT_COUNT = 10;

    public static void main(String[] args)
        {
            // Another comment

            for (int i = 0; i < REPEAT_COUNT; i++)
            {
                if ((i % 2) == 0)
                {
                    System.out.print("Hello ");
                }
                else
                {
                    System.out.println("World!");
                }
            }

            switch(REPEAT_COUNT)
            {
            case 10:
                System.out.println("10");
                break;
            default:
                break;
            }
        }
}

Whitesmith

/**
 *This is a comment
 */
public class Example
    {
    private static final int REPEAT_COUNT = 10;

    public static void main(String[] args)
        {
        // Another comment

        for (int i = 0; i < REPEAT_COUNT; i++)
            {
            if ((i % 2) == 0)
                {
                System.out.print("Hello ");
                }
            else
                {
                System.out.println("World!");
                }
            }

        switch(REPEAT_COUNT)
            {
            case 10:
                System.out.println("10");
                break;
            default:
                break;
            }
        }
    }

Ellemtel

/**
 *This is a comment
 */
public class Example
{
      private static final int REPEAT_COUNT = 10;

      public static void main(String[] args)
      {
         // Another comment

         for (int i = 0; i < REPEAT_COUNT; i++)
         {
            if ((i % 2) == 0)
            {
               System.out.print("Hello ");
            }
            else
            {
               System.out.println("World!");
            }
         }

         switch(REPEAT_COUNT)
         {
            case 10:
               System.out.println("10");
               break;
            default:
               break;
         }
      }
}

Java

/**
 *This is a comment
 */
public class Example
{
    private static final int REPEAT_COUNT = 10;

    public static void main(String[] args)
    {
        // Another comment

        for (int i = 0; i < REPEAT_COUNT; i++)
            {
                if ((i % 2) == 0)
                    {
                        System.out.print("Hello ");
                    }
                else
                    {
                        System.out.println("World!");
                    }
            }

        switch(REPEAT_COUNT)
            {
            case 10:
                System.out.println("10");
                break;
            default:
                break;
            }
    }
}
About these ads
3 Comments leave one →
  1. Claire permalink
    June 2, 2009 11:19 pm

    Of course these examples aren’t strictly conformant to the various styles, as changing the mode doesn’t seem to have affected the placement of the braces, which differs from style to style.

    Also, you can set a default style using (setq c-default-style "style") rather than using a hook.

Trackbacks

  1. Interesting Emacs Links – 2009 Week 21 « A Curious Programmer
  2. pillarist's status on Thursday, 04-Jun-09 00:27:48 UTC - Identi.ca

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: