LoadCustomSortMode

Far Manager Macro System

LoadCustomSortMode


Panel.LoadCustomSortMode (Mode, Settings)

Parameters:
  Mode:         sort mode; integer >=100 and <=0x7FFFFFFF
  Settings:     a table containing the following fields:
      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:
         0 - the option is off, 1 - the option is on. Any other value (or missing value)
         mean "use the current setting of Far Manager".
      InvertByDefault:
         Whether the default sort direction is the inverse one.
      Indicator:
         Indication of sort mode on the panel, a two character string
         (1-st character for the direct sort mode, 2-nd for the inverse one).
      NoSortEqualsByName:
         By default the elements equal from the sorting algorithm's point of view
         are sorted by name. If that is not desired, set this field in true.
      Description:
         Textual description of the sorting mode. If this field is specified
         it is used in the custom sort menu (see Panel.CustomSortMenu).
      SortFunction:
         Specify the sorting algorithm out of the 2 available ones.
         It is a string: "shellsort" (default value) or "qsort".
      InitSort:
         Function. If specified, it will be called before the sorting begins.
         It receives one argument: FarOptions table (see the same-named parameter
         of the function Compare).
      EndSort:
         Function. If specified, it will be called after the sorting ends.

    If the value of Settings is nil or false, it means unloading (removal) of the given
    sorting mode.

Returns:
  Nothing

Description:
  This function loads (or unloads) a custom panel sort mode.
  Once the mode is loaded it can be set in the panel by means of calling
  Panel.SetCustomSortMode.

Function Compare
  result = Compare (Pi1, Pi2, FarOptions)

  Parameters:
    Pi1 и Pi2  - panel elements being compared, structures of SortingPanelItem type.
    FarOptions - a table containing the current Far Manager panel sort options
                 (all values are boolean): DirectoriesFirst, SelectedFirst,
                 RevertSorting, SortGroups, NumericSort, CaseSensitiveSort.

  Returns:
    result     - if the 1-st element should appear after direct sorting above the 2-nd one,
                 a negative number should be returned, if below - a positive number,
                 and if the elements are equal by sorting criteria - return zero.

Note 1:
  Custom panel sorting uses the LuaJIT's FFI library. The use of FFI requires familiarity
  with its documentation.

Note 2:
  Custom panel sort modes are automatically restored after Far Manager restart, provided
  that the configuration has been saved and the corresponding Panel.LoadCustomSortMode()
  calls are done during the process of loading macros.
  Restoring of custom panel sort modes takes place after the macros have been loaded,
  before the execution of auto-starting macros.

Note 3:
  The custom panel sort modes are forcibly unloaded when the macros are unloaded.


Example:
  -- Load the sorting by file name length.
  local ffi = require "ffi"
  local C = ffi.C
  Panel.LoadCustomSortMode (100,
    { 
      Compare = function(p1, p2, opt)
        local l1, l2 = C.wcslen(p1.FileName), C.wcslen(p2.FileName)
        return l1<l2 and -1 or l1>l2 and 1 or 0
      end;
      Indicator = "bB";
    })