Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getLexer and setLexer do not work #271

Open
dejudicibus opened this issue Jan 6, 2023 · 14 comments
Open

getLexer and setLexer do not work #271

dejudicibus opened this issue Jan 6, 2023 · 14 comments
Labels

Comments

@dejudicibus
Copy link

dejudicibus commented Jan 6, 2023

It looks like it is not possible anymore to use setLexer to set a language in Notepad++. Scintilla 5 has moved the lexers from Scintilla into a new Lexilla project.

For example, the following code DOES duplicate a file but DOES NOT set its language as the same of original one.

# Get data from current file
anchor = editor.getAnchor()
lang = editor.getLexer()

# Copy all text
editor.selectAll()
editor.copy()

# Clear selection
editor.setAnchor(anchor)
editor.setCurrentPos(anchor)

# Generate new file with same lexer and paste all text
notepad.new()
editor.setLexer(lang)
editor.paste()
@alankilborn
Copy link

This may be of interest to you: https://community.notepad-plus-plus.org/topic/23922/new-ilexer-interface-from-pythonscript

@chcg chcg added the Feature label Jan 6, 2023
@chcg
Copy link
Collaborator

chcg commented Jan 6, 2023

That feature got broken with the switch to scintilla 5.x used by N++ 8.4 and later.
@dejudicibus Which combination of versions of pythonscript plugin and N++ do you use?

@dejudicibus
Copy link
Author

PythonScript 2 with Notepad++ v8.4.8

@vinsworldcom
Copy link
Contributor

vinsworldcom commented Jan 23, 2023

I've used this:

import ctypes
from ctypes import addressof, create_unicode_buffer, windll
from ctypes.wintypes import HWND, WPARAM, LPARAM, LPCWSTR, UINT

from Npp import editor

SendMessage          = windll.user32.SendMessageW
LRESULT              = LPARAM
SendMessage.argtypes = [HWND, UINT, WPARAM, LPARAM]
SendMessage.restype  = LRESULT

FindWindow           = ctypes.windll.user32.FindWindowW
FindWindow.argtypes  = [LPCWSTR, LPCWSTR]
FindWindow.restype   = HWND

NPPM_CREATELEXER = 2134

hwnd = FindWindow(u'Notepad++', None)
lexer = create_unicode_buffer("markdown")

lexerPtr = SendMessage(hwnd, NPPM_CREATELEXER, 0, addressof(lexer))
editor.setILexer(lexerPtr)

replace "markdown" with whatever the name of your Lexer is. Must use the naming from the LexerModule() command in the Lexilla libraries compiled into Notepad++.

editor.getLexerLanguage()

Cheers.

@dejudicibus
Copy link
Author

@vinsworldcom I am not sure to understand which name should I use for Lexer. I mean, I do not know which is the lexer used for a file. That is why I use getLexer. To use the same lexer for the new one, as follows:

# Get data from current file
anchor = editor.getAnchor()
lang = editor.getLexer()

# Copy all text
editor.selectAll()
editor.copy()

# Clear selection
editor.setAnchor(anchor)
editor.setCurrentPos(anchor)

# Generate new file with same lexer and paste all text
notepad.new()
editor.paste()
editor.setLexer(lang)

@vinsworldcom
Copy link
Contributor

Perhaps PythonScript 3 has this feature and PythonScript 2 does not?

image

Cheers.

@alankilborn
Copy link

...and PythonScript 2 does not?

From PS 2.0:

image

@dejudicibus
Copy link
Author

dejudicibus commented Jan 25, 2023 via email

@vinsworldcom
Copy link
Contributor

vinsworldcom commented Jan 25, 2023

Where do you see editor.setLexerLanguage() in my post? I think @alankilborn settled this for us. Seems PS2 may have the needed parts, you just need to move to the new API. The old set/getLexer is not going to work anymore.

Maybe this excerpt and addition from/to my example above helps:

lang = editor.getLexerLanguage()
lexer = create_unicode_buffer(lang)

Cheers.

@dejudicibus
Copy link
Author

dejudicibus commented Jan 25, 2023 via email

@dejudicibus
Copy link
Author

dejudicibus commented Jan 25, 2023 via email

@vinsworldcom
Copy link
Contributor

vinsworldcom commented Jan 25, 2023

The problem is to SET the language. editor.setLexer(4) and
editor.setLexerLanguage('hypertext') do not work. They give no error but do
nothing.

You're not understanding (or perhaps I'm not). You can no longer user set/getLexer - forget them. You need to use the new API. There is no such thing as setLexerLanguage() - that does not exist in the new API.

To set lexers in the new API, you do not use a number. You use a pointer to the lexer name. Please look closely - the command is setILexer() - that is the word "set" followed by the capital letter "I" as in ... H I J ..., followed by the sentence-case word "Lexer".

Please try replacing the editor.setLexer(lang) line in your script with this:

import ctypes
from ctypes import addressof, create_unicode_buffer, windll
from ctypes.wintypes import HWND, WPARAM, LPARAM, LPCWSTR, UINT

from Npp import editor

SendMessage          = windll.user32.SendMessageW
LRESULT              = LPARAM
SendMessage.argtypes = [HWND, UINT, WPARAM, LPARAM]
SendMessage.restype  = LRESULT

FindWindow           = ctypes.windll.user32.FindWindowW
FindWindow.argtypes  = [LPCWSTR, LPCWSTR]
FindWindow.restype   = HWND

NPPM_CREATELEXER = 2134

hwnd = FindWindow(u'Notepad++', None)
lang = editor.getLexerLanguage()
lexer = create_unicode_buffer(lang)

lexerPtr = SendMessage(hwnd, NPPM_CREATELEXER, 0, addressof(lexer))
editor.setILexer(lexerPtr)

Cheers.

@chcg
Copy link
Collaborator

chcg commented Jan 25, 2023

See release notes for https://github.com/bruderstein/PythonScript/releases/tag/v2.0.0. Just N++ up to 8.3.x is supported.
N++ 8.4.x contains scintilla versions 5.x with a big change how lexers are handled. Unfortunately just pythonscript 3.0.x is updated yet to support N++ 8.4.x but also there the handling for settting lexers is an open issue. If that point is fixed I'm planning to backport the changes also to the pythonscript 2.x line.

@dejudicibus
Copy link
Author

dejudicibus commented Jan 25, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants