From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andreas Politz Newsgroups: gmane.emacs.help Subject: Re: ident the code Date: Tue, 26 Aug 2008 18:10:20 +0200 Organization: FH-Trier Message-ID: <1219767160.989250@arno.fh-trier.de> References: <87ljyka3kg.fsf@localhorst.mine.nu> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1219768900 9529 80.91.229.12 (26 Aug 2008 16:41:40 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 26 Aug 2008 16:41:40 +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 18:42:31 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 1KY1ck-0006i6-Ur for geh-help-gnu-emacs@m.gmane.org; Tue, 26 Aug 2008 18:42:31 +0200 Original-Received: from localhost ([127.0.0.1]:51101 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KY1bm-0002Xj-NE for geh-help-gnu-emacs@m.gmane.org; Tue, 26 Aug 2008 12:41:30 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!newsfeed.straub-nv.de!newsfeed01.sul.t-online.de!t-online.de!news.belwue.de!news.uni-kl.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 125 Original-NNTP-Posting-Host: 143-93-54-11.arno.fh-trier.de Original-X-Trace: news.uni-kl.de 1219767158 19521 143.93.54.11 (26 Aug 2008 16:12:38 GMT) Original-X-Complaints-To: usenet@news.uni-kl.de Original-NNTP-Posting-Date: Tue, 26 Aug 2008 16:12:38 +0000 (UTC) User-Agent: Mozilla-Thunderbird 2.0.0.16 (X11/20080724) In-Reply-To: Cache-Post-Path: arno.fh-trier.de!unknown@dslb-084-059-194-132.pools.arcor-ip.net X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/) Original-Xref: news.stanford.edu gnu.emacs.help:161664 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:57007 Archived-At: 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() > (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 >> >> > > Here is how I would do it, with my current knowledge of emacs-fu. (defun pretty-operator (start end) (interactive "r") (save-excursion (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 " = "))))) -ap