Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/elfmz/far2l.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/align
diff options
context:
space:
mode:
authorelfmz <fenix1905@tut.by>2016-11-17 23:32:46 +0300
committerelfmz <fenix1905@tut.by>2016-11-17 23:32:46 +0300
commit52235a4ae4a15006f2f2c13ccb6bcfa5623fa192 (patch)
tree6340f25a50deed3bcf650bb918642cd13c46e28c /align
parent581b9d30f6949ee87fe2032e1639e63ac5ebb6cc (diff)
ported align plugin
Diffstat (limited to 'align')
-rw-r--r--align/CMakeLists.txt33
-rw-r--r--align/configs/plug/AlignEng.lng9
-rw-r--r--align/configs/plug/AlignRus.lng9
-rw-r--r--align/src/Align.cpp383
-rw-r--r--align/src/Align.hpp23
-rw-r--r--align/src/AlignLng.hpp10
-rw-r--r--align/src/AlignMix.icpp38
7 files changed, 505 insertions, 0 deletions
diff --git a/align/CMakeLists.txt b/align/CMakeLists.txt
new file mode 100644
index 00000000..4732a4d1
--- /dev/null
+++ b/align/CMakeLists.txt
@@ -0,0 +1,33 @@
+
+project(align)
+
+set(SOURCES
+src/Align.cpp
+)
+
+add_library (align MODULE ${SOURCES})
+
+if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
+ set(CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS "${CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS} -flat_namespace -undefined suppress")
+endif()
+
+target_link_libraries(align utils)
+
+target_compile_definitions(align PRIVATE -DWINPORT_DIRECT -DUNICODE -DFAR_DONT_USE_INTERNALS)
+
+target_include_directories(align PRIVATE .)
+target_include_directories(align PRIVATE ../far2l)
+target_include_directories(align PRIVATE ../far2l/Include)
+target_include_directories(align PRIVATE ../WinPort)
+
+set_target_properties(align
+ PROPERTIES
+ LIBRARY_OUTPUT_DIRECTORY "${INSTALL_DIR}/Plugins/align/plug"
+ PREFIX ""
+ SUFFIX ".far-plug-wide")
+
+
+add_custom_command(TARGET align POST_BUILD
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/configs
+ COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/configs "${INSTALL_DIR}/Plugins/align"
+)
diff --git a/align/configs/plug/AlignEng.lng b/align/configs/plug/AlignEng.lng
new file mode 100644
index 00000000..18f566fd
--- /dev/null
+++ b/align/configs/plug/AlignEng.lng
@@ -0,0 +1,9 @@
+.Language=English,English
+
+"Ok"
+"Cancel"
+"Align block"
+"Right &margin"
+"&Reformat block"
+"Do not leave &short words at line ends"
+"&Justify right border" \ No newline at end of file
diff --git a/align/configs/plug/AlignRus.lng b/align/configs/plug/AlignRus.lng
new file mode 100644
index 00000000..579e9a8e
--- /dev/null
+++ b/align/configs/plug/AlignRus.lng
@@ -0,0 +1,9 @@
+.Language=Russian,Russian (Русский)
+
+"Продолжить"
+"Отменить"
+"Выровнять блок"
+"&Отступ справа"
+"&Переформатировать блок"
+"Не оставлять &короткие слова в конце строк"
+"&Выровнять правую границу"
diff --git a/align/src/Align.cpp b/align/src/Align.cpp
new file mode 100644
index 00000000..b16ea782
--- /dev/null
+++ b/align/src/Align.cpp
@@ -0,0 +1,383 @@
+#include "../../etc/plugs.h"
+
+
+#include "AlignLng.hpp"
+#include "Align.hpp"
+#include "../../etc/WrapReg.icpp"
+#include "AlignMix.icpp"
+
+#ifndef UNICODE
+#define GetCheck(i) DialogItems[i].Selected
+#define GetDataPtr(i) DialogItems[i].Data
+#else
+#define GetCheck(i) (int)Info.SendDlgMessage(hDlg,DM_GETCHECK,i,0)
+#define GetDataPtr(i) ((const TCHAR *)Info.SendDlgMessage(hDlg,DM_GETCONSTTEXTPTR,i,0))
+#endif
+
+
+static void ReformatBlock(int RightMargin,int SmartMode,int Justify);
+static void JustifyBlock(int RightMargin);
+static int JustifyString(int RightMargin,struct EditorSetString &ess);
+
+SHAREDSYMBOL int WINAPI EXP_NAME(GetMinFarVersion)()
+{
+ return FARMANAGERVERSION;
+}
+
+
+SHAREDSYMBOL void WINAPI EXP_NAME(SetStartupInfo)(const struct PluginStartupInfo *Info)
+{
+ ::Info=*Info;
+ ::FSF=*Info->FSF;
+ ::Info.FSF=&::FSF;
+ lstrcpy(PluginRootKey,Info->RootKey);
+ lstrcat(PluginRootKey,_T("\\Align"));
+}
+
+
+SHAREDSYMBOL HANDLE WINAPI EXP_NAME(OpenPlugin)(int OpenFrom,INT_PTR Item)
+{
+ struct InitDialogItem InitItems[]={
+ {DI_DOUBLEBOX,3,1,72,8,0,0,0,0,(TCHAR *)MAlign},
+ {DI_FIXEDIT,5,2,7,3,1,0,0,0,_T("")},
+ {DI_TEXT,9,2,0,0,0,0,0,0,(TCHAR *)MRightMargin},
+ {DI_CHECKBOX,5,3,0,0,0,0,0,0,(TCHAR *)MReformat},
+ {DI_CHECKBOX,5,4,0,0,0,0,0,0,(TCHAR *)MSmartMode},
+ {DI_CHECKBOX,5,5,0,0,0,0,0,0,(TCHAR *)MJustify},
+ {DI_TEXT,5,6,0,0,0,0,DIF_BOXCOLOR|DIF_SEPARATOR,0,_T("")},
+ {DI_BUTTON,0,7,0,0,0,0,DIF_CENTERGROUP,1,(TCHAR *)MOk},
+ {DI_BUTTON,0,7,0,0,0,0,DIF_CENTERGROUP,0,(TCHAR *)MCancel}
+ };
+
+ struct FarDialogItem DialogItems[ARRAYSIZE(InitItems)];
+ InitDialogItems(InitItems,DialogItems,ARRAYSIZE(InitItems));
+ int RightMargin=GetRegKey(HKEY_CURRENT_USER,_T(""),_T("RightMargin"),75);
+ int Reformat=GetRegKey(HKEY_CURRENT_USER,_T(""),_T("Reformat"),TRUE);
+ int SmartMode=GetRegKey(HKEY_CURRENT_USER,_T(""),_T("SmartMode"),FALSE);
+ int Justify=GetRegKey(HKEY_CURRENT_USER,_T(""),_T("Justify"),FALSE);
+#ifdef UNICODE
+ wchar_t marstr[32];
+ DialogItems[1].PtrData = marstr;
+ FSF.snprintf(marstr,ARRAYSIZE(marstr),L"%d",RightMargin);
+#else
+ FSF.snprintf(DialogItems[1].Data,ARRAYSIZE(DialogItems[1].Data),"%d",RightMargin);
+#endif
+ DialogItems[3].Selected=Reformat;
+ DialogItems[4].Selected=SmartMode;
+ DialogItems[5].Selected=Justify;
+#ifndef UNICODE
+ int ExitCode=Info.Dialog(Info.ModuleNumber,-1,-1,76,10,NULL,DialogItems,
+ ARRAYSIZE(DialogItems));
+#else
+ HANDLE hDlg=Info.DialogInit(Info.ModuleNumber,-1,-1,76,10,NULL,DialogItems,
+ ARRAYSIZE(DialogItems),0,0,NULL,0);
+ if (hDlg == INVALID_HANDLE_VALUE)
+ return INVALID_HANDLE_VALUE;
+
+ int ExitCode=Info.DialogRun(hDlg);
+#endif
+ if (ExitCode!=7)
+ goto done;
+ RightMargin=FSF.atoi(GetDataPtr(1));
+ Reformat=GetCheck(3);
+ SmartMode=GetCheck(4);
+ Justify=GetCheck(5);
+ SetRegKey(HKEY_CURRENT_USER,_T(""),_T("Reformat"),Reformat);
+ SetRegKey(HKEY_CURRENT_USER,_T(""),_T("RightMargin"),RightMargin);
+ SetRegKey(HKEY_CURRENT_USER,_T(""),_T("SmartMode"),SmartMode);
+ SetRegKey(HKEY_CURRENT_USER,_T(""),_T("Justify"),Justify);
+ Info.EditorControl(ECTL_TURNOFFMARKINGBLOCK,NULL);
+ if (Reformat)
+ ReformatBlock(RightMargin,SmartMode,Justify);
+ else
+ if (Justify)
+ JustifyBlock(RightMargin);
+done:
+#ifdef UNICODE
+ Info.DialogFree(hDlg);
+#endif
+ return(INVALID_HANDLE_VALUE);
+}
+
+
+void ReformatBlock(int RightMargin,int SmartMode,int Justify)
+{
+ EditorInfo ei;
+ Info.EditorControl(ECTL_GETINFO,&ei);
+
+ if (ei.BlockType!=BTYPE_STREAM || RightMargin<1)
+ return;
+
+ struct EditorSetPosition esp;
+ memset(&esp,-1,sizeof(esp));
+ esp.CurLine=ei.BlockStartLine;
+ esp.CurPos=0;
+ Info.EditorControl(ECTL_SETPOSITION,&esp);
+
+ TCHAR *TotalString=NULL;
+ int TotalLength=0,IndentSize=0x7fffffff;
+
+ while (1)
+ {
+ struct EditorGetString egs;
+ egs.StringNumber=-1;
+ if (!Info.EditorControl(ECTL_GETSTRING,&egs))
+ break;
+ if (egs.SelStart==-1 || egs.SelStart==egs.SelEnd)
+ break;
+ int ExpandNum=-1;
+ Info.EditorControl(ECTL_EXPANDTABS,&ExpandNum);
+ Info.EditorControl(ECTL_GETSTRING,&egs);
+
+ int SpaceLength=0;
+ while (SpaceLength<egs.StringLength && egs.StringText[SpaceLength]==_T(' '))
+ SpaceLength++;
+
+ while (egs.StringLength>0 && *egs.StringText==_T(' '))
+ {
+ egs.StringText++;
+ egs.StringLength--;
+ }
+
+ if (egs.StringLength>0)
+ {
+ if (SpaceLength<IndentSize)
+ IndentSize=SpaceLength;
+
+ TotalString=(TCHAR *)realloc(TotalString,(TotalLength+egs.StringLength+2)*sizeof(TCHAR));
+ if (TotalLength!=0 && TotalString[TotalLength-1]!=_T(' '))
+ TotalString[TotalLength++]=_T(' ');
+
+ _tmemcpy(TotalString+TotalLength,egs.StringText,egs.StringLength);
+ TotalLength+=egs.StringLength;
+ }
+ if (!Info.EditorControl(ECTL_DELETESTRING,NULL))
+ {
+ free(TotalString);
+ return;
+ }
+ }
+ if(TotalString)
+ TotalString[TotalLength++]=_T(' ');
+
+ if (IndentSize>=RightMargin)
+ IndentSize=RightMargin-1;
+
+ const int MaxIndent=1024;
+ if (IndentSize>=MaxIndent)
+ IndentSize=MaxIndent-1;
+
+ TCHAR IndentBuf[MaxIndent];
+ if (IndentSize>0)
+ {
+ _tmemset(IndentBuf,_T(' '),IndentSize);
+ IndentBuf[IndentSize]=0;
+ }
+
+ Info.EditorControl(ECTL_SETPOSITION,&esp);
+ Info.EditorControl(ECTL_INSERTSTRING,NULL);
+ Info.EditorControl(ECTL_SETPOSITION,&esp);
+
+ int LastSplitPos=0,PrevSpacePos;
+ while (LastSplitPos<TotalLength && TotalString[LastSplitPos]==_T(' '))
+ LastSplitPos++;
+ PrevSpacePos=LastSplitPos;
+
+ for (int I=LastSplitPos;I<TotalLength;I++)
+ {
+ int Length=I-LastSplitPos;
+ int LastLength=PrevSpacePos-LastSplitPos;
+ if (TotalString[I]==_T(' ') && Length>RightMargin-IndentSize && LastLength<=RightMargin-IndentSize)
+ {
+ if (LastLength<=0)
+ {
+ PrevSpacePos=I;
+ LastLength=Length;
+ }
+
+ if (SmartMode)
+ {
+ int Space1=-1,Space2=-1;
+ for (int J=PrevSpacePos-1;J>LastSplitPos+20;J--)
+ {
+ if (TotalString[J]==_T(' '))
+ {
+ if (Space2==-1)
+ Space2=J;
+ else
+ {
+ if (Space1==-1)
+ Space1=J;
+ else
+ break;
+ }
+ }
+ }
+ if (Space2!=-1 && PrevSpacePos-Space2<4)
+ if (Space1==-1 || Space2-Space1>4 || PrevSpacePos-Space2==2)
+ {
+ PrevSpacePos=Space2;
+ while (PrevSpacePos>LastSplitPos+1 && TotalString[PrevSpacePos-1]==_T(' '))
+ PrevSpacePos--;
+ LastLength=PrevSpacePos-LastSplitPos;
+ }
+ }
+
+ I=PrevSpacePos;
+
+ struct EditorSetString ess;
+ ess.StringNumber=-1;
+ ess.StringText=TotalString+LastSplitPos;
+ ess.StringEOL=NULL;
+ ess.StringLength=LastLength;
+ while (ess.StringLength>0 && ess.StringText[ess.StringLength-1]==_T(' '))
+ ess.StringLength--;
+ if (!Justify || ess.StringLength>=RightMargin-IndentSize || !JustifyString(RightMargin-IndentSize,ess))
+ Info.EditorControl(ECTL_SETSTRING,&ess);
+ else
+ LastLength=RightMargin;
+
+ while (I<TotalLength && TotalString[I]==_T(' '))
+ PrevSpacePos=I++;
+
+ struct EditorSetPosition esp;
+ memset(&esp,-1,sizeof(esp));
+ esp.CurLine=-1;
+
+ if (IndentSize>0)
+ {
+ esp.CurPos=0;
+ Info.EditorControl(ECTL_SETPOSITION,&esp);
+ Info.EditorControl(ECTL_INSERTTEXT,(void *)IndentBuf);
+ }
+
+ esp.CurPos=LastLength+IndentSize;
+ Info.EditorControl(ECTL_SETPOSITION,&esp);
+ Info.EditorControl(ECTL_INSERTSTRING,NULL);
+
+ LastSplitPos=I;
+ }
+ if (TotalString[I]==_T(' '))
+ PrevSpacePos=I;
+ }
+ struct EditorSetString ess;
+ ess.StringNumber=-1;
+ ess.StringText=TotalString+LastSplitPos;
+ ess.StringEOL=NULL;
+ ess.StringLength=TotalLength-LastSplitPos;
+ while (ess.StringLength>0 && ess.StringText[ess.StringLength-1]==_T(' '))
+ ess.StringLength--;
+ Info.EditorControl(ECTL_SETSTRING,&ess);
+
+ if (IndentSize>0)
+ {
+ struct EditorSetPosition esp;
+ memset(&esp,-1,sizeof(esp));
+ esp.CurLine=-1;
+ esp.CurPos=0;
+ Info.EditorControl(ECTL_SETPOSITION,&esp);
+ Info.EditorControl(ECTL_INSERTTEXT,(void *)IndentBuf);
+ }
+
+ free(TotalString);
+
+ memset(&esp,-1,sizeof(esp));
+ esp.CurLine=ei.CurLine;
+ esp.CurPos=ei.CurPos;
+ Info.EditorControl(ECTL_SETPOSITION,&esp);
+}
+
+
+void JustifyBlock(int RightMargin)
+{
+ EditorInfo ei;
+ Info.EditorControl(ECTL_GETINFO,&ei);
+
+ if (ei.BlockType!=BTYPE_STREAM)
+ return;
+
+ struct EditorGetString egs;
+ egs.StringNumber=ei.BlockStartLine;
+
+ while (1)
+ {
+ if (!Info.EditorControl(ECTL_GETSTRING,&egs))
+ break;
+ if (egs.SelStart==-1 || egs.SelStart==egs.SelEnd)
+ break;
+ int ExpNum=egs.StringNumber;
+ if (!Info.EditorControl(ECTL_EXPANDTABS,&ExpNum))
+ break;
+ Info.EditorControl(ECTL_GETSTRING,&egs);
+
+ struct EditorSetString ess;
+ ess.StringNumber=egs.StringNumber;
+
+ ess.StringText=(TCHAR*)egs.StringText;
+ ess.StringEOL=(TCHAR*)egs.StringEOL;
+ ess.StringLength=egs.StringLength;
+
+ if (ess.StringLength<RightMargin)
+ JustifyString(RightMargin,ess);
+
+ egs.StringNumber++;
+ }
+}
+
+
+int JustifyString(int RightMargin,struct EditorSetString &ess)
+{
+ int WordCount=0;
+ int I;
+ for (I=0;I<ess.StringLength-1;I++)
+ if (ess.StringText[I]!=_T(' ') && ess.StringText[I+1]==_T(' '))
+ WordCount++;
+ if (ess.StringLength>0 && ess.StringText[ess.StringLength-1]==_T(' '))
+ WordCount--;
+ if (WordCount<=0)
+ return(FALSE);
+ while (ess.StringLength>0 && ess.StringText[ess.StringLength-1]==_T(' '))
+ ess.StringLength--;
+ int TotalAddSize=RightMargin-ess.StringLength;
+ int AddSize=TotalAddSize/WordCount;
+ int Reminder=TotalAddSize%WordCount;
+
+ TCHAR *NewString=(TCHAR *)malloc(RightMargin*sizeof(TCHAR));
+ _tmemset(NewString,_T(' '),RightMargin);
+ _tmemcpy(NewString,ess.StringText,ess.StringLength);
+
+ for (I=0;I<RightMargin-1;I++)
+ if (NewString[I]!=_T(' ') && NewString[I+1]==_T(' '))
+ {
+ int MoveSize=AddSize;
+ if (Reminder)
+ {
+ MoveSize++;
+ Reminder--;
+ }
+ if (MoveSize==0)
+ break;
+ memmove(NewString+I+1+MoveSize,NewString+I+1,(RightMargin-(I+1+MoveSize))*sizeof(TCHAR));
+ while (MoveSize--)
+ NewString[I+1+MoveSize]=_T(' ');
+ }
+
+ ess.StringText=NewString;
+ ess.StringLength=RightMargin;
+ Info.EditorControl(ECTL_SETSTRING,&ess);
+ free(NewString);
+ return(TRUE);
+}
+
+
+SHAREDSYMBOL void WINAPI EXP_NAME(GetPluginInfo)(struct PluginInfo *Info)
+{
+ Info->StructSize=sizeof(*Info);
+ Info->Flags=PF_EDITOR|PF_DISABLEPANELS;
+ Info->DiskMenuStringsNumber=0;
+ static const TCHAR *PluginMenuStrings[1];
+ PluginMenuStrings[0]=GetMsg(MAlign);
+ Info->PluginMenuStrings=PluginMenuStrings;
+ Info->PluginMenuStringsNumber=ARRAYSIZE(PluginMenuStrings);
+ Info->PluginConfigStringsNumber=0;
+}
diff --git a/align/src/Align.hpp b/align/src/Align.hpp
new file mode 100644
index 00000000..8eff1b7f
--- /dev/null
+++ b/align/src/Align.hpp
@@ -0,0 +1,23 @@
+struct InitDialogItem
+{
+ unsigned char Type;
+ unsigned char X1,Y1,X2,Y2;
+ unsigned char Focus;
+ DWORD_PTR Selected;
+ unsigned int Flags;
+ unsigned char DefaultButton;
+ const TCHAR *Data;
+};
+
+void SetRegKey(HKEY hRoot,const TCHAR *Key,const TCHAR *ValueName,DWORD ValueData);
+void SetRegKey(HKEY hRoot,const TCHAR *Key,const TCHAR *ValueName,TCHAR *ValueData);
+int GetRegKey(HKEY hRoot,const TCHAR *Key,const TCHAR *ValueName,int &ValueData,DWORD Default);
+int GetRegKey(HKEY hRoot,const TCHAR *Key,const TCHAR *ValueName,DWORD Default);
+int GetRegKey(HKEY hRoot,const TCHAR *Key,const TCHAR *ValueName,TCHAR *ValueData,const TCHAR *Default,DWORD DataSize);
+const TCHAR *GetMsg(int MsgId);
+void InitDialogItems(const struct InitDialogItem *Init,struct FarDialogItem *Item,int ItemsNumber);
+
+
+static struct PluginStartupInfo Info;
+struct FarStandardFunctions FSF;
+TCHAR PluginRootKey[80];
diff --git a/align/src/AlignLng.hpp b/align/src/AlignLng.hpp
new file mode 100644
index 00000000..91c0d489
--- /dev/null
+++ b/align/src/AlignLng.hpp
@@ -0,0 +1,10 @@
+enum
+{
+ MOk,
+ MCancel,
+ MAlign,
+ MRightMargin,
+ MReformat,
+ MSmartMode,
+ MJustify
+};
diff --git a/align/src/AlignMix.icpp b/align/src/AlignMix.icpp
new file mode 100644
index 00000000..5776f743
--- /dev/null
+++ b/align/src/AlignMix.icpp
@@ -0,0 +1,38 @@
+const TCHAR *GetMsg(int MsgId)
+{
+ return(Info.GetMsg(Info.ModuleNumber,MsgId));
+}
+
+void InitDialogItems(const struct InitDialogItem *Init,struct FarDialogItem *Item,int ItemsNumber)
+{
+ int I;
+ struct FarDialogItem *PItem=Item;
+ const struct InitDialogItem *PInit=Init;
+ for (I=0;I<ItemsNumber;I++,PItem++,PInit++)
+ {
+ PItem->Type=PInit->Type;
+ PItem->X1=PInit->X1;
+ PItem->Y1=PInit->Y1;
+ PItem->X2=PInit->X2;
+ PItem->Y2=PInit->Y2;
+ PItem->Focus=PInit->Focus;
+ PItem->History=(const TCHAR *)PInit->Selected;
+ PItem->Flags=PInit->Flags;
+ PItem->DefaultButton=PInit->DefaultButton;
+#ifdef UNICODE
+ PItem->MaxLen=0;
+#endif
+ if ((DWORD_PTR)PInit->Data<2000)
+#ifndef UNICODE
+ lstrcpy(PItem->Data,GetMsg((unsigned int)(DWORD_PTR)PInit->Data));
+#else
+ PItem->PtrData = GetMsg((unsigned int)(DWORD_PTR)PInit->Data);
+#endif
+ else
+#ifndef UNICODE
+ lstrcpy(PItem->Data,PInit->Data);
+#else
+ PItem->PtrData = PInit->Data;
+#endif
+ }
+}