emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C label indentation: emacs and GNU indent disagree


From: Jim Meyering
Subject: C label indentation: emacs and GNU indent disagree
Date: Mon, 06 Jun 2011 09:25:25 +0200

I've just discovered that GNU indent's default indentation
of C labels differs from the indentation that I get in emacs.
With GNU indent, an unnested label is left justified.  I.e.,
it starts in the first column.  However, emacs puts a space
before it:

$ printf 'f(){l:;}'|indent > k.c; cat k.c
f ()
{
l:;
}

However, if you run "emacs -q k.c", move point to the line with
the label and hit TAB, it inserts a space:

f ()
{
 l:;
}

I did a quick survey of coreutils and see there isn't a clear favorite:

  $ git grep -E '^[a-z_]+:' src/
  src/copy.c:preserve_metadata:
  src/copy.c:close_src_and_dst_desc:
  src/copy.c:close_src_desc:
  src/copy.c:un_backup:
  src/echo.c:just_echo:
  src/find-mount-point.c:done:
  src/head.c:free_lbuffers:
  src/od.c:cleanup:;
  src/split.c:no_filters:
  src/stat.c:print_mount_point:
  src/tail.c:free_lbuffers:
  src/tail.c:free_cbuffers:

  $ git grep -E '^ [a-z_]+:' src/
  src/du.c: argv_iter_done:
  src/kill.c: no_more_options:;
  src/ls.c: done:
  src/paste.c: done:;
  src/sort.c: greater:
  src/sort.c: not_equal:
  src/sort.c: finish:
  src/uniq.c: closefiles:
  src/wc.c: argv_iter_done:

Note that there is a good reason to prefer the indented label.
If I make a change to copy.c just after the un_backup: label,
here's the "diff" output I see:

diff --git a/src/copy.c b/src/copy.c
index 801a474..ff229f0 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -2521,6 +2521,7 @@ un_backup:

   if (dst_backup)
     {
+
       if (rename (dst_backup, dst_name) != 0)
         error (0, errno, _("cannot un-backup %s"), quote (dst_name));
       else

However, if I "correct" the indentation of that label so that it is not
left justified, the diff then refers to the containing function name,
copy_internal, rather than to the label name:

diff --git a/src/copy.c b/src/copy.c
index b685a6b..b7fd433 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -2521,6 +2521,7 @@ copy_internal (char const *src_name, char const *dst_name,

   if (dst_backup)
     {
+
       if (rename (dst_backup, dst_name) != 0)
         error (0, errno, _("cannot un-backup %s"), quote (dst_name));
       else

I prefer emacs' indentation style and will switch to it.  FYI, for
automated formatting, I am inclined to use uncrustify, which currently
acts just like GNU indent in this respect, but will be easy to fix.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]