Sub coloration_caml() ' ' coloration_caml Macro ' Macro enregistrée le 13/05/2006 par Mathias ' La fonction reconait tout les motsclefs de Caml (je crois), les chaines de caractères et les commentaires. ' Pour qu'elle fonctionne, il vous faut des styles "Code", "Code : instructions", "Code : comentaires" et "Code : chaines de caractères". ' Le code à colorer doit avoir le style "Code", les autres styles sont utilisé par la fonction. 'On reconnait les mots clefs ci-dessous 'Les commentaires, y compris s'ils ont imbriqués. 'Les chaines de caractères en évitant le caractère " s'il est compris entre des `. Const N As Integer = 27 Dim keyword(1 To N) As String keyword(1) = "if" keyword(2) = "let" keyword(3) = "for" keyword(4) = "to" keyword(5) = "downto" keyword(6) = "do" keyword(7) = "done" keyword(8) = "begin" keyword(9) = "end" keyword(10) = "and" keyword(11) = "match" keyword(12) = "with" keyword(13) = "true" keyword(14) = "false" keyword(15) = "fun" keyword(16) = "function" keyword(17) = "rec" keyword(18) = "then" keyword(19) = "else" keyword(20) = "in" keyword(21) = "type" keyword(22) = "int" keyword(23) = "list" keyword(24) = "vect" keyword(25) = "float" keyword(26) = "char" keyword(27) = "bool" Dim i As Integer Dim j As Integer Dim k As Integer 'Recherche des mots clefs. For i = 1 To N Selection.Find.ClearFormatting With Selection.Find .Text = keyword(i) .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = True .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Style = ActiveDocument.Styles("Code") End With While Selection.Find.Execute 'Ici on évite que dans des identifiant du type do_list les mots clefs do et list soient reconnus. j = Selection.Start k = Selection.End Selection.Start = j - 1 Selection.End = j If Selection = "_" Then GoTo finwhile Selection.Start = k Selection.End = k + 1 If Selection = "_" Then GoTo finwhile Selection.Start = j Selection.End = k Selection.Style = ActiveDocument.Styles("Code : instructions") GoTo finfinwhile finwhile: Selection.Start = j Selection.End = k finfinwhile: Wend Next 'Parcourt du texte à la recherche de chaines de caractères ou de commentaires. For i = 0 To Selection.StoryLength - 1 Selection.Start = i Selection.End = i 'If Selection.Style = Nothing Then GoTo finboucle If Selection.Style <> ActiveDocument.Styles("Code") Then GoTo finboucle Selection.End = i + 1 If Selection.Text = Chr(34) Then j = i + 1 Selection.Start = j Selection.End = i + 2 If Selection.Text <> "'" And Selection.Text <> "`" Then Do Selection.End = j + 1 If Selection.Text = Chr(34) Then Selection.Start = i Selection.Style = ActiveDocument.Styles("Code : chaines de caractères") i = j Exit Do End If j = j + 1 Loop While j < Selection.StoryLength End If End If Selection.Start = i Selection.End = i + 2 If Selection.Text = "(*" Then j = i + 1 k = 1 Do Selection.Start = j Selection.End = j + 2 If Selection.Text = "*)" Then k = k - 1 ElseIf Selection.Text = "(*" Then k = k + 1 End If If k = 0 Then Selection.Start = i Selection.Style = ActiveDocument.Styles("Code : commentaires") i = j Exit Do End If j = j + 1 Loop While j < Selection.StoryLength End If finboucle: Next i End Sub