Huge refactor of rc.lua & added vim-like buffer bindings
[luakit.git] / lib / util.lua
blobd7c2f04eac1fa6fa505bccd35ff729b5fb351e20
1 ---------------------------------------------------------------------------
2 -- Copyright Julien Danjou <julien@danjou.info> 2008
3 -- Copyright Mason Larobina <mason.larobina@gmail.com> 2010
4 ---------------------------------------------------------------------------
6 -- Grab environment we need
7 local assert = assert
8 local debug = debug
9 local io = io
10 local ipairs = ipairs
11 local os = os
12 local pairs = pairs
13 local pairs = pairs
14 local rtable = table
15 local string = string
16 local type = type
17 local print = print
19 -- Utility module for awful
20 module("util")
22 table = {}
24 local xml_entity_names = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
25 -- Escape a string from XML char.
26 function escape(text)
27 return text and text:gsub("['&<>\"]", xml_entity_names) or nil
28 end
30 local xml_entity_chars = { lt = "<", gt = ">", nbsp = " ", quot = "\"", apos = "'", ndash = "-", mdash = "-", amp = "&" };
31 -- Unescape a string from entities.
32 function unescape(text)
33 return text and text:gsub("&(%a+);", xml_entity_chars) or nil
34 end
36 -- Return the difference of another table as a new table.
37 -- (I.e. all elements in the first table but not in the other)
38 function table.difference(t, other)
39 local ret = {}
40 for k, v in pairs(t) do
41 if type(k) == "number" then
42 local found = false
43 for _, ov in ipairs(other) do
44 if ov == v then
45 found = true
46 break
47 end
48 end
49 if not found then rtable.insert(ret, v) end
50 else
51 if not other[k] then ret[k] = v end
52 end
53 end
54 return ret
55 end
57 -- Join all tables given as parameters.
58 -- This will iterate all tables and insert all their keys into a new table.
59 function table.join(...)
60 local ret = {}
61 for i = 1, arg.n do
62 if arg[i] then
63 for k, v in pairs(arg[i]) do
64 if type(k) == "number" then
65 rtable.insert(ret, v)
66 else
67 ret[k] = v
68 end
69 end
70 end
71 end
72 return ret
73 end
75 -- Check if a table has an item and return its key.
76 function table.hasitem(t, item)
77 for k, v in pairs(t) do
78 if v == item then
79 return k
80 end
81 end
82 end
84 -- Get a sorted table with all integer keys from a table
85 function table.keys(t)
86 local keys = { }
87 for k, _ in pairs(t) do
88 rtable.insert(keys, k)
89 end
90 rtable.sort(keys, function (a, b)
91 return type(a) == type(b) and a < b or false
92 end)
93 return keys
94 end
96 -- Reverse a table
97 function table.reverse(t)
98 local tr = { }
99 -- reverse all elements with integer keys
100 for _, v in ipairs(t) do
101 rtable.insert(tr, 1, v)
103 -- add the remaining elements
104 for k, v in pairs(t) do
105 if type(k) ~= "number" then
106 tr[k] = v
109 return tr
112 -- Clone a table
113 function table.clone(t)
114 local c = { }
115 for k, v in pairs(t) do
116 c[k] = v
118 return c
121 -- Return true if table `b` is identical to table `a`
122 function table.isclone(a, b)
123 if #a ~= #b then return false end
124 for k, v in pairs(a) do
125 if a[k] ~= b[k] then return false end
127 return true
130 -- Remove an element at a given position (or key) in a table and return the
131 -- value that was in that position.
132 function table.pop(t, k)
133 local v = t[k]
134 if type(k) == "number" then
135 table.remove(t, k)
136 else
137 t[k] = nil
139 return v
142 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80