From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "filebat Mark" Newsgroups: gmane.emacs.help Subject: Re: ident the code Date: Tue, 26 Aug 2008 23:56:27 +0800 Message-ID: References: <87ljyka3kg.fsf@localhorst.mine.nu> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_10608_1816529.1219766187211" X-Trace: ger.gmane.org 1219766276 31258 80.91.229.12 (26 Aug 2008 15:57:56 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 26 Aug 2008 15:57:56 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Aug 26 17:58:48 2008 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KY0vw-0000Mf-Bh for geh-help-gnu-emacs@m.gmane.org; Tue, 26 Aug 2008 17:58:16 +0200 Original-Received: from localhost ([127.0.0.1]:53968 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KY0uy-00081C-Dm for geh-help-gnu-emacs@m.gmane.org; Tue, 26 Aug 2008 11:57:16 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KY0uD-0007pP-Q6 for help-gnu-emacs@gnu.org; Tue, 26 Aug 2008 11:56:29 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KY0uC-0007ou-Vv for help-gnu-emacs@gnu.org; Tue, 26 Aug 2008 11:56:29 -0400 Original-Received: from [199.232.76.173] (port=54281 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KY0uC-0007ol-PW for help-gnu-emacs@gnu.org; Tue, 26 Aug 2008 11:56:28 -0400 Original-Received: from wr-out-0506.google.com ([64.233.184.232]:48098) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KY0uB-0005KC-TY for help-gnu-emacs@gnu.org; Tue, 26 Aug 2008 11:56:28 -0400 Original-Received: by wr-out-0506.google.com with SMTP id c30so2310828wra.14 for ; Tue, 26 Aug 2008 08:56:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:in-reply-to:mime-version:content-type:references; bh=6AvIcy/RKPkz3WWGQXitl9EVyqq3FqtwXa1bs0jHhc4=; b=m7oFPERuT+lLsxsouZouNurvPkkW+fdCVXRQitp2ngyeR7iDjIUE/5YbQt+HGTMBpl IJe4iWDg5xD80hat3NifDjQOlCpO9AoGwvXKWPciW9QwGgYkrya6DB4uFe9qHRUCKdLh oyYbzgFD76mo+etAIcsmYq0vcEECX+dO0Sh4I= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version :content-type:references; b=StL4RFLXiQmhimbti8fu0dGIW+wCtN0/wGg6Pn7UjA4nLn/Q6eRrokdGIfzKB+LwF5 OI6NxUnQJROaHf80Ykx2qzXPp+5JVdferSGKb7HkYZiz+/3PuK5hUqZizfS2+A7A2QVI VY8e9KWPHRSlXJXzQ047m0La1sFxWAnSFZLEc= Original-Received: by 10.90.73.5 with SMTP id v5mr7316160aga.41.1219766187192; Tue, 26 Aug 2008 08:56:27 -0700 (PDT) Original-Received: by 10.90.87.6 with HTTP; Tue, 26 Aug 2008 08:56:27 -0700 (PDT) In-Reply-To: X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 2) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:57006 Archived-At: ------=_Part_10608_1816529.1219766187211 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 > 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 > > 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 ------=_Part_10608_1816529.1219766187211 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
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

------=_Part_10608_1816529.1219766187211--