MaxRusov / far-plugins

Automatically exported from code.google.com/p/far-plugins
49 stars 12 forks source link

Fix для Review v1.20 #57

Open VictorVG opened 3 years ago

VictorVG commented 3 years ago
--- Review-Macros.lua   original
+++ Review-Macros.lua   fixed
@@ -3,7 +3,7 @@
 -- Размещается в каталоге Macro\scripts
 --

-require "Review"
+Review = require "Review"

 Macro
 {

в противном случае модуль не подгружается и первый же макрос прерывает загрузку со стеком вида:

Review-Macros.lua:42: attempt to index global 'Review' (a nil value)
 Stack Traceback
 ===============
 (1) Lua metamethod '__index' at file '_macroinit.lua:2'
    Local variables:
     (*temporary) = C function: builtin#88
     (*temporary) = table: 0x03f25d00  {1:table: 0x03d2a300, 2:table: 0x03d2a380, 3:table: 0x03398688, 4:
 table: 0x03d1e318 (more...)}
 (2) main chunk of file 'Review-Macros.lua' at line 42
 (3) global C function 'xpcall'
 (4) Lua function 'LoadRegularFile' at file '.\Far\plugins\luamacro\utils.lua:780' (best
 guess)
    Local variables:
     FindData = table: 0x04a4d710  {FileAttributes:ac, CreationTime:13261094788076, ChangeTime:
 13262655783050 (more...)}
     FullPath = string: "Review-Macros.lua"
     macroinit = string: "_macroinit.lua"
     isMoonScript = nil
     f = Lua function '?' (defined at line 0 of chunk Review-Macros.lua)
     msg = nil
     env = table: 0x03c919c8  {NoMacro:function: 0x03cfba48, PanelModule:function: 0x04a4ddd0, CommandLine:
 function: 0x0492ea10 (more...)}
 (5) field C function 'RecursiveSearch'
 (6) Lua function 'LoadMacros' at file 'utils.lua:849' (best guess)
    Local variables:
     unload = boolean: false
     paths = string: "Far\\Profile\\Macros\\scripts"
     allAreas = boolean: true
     numerrors = number: 0
     newAreas = table: 0x03cfba00  {0:table: 0x03f69eb8, 1:table: 0x048a93c8, 2:table: 0x048dee40, 3:table:
  0x03f6fce0 (more...)}
     AreaNames = table: 0x02fc6058  {0:other, 1:shell, 2:viewer, 3:editor, 4:dialog, 5:search, 6:disks, 7:

Без подключённого StackTracePlus.lua поймать ошибку труднее, но она проста:

вызов require = "module" не загружает его т.к. имя модуля не определено, нужно явно его определить в форме local module = require "module" или module = require "module"

MaxRusov commented 3 years ago

Да, спасибо. Недовыложил.

VictorVG commented 3 years ago

Я когда-то сходно попался и не сразу понял что нужно сначала указать имя модуля т.к. в гл. 5.3 документации этот момент не очень чётко описан. Вот выписка:

5.3 - Modules The package library provides basic facilities for loading and building modules in Lua. It exports two of its functions directly in the global environment: require and module. Everything else is exported in a table package.


module (name [, ···]) Creates a module. If there is a table in package.loaded[name], this table is the module. Otherwise, if there is a global table t with the given name, this table is the module. Otherwise creates a new table t and sets it as the value of the global name and the value of package.loaded[name]. This function also initializes t._NAME with the given name, t._M with the module (t itself), and t._PACKAGE with the package name (the full module name minus last component; see below). Finally, module sets t as the new environment of the current function and the new value of package.loaded[name], so that require returns t.

If name is a compound name (that is, one with components separated by dots), module creates (or reuses, if they already exist) tables for each component. For instance, if name is a.b.c, then module stores the module table in field c of field b of global a.

This function can receive optional options after the module name, where each option is a function to be applied over the module.


require (modname) Loads the given module. The function starts by looking into the package.loaded table to determine whether modname is already loaded. If it is, then require returns the value stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.

To find a loader, require is guided by the package.loaders array. By changing this array, we can change how require looks for a module. The following explanation is based on the default configuration for package.loaders.

First require queries package.preload[modname]. If it has a value, this value (which should be a function) is the loader. Otherwise require searches for a Lua loader using the path stored in package.path. If that also fails, it searches for a C loader using the path stored in package.cpath. If that also fails, it tries an all-in-one loader (see package.loaders).

Once a loader is found, require calls the loader with a single argument, modname. If the loader returns any value, require assigns the returned value to package.loaded[modname]. If the loader returns no value and has not assigned any value to package.loaded[modname], then require assigns true to this entry. In any case, require returns the final value of package.loaded[modname].

If there is any error loading or running the module, or if it cannot find any loader for the module, then require signals an error.

а я действовал как привык работать с PL/1 - указал неявное объявление инклюда - там компилятор это сам понимает, а в Lua нужно его сначала явно объявить до использования. То же пришлось исправить.

У себя я объявил данный инклюд как local чтобы он не попадал в глобальные переменные и не вызывал потенциальные ошибки в других модулях:

local Review = require "Review"

и сохранил скрипты в UTF-8 с BOM - это заодно устраняет и ошибочный выбор кодировки в Visual Compare т.к. иначе она считает что кодировка DOS и искажает текст. А если UTF-8 c BOM её автоматика сразу отрабатывает правильно. Я и свои скрипты так сохраняю, хотя сегодня LuaMacro ждёт их просто в UTF-8 согласно LuaFar Manual:

Note: the functions of the Unicode LuaFAR library assume string arguments and string return values to be in the UTF-8 encoding. (There are a few exceptions from this rule, mostly when strings are used to exchange binary data).