[Top][All Lists]

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

Re: ident the code

From: Andreas Politz
Subject: Re: ident the code
Date: Tue, 26 Aug 2008 18:10:20 +0200
User-agent: Mozilla-Thunderbird (X11/20080724)

filebat Mark wrote:
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()
  ;;remove blank lines
  (goto-char 0)
  (flush-lines "^$")
  (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)
  (revert-buffer t t)

2008/8/26 filebat Mark <address@hidden>

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 =

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.

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

strcpy(str,        "hello");

test( char* str )
 int i;
 i = 2;

 strcpy(str, "hello");

But what I want is 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 <address@hidden>

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
this elisp code, cause I don't want to reinvent the wheel.
Tried this?  It has tons of options.


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


Thanks & Regards

Denny Zhang

Here is how I would do it, with my current knowledge
of emacs-fu.

(defun pretty-operator (start end)
  (interactive "r")
    (goto-char start)
    (while (search-forward-regexp "\\(\\b\\|\\s-*\\)=\\(\\b\\|\\s-*\\)" (1+ 
end) t)
      (when (not (or (nth 4 (syntax-ppss)) ;not string
                     (nth 3 (syntax-ppss)))) ;not comment
        (replace-match " = ")))))


reply via email to

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