Code: Select all
;{- Enumerations / DataSections
Global NewMap decrement.S()
Global Compteur_tab=0
decrement.S("endenumeration")="endenumeration"
decrement.S("endstructure")="endstructure"
decrement.S( "endprocedure")="endprocedure"
decrement.S( "enddatasection")="enddatasection"
decrement.S( "endselect")="endselect"
decrement.S( "stopdrawing")="stopdrawing"
decrement.S( "endinterface")="endinterface"
decrement.S( "endstructure")="endstructure"
decrement.S( "endimport")= "endimport"
decrement.S( "endmacro")= "endmacro"
decrement.S( "endwith")="endwith"
decrement.S( "endstructureunion")="endstructureunion"
decrement.S("stop3d")="stop3d"
decrement.S("compilerendif")="compilerendif"
decrement.S("compilerendselect")="compilerendselect"
decrement.S("endmodule")="endmodule"
decrement.S("enddeclaremodule")="enddeclaremodule"
decrement.S("closefile")="closefile"
decrement.S("next")="next"
decrement.S("until")="until"
decrement.S("forever")="forever"
decrement.S("wend")="wend"
decrement.S( "endif")="endif"
; *******************************************
; attention l'ordre des termes a son importance
Global NewMap increment.S()
increment.S( "enumeration")="enumeration"
increment.S( "datasection")="datasection"
increment.S( "startdrawing")= "startdrawing"
increment.S( "structure")= "structure"
increment.S( "proceduredll")= "proceduredll"
increment.S( "procedurecdll")= "procedurecdll"
increment.S( "procedurec")="procedurec"
increment.S( "procedure")= "procedure"
increment.S( "importc")= "importc"
increment.S( "import")= "import"
increment.S( "macro")="macro"
increment.S( "interface")= "interface"
increment.S( "structureunion")= "structureunion"
increment.S("start3d")="start3d"
increment.S("compilerif")="compilerif"
increment.S("compilerselect")="compilerselect"
increment.S("module")="module"
increment.S("declaremodule")="declaremodule"
increment.S( "with")= "with"
increment.S( "select")= "select"
increment.S("openfile")="openfile"
increment.S("readfile")="readfile"
increment.S("createfile")="createfile"
increment.S( "foreach")="foreach"
increment.S( "for")="for"
increment.S( "repeat")="repeat"
increment.S( "while")="while"
increment.S( "if")="if"
increment.S("case")="case"
;{ Windows
Enumeration
#Win
EndEnumeration
;}
;{ Gadgets
Enumeration
#Button_Load
#Text_titre
#Editor
#tabulation
#detabulation
EndEnumeration
;}
;{ Fonts
Enumeration
#Font_Text_titre
EndEnumeration
Declare openwindow_win()
Declare pos_com_max()
Declare.s tabulation(txt.s)
;}
Define.l Event
Global NewList code.S()
Global pos_max
;}
OpenWindow_Win()
;{- Event loop
Repeat
Event = WaitWindowEvent(12)
Select Event
; ///////////////////
Case #PB_Event_Menu ; Alt+Tab
Select EventMenu()
Case #tabulation
; ; cherche la position max d'un ";"
; pos_max=pos_com_max()
; ClearList(code.s())
; For i=0 to CountGadgetItems(#editor)
; line.S=GetGadgetItemText(#editor, i)
; a$=LTrim(line.S,Chr(9))
; a$=LTrim(a$)
; AddElement(code.S())
; code.S()=a$
; Next i
ClearGadgetItems(#editor)
ForEach code.S()
txt.S=code.S()
AddGadgetItem(#editor, -1, tabulation(txt.S) ) ; ajoute chaque ligne tabuléees
Next
Case #detabulation
ClearList(code.s())
For i=0 to CountGadgetItems(#editor)
line.S=GetGadgetItemText(#editor, i)
a$=LTrim(line.S,Chr(9))
a$=LTrim(a$)
AddElement(code.S())
code.S()=a$
Next i
ClearGadgetItems(#editor)
ForEach code.S()
txt.S=code.S()
AddGadgetItem(#editor, -1,txt.S ) ; ajoute chaque ligne tabuléees
Next
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case #Button_Load
local.S=GetCurrentDirectory()
file.S=OpenFileRequester("ouvrir code",local.S,"*.pb",1)
OpenFile(0,file.S)
While Eof(0) = 0 ; Boucle tant que la fin du fichier n'est pas atteinte. (Eof = 'End Of File')
line.S=ReadString(0)
a$=LTrim(line.S,Chr(9))
a$=LTrim(a$)
AddElement(code.S())
code.S()=a$
Wend
CloseFile(0)
; Tabulation au chargement
ForEach code.S()
txt.S=code.S()
AddGadgetItem(#editor, -1, tabulation(txt.S) ) ; ajoute chaque ligne tabuléees
Next
Case #Text_titre
Case #Editor
EndSelect
; ////////////////////////
Case #PB_Event_CloseWindow
Select EventWindow()
Case #Win
CloseWindow(#Win)
Break
EndSelect
EndSelect
Forever
;-Zone Procedures
Procedure OpenWindow_Win()
If OpenWindow(#Win, 405, 47, 720, 597, "Exemple indentation", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
ButtonGadget(#Button_Load, 20, 20, 90, 35, "Load *.Pb")
TextGadget(#Text_titre, 205, 15, 255, 40, "Exemple Tabulation")
EditorGadget(#Editor, 25, 80, 680, 500)
; Gadget Fonts
SetGadgetFont(#Text_titre, LoadFont(#Font_Text_titre, "Microsoft Sans Serif", 20, #PB_Font_HighQuality))
; Raccourcis :
AddKeyboardShortcut(#win, #PB_Shortcut_Control | #PB_Shortcut_Tab, #tabulation)
AddKeyboardShortcut(#win, #PB_Shortcut_Shift | #PB_Shortcut_Tab, #detabulation)
EndIf
EndProcedure
Procedure pos_com_max()
; on recherche dans tout le code la possition max d'un commentaire (le plus a droite dans une ligne )
For i=0 to CountGadgetItems(#editor)
line.S=GetGadgetItemText(#editor, i)
a$=LTrim(line.S,Chr(9))
a$=LTrim(a$)
pos=FindString(a$,";") ; on recherche ";"
if pos>mem_pos ; si la position est plus grande
mem_pos=pos ; on garde
Endif
Next i
ProcedureReturn mem_pos ; voici la position maximum d'un ";" dans le code
EndProcedure
Procedure.S tabulation(txt.S)
; procedure de tabulation by Dobro (plus court tu meurts !! )
;
; pos= findstring(StringField(txt.S,1,chr(34)),";")
; if pos<pos_max and Pos>0
; txt.s=RemoveString(txt.s,chr(9))
;
; nbbn=pos_max-pos
;
; tt.s=""
; For z=1 to nbbn
; tt.s=tt.s+" "
; Next z
; txt.s=ReplaceString(txt.s,";",tt.s +";" )
; nbbn-(Compteur_tab*3)
; Endif
;
extrait.S=LCase(StringField(txt.S,1,":")) ; recup avant multipligne
; extrait.S=StringField(extrait.S,1,";"); recupe avant commentaire : ceci ne tabule pas les comentaires ... c'est un choix
extrait.S=StringField(extrait.S,1," "); recupe premier mot
extrait.S=StringField(extrait.S,1,"("); pour les fonctions
extrait.S=StringField(extrait.S,1,"."); pour les fonctions (typées)
extrait.S=LCase(Trim(extrait.S))
; If extrait.S="" :Goto su :EndIf ; saute les lignes vides
tab.S=LSet(tab.S,Compteur_tab,Chr(9)) ; pour l'increment se fait dans un deuxieme appel
; -- Else
If extrait.S="else" Or extrait.S="elseif" Or extrait.S="case"
mem_Compteur_tab=Compteur_tab
Compteur_tab=Compteur_tab-1
tab.S=LSet(tab.S,Compteur_tab,Chr(9)) ; pour un decrement, c'est tout de suite
Compteur_tab=mem_Compteur_tab
Goto su
EndIf
;++++ increment
If extrait.S= increment.S(extrait.S) And extrait.S<>""
; regarde si un decrement se trouve sur la meme ligne qu'un increment , dans ce cas on tabule en arriere puis on remet la valeur de tabulation
extrait2.S=StringField(txt.S,1,";")
ForEach decrement.S()
pooo=FindString(extrait2.S,decrement.S(),Len(extrait.S),#PB_String_NoCase)
If pooo>0
If Asc(Mid( extrait2.S,pooo+Len(decrement.S()),1))<33
Goto su
Else
Break
EndIf
EndIf
Next
; regarde si un decrement se trouve sur la meme ligne qu'un increment dans ce cas on ajoute une tabulation
extrait2.S=StringField(txt.S,1,"(") :extrait2.S=StringField(extrait2.S,1,"."):extrait2.S=StringField(extrait2.S,1,":")
ForEach increment.S()
;If FindString(extrait2.S,increment.S(),Len(extrait.S),#PB_String_NoCase)>0
pooo=FindString(extrait2.S,increment.S(),Len(extrait.S),#PB_String_NoCase) ; on a trouvée un semblant d'increment
If pooo>0
If Asc(Mid( extrait2.S,pooo+Len(increment.S()),1))<33; on regarde le caractere suivant, si inf a 33 c'est vrais c'est un increment
Compteur_tab=Compteur_tab+1
Break
EndIf
EndIf
Next
;
Compteur_tab=Compteur_tab+1
Goto su
EndIf
;----- decrement
If extrait.S= decrement.S(extrait.S) And extrait.S<>""
Compteur_tab=Compteur_tab-1
tab.S=LSet(tab.S,Compteur_tab,Chr(9)) ; pour un decrement, c'est tout de suite
Goto su
EndIf
su:
If Compteur_tab<=0:Compteur_tab=0
tab.S=""
EndIf
txt.S=tab.S+txt.S;+"("+str(compteur_tab)+")"
ProcedureReturn txt.S
EndProcedure
;
;}