dail8859 / LuaScript

Notepad++ plugin for Lua scripting capabilities
https://dail8859.github.io/LuaScript/
GNU General Public License v2.0
109 stars 23 forks source link

Known issues with Notepad++ v7.8.1 (64 bit) under Windows 10 v1809? #78

Closed dinkumoil closed 4 years ago

dinkumoil commented 4 years ago

Today I ran for the first time a portable version of Notepad++ v7.8.1 (64 bit) with LuaScript plugin v0.9 on my Windows 10 v1809 (build 17763.805).

In the startup script of the plugin I use the following code to get auto indentation for Lua. I found that code in the Notepad++ community forum.

-- =============================================================================
-- Auto indentation for Lua
-- =============================================================================

-- Regexs to determine when to indent or unindent
-- From: https://github.com/sublimehq/Packages/blob/master/Lua/Indent.tmPreferences
local decreaseIndentPattern = [[^\s*(elseif|else|end|until|\})\s*$]]
local increaseIndentPattern = [[^\s*(else|elseif|for|(local\s+)?function|if|repeat|while)\b((?!end).)*$|\{\s*$]]

do_increase = false

-- Get the start and end position of a specific line number
local function getLinePositions(line_num)
  local start_pos = editor:PositionFromLine(line_num)
  local end_pos = start_pos + editor:LineLength(line_num)

  return start_pos, end_pos
end

-- Check any time a character is added
local function autoIndent_OnChar(ch)
  if ch == "\n" then
    -- Get the previous line
    local line_num = editor:LineFromPosition(editor.CurrentPos) - 1
    local start_pos, end_pos = getLinePositions(line_num)

    if editor:findtext(increaseIndentPattern, SCFIND_REGEXP, start_pos, end_pos) then
      -- This has to be delayed because N++'s auto-indentation hasn't triggered yet
      do_increase = true
    end
  else
    local line_num = editor:LineFromPosition(editor.CurrentPos)
    local start_pos, end_pos = getLinePositions(line_num)

    if editor:findtext(decreaseIndentPattern, SCFIND_REGEXP, start_pos, end_pos) then
      -- The pattern matched, now check the previous line's indenation
      if line_num > 1 and editor.LineIndentation[line_num - 1] <= editor.LineIndentation[line_num] then
        editor.LineIndentation[line_num] = editor.LineIndentation[line_num] - 2
      end
    end
  end

  return false
end

-- Work around N++'s auto indentation by delaying the indentation change
local function autoIndent_OnUpdateUI(flags)
  if do_increase then
    do_increase = false
    -- Now the the indentation can be increased since N++'s auto-indentation is done by now
    editor:Tab()
  end

  return false
end

-- See if the auto indentation should be turned on or off
local function checkAutoIndent(bufferid)
  if npp.BufferLangType[bufferid] == L_LUA then
    do_increase = false
    -- Register the event handlers
    npp.AddEventHandler("OnChar", autoIndent_OnChar)
    npp.AddEventHandler("OnUpdateUI", autoIndent_OnUpdateUI)
  else
    -- Remove the event handlers
    npp.RemoveEventHandler("OnChar", autoIndent_OnChar)
    npp.RemoveEventHandler("OnUpdateUI", autoIndent_OnUpdateUI)
  end
end

-- Only turn on the auto indentation when it is actually a lua file
-- Check it when the file is switched to and if the language changes

npp.AddEventHandler("OnSwitchFile", function(filename, bufferid)
  checkAutoIndent(bufferid)
  return false
end)

npp.AddEventHandler("OnLangChange", function()
  checkAutoIndent(npp.CurrentBufferID)
  return false
end)

Notepad++ doesn't start up as long as the two npp.AddEventHandler code blocks are active. I can see a Notepad++ process in task manager for a short time but it disappears without showing an error message. I already removed all other plugins from the plugins folder but nothing changed, same behaviour. If I comment out the two npp.AddEventHandler code blocks Notepad++ runs as normal.

On Windows 7 64 bit the same configuration runs without a problem. It is not necessary to comment out the two npp.AddEventHandler code blocks.

Are there any known issues with Windows 10 v1809 64 bit and the LuaScript plugin?

xylographe commented 4 years ago

I'm still on Win7, hence I can't test, but it could be of interest to know if the same problem occurs with NPP 7.7.1 (64-bit) on the same host.

dinkumoil commented 4 years ago

For Npp v7.7.1 on Windows 10 I have to use v0.8 of the plugin, right?

dinkumoil commented 4 years ago

I have tested under Windows 10 v1809 x64 (build 17763.805) Notepad++ v7.7.1 64 bit with LuaScript version v0.8 and v0.9. In both cases Notepad++ doesn't start up unless I comment out the two code blocks.

dail8859 commented 4 years ago

I personally don't have Windows 10 to test with. My guess would be that LuaScript needs updated to work with Scintilla v4.2.0 (headers, etc).

I tested on Windows 7, current master branch, both 32 bit and 64 bit...everything worked as expected. If anyone wants to test the latest version, they DLL files can be downloaded on appveyor.

Also, this is the test startup.lua file I was testing with to keep it to the bare minimum to try to reproduce the issue.

npp.AddEventHandler("OnSwitchFile", function(filename, bufferid)
    print("OnSwitchFile") print(filename, bufferid)
    return false
end)

npp.AddEventHandler("OnLangChange", function()
    print("OnLangChange")
    return false
end)

If it is something specific to Windows 10 then I won't be able to work on it for a while...if it is reproducible on Windows 7 then I should be able to track it down.

dinkumoil commented 4 years ago

@dail8859

The problem is Windows 10 specific, I have already tested on Windows 7 (as I wrote in my opening comment.

I will try your reduced startup script ASAP. But I guess the reason for the failure is not setting the event handlers itself but executing code because the event handlers have been set. OnSwitchFile and OnLangChange are likely to be fired when Npp starts up and opens an empty new 1 "file".

dail8859 commented 4 years ago

This issue should be fixed now. You can download the new 64-bit version here:

https://ci.appveyor.com/project/dail8859/luascript/builds/30119521/job/jrqplus6mxs7h36d/artifacts

Make sure to grab LuaScript.dll and Lua.dll. You will need a minimum version of Notepad++ 7.8 to use this.

Please reopen if needed.

dinkumoil commented 4 years ago

@dail8859

Tested and works. Thank you!