I have wrote the following elisp function. I think it works from my side.
Hopes it's useful for someone else.


(global-set-key [M-f12] 'my_indent_code);;indent code
(defun my_indent_code()
  (interactive)
  ;;remove blank lines
  (goto-char 0)
  (flush-lines "^$")
  (save-buffer)
  (setq current_filename (buffer-name (current-buffer)))
  ;;indent the code, add or remove some space
  (setq commandline "indent ")
  (setq commandline (concat commandline current_filename))
  (shell-command-to-string commandline)
  ;;convert the line endings of text files from DOS style to unix style
  (setq commandline "dos2unix ")
  (setq commandline (concat commandline current_filename))
  (shell-command-to-string commandline)
  ;;reload
  (revert-buffer t t)
)



2008/8/26 filebat Mark <filebat.mark@gmail.com>
to Peter:
I think our replace-regex doesn't work well.
1) Supposing a code line is "if(s!=0)", it will be changed into "if(s! = 0)". That's definitely not the thing we want. The same problem occurs for "if (s==0)".
2) A line of "strcpy(str, "a=2")", will be changed into "strcpy(str, "a = 2").

So we should spend more effort to solve those problems.
-----------------------------------------------------------------------------------------------------------------------
To David:
Indent is quite a useful utility. Thanks very much for your suggestion!
There are still something that doesn't look good for me.
Such as, script1 will be changed to srcipt2.
script1

void test(char* str)
{
 int i;
i=    2;
 
strcpy(str,        "hello");
}

script2
void
test( char* str )
{
 int i;
 i = 2;

 strcpy(str, "hello");
}


But what I want is script3.
script3:
void test( char* str )
{
 int i;
 i = 2;
 strcpy(str, "hello");
}

-----------------------------------------------------------------------------------------------------------------------
I'am sorry to bother you guys. I just curious how skilled programmers indent their codes, to make them look nice. They may have written tons of codes, not only C/C++, but also java, python, whatever.


2008/8/26 David Hansen <david.hansen@gmx.net>

On Mon, 25 Aug 2008 21:59:01 +0800 filebat Mark wrote:

> I think maybe an elisp function can do this job. I am wondering how to find
> this elisp code, cause I don't want to reinvent the wheel.

Tried this?  It has tons of options.

INDENT(1)

NAME
      indent  - changes the appearance of a C program by inserting or
      deleting whitespace.

David






--
Thanks & Regards

Denny Zhang




--
Thanks & Regards

Denny Zhang