unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Andreas Politz <politza@fh-trier.de>
To: help-gnu-emacs@gnu.org
Subject: Re: ident the code
Date: Tue, 26 Aug 2008 18:10:20 +0200	[thread overview]
Message-ID: <1219767160.989250@arno.fh-trier.de> (raw)
In-Reply-To: <mailman.17594.1219766191.18990.help-gnu-emacs@gnu.org>

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 <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
>>
>>
> 
> 


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


      parent reply	other threads:[~2008-08-26 16:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-25 13:59 ident the code filebat Mark
2008-08-25 23:45 ` Peter Dyballa
2008-08-26  2:54 ` David Hansen
2008-08-26 14:35   ` filebat Mark
2008-08-26 15:56     ` filebat Mark
     [not found]     ` <mailman.17594.1219766191.18990.help-gnu-emacs@gnu.org>
2008-08-26 16:10       ` Andreas Politz [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1219767160.989250@arno.fh-trier.de \
    --to=politza@fh-trier.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).