Skip to content
Permalink
Browse files

В функции Panel.LoadCustomSortMode таблица параметров может включать …

…в себя опциональное поле

'Condition' (функцию). Данная функция может использоваться, например, в интерактивном режиме
работы для изменения параметров сортировки, а также для её отмены.
  • Loading branch information...
shmuz committed Mar 12, 2016
1 parent bf54c3a commit 39cd66ca80f987fde8641152f139135a9bae73fc
@@ -841,14 +841,18 @@ lv=3
dt=Text
nm=LoadCustomSortMode
ctime=3585324926
mtime=3665124528
mtime=3666993887
<article>
#_Panel.LoadCustomSortMode (Mode, Settings)
#_
#_**Parameters:**
#_ Mode: sort mode; integer >=100 and <=0x7FFFFFFF
#_ Settings: a table containing the following fields:
#_ *Compare* (required field):
#_ *Condition*:
#_ Function. If it is specified it will be called with one argument - sort mode.
#_ If the return value is false then the sort is cancelled.
#_ Note that this function can reload all sorting parameters by calling again Panel.LoadCustomSortMode().
#_ *Compare*:
#_ Function, see its description below.
#_ *DirectoriesFirst, SelectedFirst, RevertSorting, SortGroups:*
#_ These optional fields specify corresponding sorting options:
@@ -830,14 +830,19 @@ lv=3
dt=Text
nm=LoadCustomSortMode
ctime=3585324926
mtime=3665124484
mtime=3666994029
<article>
#_Panel.LoadCustomSortMode (Mode, Settings)
#_
#_**���������:**
#_ Mode: ����� ����������, ����� ����� >=100 � <=0x7FFFFFFF
#_ Settings: �������, �������� ��������� ����:
#_ *Compare* (������������ ����):
#_ *Condition*:
#_ �������. ���� ������, �� ����� ������� � ����� ���������� - ����� ����������.
#_ ���� ������������ �������� �����, �� ���������� ����������.
#_ ������� ��������, ��� ������ ������� ����� ������������� ��� ��������� ����������,
#_ ����� ������ Panel.LoadCustomSortMode().
#_ *Compare*:
#_ �������, ��. � �������� ����.
#_ *DirectoriesFirst, SelectedFirst, RevertSorting, SortGroups:*
#_ ������ ������������ ���� ������ ��������������� ����� ����������:
@@ -1,4 +1,10 @@
drkns 12.03.2016 16:37:16 +0200 - build 4576
shmuel 13.03.2016 01:35:20 +0200 - build 4577

1. В функции Panel.LoadCustomSortMode таблица параметров может включать в себя опциональное поле
'Condition' (функцию). Данная функция может использоваться, например, в интерактивном режиме
работы для изменения параметров сортировки, а также для её отмены.

drkns 12.03.2016 16:37:16 +0200 - build 4576

1. 0003195: Неверная подстановка имён файлов/каталогов с пробелами при включённой опции "Подставлять первый подходящий вариант"

@@ -164,6 +164,22 @@ struct CustomSort
HANDLE hSortPlugin;
};

bool SortFileList(CustomSort *cs, wchar_t *indicator)
{
FarMacroValue values[]={cs};
FarMacroCall fmc={sizeof(FarMacroCall),ARRAYSIZE(values),values,nullptr,nullptr};
OpenMacroPluginInfo info={MCT_PANELSORT,&fmc};
void *ptr;

if (Global->CtrlObject->Plugins->CallPlugin(Global->Opt->KnownIDs.Luamacro.Id, OPEN_LUAMACRO, &info, &ptr) && ptr)
{
indicator[0] = info.Ret.Values[0].String[0];
indicator[1] = info.Ret.Values[0].String[1];
return true;
}
return false;
}

};

struct FileList::PrevDataItem: swapable<PrevDataItem>
@@ -379,7 +395,7 @@ static struct list_less
};

const auto IsParentDirB = IsParentDir(b);

if (IsParentDir(a))
{
return IsParentDirB? a.Position < b.Position : true;
@@ -686,16 +702,7 @@ void FileList::SortFileList(int KeepPosition)
cs.ListCaseSensitiveSort = ListCaseSensitiveSort;
cs.hSortPlugin = hSortPlugin;

FarMacroValue values[]={&cs};
FarMacroCall fmc={sizeof(FarMacroCall),ARRAYSIZE(values),values,nullptr,nullptr};
OpenMacroPluginInfo info={MCT_PANELSORT,&fmc};
void *ptr;
if (Global->CtrlObject->Plugins->CallPlugin(Global->Opt->KnownIDs.Luamacro.Id, OPEN_LUAMACRO, &info, &ptr) && ptr)
{
CustomSortIndicator[0] = info.Ret.Values[0].String[0];
CustomSortIndicator[1] = info.Ret.Values[0].String[1];
}
else
if (!custom_sort::SortFileList(&cs, CustomSortIndicator))
{
SetSortMode(panel_sort::BY_NAME); // recursive call
return;
@@ -4695,19 +4702,22 @@ void FileList::SelectSortMode()
// custom sort modes
else if (SortCode>=(int)ARRAYSIZE(SortModes) + 1 && SortCode<(int)(ARRAYSIZE(SortModes) + 1 + extra - 1))
{
const int index = 3*(SortCode-ARRAYSIZE(SortModes)-1);
const int mode = (int)mpr->Values[index].Double;
const bool InvertByDefault = mpr->Values[index+1].Boolean != 0;

bool KeepOrder = false;
int index = 3*(SortCode-ARRAYSIZE(SortModes)-1);
int mode = (int)mpr->Values[index].Double;

if (!InvertPressed)
if (CanDoCustomSort(mode))
{
m_ReverseSortOrder = !PlusPressed;
KeepOrder = true;
}
bool InvertByDefault = mpr->Values[index+1].Boolean != 0;
bool KeepOrder = false;

SetCustomSortMode(mode, KeepOrder, InvertByDefault);
if (!InvertPressed)
{
m_ReverseSortOrder = !PlusPressed;
KeepOrder = true;
}

SetCustomSortMode(mode, KeepOrder, InvertByDefault);
}
}
// sort options
else
@@ -8751,3 +8761,13 @@ bool FileList::IsColumnDisplayed(int Type)
{
return IsColumnDisplayed([&Type](const column& i) {return static_cast<int>(i.type & 0xff) == Type;});
}

bool FileList::CanDoCustomSort(int SortMode)
{
FarMacroValue values[] = {(double)SortMode};
FarMacroCall fmc = {sizeof(FarMacroCall),ARRAYSIZE(values),values,nullptr,nullptr};
OpenMacroPluginInfo info = {MCT_CANPANELSORT,&fmc};
void *ptr;

return Global->CtrlObject->Plugins->CallPlugin(Global->Opt->KnownIDs.Luamacro.Id, OPEN_LUAMACRO, &info, &ptr) && ptr;
}
@@ -204,6 +204,7 @@ class FileList:public Panel
virtual void SortFileList(int KeepPosition) override;
virtual void SetViewMode(int ViewMode) override;
virtual void SetSortMode(panel_sort SortMode, bool KeepOrder = false) override;
virtual bool CanDoCustomSort(int SortMode) override;
virtual void SetCustomSortMode(int SortMode, bool KeepOrder = false, bool InvertByDefault = false) override;
virtual void ChangeSortOrder(bool Reverse) override;
virtual void ChangeNumericSort(bool Mode) override;
@@ -2260,8 +2260,11 @@ intptr_t KeyMacro::CallFar(intptr_t CheckCode, FarMacroCall* Data)
if (panel)
{
int SortMode = (int)Data->Values[1].Double;
bool InvertByDefault = Data->Values[2].Boolean != 0;
panel->SetCustomSortMode(SortMode, false, InvertByDefault);
if (panel->CanDoCustomSort(SortMode))
{
bool InvertByDefault = Data->Values[2].Boolean != 0;
panel->SetCustomSortMode(SortMode, false, InvertByDefault);
}
}
}
break;
@@ -115,7 +115,7 @@ namespace detail
QVIEW_PANEL,
INFO_PANEL
};};

struct panel_mode { enum value_type
{
NORMAL_PANEL,
@@ -204,7 +204,7 @@ class Panel: public ScreenObject, public std::enable_shared_from_this<Panel>
virtual int GetCurBaseName(string &strName, string &strShortName) const;
virtual int GetFileName(string &strName, int Pos, DWORD &FileAttr) const { return FALSE; }
virtual int GetCurrentPos() const {return 0;}

virtual bool IsFocused() const;
virtual void OnFocusChange(bool Get);

@@ -235,6 +235,7 @@ class Panel: public ScreenObject, public std::enable_shared_from_this<Panel>
virtual bool GetPrevDirectoriesFirst() const { return m_DirectoriesFirst; }
virtual void ChangeDirectoriesFirst(bool Mode) { SetDirectoriesFirst(Mode); }
virtual void SetSortMode(panel_sort Mode, bool KeepOrder = false) { m_SortMode = Mode; }
virtual bool CanDoCustomSort(int SortMode) { return false; }
virtual void SetCustomSortMode(int SortMode, bool KeepOrder = false, bool InvertByDefault = false) {}
virtual void ChangeSortOrder(bool Reverse) {SetSortOrder(Reverse);}
virtual void IfGoHome(wchar_t Drive) {}
@@ -2677,6 +2677,7 @@ enum MACROCALLTYPE
MCT_GETCUSTOMSORTMODES = 10,
MCT_ADDMACRO = 11,
MCT_KEYMACRO = 12,
MCT_CANPANELSORT = 13,
};

enum MACROPLUGINRETURNTYPE
@@ -1 +1 @@
m4_define(BUILD,4576)m4_dnl
m4_define(BUILD,4577)m4_dnl
@@ -1,7 +1,7 @@
function export.GetGlobalInfo()
return {
Version = { 1, 0, 0, 564 },
MinFarVersion = { 3, 0, 0, 4450 },
Version = { 1, 0, 0, 565 },
MinFarVersion = { 3, 0, 0, 4577 },
Guid = win.Uuid("4EBBEFC8-2084-4B7F-94C0-692CE136894D"),
Title = "LuaMacro",
Description = "Far macros in Lua",
@@ -1,4 +1,10 @@
shmuel 08.03.2016 23:42:18 +0200 - build 564
shmuel 13.03.2016 01:28:38 +0200 - build 565

1. В функции Panel.LoadCustomSortMode таблица параметров может включать в себя опциональное поле
'Condition' (функцию). Данная функция может использоваться, например, в интерактивном режиме
работы для изменения параметров сортировки, а также для её отмены.

shmuel 08.03.2016 23:42:18 +0200 - build 564

1. Рефакторинг.

@@ -393,6 +393,8 @@ function export.Open (OpenFrom, arg1, ...)
TablePanelSort = panelsort.GetSortModes()
return TablePanelSort
end
elseif calltype==F.MCT_CANPANELSORT then
return panelsort and panelsort.CanDoPanelSort(...)
end

elseif OpenFrom == F.OPEN_COMMANDLINE then
@@ -1,6 +1,6 @@
#include <farversion.hpp>

#define PLUGIN_BUILD 564
#define PLUGIN_BUILD 565
#define PLUGIN_DESC "Lua Macros for Far Manager"
#define PLUGIN_NAME "LuaMacro"
#define PLUGIN_FILENAME "luamacro.dll"
@@ -10,6 +10,7 @@ local band, bor = bit.band, bit.bor -- 32 bits, be careful
local tonumber = tonumber

local CustomSortModes = {} -- key=integer, value=table
local CanChangeSortMode = true

-- shellsort [ http://lua-users.org/wiki/LuaSorting ]
-- Written by Rici Lake. The author disclaims all copyright and offers no warranty.
@@ -79,37 +80,59 @@ local TernaryProperties = {

local function LoadCustomSortMode (nMode, Settings)
assert(type(nMode)=="number" and nMode==math.floor(nMode) and nMode>=100 and nMode<=0x7FFFFFFF)
if Settings then
assert(type(Settings)=="table")
assert(type(Settings.Compare)=="function")
if type(Settings) == "table" then
local t = {}

if type(Settings.Compare) == "function" then t.Compare=Settings.Compare end
local cond = Settings.Condition or Settings.condition -- allow lower case for 'Condition'
if type(cond) == "function" then t.Condition=cond end
if not (t.Compare or t.Condition) then
CustomSortModes[nMode] = nil
return
end

if type(Settings.InitSort) == "function" then t.InitSort=Settings.InitSort end
if type(Settings.EndSort) == "function" then t.EndSort=Settings.EndSort end
if type(Settings.SortFunction) == "string" then t.SortFunction=Settings.SortFunction end
if type(Settings.Description) == "string" then t.Description=Settings.Description end

local t = { Compare=Settings.Compare }
if type(Settings.InitSort)=="function" then t.InitSort=Settings.InitSort end
if type(Settings.EndSort)=="function" then t.EndSort=Settings.EndSort end
if type(Settings.SortFunction)=="string" then t.SortFunction=Settings.SortFunction end
if type(Settings.Indicator) == "string" then t.Indicator=(Settings.Indicator.." "):sub(1,2)
else t.Indicator = " "
end

for _,v in ipairs(BooleanProperties) do t[v] = not not Settings[v] end
for _,v in ipairs(TernaryProperties) do t[v] = Settings[v]==0 and 0 or Settings[v]==1 and 1 or 2 end

if type(Settings.Description)=="string" then t.Description = Settings.Description end
if type(Settings.Indicator)=="string" then
t.Indicator = Settings.Indicator
local len = t.Indicator:len()
if len<2 then t.Indicator = t.Indicator..(" "):rep(2-len) end
else
t.Indicator = " "
end
CustomSortModes[nMode] = t
else
CustomSortModes[nMode] = nil
end
end

local function CanDoPanelSort (SortMode)
local tSettings = CustomSortModes[SortMode]
if tSettings then
if tSettings.Condition then
CanChangeSortMode = false
local ok, ret = pcall(tSettings.Condition, SortMode)
CanChangeSortMode = true
if not ok then Shared.ErrMsg(ret); return; end
if not ret then return; end
tSettings = CustomSortModes[SortMode] -- retrieve again as it may be reloaded by Condition()
return tSettings and tSettings.Compare and true
else
return true
end
end
end

local function SetCustomSortMode (nMode, whatpanel)
local Settings = CustomSortModes[nMode]
if Settings then
whatpanel = whatpanel==1 and 1 or 0
Shared.MacroCallFar(MCODE_F_SETCUSTOMSORTMODE, whatpanel, nMode, Settings.InvertByDefault)
if CanChangeSortMode then
local Settings = CustomSortModes[nMode]
if Settings then
whatpanel = whatpanel==1 and 1 or 0
Shared.MacroCallFar(MCODE_F_SETCUSTOMSORTMODE, whatpanel, nMode, Settings.InvertByDefault)
end
end
end

@@ -190,7 +213,7 @@ local function SortPanelItems (params)
jit.flush()
params = ffi.cast("CustomSort*", params)
local tSettings = CustomSortModes[tonumber(params.SortMode)]
if not tSettings then return end
if not (tSettings and tSettings.Compare) then return end

local Compare = tSettings.Compare
local SortEqualsByName = not tSettings.NoSortEqualsByName
@@ -295,4 +318,5 @@ return {
CustomSortMenu=CustomSortMenu,
GetSortModes=GetSortModes,
DeleteSortModes=function() CustomSortModes={} end,
CanDoPanelSort=CanDoPanelSort,
}

0 comments on commit 39cd66c

Please sign in to comment.
You can’t perform that action at this time.