PythonScript icon indicating copy to clipboard operation
PythonScript copied to clipboard

getLexer and setLexer do not work

Open dejudicibus opened this issue 3 years ago • 14 comments

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()

dejudicibus avatar Jan 06 '23 13:01 dejudicibus

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

alankilborn avatar Jan 06 '23 13:01 alankilborn

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?

chcg avatar Jan 06 '23 13:01 chcg

PythonScript 2 with Notepad++ v8.4.8

dejudicibus avatar Jan 06 '23 15:01 dejudicibus

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.

vinsworldcom avatar Jan 23 '23 19:01 vinsworldcom

@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)

dejudicibus avatar Jan 25 '23 16:01 dejudicibus

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

image

Cheers.

vinsworldcom avatar Jan 25 '23 16:01 vinsworldcom

...and PythonScript 2 does not?

From PS 2.0:

image

alankilborn avatar Jan 25 '23 16:01 alankilborn

I tried lang = editor.getLexerLanguage() and then I tried to apply an editor.setLexerLanguage(lang) to the new document but it does not work.

On Wed, Jan 25, 2023 at 5:19 PM alankilborn @.***> wrote:

...and PythonScript 2 does not?

From PS 2.0:

[image: image] https://user-images.githubusercontent.com/77065706/214617236-78719543-043b-4b69-9fd1-5acf8a6b5b51.png

— Reply to this email directly, view it on GitHub https://github.com/bruderstein/PythonScript/issues/271#issuecomment-1403872328, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAK5PEE6KAR7IUMLHECPZ6DWUFHABANCNFSM6AAAAAATTCHZRQ . You are receiving this because you were mentioned.Message ID: @.***>

-- Dario de Judicibus, Rome, Italy (EU) Site: https://www.dejudicibus.it, https://genealogia.dejudicibus.it Blog: https://www.lindipendente.eu Book: https://www.lalamanera.it, https://www.lasorgentedeimondi.it

dejudicibus avatar Jan 25 '23 16:01 dejudicibus

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.

vinsworldcom avatar Jan 25 '23 16:01 vinsworldcom

The problem is not to get the language: editor.getLexer() returns 4 for PHP and editor.getLexerLanguage() returns 'hypertext'. The problem is to SET the language. editor.setLexer(4) and editor.setLexerLanguage('hypertext') do not work. They give no error but do nothing.

On Wed, Jan 25, 2023 at 5:30 PM Dario de Judicibus @.***> wrote:

I tried lang = editor.getLexerLanguage() and then I tried to apply an editor.setLexerLanguage(lang) to the new document but it does not work.

On Wed, Jan 25, 2023 at 5:19 PM alankilborn @.***> wrote:

...and PythonScript 2 does not?

From PS 2.0:

[image: image] https://user-images.githubusercontent.com/77065706/214617236-78719543-043b-4b69-9fd1-5acf8a6b5b51.png

— Reply to this email directly, view it on GitHub https://github.com/bruderstein/PythonScript/issues/271#issuecomment-1403872328, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAK5PEE6KAR7IUMLHECPZ6DWUFHABANCNFSM6AAAAAATTCHZRQ . You are receiving this because you were mentioned.Message ID: @.***>

-- Dario de Judicibus, Rome, Italy (EU) Site: https://www.dejudicibus.it, https://genealogia.dejudicibus.it Blog: https://www.lindipendente.eu Book: https://www.lalamanera.it, https://www.lasorgentedeimondi.it

-- Dario de Judicibus, Rome, Italy (EU) Site: https://www.dejudicibus.it, https://genealogia.dejudicibus.it Blog: https://www.lindipendente.eu Book: https://www.lalamanera.it, https://www.lasorgentedeimondi.it

dejudicibus avatar Jan 25 '23 16:01 dejudicibus

THe problem occurs only for new (not yet saved) files.

On Wed, Jan 25, 2023 at 5:36 PM Dario de Judicibus @.***> wrote:

The problem is not to get the language: editor.getLexer() returns 4 for PHP and editor.getLexerLanguage() returns 'hypertext'. The problem is to SET the language. editor.setLexer(4) and editor.setLexerLanguage('hypertext') do not work. They give no error but do nothing.

On Wed, Jan 25, 2023 at 5:30 PM Dario de Judicibus @.***> wrote:

I tried lang = editor.getLexerLanguage() and then I tried to apply an editor.setLexerLanguage(lang) to the new document but it does not work.

On Wed, Jan 25, 2023 at 5:19 PM alankilborn @.***> wrote:

...and PythonScript 2 does not?

From PS 2.0:

[image: image] https://user-images.githubusercontent.com/77065706/214617236-78719543-043b-4b69-9fd1-5acf8a6b5b51.png

— Reply to this email directly, view it on GitHub https://github.com/bruderstein/PythonScript/issues/271#issuecomment-1403872328, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAK5PEE6KAR7IUMLHECPZ6DWUFHABANCNFSM6AAAAAATTCHZRQ . You are receiving this because you were mentioned.Message ID: @.***>

-- Dario de Judicibus, Rome, Italy (EU) Site: https://www.dejudicibus.it, https://genealogia.dejudicibus.it Blog: https://www.lindipendente.eu Book: https://www.lalamanera.it, https://www.lasorgentedeimondi.it

-- Dario de Judicibus, Rome, Italy (EU) Site: https://www.dejudicibus.it, https://genealogia.dejudicibus.it Blog: https://www.lindipendente.eu Book: https://www.lalamanera.it, https://www.lasorgentedeimondi.it

-- Dario de Judicibus, Rome, Italy (EU) Site: https://www.dejudicibus.it, https://genealogia.dejudicibus.it Blog: https://www.lindipendente.eu Book: https://www.lalamanera.it, https://www.lasorgentedeimondi.it

dejudicibus avatar Jan 25 '23 16:01 dejudicibus

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.

vinsworldcom avatar Jan 25 '23 16:01 vinsworldcom

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.

chcg avatar Jan 25 '23 22:01 chcg

Well, I hope that some function will be developed to make easier to get and set le ed info because the code that was proposed is really ugly if you have to use it every time in simple script. Something similar to getLexer and setLexer should be developed in my opinion.

On Wed, 25 Jan 2023 at 23:30 chcg @.***> wrote:

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 supported 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.

— Reply to this email directly, view it on GitHub https://github.com/bruderstein/PythonScript/issues/271#issuecomment-1404312274, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAK5PEAPJCRECTNDU2HKL6DWUGSRVANCNFSM6AAAAAATTCHZRQ . You are receiving this because you were mentioned.Message ID: @.***>

-- Dario de Judicibus, Rome, Italy (EU) Site: https://www.dejudicibus.it, https://genealogia.dejudicibus.it Blog: https://www.lindipendente.eu Book: https://www.lalamanera.it, https://www.lasorgentedeimondi.it

dejudicibus avatar Jan 25 '23 22:01 dejudicibus