monotone

monotone Mtn Source Tree

Root/contrib/ext_hooks.lua.in

1-- this is an add-on to the standard set of lua hooks for monotone;
2-- the intention is to store the hooks in arrays of hooks and to load
3-- extra .lua files from a directory given by the environment variable
4-- $MTN_LUAHOOKSDIR
5--
6-- Hooks are supposed to be added using the following syntax:
7--
8--add_hook("edit_comment",
9-- function (commentary, user_log_message)
10-- -- do whatever needs to be done
11-- end
12-- )
13--
14-- The existing standard hooks are automatically added as the first item
15-- to each array of hooks, and will be therefore executed first.
16--
17-- Also, should the standard hook functions be defined by any of the loaded
18-- .lua files, the last definition will be added as the last item to each
19-- array of hooks.
20--
21-- It's perfectly possible to have several implementations of the same
22-- hook. Those will be executed in the same order the were added as hooks.
23--
24-- Any non-nil result is stored in a global variable named like the hook
25-- with "res_" prepended. For example, any non-nil result from "edit_comment"
26-- hooks is stored in the global variable res_edit_comment. When all
27-- "edit_comment" hooks have been executed, the contents of res_edit_comment
28-- is returned by the global edit_comment function. The same goes for all
29-- other hook executors.
30--
31-- ----------------------------------------------------------------------------
32
33-- Define the table that will contain pointers to all the hooks.
34-- Each entry is the tuple <name, fn-table>, where {name} is the name
35-- of the hook in question and fn-table is a table of functions.
36mtn_luahooks = {}
37
38-- Define functions to manipulate the table of hooks.
39function add_hook(name, fun)
40 if name ~= nil and fun ~= nil then
41 if mtn_luahooks[name] == nil then mtn_luahooks[name] = {} end
42 table.insert(mtn_luahooks[name], fun)
43 end
44end
45
46_known_hooks = {
47 "@@HOOKS_LIST@@",
48}
49function _add_known_hooks()
50 for i,name in pairs(_known_hooks) do
51 local fntab = getfenv(0)
52 local fn = fntab[name]
53 fntab[name] = nil
54 setfenv(0, fntab)
55 add_hook(name,fn)
56 end
57end
58
59-- This stores the definitions of all the known hooks first in the table.
60_add_known_hooks()
61
62-- Load all the *.lua files found in $MTN_LUAHOOKSDIR if that variable is
63-- defined.
64_mtn_luahooksdir = os.getenv("MTN_LUAHOOKSDIR")
65if _mtn_luahooksdir ~= nil and _mtn_luahooksdir ~= "" then
66 includedirpattern(_mtn_luahooksdir, "*.lua")
67end
68
69-- If anyone was foolish enough to define a hook function directly instead
70-- of using "add_hook", the last will win and will be inserted in the table
71-- of hooks.
72_add_known_hooks()
73
74-- Finally, redefine all known functions to use the hooks to do their job
75@@HOOKS_FUNCTIONS@@

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status