* ident the code
@ 2008-08-25 13:59 filebat Mark
2008-08-25 23:45 ` Peter Dyballa
2008-08-26 2:54 ` David Hansen
0 siblings, 2 replies; 6+ messages in thread
From: filebat Mark @ 2008-08-25 13:59 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 440 bytes --]
Hi, all
I have some C++ source codes. I want make sure there is just a space on both
sides of "=".
That means script1 will be changed to script2, as following.
Script1:
int a= 2;
a =a+3;
Script2:
int a = 2;
a = a+3;
-------------------
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.
Thanks for your attention.
--
Thanks & Regards
Denny Zhang
[-- Attachment #2: Type: text/html, Size: 576 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: ident the code
2008-08-25 13:59 ident the code filebat Mark
@ 2008-08-25 23:45 ` Peter Dyballa
2008-08-26 2:54 ` David Hansen
1 sibling, 0 replies; 6+ messages in thread
From: Peter Dyballa @ 2008-08-25 23:45 UTC (permalink / raw)
To: filebat Mark; +Cc: help-gnu-emacs
Am 25.08.2008 um 15:59 schrieb filebat Mark:
> I have some C++ source codes. I want make sure there is just a
> space on both
> sides of "=".
Do you think you could achieve your aim with regular expressions?
(replace-regexp " *= *" " = ")
Replace all occurrences of zero or more spaces followed by "=" and
followed by zero or more spaces with exactly " = ". This would also
change initialisation of loops ... but the regular expression could
be made more complicated to handle parentheses on the line, to avoid
replacements when they exist on that line.
--
Greetings
Pete
There is no national science just as there is no national
multiplication table; what is national is no longer science.
– Anton Checov
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: ident the code
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
1 sibling, 1 reply; 6+ messages in thread
From: David Hansen @ 2008-08-26 2:54 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: ident the code
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>
0 siblings, 2 replies; 6+ messages in thread
From: filebat Mark @ 2008-08-26 14:35 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 1710 bytes --]
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
[-- Attachment #2: Type: text/html, Size: 2511 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: ident the code
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>
1 sibling, 0 replies; 6+ messages in thread
From: filebat Mark @ 2008-08-26 15:56 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 2679 bytes --]
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
[-- Attachment #2: Type: text/html, Size: 3924 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <mailman.17594.1219766191.18990.help-gnu-emacs@gnu.org>]
* Re: ident the code
[not found] ` <mailman.17594.1219766191.18990.help-gnu-emacs@gnu.org>
@ 2008-08-26 16:10 ` Andreas Politz
0 siblings, 0 replies; 6+ messages in thread
From: Andreas Politz @ 2008-08-26 16:10 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-08-26 16:10 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).