all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Bruce Korb <bruce.korb@gmail.com>
To: 20778@debbugs.gnu.org
Subject: bug#20778: Flush left indentation in c-mode
Date: Fri, 26 Jun 2015 14:24:03 -0700	[thread overview]
Message-ID: <CAKRnqNLzND2okftnE89A1b8J3P0SkA98LuLV3mF=uddPcTbYfg@mail.gmail.com> (raw)
In-Reply-To: <5577190B.4000003@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 733 bytes --]

Here is a short program that went into this weirdo mode several times
fairly quickly.
Adding #include directives might be involved, but maybe not.  Again,
the same symptom:
everything goes flush left until I close out and re-visit the file.
So, when you first visit
the file, I am sure it will indent properly.  And just because you
fiddle it, it doesn't mean
it will stop indenting properly.  I do not know what the trigger
mechanism is.  I just
know it was much more frequent (several times in a few minutes).

Again, "linux" style:

$ cat ../.dir-locals.el

(
 (nil . ((indent-tabs-mode . t)
         (tab-width . 8)
         (show-trailing-whitespace . t)
         (fill-column . 132)))

 (c-mode . ((c-file-style . "linux")))
)

[-- Attachment #2: fix-tabs.c --]
[-- Type: text/x-csrc, Size: 1803 bytes --]


#include <sys/types.h>
#include <sys/stat.h>

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define NL  '\n'
#define NUL '\0'
#define HT  '\t'

static void
fserr(char const * fn, char const * op)
{
	char const * err = strerror(errno);

	fprintf(stderr, "fs error %u (%s) %s on %s\n",
		errno, err, op, fn);
	exit(EXIT_FAILURE);
}

static inline char *
detab(char * line)
{
	static union {
		unsigned long	spaces;
		unsigned char	sp[sizeof(unsigned long)];
	} const u = { .sp = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' } };
	int tab_ct = 0;
	unsigned long * lp = (void *)line;

	while (*lp == u.spaces) {
		tab_ct++;
		lp++;
	}

	line = (char *)lp;

	// Trim trailing white space
	{
		char * eol  = line + strlen(line);
		while ((eol > line) && isspace((unsigned)eol[-1]))
			eol--;
		*(eol++) = NL;
		*eol     = NUL;
		if (eol == line + 1)
			return line; // blank line
	}

	// insert "tab_ct" tab characters before the point where "line" points
	//
	while (tab_ct-- > 0)
		*(--line) = HT;
	return line;
}

static inline void
fix_file(char const * fname)
{
	char line[0x2000]; // any line over 8K deserves being mangled.

	FILE * ifp = fopen(fname, "r");

	if (ifp == NULL)
		fserr("open-read", fname);

	char tpl[] = "fix-tabs-XXXXXX";
	int fd = mkstemp(tpl);

	if (fd < 0)
		fserr("mkstemp", tpl);

	FILE * ofp = fdopen(fd, "w");

	if (ofp == NULL)
		fserr("open-write", tpl);

	for (;; ) {
		char * p = fgets(line, sizeof(line), ifp);
		if (p == NULL)
			break;
		fputs(detab(line), ofp);
	}
	{
		struct stat sb;
		if (fstat(fileno(ifp), &sb) == 0)
			fchmod(fd, sb.st_mode);
	}
	fclose(ifp);
	fclose(ofp);
	unlink(fname);
	rename(tpl, fname);
}

int
main(int argc, char ** argv)
{
	while (--argc > 0)
		fix_file(*(++argv));
}

  parent reply	other threads:[~2015-06-26 21:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-09 16:49 bug#20778: Flush left indentation in c-mode Bruce Korb
     [not found] ` <mailman.4701.1433868613.904.bug-gnu-emacs@gnu.org>
2015-06-10 11:00   ` Alan Mackenzie
2015-06-12 14:55     ` Bruce Korb
2015-06-26 21:24 ` Bruce Korb [this message]
2021-08-16 13:05   ` Lars Ingebrigtsen
2021-09-14 10:54     ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKRnqNLzND2okftnE89A1b8J3P0SkA98LuLV3mF=uddPcTbYfg@mail.gmail.com' \
    --to=bruce.korb@gmail.com \
    --cc=20778@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.