mjolnirapp / mjolnir

Lightweight automation and productivity app for OS X
5.2k stars 129 forks source link

Loading init.lua failed indeifinitely #543

Closed iveney closed 4 years ago

iveney commented 9 years ago

Sometimes when I start Mjolnir, I got the following errors:

-- Loading ~/.mjolnir/init.lua
/usr/local/share/lua/5.2/mjolnir/hotkey.lua:35: bad argument #2 to '_new' (number expected, got nil)
stack traceback:
    [C]: in function '_new'
    /usr/local/share/lua/5.2/mjolnir/hotkey.lua:35: in function 'new'
    /usr/local/share/lua/5.2/mjolnir/hotkey.lua:43: in function 'bind'
    /Users/iveney/.mjolnir/init.lua:61: in main chunk
    [C]: in function 'xpcall'
    /Applications/Mjolnir.app/Contents/Resources/setup.lua:73: in main chunk

However, reloading the config from the menu bar will work normally. Have no idea why this happens. Is it related to the order I put the 'require' statement? My order is like this:

local application = require "mjolnir.application"
local window = require "mjolnir.window"
local fnutils = require "mjolnir.fnutils"
local alert = require "mjolnir.alert"
local hotkey = require "mjolnir.hotkey"
local grid = require "mjolnir.sd.grid"
local tiling = require "mjolnir.tiling"
local geometry = require "mjolnir.geometry"
cmsj commented 9 years ago

Might be an idea to post your whole config, so we can see what/how you're binding keys.

iveney commented 9 years ago
local application = require "mjolnir.application"
local window = require "mjolnir.window"
local fnutils = require "mjolnir.fnutils"
local alert = require "mjolnir.alert"
local hotkey = require "mjolnir.hotkey"
local grid = require "mjolnir.sd.grid"
local tiling = require "mjolnir.tiling"
local geometry = require "mjolnir.geometry"

local mash = {"ctrl", "alt", "cmd"}
local delta = 80

-- Tiling

hotkey.bind(mash, "c", function() tiling.cyclelayout() end)
hotkey.bind(mash, ".", function() tiling.cycle(1) end)
hotkey.bind(mash, ",", function() tiling.cycle(-1) end)
hotkey.bind(mash, "space", function() tiling.promote() end)

-- reload config

hotkey.bind(mash, "R", function()
  mjolnir.reload()
end)

local move = function(dx, dy)
    return function()
          local win = window.focusedwindow()
          local f = win:frame()
          f.x = f.x + dx
          f.y = f.y + dy
          win:setframe(f)
    end
end

local resize = function(dx, dy)
    return function()
          local win = window.focusedwindow()
          -- alert.show(geometry.rect(1,2,3,4))
          local frame = win:frame()
          frame.x = frame.x - dx/2
          frame.w = frame.w + dx
          frame.y = frame.y - dy/2
          frame.h = frame.h + dy
          win:setframe(frame)
    end
end 

-- move the window
hotkey.bind(mash, "Left", move(-delta, 0))
hotkey.bind(mash, "Right", move(delta, 0))
hotkey.bind(mash, "Up", move(0, -delta))
hotkey.bind(mash, "Down", move(0, delta))

-- expand/shrink the window
hotkey.bind(mash, '=', resize(delta, delta))
hotkey.bind(mash, '-', resize(-delta, -delta))
hotkey.bind(mash, '[', resize(-delta, 0))
hotkey.bind(mash, ']', resize(delta, 0))
hotkey.bind(mash, ';', resize(0, -delta))
hotkey.bind(mash, '\'', resize(0, delta))

-- snap the window

grid.MARGINX = 0
grid.MARGINY = 0
grid.GRIDWIDTH = 2
grid.GRIDHEIGHT = 2

-- a helper function that returns another function that resizes the current window
-- to a certain grid size.
local gridset = function(x, y, w, h)
    return function()
        cur_window = window.focusedwindow()
        grid.set(
            cur_window,
            {x=x, y=y, w=w, h=h},
            cur_window:screen()
        )
    end
end

hotkey.bind(mash, 'n', grid.pushwindow_nextscreen)
hotkey.bind(mash, 'j', gridset(0, 0, 1, 2)) -- left half
hotkey.bind(mash, 'l', gridset(1, 0, 1, 2)) -- right half
hotkey.bind(mash, 'i', gridset(0, 0, 2, 1)) -- up half
hotkey.bind(mash, 'k', gridset(0, 1, 2, 1)) -- bottom half
hotkey.bind(mash, 'm', grid.maximize_window)-- maximize
githaff commented 9 years ago

Issue also was reproduced on my simpliest config. Only difference from config in README is that Luarocks are installed in the local user directory /Users/haff/.luarocks.

Each app start was followed by this issue. But then conifg reload works ok.

Was investigating this issue when decided to move Mjolnir.app from Desktop (from where I was running it) to Applications folder. After that issue dissapeared. What is interesting is that when I moved app back to Desktop (for tests sake) issue is still gone so I can't reproduce it again.

Config:

package.path = package.path .. ';' .. os.getenv("HOME") .. "/.luarocks/share/lua/5.2/?.lua"
package.cpath = package.cpath .. ';' .. os.getenv("HOME") .. "/.luarocks/lib/lua/5.2/?.so"

local application = require "mjolnir.application"
local hotkey = require "mjolnir.hotkey"
local window = require "mjolnir.window"
local fnutils = require "mjolnir.fnutils"

hotkey.bind({ "cmd", "alt", "ctrl" }, "D", function()
      local win = window.focusedwindow()
      local f = win:frame()
      f.x = f.x + 10
      win:setframe(f)
end)
iveney commented 9 years ago

I have always put my Mjolnir.app in application folder. The issue still appears (but unpredictably).

githaff commented 9 years ago

I think I figured out what this issue is about. In my case fails where appearing because mjolnir was executed while system layout was russian.

In init config I bind test action to "D" with code:

hotkey.bind({ "cmd", "alt", "ctrl" }, "D", function()

but keycodes.map contains codes table for layout with which it was executed (russian). And so "D" is missing, we get nil as keycode. The easiest and most flexible solution I see is to add ability to specify keys in form of keycodes (e.g. "#2" instead of "D"). I've implemented it and will create pull request.

github-actions[bot] commented 4 years ago

Stale issue message