revise the naming function

This commit is contained in:
Xinhao Yuan 2019-07-14 13:33:39 -04:00
parent b39d60741e
commit dfb0f19512
4 changed files with 8 additions and 9 deletions

View File

@ -131,8 +131,7 @@ If `cycle` is true, it then moves the window by cycling all regions.
### `name` as a function in `machi.layout.create` ### `name` as a function in `machi.layout.create`
When passed in as a function, `name` takes the tag `t` and a boolean flag `p` and returns a function for the tag-dependent name of the layout. When passed in as a function, `name` takes the tag `t` and returns (1) a string for the tag-dependent name of the layout, and (2) a boolean indicating the persistence of the layout.
Flag `p` is set true when the name is used for persisting the layout. If the layout is not intented to persist, returning `nil` when `p` is true to skip persisting.
The default layout, `machi.default_layout`, uses the screen geometry and the tag name for name, thus allows the actual layout to be tag- and screen-dependent. The default layout, `machi.default_layout`, uses the screen geometry and the tag name for name, thus allows the actual layout to be tag- and screen-dependent.
To differentiate tags with the same name, you may need a more advanced naming function. To differentiate tags with the same name, you may need a more advanced naming function.

View File

@ -653,8 +653,8 @@ local function create(data)
-- bring the current cmd to the front -- bring the current cmd to the front
data.cmds[#data.cmds + 1] = current_cmd data.cmds[#data.cmds + 1] = current_cmd
local instance_name = layout.machi_get_instance_name(tag, true) local instance_name, persistent = layout.machi_get_instance_name(tag)
if instance_name ~= nil then if persistent then
data.last_cmd[instance_name] = current_cmd data.last_cmd[instance_name] = current_cmd
if data.history_file then if data.history_file then
local file, err = io.open(data.history_file, "w") local file, err = io.open(data.history_file, "w")

View File

@ -3,13 +3,13 @@ local editor = require(... .. ".editor")
local switcher = require(... .. ".switcher") local switcher = require(... .. ".switcher")
local default_editor = editor.create() local default_editor = editor.create()
local default_layout = layout.create( local default_layout = layout.create(
function (tag, _persist) function (tag)
if tag.machi_name_cache == nil then if tag.machi_name_cache == nil then
tag.machi_name_cache = tag.machi_name_cache =
tostring(tag.screen.geometry.width) .. "x" .. tostring(tag.screen.geometry.height) .. "+" .. tostring(tag.screen.geometry.width) .. "x" .. tostring(tag.screen.geometry.height) .. "+" ..
tostring(tag.screen.geometry.x) .. "+" .. tostring(tag.screen.geometry.y) .. '+' .. tag.name tostring(tag.screen.geometry.x) .. "+" .. tostring(tag.screen.geometry.y) .. '+' .. tag.name
end end
return tag.machi_name_cache return tag.machi_name_cache, true
end, end,
default_editor) default_editor)
local gcolor = require("gears.color") local gcolor = require("gears.color")

View File

@ -51,14 +51,14 @@ local function create(name, editor)
if type(name) == "function" then if type(name) == "function" then
get_instance_name = name get_instance_name = name
else else
get_instance_name = function () return name end get_instance_name = function () return name, true end
end end
local function get_instance(tag) local function get_instance(tag)
local name = get_instance_name(tag, false) local name, persistent = get_instance_name(tag)
if instances[name] == nil then if instances[name] == nil then
instances[name] = { instances[name] = {
cmd = editor.get_last_cmd(name), cmd = persistent and editor.get_last_cmd(name) or nil,
regions_cache = {}, regions_cache = {},
} }
end end