| View previous topic :: View next topic |
| Author |
Message |
toralf
Joined: 31 Jan 2005 Posts: 3906 Location: Bremen, Germany
|
Posted: Fri Feb 25, 2005 1:04 pm Post subject: Syntax-Tidy for scripts (indentation and case correction) |
|
|
This is an old thread. The new thread with a newer version can be found here:
www.autohotkey.com/forum/viewtopic.php?p=46338
Dear all,
This is version 6 of a script for all those people like me and Hajos who get lazy with adjusting the indentation manually as it should be (for readability of the script). Until now it works ok for our scripts. Have a try and test it. (To work correctly for hotstrings or hotkeys a space or comma has to be between the colon ":" and the action to be performed). We are open for suggestions. Please see also notes in the scripts header.
Happy scripting
| Code: | | Code removed, because it is out of date |
Edit: 2005-02-26: (Fixed) If a rememberstring was last command in file without a newline, it wasn't put into the new file. (thanks ChrisM)
Edit: 2005-02-26: New: Changed the whole script. New Functionality, new Options, new Gui, two style options, option to use tabs or spaces, fixed some indentation for complex block structures.
Edit: 2005-03-03: Script now supports one-line if-statements.
Edit: 2005-03-07:
- Added new functionality: a)Case Correction, b) Gui remembers Position on screen
- Script name has changed to reflect these changes
Edit: 2005-03-09:
- Added new functionality: Gui remembers settings between sessions
- Hotkey has changed to F2
- Fixed: Indentation within a editor was damaged, due to `r`n at end of line (thanks AGU)
- Fixed: Indentation within a editor added additional īn each time
- Improved speed (setbatchline)
Edit: 2005-06-06:
- Added indentation and casecorrection for functions (thanks to HJS)
Edit: 2005-06-09:
- Added indentation for continuation lines (thanks to HJS)
- Made Hajos a co-author _________________ Ciao
toralf 
Last edited by toralf on Fri Feb 03, 2006 7:28 am; edited 12 times in total |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Fri Feb 25, 2005 1:45 pm Post subject: |
|
|
Und, was sagt Deine Frau zu dem Script ?
OK, OK - war nicht so gemeint.  |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5037 Location: /b/
|
Posted: Fri Feb 25, 2005 2:14 pm Post subject: |
|
|
Woah, great stuff ! _________________ Chat (IRC) PlusNet Scripts IronAHK Contact by email not private message. |
|
| Back to top |
|
 |
Rajat
Joined: 28 Mar 2004 Posts: 1687
|
Posted: Fri Feb 25, 2005 3:16 pm Post subject: |
|
|
great start toralf! just don't stop here!!
next is applying proper case to commands and keywords... and the indentation needs some optional settings... like u use the first one here, and i use the second:
| Code: |
loop
{
some function
}
|
| Code: |
loop
{
some function
}
|
by the way mine has a real tab there.. _________________
 |
|
| Back to top |
|
 |
ChrisM
Joined: 28 Nov 2004 Posts: 58
|
Posted: Sat Feb 26, 2005 3:23 am Post subject: |
|
|
Great script!
One problem I found:
If a script ends with 'return' and no newline after it - the 'return' never gets copied to the new file.
I tracked it down to the 'RememberString' variable. The last 'return' is stored in it but since there is no newline the Loop ends and that 'return' never gets added to the 'String' varialble.
To solve it I moved this section of code to the bottom of the Loop:
| Code: |
;Create current indentation depending on indentation levels
Indent =
;turn AutoTrim off, to be able to process tabs and spaces
AutoTrim, Off
Loop, %IndentCount%
Indent = %Indent%%IndentSize%
;If a line had to been remembered from previous loop, add it to string and set it to nothing
If RememberString
String = %String%%Indent%%RememberString%
;turn AutoTrim on, to remove leading and trailing tabs and spaces
AutoTrim, On
;Clear Rememberstring
RememberString =
|
I believe this is safe because the first line in any script should always have
no indentation, so 'Indent' variable need only be set after the first line is read. _________________ ChrisM |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3906 Location: Bremen, Germany
|
Posted: Sat Feb 26, 2005 12:12 pm Post subject: |
|
|
Thank you ChrisM,
You are right. I will update the fist post. _________________ Ciao
toralf  |
|
| Back to top |
|
 |
ChrisM
Joined: 28 Nov 2004 Posts: 58
|
Posted: Sat Feb 26, 2005 10:26 pm Post subject: |
|
|
Here's a version that indents all text in an active editor window by hotstring Ctl-i
Braces may or maynot be indented as Rajat requested.
Works with hotkeys and hotstrings.
Problems:
- after all text is highlighted(ctl-a) sometimes the script takes a long time to return, the text in the window is left highlighted and vulnerable to deletion.
- with Wordpad every time the hotkey is pressed an empty newline is added to the end of the file. PSPad and Notepad do not have this problem.
- after indentation the cursor is always put to the end of the file. Would be better to put cursor back to the line number it was on when the hotkey was pressed, but this will probably be editor dependant.
Comment from moderator: For the lateset version, see http://www.autohotkey.com/forum/viewtopic.php?t=7810 _________________ ChrisM |
|
| Back to top |
|
 |
toralf as guest Guest
|
Posted: Sun Feb 27, 2005 1:29 am Post subject: |
|
|
I found two problems with my script. And it will most propably also happen with ChrisM Script, since it looks like he hasn't changed the indentation part.
Problem 1)
Indentation will be incorrect if there is a one-line if-statement in the code like | Code: | IfEqual, x, 1, Sleep, 1
|
Problem 2)
There will be problems if the code doesn't use brackets with multiple inclded if-statements, e.g.
| Code: | if x = 1
if y =2
if z = 3
MsgBox, toralf doens't do the indentation correct for next line.
MsgBox, this line is indented too much |
3)
And what is the correct indentaion of the following example? Chris can you please respond to this, it gives me some trouble:
| Code: | if x = 1
if y =2
if z = 3
MsgBox, toralf tried to do the indentation correct.
Else if w =4
if v = 5
MsgBox, I do not were I am
else
MsgBox, I'm totally lost |
Thanks.
I will not fix Problem 1. On problem two I'm working. And I hope to fix it when I understand 3. The current workaround is to use {} for every block (loops, ifs, etc.) even if they only have one line in the block.
Though, the code will work fine with
| Code: | MsgBox, No indent
if z = 3
MsgBox, indentation
MsgBox, No indentation |
Ciao
toralf |
|
| Back to top |
|
 |
Rajat
Joined: 28 Mar 2004 Posts: 1687
|
Posted: Sun Feb 27, 2005 7:10 am Post subject: |
|
|
Nice work ChrisM !
keep going guyz! _________________
 |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3906 Location: Bremen, Germany
|
Posted: Sun Feb 27, 2005 10:53 am Post subject: |
|
|
Please help/correct me:
Ok, I have played around with 3). Which was
| toralf as guest wrote: | And what is the correct indentaion of the following example? Chris can you please respond to this, it gives me some trouble:
| Code: | if x = 1
if y =2
if z = 3
MsgBox, toralf tried to do the indentation correct.
Else if w =4
if v = 5
MsgBox, I do not were I am
else
MsgBox, I'm totally lost |
|
Here is what I think is the correct indentation. Please let me know if I'm correct or give help where the error is. Thanks
| Code: | if x = 1
if y = 2
if z = 3
MsgBox,up to here indentation should be correct.
Else if w = 4
if v = 5
MsgBox, don't know if correct
else
MsgBox, please help
MsgBox, This line should be without indentation,correct?
|
_________________ Ciao
toralf  |
|
| Back to top |
|
 |
Rajat
Joined: 28 Mar 2004 Posts: 1687
|
Posted: Sun Feb 27, 2005 11:08 am Post subject: |
|
|
i think that the above is the optimum way. _________________
 |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10667
|
Posted: Sun Feb 27, 2005 11:15 pm Post subject: |
|
|
Yes, I think that's the standard way to indent such an extreme example  |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3906 Location: Bremen, Germany
|
Posted: Tue Mar 01, 2005 9:19 am Post subject: |
|
|
I re-wrote the whole script, please see first post. Lot's of requests have been added and bugs fixed. _________________ Ciao
toralf  |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10667
|
Posted: Tue Mar 01, 2005 12:51 pm Post subject: |
|
|
| It looks great. Thanks for posting it. |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3906 Location: Bremen, Germany
|
Posted: Tue Mar 01, 2005 2:21 pm Post subject: |
|
|
I wonder what Rajat thinks of it? _________________ Ciao
toralf  |
|
| Back to top |
|
 |
|