1 mike 1.1 Option Explicit
2
3 '------------------------------------------------------------------------------
4 'FILE DESCRIPTION: Routines to reformat C/C++ code.
5 '------------------------------------------------------------------------------
6
7 Sub MakeCodeNicer()
8 'DESCRIPTION: Reformats the source code to look nicer, the way I like it.
9 ' Written by Alvaro Mendez on 06/10/1999
10 ' Taken from http://www.codeguru.com/devstudio_macros/MakeCodeNicer.shtml
11
12 ' Check that the current document can be changed
13 if ActiveDocument.ReadOnly then
14
15 ' If we're connected to SourceSafe, let it prompt for check out
16 ActiveDocument.Selection = "a"
17 ActiveDocument.Undo
18
19 if ActiveDocument.ReadOnly then ' check again
20 MsgBox "This macro cannot be executed on a read-only file.", _
21 vbExclamation
22 mike 1.1 exit sub
23 end if
24 end if
25
26 ' Save current line so we can return to it at the end
27 dim nLine
28 nLine = ActiveDocument.Selection.CurrentLine
29
30 ' Add spaces in a few places and get rid of it in others
31 Replace "\:b+;", ";"
32 Replace "\:b+::\:b+", "::"
33 Replace "\:b+(", "("
34 Replace "if(", "if ("
35 Replace "for(", "for ("
36 Replace "while(", "while ("
37 Replace "switch(", "switch ("
38 Replace "catch(", "catch ("
39 Replace "return(", "return ("
40 Replace "(\:b+", "("
41 Replace "\:b+)", ")"
42 Replace ";)", "; )"
43 mike 1.1 Replace ";;\:b+)", ";;)"
44 Replace "\[\:b+", "["
45 Replace "\:b+\]", "]"
46 Replace "\:b+\[", "["
47
48 ' Make sure these statements don't end on the same line they started.
49 BreakSingleLiners "if ("
50 BreakSingleLiners "for ("
51 BreakSingleLiners "switch ("
52
53 ' Break up any lines containing multiple statements
54 BreakLinesWithMultipleStatements
55
56 ' Make sure braces are on lines by themselves (unless followed by comments)
57 IsolateOnLeft "{"
58 IsolateOnRight "{"
59 IsolateOnRight "}"
60 IsolateOnLeft "}"
61
62 ' Break up case statements appearing on single lines
63 IsolateOnRight "case .+: "
64 mike 1.1 IsolateOnLeft "break;"
65
66 ' Add a space between these operators
67 FixOperator "=", 1
68 FixOperator "==", 2
69 FixOperator "!=", 2
70 FixOperator "\+=", 2
71 FixOperator "-=", 2
72 FixOperator "\*=", 2
73 FixOperator "/=", 2
74 FixOperator "\+", 1
75 FixOperator "-", 1
76 FixOperator "<=", 2
77 FixOperator ">=", 2
78 FixOperator "<<", 2
79 FixOperator ">>", 2
80 FixOperator "&&", 2
81 FixOperator "||", 2
82 FixOperator "|", 1
83 FixLessThanOperator
84 FixExponents
85 mike 1.1
86 ' Append a space after these
87 AppendSpace ","
88 AppendSpace ";"
89
90 ' Make sure C++ comments (followed by words) have a space after them
91 while ActiveDocument.Selection.FindText("//[A-Z,a-z,0-9]", dsMatchRegExp)
92 ActiveDocument.Selection.CharRight
93 ActiveDocument.Selection.CharLeft
94 ActiveDocument.Selection = " "
95 wend
96
97 ' Replace all the trailing whitespace (thanks to Paul Bludov)
98 ActiveDocument.Selection.ReplaceText "\:b+\($\)", "\1", dsMatchRegExp
99
100 ' Fix tabs within code surrounded by braces
101 TabifyMatchingBraces
102
103 ' Remove any lines that are considered extraneous (ie. extra blank lines)
104 RemoveExtraneousLines
105
106 mike 1.1 ' Indent every "case" inside switch statements (thanks to Jim Cooper)
107 IndentSwitchBody
108
109 ' Go back to where we were at the beginning
110 ActiveDocument.Selection.GoToLine nLine
111
112 End Sub
113
114 ' Is the cursor currently within a quoted string (or character)
115 function IsWithinQuotes
116 dim nCurrentLine, nCurrentColumn, iPos, strBuffer, nCount
117
118 nCurrentLine = ActiveDocument.Selection.CurrentLine
119 nCurrentColumn = ActiveDocument.Selection.CurrentColumn
120
121 ActiveDocument.Selection.Cancel
122 ActiveDocument.Selection.StartOfLine dsFirstText, dsExtend
123
124 nCount = 0
125 iPos = 0
126 strBuffer = ActiveDocument.Selection
127 mike 1.1
128 ' Count all occurrences of a double quote which apply to quoted strings
129 do while true
130 iPos = InStr(iPos + 1, strBuffer, """", vbTextCompare)
131 if not (iPos > 0) then
132 exit do
133 end if
134
135 if iPos = 1 then ' if it's the first character, then it's valid
136 nCount = nCount + 1
137 else
138 ' Make sure it's not preceded by a \ or a \\
139 if Mid(strBuffer, iPos - 1, 1) <> "\" then
140 nCount = nCount + 1
141 elseif (iPos > 2) and (Mid(strBuffer, iPos - 2, 1) = "\") then
142 nCount = nCount + 1
143 end if
144 end if
145 loop
146
147 ' If number of quotes is odd, we must be inside a quoted string!
148 mike 1.1 IsWithinQuotes = ((nCount > 0) and ((nCount Mod 2) <> 0))
149
150 ActiveDocument.Selection.MoveTo nCurrentLine, nCurrentColumn
151
152 ' If we're not inside a quoted string, check for a quoted character
153 if not IsWithinQuotes then
154 ActiveDocument.Selection.CharLeft dsExtend
155
156 ' If we find a quoted character left of us, check for one on the right
157 if ActiveDocument.Selection = "'" then
158 ActiveDocument.Selection.CharRight
159 ActiveDocument.Selection.CharRight dsExtend
160 if ActiveDocument.Selection = "\" then
161 ActiveDocument.Selection.CharRight
162 ActiveDocument.Selection.CharRight dsExtend
163 end if
164 ActiveDocument.Selection.CharRight
165 ActiveDocument.Selection.CharRight dsExtend
166
167 if ActiveDocument.Selection = "'" then
168 IsWithinQuotes = true
169 mike 1.1 end if
170 end if
171 ActiveDocument.Selection.MoveTo nCurrentLine, nCurrentColumn
172 end if
173
174 ' If we're inside quotes, proceed from the next character
175 if IsWithinQuotes then
176 ActiveDocument.Selection.CharRight
177 end if
178
179 end function
180
181 ' Is current selection preceded by a C++ comment? ("//")
182 function IsWithinComment
183 dim nCurrentLine, nCurrentColumn
184
185 nCurrentLine = ActiveDocument.Selection.CurrentLine
186 nCurrentColumn = ActiveDocument.Selection.CurrentColumn
187
188 ActiveDocument.Selection.Cancel
189 ActiveDocument.Selection.StartOfLine dsFirstText, dsExtend
190 mike 1.1
191 IsWithinComment = false
192 if (InStr(1, ActiveDocument.Selection, "//", vbTextCompare) > 0) then
193 IsWithinComment = true ' since it's commented out
194 nCurrentLine = nCurrentLine + 1 ' we proceed from the next line
195 end if
196
197 ActiveDocument.Selection.MoveTo nCurrentLine, 1
198
199 end function
200
201 ' Should the current selection be ignored?
202 ' (ie., is it within a comment or between quotes?)
203 function ShouldIgnore
204
205 ShouldIgnore = false
206
207 if IsWithinQuotes then
208 ShouldIgnore = true
209 exit function
210 end if
211 mike 1.1
212 if IsWithinComment then
213 ShouldIgnore = true
214 end if
215
216 end function
217
218 ' Put the cursor at the top of the document and return "" to be passed
219 ' initially to GetCurrenPosition
220 function InitializePosition
221 ActiveDocument.Selection.StartOfDocument
222 InitializePosition = ""
223 end function
224
225 ' Retrieve the current position and return true if it's greater than the
226 ' last one. This is used to ensure that the file is only searched once
227 ' (provided the search is started from the top)
228 function GetCurrentPosition(strPos)
229 dim nLastLine, nLastColumn, nCurrentLine, nCurrentColumn, iPos, ch
230
231 nLastLine = -1
232 mike 1.1 nLastColumn = -1
233
234 nCurrentLine = ActiveDocument.Selection.CurrentLine
235 nCurrentColumn = ActiveDocument.Selection.CurrentColumn
236
237 ' Parse the last position and extract the line and column
238 for iPos = 1 to Len(strPos)
239 ch = Mid(strPos, iPos, 1)
240 if ch = "," then
241 nLastLine = Int(Mid(strPos, 1, iPos))
242 nLastColumn = Int(Mid(strPos, iPos + 1))
243 exit for
244 end if
245 next
246
247 ' Return true if we're currently past the last position
248 strPos = nCurrentLine & "," & nCurrentColumn
249 GetCurrentPosition = (nCurrentLine > nLastLine) or _
250 ((nLastLine = nCurrentLine) and (nCurrentColumn > nLastColumn))
251
252 end function
253 mike 1.1
254 ' Move by a certain number of columns
255 sub MoveByColumns(nBy)
256 ActiveDocument.Selection.MoveTo ActiveDocument.Selection.CurrentLine, _
257 ActiveDocument.Selection.CurrentColumn + nBy
258 end sub
259
260 ' Replace the given strFrom with strTo case sensitively
261 sub Replace(strFrom, strTo)
262 dim strLastPos, bContinue
263
264 strLastPos = InitializePosition
265 do while ActiveDocument.Selection.FindText(strFrom, _
266 dsMatchCase + dsMatchRegExp)
267
268 bContinue = GetCurrentPosition(strLastPos)
269
270 ' Check if we're inside a comment or between quotes
271 if not ShouldIgnore then
272
273 ' Repeat the search since ShouldIgnore puts the cursor at
274 mike 1.1 ' the beginning of the line
275 ActiveDocument.Selection.FindText strFrom, _
276 dsMatchCase + dsMatchRegExp
277 ActiveDocument.Selection = strTo
278
279 elseif not bContinue then
280 exit do
281 end if
282 loop
283
284 end sub
285
286 ' Break the given str ending in (, so that instead of this:
287 ' if (a) return b;
288 ' it looks like this:
289 ' if (a)
290 ' return b;
291 sub BreakSingleLiners(str)
292 dim strLastPos, strFound, nCol, bBreak, strAfterFound
293
294 ' Verify str ends in (, the beginning parenthesis
295 mike 1.1 if Right(str, 1) <> "(" then
296 exit sub
297 end if
298
299 strLastPos = InitializePosition
300
301 while ActiveDocument.Selection.FindText(str, dsMatchCase) and _
302 GetCurrentPosition(strLastPos)
303
304 ' Check if we're inside a comment or between quotes
305 if not ShouldIgnore then
306
307 ' Repeat the search since ShouldIgnore puts the cursor at the
308 ' beginning of the line
309 ActiveDocument.Selection.FindText str, dsMatchCase
310
311 ' Find the matching brace and go to the end of the line
312 ActiveDocument.Selection.CharRight
313 ActiveDocument.Selection.CharLeft
314 ExecuteCommand "GoToMatchBrace"
315 ActiveDocument.Selection.CharRight
316 mike 1.1 nCol = ActiveDocument.Selection.CurrentColumn
317 ActiveDocument.Selection.EndOfLine dsExtend
318 strFound = LTrimTabs(ActiveDocument.Selection)
319
320 ' If there's anything after the brace that isn't a comment, move
321 ' it to the next line
322 if (Len(strFound) > 0) and (Left(strFound, 1) <> "/") then
323 bBreak = false
324
325 ' Check if there's a "{" after the statement which should
326 ' also be broken into multiple lines
327 if (Mid(strFound, 1, 1) = "{") and (Len(strFound) > 1) then
328 strAfterFound = LTrimTabs(Mid(strFound, 2))
329 if strAfterFound <> "" then
330 ActiveDocument.Selection = "{" + strAfterFound
331 ActiveDocument.Selection.MoveTo _
332 ActiveDocument.Selection.CurrentLine, nCol
333 ActiveDocument.Selection.NewLine
334 ActiveDocument.Selection.CharRight
335 ActiveDocument.Selection.NewLine
336
337 mike 1.1 bBreak = true ' primitive but it works
338 end if
339 end if
340
341 if not bBreak then
342 ActiveDocument.Selection = strFound
343 ActiveDocument.Selection.MoveTo _
344 ActiveDocument.Selection.CurrentLine, nCol
345 ActiveDocument.Selection.NewLine
346 end if
347 end if
348 end if
349 wend
350
351 end sub
352
353 ' Trim blanks AND tabs from the left side
354 function LTrimTabs(str)
355 dim iPos, ch
356
357 for iPos = 1 to Len(str)
358 mike 1.1 ch = Mid(str, iPos, 1)
359 if ch <> " " and ch <> vbTab then
360 exit for
361 end if
362 next
363
364 LTrimTabs = Mid(str, iPos)
365
366 end function
367
368 ' Isolate the given str on a new line with nothing left of it
369 sub IsolateOnLeft(str)
370 dim strLastPos, nLen, bContinue, nCurrentLine, nCurrentColumn
371
372 strLastPos = InitializePosition
373
374 while ActiveDocument.Selection.FindText("^.*" & str, dsMatchRegExp) and _
375 GetCurrentPosition(strLastPos)
376
377 ' Check if we're inside a comment or between quotes
378 if not ShouldIgnore then
379 mike 1.1
380 ' Repeat the search since ShouldIgnore puts the cursor at the
381 ' beginning of the line
382 ActiveDocument.Selection.FindText "^.*" & str, dsMatchRegExp
383
384 ' Get the length of the found string
385 ' (which may have been a regular expression)
386 ActiveDocument.Selection.CharRight
387 ActiveDocument.Selection.FindText str, _
388 dsMatchBackward + dsMatchRegExp
389 nLen = Len(ActiveDocument.Selection)
390
391 ActiveDocument.Selection.CharLeft
392 if not ShouldIgnore then
393
394 ' Now that we have the length, we need to redo
395 ' the search and go on
396 ActiveDocument.Selection.StartOfLine
397 ActiveDocument.Selection.FindText "^.*" & str, dsMatchRegExp
398
399 bContinue = false
400 mike 1.1
401 ' If we're isolating a brace, make sure its matching brace
402 ' isn't on the same line
403 if (str = "{") or (str = "}") then
404 ActiveDocument.Selection.CharRight
405 nCurrentLine = ActiveDocument.Selection.CurrentLine
406 nCurrentColumn = ActiveDocument.Selection.CurrentColumn
407 ActiveDocument.Selection.CharLeft
408
409 ExecuteCommand "GoToMatchBrace"
410 if ActiveDocument.Selection.CurrentLine = nCurrentLine then
411 ActiveDocument.Selection.MoveTo _
412 nCurrentLine, nCurrentColumn
413 bContinue = true ' we found it so move to the next match
414 else
415 ActiveDocument.Selection.MoveTo nCurrentLine, 1
416 ActiveDocument.Selection.FindText "^.*" & str, _
417 dsMatchRegExp
418 end if
419 end if
420
421 mike 1.1
422 if LTrimTabs(ActiveDocument.Selection) <> str and _
423 not bContinue then
424 ActiveDocument.Selection.CharRight
425 MoveByColumns -nLen
426 ActiveDocument.Selection.NewLine
427 MoveByColumns nLen
428 end if
429
430 GetCurrentPosition strLastPos
431 end if
432 end if
433
434 wend
435
436 end sub
437
438 ' Isolate the given str so that everything after it is placed on the next line
439 sub IsolateOnRight(str)
440 dim strLastPos, strRight, nCurrentLine, nCurrentColumn, nLen
441
442 mike 1.1 strLastPos = InitializePosition
443
444 while ActiveDocument.Selection.FindText(str & ".+$", dsMatchRegExp) and _
445 GetCurrentPosition(strLastPos)
446
447 ' Check if we're inside a comment or between quotes
448 ActiveDocument.Selection.CharLeft
449 if not ShouldIgnore then
450
451 ' Repeat the search since ShouldIgnore puts the cursor at the
452 ' beginning of the line
453 ActiveDocument.Selection.FindText str & ".+$", dsMatchRegExp
454
455 ' Get the length of the found string
456 ' (which may have been a regular expression)
457 ActiveDocument.Selection.CharLeft
458 ActiveDocument.Selection.FindText str, dsMatchRegExp
459 nLen = Len(ActiveDocument.Selection)
460
461 ' Now that we have the length, we need to redo the search and go on
462 ActiveDocument.Selection.CharLeft
463 mike 1.1 ActiveDocument.Selection.FindText str & ".+$", dsMatchRegExp
464
465 strRight = LTrimTabs(Mid(ActiveDocument.Selection, nLen + 1))
466
467 ' Handle braces a bit differently
468 if (str = "{") or (str = "}") then
469
470 ' If it's a closing brace, and the code after it contains
471 ' a semicolon, leave it alone (ie. variable definition).
472 if (str = "}") then
473 ActiveDocument.Selection.EndOfLine dsExtend
474 if (InStr(1, ActiveDocument.Selection, ";", vbTextCompare) _
475 > 0) then
476 strRight = "" ' we found it so move on to the next match
477 end if
478 ActiveDocument.Selection.CharLeft
479 end if
480
481 ' If we're isolating a brace make sure the matching brace
482 ' isn't on the same line
483 if (strRight <> "") then
484 mike 1.1 ActiveDocument.Selection.CharLeft
485 nCurrentLine = ActiveDocument.Selection.CurrentLine
486 nCurrentColumn = ActiveDocument.Selection.CurrentColumn
487
488 ExecuteCommand "GoToMatchBrace"
489 if ActiveDocument.Selection.CurrentLine = nCurrentLine then
490 ActiveDocument.Selection.MoveTo _
491 nCurrentLine, nCurrentColumn + 1
492 strRight = "" ' we found it so move on to the next match
493 else
494 ActiveDocument.Selection.MoveTo _
495 nCurrentLine, nCurrentColumn
496 ActiveDocument.Selection.FindText _
497 str & ".+$", dsMatchRegExp
498 end if
499 end if
500 end if
501
502 if (strRight <> "") and _
503 (Left(strRight, 1) <> "/") and _
504 (strRight <> ",") and _
505 mike 1.1 (strRight <> ";") and _
506 (strRight <> "\") then
507 ActiveDocument.Selection.CharLeft
508 MoveByColumns nLen
509 ActiveDocument.Selection.NewLine
510 end if
511
512 end if
513 wend
514
515 end sub
516
517 ' Place the given strOperator (of nLen REAL characters)
518 ' between spaces (if needed)
519 sub FixOperator(strOperator, nLen)
520 dim strLastPos, strFind
521
522 strLastPos = InitializePosition
523
524 ' Add one space between the operator
525 while ActiveDocument.Selection.FindText("[A-Z,a-z,0-9,\),_,\]]" & _
526 mike 1.1 strOperator & "[A-Z,a-z,0-9,\(,_,\*,"",',&]", dsMatchRegExp) and _
527 GetCurrentPosition(strLastPos)
528
529 ' Check if we're inside a comment or between quotes
530 ActiveDocument.Selection.CharLeft
531 if not ShouldIgnore then
532
533 ' Repeat the search since ShouldIgnore puts the cursor at the
534 ' beginning of the line
535 ActiveDocument.Selection.FindText "[A-Z,a-z,0-9,\),_,\]]" & _
536 strOperator & "[A-Z,a-z,0-9,\(,_,\*,"",',&]", dsMatchRegExp
537
538 ActiveDocument.Selection.CharLeft
539 ActiveDocument.Selection.CharRight
540 ActiveDocument.Selection = " "
541 MoveByColumns nLen
542 ActiveDocument.Selection = " "
543
544 end if
545 wend
546
547 mike 1.1 strLastPos = InitializePosition
548
549 ' Fix any C++ "operator" member functions which were broken above
550 while ActiveDocument.Selection.FindText("operator " & strOperator & " ", _
551 dsMatchRegExp) and GetCurrentPosition(strLastPos)
552
553 ' Check if we're inside a comment or between quotes
554 if not ShouldIgnore then
555
556 ' Repeat the search since ShouldIgnore puts the cursor at the
557 ' beginning of the line
558 ActiveDocument.Selection.FindText "operator " & strOperator & " ", _
559 dsMatchRegExp
560 ActiveDocument.Selection.CharRight
561 ActiveDocument.Selection.Backspace
562 MoveByColumns -nLen
563 ActiveDocument.Selection.Backspace
564
565 end if
566 wend
567
568 mike 1.1 end sub
569
570 ' Fix < operator without altering template<T> code and operator <<
571 function FixLessThanOperator()
572 dim strLastPos, strFound, strTemplate
573
574 strLastPos = InitializePosition
575
576 while ActiveDocument.Selection.FindText("^.*[^ <]<.", dsMatchRegExp) and _
577 GetCurrentPosition(strLastPos)
578
579 ' Check if we're inside a comment or between quotes
580 if not ShouldIgnore then
581
582 ' Repeat the search since ShouldIgnore puts the cursor at the
583 ' beginning of the line
584 ActiveDocument.Selection.FindText "^.*[^ <]<.", dsMatchRegExp
585
586 strFound = ActiveDocument.Selection
587
588 ' Fix the left side
589 mike 1.1 strFound = Left(strFound, Len(strFound) - 2) & " " & _
590 Right(strFound, 2)
591 ActiveDocument.Selection = strFound
592
593 ' Fix the right side
594 strTemplate = Right(strFound, 11)
595 if (Left(strTemplate, 8) <> "template") and _
596 (Right(strFound, 1) <> " ") and _
597 (Right(strFound, 1) <> "=") and _
598 (Right(strFound, 1) <> "<") and _
599 (Right(strFound, 1) <> ">")then
600 ActiveDocument.Selection.CharLeft
601 ActiveDocument.Selection = " "
602 end if
603
604 end if
605 wend
606
607 end function
608
609 ' Append a space after the given strOperator (if it needs it)
610 mike 1.1 sub AppendSpace(strOperator)
611 dim strLastPos
612
613 strLastPos = InitializePosition
614
615 while ActiveDocument.Selection.FindText(strOperator & _
616 "[A-Z,a-z,0-9,\(,\-,_,\*,"",',&]", dsMatchRegExp) and _
617 GetCurrentPosition(strLastPos)
618
619 ' Check if we're inside a comment or between quotes
620 ActiveDocument.Selection.CharLeft
621 if not ShouldIgnore then
622
623 ActiveDocument.Selection.FindText strOperator & _
624 "[A-Z,a-z,0-9,\(,\-,_,\*,"",',&]", dsMatchRegExp
625
626 ActiveDocument.Selection.CharLeft
627 MoveByColumns Len(strOperator)
628 ActiveDocument.Selection = " "
629
630 end if
631 mike 1.1 wend
632
633 end sub
634
635 ' Fix tabbing within function blocks (surrounded by braces)
636 function TabifyMatchingBraces()
637 dim strLastPos, cBeforeBrace
638
639 strLastPos = InitializePosition
640
641 while ActiveDocument.Selection.FindText("{") and _
642 GetCurrentPosition(strLastPos)
643
644 ' Check if we're inside a comment or between quotes
645 if not ShouldIgnore then
646
647 ' Repeat the action since ShouldIgnore puts the cursor at the
648 ' beginning of the line
649 ActiveDocument.Selection.FindText "{"
650
651 ' Go to matching brace and reformat tabs
652 mike 1.1 ExecuteCommand "GoToMatchBraceExtend"
653 ActiveDocument.Selection.SmartFormat
654
655 cBeforeBrace = Mid(ActiveDocument.Selection, _
656 Len(ActiveDocument.Selection) - 1, 1)
657
658 ' If SmartFormat indents the block (by mistake), unindent it
659 if (cBeforeBrace = vbTab or cBeforeBrace = " ") then
660 ActiveDocument.Selection.Unindent
661 end if
662 end if
663 wend
664
665 end function
666
667 ' Since Microsoft's "SmartFormat" is not smart enough to indent case
668 ' statements inside the switch body, we'll do it here.
669 ' (Thanks to Jim Cooper)
670 function IndentSwitchBody()
671 dim nSwitchLine, nFirstLine, nLastLine, strLastPos, iLine
672
673 mike 1.1 strLastPos = InitializePosition
674
675 while ActiveDocument.Selection.FindText("switch", _
676 dsMatchWord + dsMatchCase) and GetCurrentPosition(strLastPos)
677
678 ' Check if we're inside a comment or between quotes
679 if not ShouldIgnore then
680
681 nSwitchLine = ActiveDocument.Selection.CurrentLine
682
683 ' Now find the opening brace and make sure it's on the next line
684 if ActiveDocument.Selection.FindText("{") and _
685 not ShouldIgnore and _
686 (ActiveDocument.Selection.CurrentLine = nSwitchLine + 1) then
687
688 ' Repeat the action since ShouldIgnore puts the cursor at the
689 ' beginning of the line
690 ActiveDocument.Selection.FindText "{"
691
692 ' Find next line in file, since earlier code put '{' on
693 ' a line by itself
694 mike 1.1 nFirstLine = ActiveDocument.Selection.CurrentLine + 1
695
696 ' Go to matching brace and reformat tabs
697 ExecuteCommand "GoToMatchBrace"
698
699 ' Find previous line in file, since earlier code put '}' on
700 ' line by itself
701 nLastLine = ActiveDocument.Selection.CurrentLine
702
703 ' Move to the line after the opening brace
704 ActiveDocument.Selection.GoToLine nFirstLine, 1
705
706 ' Select everything between the braces and indent it
707 for iLine = nFirstLine to nLastLine - 1
708 ActiveDocument.Selection.LineDown dsExtend
709 next
710 ActiveDocument.Selection.Indent
711 end if
712 end if
713 wend
714 end function
715 mike 1.1
716 ' Remove any lines that are considered extraneous (usually blank ones).
717 function RemoveExtraneousLines()
718 dim strLastPos, nCurrentLine, nCurrentColumn
719
720 strLastPos = InitializePosition
721
722 ' Remove any blank lines that fall below any open braces ("{")
723 while ActiveDocument.Selection.FindText("{") and _
724 GetCurrentPosition(strLastPos)
725
726 ' Check if we're inside a comment or between quotes
727 if not ShouldIgnore then
728 ' Repeat the action since ShouldIgnore puts the cursor at the
729 ' beginning of the line
730 ActiveDocument.Selection.FindText "{"
731
732 nCurrentLine = ActiveDocument.Selection.CurrentLine
733 nCurrentColumn = ActiveDocument.Selection.CurrentColumn
734
735 ActiveDocument.Selection.LineDown
736 mike 1.1
737 ' Cut any blank lines below the {
738 do while true
739 ActiveDocument.Selection.StartOfLine
740 ActiveDocument.Selection.EndOfLine dsExtend
741
742 if LTrimTabs(ActiveDocument.Selection) <> "" then
743 exit do
744 end if
745 ExecuteCommand "LineCut"
746
747 ' Make sure we haven't hit the bottom of the file
748 ActiveDocument.Selection.EndOfDocument
749 if ActiveDocument.Selection.CurrentLine = nCurrentLine + 1 then
750 exit do
751 end if
752 ActiveDocument.Selection.MoveTo nCurrentLine + 1, 1
753 loop
754
755 ActiveDocument.Selection.MoveTo nCurrentLine, nCurrentColumn
756 end if
757 mike 1.1 wend
758
759 strLastPos = InitializePosition
760
761 ' Remove any blank lines right above any closing braces ("}")
762 while ActiveDocument.Selection.FindText("}") and _
763 GetCurrentPosition(strLastPos)
764
765 ' Check if we're inside a comment or between quotes
766 if not ShouldIgnore then
767 ' Repeat the action since ShouldIgnore puts the cursor at the
768 ' beginning of the line
769 ActiveDocument.Selection.FindText "}"
770 ActiveDocument.Selection.CharLeft
771
772 ' Cut blank lines above the }
773 do while true
774 ActiveDocument.Selection.LineUp
775 ActiveDocument.Selection.StartOfLine
776 ActiveDocument.Selection.EndOfLine dsExtend
777
778 mike 1.1 if LTrimTabs(ActiveDocument.Selection) <> "" then
779
780 if ActiveDocument.Selection.CurrentLine > 1 then
781 ActiveDocument.Selection.LineDown
782 end if
783
784 ActiveDocument.Selection.StartOfLine
785 ActiveDocument.Selection.FindText "}"
786 strLastPos = ActiveDocument.Selection.CurrentLine & "," & _
787 ActiveDocument.Selection.CurrentColumn
788
789 ActiveDocument.Selection.LineDown
790 ActiveDocument.Selection.StartOfLine
791 exit do
792 end if
793 ExecuteCommand "LineCut"
794 loop
795 end if
796 wend
797
798 end function
799 mike 1.1
800 ' Remove all spaces and tabs found in the current selection
801 function RemoveSpacesInSelection
802 dim iPos, ch, strNoSpaces
803
804 for iPos = 1 to Len(ActiveDocument.Selection)
805 ch = Mid(ActiveDocument.Selection, iPos, 1)
806 if ch <> " " and ch <> vbTab then
807 strNoSpaces = strNoSpaces + ch
808 end if
809 next
810
811 ActiveDocument.Selection = strNoSpaces
812 end function
813
814 ' Fix any code with exponential notation (ie. 3.4e-2) which was
815 ' broken when the + and - operators were fixed above (by FixOperator).
816 function FixExponents
817
818 while ActiveDocument.Selection.FindText("[0-9,\.]e [\+\!\-] [0-9]", _
819 dsMatchRegExp)
820 mike 1.1 RemoveSpacesInSelection
821 wend
822
823 end function
824
825 ' Break any lines containing multiple statements (separated by semicolons)
826 function BreakLinesWithMultipleStatements()
827 dim strLastPos, nCurrentLine
828
829 strLastPos = InitializePosition
830
831 ' Search for multiple semicolons on the same line
832 while ActiveDocument.Selection.FindText(";.+;", dsMatchRegExp) and _
833 GetCurrentPosition(strLastPos)
834
835 ' Check if we're inside a comment or between quotes
836 if not ShouldIgnore then
837 ' Repeat the action since ShouldIgnore puts the cursor at the
838 ' beginning of the line
839 ActiveDocument.Selection.FindText ";.+;", dsMatchRegExp
840
841 mike 1.1 nCurrentLine = ActiveDocument.Selection.CurrentLine
842 ActiveDocument.Selection.CharLeft
843 ActiveDocument.Selection.StartOfLine dsFirstText, dsExtend
844
845 ' If found, check that the semicolons don't belong to a for loop
846 if (InStr(1, ActiveDocument.Selection, "for (", _
847 vbTextCompare) > 0) then
848 ActiveDocument.Selection.MoveTo nCurrentLine + 1, 1
849 else
850 ActiveDocument.Selection.CharRight
851 ActiveDocument.Selection.CharRight
852 ActiveDocument.Selection.NewLine
853 end if
854 end if
855 wend
856
857 end function
858
859
860
861 Sub MakeSelectedCodeNicer()
862 mike 1.1 'DESCRIPTION: Reformats the currently selected source code to look nicer.
863 ' Written by Alvaro Mendez on 07/15/1999
864
865 ' Check if there's a valid selection
866 if ActiveDocument.Selection = "" then
867 exit sub
868 end if
869
870 ' Copy the selection to the clipboard
871 ActiveDocument.Selection.Copy
872
873 ' Open a new document and changed its language to C/C++
874 ' This is required for SmartIndent to work.
875 Documents.Add "Text"
876 ExecuteCommand "Properties"
877 ActiveDocument.Language = "C/C++"
878
879 ' Paste the selection into the document and run the macro
880 ActiveDocument.Selection.Paste
881 ExecuteCommand "MakeCodeNicer"
882
883 mike 1.1 ' Select the resulting code and copy it to the clipboard
884 ActiveDocument.Selection.SelectAll
885 ActiveDocument.Selection.Copy
886
887 ' Close the new document (without saving it)
888 ActiveWindow.Close dsSaveChangesNo
889
890 ' Paste the reformatted code back over the original selection
891 ActiveDocument.Selection.Paste
892
893 End Sub
894
|