Copy Link in Caret /w Ctrl+P (LuaScript)
-
I need help /w a LuaScripting problem, I see that when you Right-Click a Link it lets you “Copy Link” I want a keyboard shortcut for it instead to speed up my Copy\ Pasta. Could someone help me, the codes from ChatGPT & Bard were buggy & didn’t work. For instance, when I click anywhere on a URL & press Ctrl+P I want the entire URL copied to the Clipboard. Thanks !
-
That command is macro-playable, even though it’s not macro-recordable:
<Macro name="CopyLink" Ctrl="yes" Alt="no" Shift="no" Key="80"> <Action type="2" message="0" wParam="42082" lParam="0" sParam="" /> </Macro>
If you paste that into your
<Macros>
section ofshortcuts.xml
and restart Notepad++,Ctrl+P
will run the Copy Link entry – er, well, it will if you use Settings > Preferences > Shortcut Mapper to turn off theCtrl+P
shortcut for File > Print. – No plugin required, this will work with a vanilla copy of Notepad++.I got the
42082
from the english.xml localization file, or equivalently from the menuCmdID.hI don’t use LuaScript, so I don’t know the syntax: I will use the kind of phraseology that I’d use to describe how to do it in PythonScript. But I assume LuaScript has a wrapper for the NPPM_MENUCOMMAND message, which you would pass in
lParam="42082"
(whatever syntax the LuaScript uses: they might even have a constant/enumeration for it, similar toIDM_EDIT_COPY_LINK
). I assume that LuaScript mechanism to map a shortcut to a particular script, so if you used that, you’d have the mapping. -
@PeterJones Thanks, that worked ! I will use it till someone musters the generosity of posting the LuaScript Startup code :D
This is the code I got from ChatGPT, the first line was taken off another working LuaScript code I got from someone on Github (this code is broken, & doesn’t work):
– Copy Link In Caret w/o Right-Click
npp.AddShortcut(“Copy Link”, “Ctrl+P”, function()
local editor = notepad.getCurrentBuffer()
local currentPos = editor.currentPos
local startPos = editor:wordStartPosition(currentPos, true)
local endPos = editor:wordEndPosition(currentPos, true)
local link = editor:getTextRange(startPos, endPos)
if link ~= “” then
editor:copyText(link)
end
end) -
@alienfxfiend said in Copy Link in Caret /w Ctrl+P (LuaScript):
This is the code I got from ChatGPT…this code doesn’t work
Not sure I see the value at all in posting non-working code that wasn’t even written by a human. :-(
-
@alienfxfiend said in Copy Link in Caret /w Ctrl+P (LuaScript):
@PeterJones Thanks, that worked ! I will use it till someone musters the generosity of posting the LuaScript Startup code :D
Here, freshly coded by a human as a gift to a human :)
-- Topic: https://community.notepad-plus-plus.org/topic/24681/copy-link-in-caret-w-ctrl-p-luascript -- Valid url characters: https://stackoverflow.com/a/1547940 -- ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;= -- Include % for url encoding npp.AddShortcut("Copy this link", "Ctrl+Alt+1", function() local pos = editor.CurrentPos local col = editor.Column[pos] local line = editor:GetCurLine() local urlpattern = "[%w%-%._~:/%?#%[%]@!%$&'%(%)*%+,;=%%]+" for startpos, word, endpos in string.gmatch(line, '()(' .. urlpattern .. ')()') do if col > startpos and col < endpos then if not string.find(word, '://') then print('Invalid link: ' .. word) else print('Copied link: ' .. word) editor:CopyText(word) return end end end end)
The
print()
lines can be removed once tested as working ok. Check the LuaScript console for the printed output to validate.Example:
Invalid link: Topic: Copied link: https://community.notepad-plus-plus.org/topic/24681/copy-link-in-caret-w-ctrl-p-luascript
A URL can be almost any character though usually has the substring of
://
which is whatstring.find()
searchs for to determine if is a URL and not some other word at the point of the caret. -
-
@mpheath Wow, thanks a million ! This is exactly what I wanted. I’am so grateful to you for helping me out —this will 10x my research efforts !! Works like a charm. Amazing genius-level solution ;-) Also thank you PeterJones, your method is also flawless.
-
Hello, @alienfxfiend, @peterjones, @alan-kilborn, @mpheath and All,
Hum… I’m really embarrassed, @mpheath ! Because the macro, provided by @peterjones works flawlessly but your
lua
script does not work as expected :-(I’ve just did a first try on my old
Win XP - 32 bits
machine, withN++ v7.9.2
andLuaScript v10.1
and no chance ! ( Note that the recentLuaScript
versionsv11
andv12
do not work withN++ v7.9.2
)I surely miss something obvious, but I can’t see why !
Best Regards,
guy038
-
@guy038 Hello
Peter’s answer would be like the Copy Link already in the contextmenu source. So should be good. LuaScript can do the IDM_EDIT_COPY_LINK command though it needs to be assigned first before use. Easier than my initial code…
npp.AddShortcut("Copy this link", "Ctrl+Alt+1", function() local IDM_EDIT_COPY_LINK = 42082 npp:MenuCommand(IDM_EDIT_COPY_LINK) end)
Still on Win XP, good for you. I am on Win 7 though it is 64 bit OS.
I just downloaded the N++ portable v7.9.2 32 bit and added LuaScript which states it is v0.10 32 bit, not v10.1 that you mention. It look like v0.10.1 was released 3 days after v0.10. The v0.10 archive on Github is missing the lua.dll file though Plugin Admin did download lua.dll so perhaps it is actual v0.10.1 though the about box says v0.10 .
I have added the posted lua code in the Lua startup script, restarted N++ and tested with the LuaScript console open so as to see the printed messages. I appears to be working just like in N++ v8.5.4 .
I swapped LuaScript v0.10 with v0.10.1 which I doubt is a difference as just a appveyor.yml script was updated, so the version downloaded from Plugin Admin should still be ok. Even the checksums for the dll files are the same. Started N++ again and tested. It is working. The checksums…
File: LuaScript\Lua.dll CRC-32: 30670d6b MD5: ca8e5c8431e0904dac146ee98eb492f5 SHA-1: 4e522b6f0acf554974d6321581b2d8a626f1fe6d File: LuaScript\LuaScript.dll CRC-32: c0f063e1 MD5: 81a00ae7bb7a7de32cf429fb3b170035 SHA-1: 6c56da18147f2b41e576b5ec678d7010cbb3f11b
Are these the same as your dll files? I doubt you have 64 dll files as I got a warning from N++ on startup as it could not load them, so that could not be causing the issue.
Perhaps the caret position is at the start of the URL string. Try changing this line
if col > startpos and col < endpos then
to
if col >= startpos - 1 and col < endpos then
which which gets the URL from the starting edge of it.
Finally, try restarting N++. Open the LuaScript console as it should print a result. Test with the URLs in the startup script as that is what I tested with. The shortcut in posted code is Ctrl+Alt+1 as in the number 1, not the letter L. Check the shortcut mapper in case a conflict is happening. Also try the link in the menu Plugins > LuaScript > Copy this link .
If you have further troubles, I could install Win XP into a Virtual Machine and try it there to see if I can make it fail.
-
Hi, @mpheath and All,
Many thanks for your reply and do not bother about installing
Wn XP
on a virtual machine !Because, in addition to my old laptop, I’ve ALSO got, since July 2021, a decent
Win-10 64 bits Pro
machine where I installed the very last0.12 - x64
version ofLuaScript
, as well as the last version of thePythonScript
andNppExec
plugins. Thus, here is my debug info :Notepad++ v8.5.4 (64-bit) Build time : Jun 17 2023 - 20:42:45 Path : E:\854_x64\notepad++.exe Command Line : Admin mode : OFF Local Conf mode : ON Cloud Config : OFF OS Name : Windows 10 Pro (64-bit) OS Version : 22H2 OS Build : 19045.3086 Current ANSI codepage : 1252 Plugins : mimeTools (2.9) NppConverter (4.5) NppExport (0.4) PythonScript (2) LuaScript (0.12) NppExec (0.8.4)
And here are the checksums for the
Lua.dll
andLuaScript.dll
plugins :Nom: Lua.dll Taille: 433152 octets (423 KiB) CRC32: 39D70D83 SHA256: 55cbc36216bfcfbf44bb551ded40f8946389f4982fb096352455ec24e987367f SHA1: bea0917a9159d9ab67e2c40f848efb12cd3d17eb
And :
Nom: LuaScript.dll Taille: 589824 octets (576 KiB) CRC32: 7EC421AF SHA256: 459f4aebe1f7843a82d092f6d899edb0fe651d89c3ee7d258dc1da26e78ea691 SHA1: 1985515dc102ef6112ae63816ab5393a33ef8ba4
So, with N++
v8.5.4
, I first verified that the macro :<Macro name="Copy Link" Ctrl="no" Alt="no" Shift="no" Key="120"> <Action type="2" message="0" wParam="42082" lParam="0" sParam="" /> </Macro>
Placed in
shortcuts.xml
still works. Then, I noticed, as you said, that the Copy Link option is available in the Context Menu, as soon as current caret location is within the range of a link. BTW, I also, verified that this was ALREADY the case with N++v7.9.2
on my oldWin XP
machine ! So, all the story could stop here !
However, I’m wondering why your previous and second version of your LuaScript, below, that I even shortened, still do not work, on my recent HP laptop !
npp.AddShortcut("Copy this link", "Ctrl+Alt+3", function() npp:MenuCommand(42082) end)
I’ve just changed the shortcut to
Ctrl + Alt + 2
andCtrl + Alt + 3
in your two scripts, as I noticed, on my Win XP machine, that theCtrl + Alt + 1
shortcut is normally used by theComparPlus
plugin, by default, to set the first tab to compare !I also tried your work-around :
if col > startpos and col < endpos then
to
if col >= startpos - 1 and col < endpos then
without any success ! So, why these simple
Lua
scripts are bugging ???Normally, the process should be :
-
Go to any tab containing a link
-
Use the
Ctrl + Alt + 2
orCtrl + Alt + 3
shortcut to trigger the copy of current link contents in the clipboard -
Paste the clipboard contents anywhere else !
See you later,
guy038
P.S. : two additional points :
-
Your two scripts
Copy_Link.lua
andCopy_Link_2.lua
are installed in the same directory than mylocale
install (E:\854_x64\...
) -
I do not see the options
Copy_Link.lua Ctrl+Alt+ 2
andCopy_Link.lua Ctrl+Alt+ 3
in the Lua menu ?
Last attempt :
I added the commands print “OK” and print “OK_2” at the end of the scripts and executed them from the
Lua Console
, with the commandsdofile("Copy_Link.lua")
anddofile("Copy_Link_2.lua")
=> Correct execution but no result, too !
-
-
@guy038 With your last attempts of using like
dofile('path\\to\\copy_link.lua')
is something that I cannot get to work. No errors, no reaction at all. I tried adding the lua file into a path withinpackage.path
and triedrequire('copy_link')
and cannot get to work.I can only get it to work in the startup script which is saved as startup.lua in the Config directory. Access the startup script from the menu Plugins > LuaScript > Edit Startup Script .
npp.AddShortcut()
does not work in a separate lua file. Probably needs to be applied only at startup when the lua subsystem of LuaScript starts.dofile()
this code as test.lua and it prints the value ofpackage.path
and nothing else, no item namedTestShortcut
created in the menu.print(package.path) npp.AddShortcut('TestShortcut', '', function() print('test') end)
npp.AddShortcut()
does not want to work outside ofstartup.lua
. -
Hi, @mpheath and All,
I confirm that the command
npp.AddShortcut(...
works ONLY when appended tostartup.lua
and do not work in a separateLua
file !And, this time, after restarting N++, the new options
Copy this link Ctrl+Alt+2
andCopy this link Ctrl+Alt+3
were present in theLua
sub-menu and did the job, correctlyFinally, as this command is already included in the
contextMenu.xml
, I suppose that any script, whatever it is, that mimics theCopy link
function, seems useless !Again, thank you very much, @mpheath, for your support !
Best Regards,
guy038