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

RE: C++: Indenting and formatting program sources.



I agree that emacs cc-mode package works great for indenting.  You can
modify it quite a lot to make it indent how you want.  It does not move the
position of "{" past comments or do really sophisticated code reformatting
like that, but there are quite a few good settings you can set to help
yourself be consistent and make "cvs diff" commands not give a lot of
redundant information.  The attached file indent-settings.emacs expresses my
personal biases in coding style, of course ;>).

After doing what Gary suggests for awhile, I found (after some serious
puzzling and emacs lisp confusion) that I could do this to make the process
non-interactive:

	emacs -q -l indent-settings.emacs -l indent-run.FNAME.emacs
or
	xemacs -q -l indent-settings.emacs -l indent-run.FNAME.emacs

where you have created indent-run.FNAME.emacs from indent-run.emacs (see
file contents below) using sed or vim to replace FNAME with your program's
filename.  If you don't use emacs much, or don't have time for the
keystrokes, this non-interactive method might also be useful.  As I recall
there is no command-line option to put a single elisp command on the emacs
command-line.  If there were, the above could be make somewhat less awkward
(hints or work-arounds anyone?).  One work-around is writing a shell script,
but a small command that could be used as an alias would be nice.

One problem I've found with cc-mode is that it doesn't take compiler
variable logic into account.  If you have something like the code fragment
below, you will find that after indenting, all code below that function is
indented too much because of the extra "{" seen by cc-mode but not by the
compiler.

#ifdef MYCOMPVAR
int function (int a) {
#else
int function (int a, int b) {
#endif
	// body of code
}

This difficulty, combined with having a larger screen than when I learned C,
is what caused me to finally convert to putting "{" below the argument list!

-Kris

---- indent-settings.emacs ----
(auto-fill-mode 1)
(setq-default tab-width 4)
(setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72
76))
(setq make-backup-files nil)

(autoload 'c++-mode "cc-mode" "C++ Editing Mode" t)
(autoload 'c-mode   "cc-mode" "C Editing Mode" t)

(setq default-major-mode 'c++-mode)
(setq text-mode-hook '(lambda() (auto-fill-mode 1)))

(setq auto-mode-alist '(("\\.c$" . c++-mode)
("\\.h$" . c++-mode)
("\\.cc$" . c++-mode)
("\\.hpp$" . c++-mode)
("\\.cpp$" . c++-mode)
("\\.C$" . c++-mode)
))

(defun my-c-mode-common-hook ()
 ;; Customization for all modes provided by "cc-mode"
 (c-set-style "ellemtel")
 (setq delete-key-deletes-forward t)
 (setq c-basic-offset 4)
 (c-set-offset 'inclass '+)
 (c-set-offset 'stream-op '+)
 (c-set-offset 'arglist-cont '0)
 (c-set-offset 'arglist-cont-nonempty '+)
 (setq c-comment-only-line-offset 0)
 (setq comment-column 36)
 (auto-fill-mode 1)
 (setq fill-column 79)
)
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

(require 'font-lock)
;    (eval-after-load "font-lock" '(require 'choose-color))
(add-hook 'c-mode-hook 'turn-on-font-lock 'at-end)
(add-hook 'c++-mode-hook 'turn-on-font-lock 'at-end)
--------------------------------

---- indent-run.FNAME.emacs ----
(find-file "FNAME")
(mark-whole-buffer)
(indent-region 0 100000 nil)
(save-buffer)
(save-buffers-kill-emacs)
--------------------------------

-----Original Message-----
From: Gary Turner [mailto:kk5st@swbell.net]
Sent: Monday, November 19, 2001 4:26 PM
To: debian-user@lists.debian.org
Subject: Re: C++: Indenting and formatting program sources.


On Sun, 18 Nov 2001 17:24:22 +0200, you wrote:

>Can anyone recommend a program to indent and format C++ program sources 
>for consistency of style and perhaps better readability?
>Specifying the used options or attaching a suitable configuration file 
>(like a .ident.pro for GNU indent) is desirable.
>
>GNU indent does not targeted directly to C++ code. Is this a problem?
>The only official deb that I found for this task is astyle. I have not 
>tried it yet. Are there others? Are there commonly used programs for 
>this task that are not debianized?
>The LDP C-C++ Beautifier HOW-TO mentions bcpp. Is it commonly used?
>
>Is there a way to have vim force a standard and consistent style? Once 
>again, attaching a configuration file or pointing out to a standard one 
>is desirable.
>-- 
>
>    Shaul Karl
>    email: shaulka (replace these parenthesis with @) bezeqint,
>           delete the comma and the white space characters and add .net

Ignore this if I am not understanding the question.  It looks to me like
all you need to do is open the file in emacs, select the entire file,
and invoke indent-region (check the syntax).  There is a built library
of styles to choose from; a default, K&R, GNU, and others.  Opening
files with C/C++ extensions automatically puts you in C/C++ mode.

As for extending vim for this, I have not a clue.  For its ease of use
in writing formatted code in any number of languages, try emacs even if
you prefer vim for ordinary tasks.

gt
Yes I fear I am living beyond my mental means--Nash


-- 
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact
listmaster@lists.debian.org



Reply to: