Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support requireing modules located within the script folder #37

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

mark-cooke
Copy link

What was intended to be a fairly small PR, to include the script folder in Lua's package search path, ended up getting a little more involved:

Lua tries to only load a module once, no matter how many times it's required, but the default behaviour of assigning all unlisted files to an implicit scripts_window_open table renders that mechanism inoperable/ineffective.

This PR resolves this by:

  1. Adding support for a scripts_window_open variable.
  2. Allowing the scripts... variables to be assigned a single string, referencing a single file.
  3. Re-initialising the Lua VM when files/config have changed (needed for module changes to be "caught" since they're only loaded once) but imho it's also safer, anyway, if the changed files stored and referenced global data.

In support of the above changes I also:

  • Updated the README.md to reflect these changes and attempted to simplify and organise the information a little more clearly.
  • Refactored add_lua_file_to_list to accept the script folder path and include the call to g_build_path since the same thing was being done in a few places.
  • Pulled the 2 parts of the directory monitor handler that dealt with config reload out into a function (refresh_config_and_script).
  • Removed the unused file counter from load_config.
  • Followed clang's advice re const on args (of fuctions related to the changes).
  • Fixed a few memory leaks (found using Valgrind)

Why not other implementations?

  1. A variable/function to disable the "greedy loading"
    • You'd also need a scripts_window_open, anyway, and have 2 things to add to config when 1 would do the same job.
  2. An "exclude_files" list/table
    • Inconsistent with the rest of the script_... tables. Would also require more processing to end up with, basically, the same "window open" list anyway. Also begs a question of would it only apply to the "open" list, or all?

@dsalt This is one of a total of 5 PRs so I'm sorry for the github spam!
If you decide to accept them and would rather I group them into 1 larger PR, just let me know.

Thanks!

…pport the use of "require" for scripts in the script folder.
Enable scripts_<etc> = "filename.lua"
Reduces the noise around script definitions, helps with supporting the disablement of "greedy loading" of "open" scripts.
…ipts_window_open'

'scripts_window_open' being defined disables "greedy loading" to enable 'require' in user scripts. Defined is:
* A table with filename entries (as other script defs).
* An empty table definition.
* A single string, empty or not.
...and the call to g_build_path, since the same stuff was being done in a few places.  Also followed clangs advice and added some const to args.
Re-arrange slightly and modify to explain:
* Changes to support single file definition
* scripts_window_open addition and significance
* Explain "require"ing modules re: the previous/default behaviour.
...and more consistent/predictable for other files and the config.  The Lua VM is re-loaded if a module or any other file has changed, or if the config does, as any changes can render any global variables or state invalid.
Safe to just wait for the next edit and change event to load a valid config
src/config.c Outdated
Comment on lines 236 to 238
script_folder,
current_file,
NULL);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent with tabs, align with spaces; and if your text editor ever replaces the spaces with tabs, it's broken because doing so will break the alignment for others who use a different tab width.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 571795e

src/config.c Outdated
Comment on lines 137 to 139
script_folder,
oneFileString,
NULL);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 tabs to indent then 36 spaces (not 9 tabs == 72 spaces) to align.

Copy link
Author

@mark-cooke mark-cooke Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was fixed/changed in a later commit, which was part of the original push: 2e711f8#diff-18680f57f5d32607a3b196afffe0d6ecb250af6c0a994b304b88fc91231250bfR135

Remove unneeded indentaion left over from placement in inner loop, fix formatting of assignment.
@mark-cooke mark-cooke requested a review from dsalt February 21, 2025 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants