You are here: PSPad forum > English discussion forum > trying my first extension

trying my first extension

#1 trying my first extension

Posted by: Gorlash | Date: 2014-10-01 21:57 | IP: IP Logged

I want a "delete line" function which copies the deleted line to clipboard, so I can paste it elsewhere using Ctrl-V.

I tried copying Serge Balance's "del_end_best" script, and changing it to my requirements, which I'll show below... I created a JScript directory under pspad/scripts, and copied my script there, but it does not seem to be attached to Ctrl-K as it should be.

Obviously I'm missing a key step in this process; any guidance (including pointers to a relevant doc for doing this) are welcome!

My new script/extension:

// JavaScript Document
//*****************************************************************************
// filename : clip_line.js
// description: Delete line to clipboard
// created : 10/01/14 14:35:44
// author : Daniel D Miller
//
// You may distribute this script freely, but please keep this header intact.
//*****************************************************************************
var MODULE_NAME = "clip line";
var MODULE_VER = "0.1";
var MODULE_TITLE = "Delete line to clipboard";
function Init() {
menuName = "&" + MODULE_NAME;
subMenu = "&" + "Edit";
addMenuItem(menuName, subMenu, "main", "CTRL+K");
}

function main() {
var ed = neweditor();
ed.assignActiveEditor();
ed.command('ecLineStart');
ed.command('aCopyLine');
ed.command('ecDeleteLine');
}

Options: Reply | Quote | Up ^


#2 Re: trying my first extension

Posted by: vbr | Date: 2014-10-02 09:51 | IP: IP Logged

Gorlash:
I want a "delete line" function which copies the deleted line to clipboard, so I can paste it elsewhere using Ctrl-V.

I tried copying Serge Balance's "del_end_best" script, and changing it to my requirements, which I'll show below... I created a JScript directory under pspad/scripts, and copied my script there, but it does not seem to be attached to Ctrl-K as it should be.

Obviously I'm missing a key step in this process; any guidance (including pointers to a relevant doc for doing this) are welcome!

My new script/extension:

// JavaScript Document
//*****************************************************************************
// filename : clip_line.js
// description: Delete line to clipboard
// created : 10/01/14 14:35:44
// author : Daniel D Miller
//
// You may distribute this script freely, but please keep this header intact.
//*****************************************************************************
var MODULE_NAME = "clip line";
var MODULE_VER = "0.1";
var MODULE_TITLE = "Delete line to clipboard";
function Init() {
menuName = "&" + MODULE_NAME;
subMenu = "&" + "Edit";
addMenuItem(menuName, subMenu, "main", "CTRL+K");
}

function main() {
var ed = neweditor();
ed.assignActiveEditor();
ed.command('ecLineStart');
ed.command('aCopyLine');
ed.command('ecDeleteLine');
}

Hi,
there are several problem areas to look into:
I believe, MODULE_NAME and MODULE_VER should be all lowercase now (maybe there were some differences in older versions).

neweditor() should be newEditor()

I am not sure, whether prepending the ampersand like "&" + "Edit" ... has any effect, scripts are probably designed to be accessed via menu shortcuts rather than menu accelerators, but I may be wrong.

Only commands starting with "ec..." can be called as Editor Commands
ed.command('ecLineStart');
those with "a..." are aplication wide commands of PSPad and are run using the global function:
runPSPadAction('aCopyLine')

howewer, in this case it won't work as intended - "copy line" is a function which duplicates the current line directly in the editor and doesn't use the clipboard.

The shortcut keys should be tested for possible collisions with some already assigned functions - this part is rather tricky - generally, if your script works ok, if you call it from the menu, but not if using the shortcut - there is most likely some colliding functionality with the same shortcut.
There are several places to look - windows-wide shortcuts (like F1), program shortcuts (these can be seen in the menus and in the setting: key map), clip definition files (like Ctrl+I) in HTML clip (the currently active file format is relevant) and finally the scripts themselves which assign the shortcuts in the Init() function.

Furthermore, it is generally useful to check for a cornercase, when the script is called with no editor windows open (ed.assignActiveEditor() will cause an error in this case).

I ended up with the following basic code, which might satisfy your requirements (there are possible differences in handling the trailing newline - this version doesn't include it in the selection, but deletes it afterwards (of course it can be tweaked for a different way and special-case the last line of the text):

Quote:
var module_name = "test_js_3";
var module_ver = "0.1";

function cut_line(){
if (editorsCount()<1){return;} // quit if no text window is open
var actEd = newEditor();
actEd.assignActiveEditor();
actEd.command('ecLineStart');
actEd.command('ecLineStart'); //repeated call in case smart "Home" feature ist active (disregarding starting whitespace)
actEd.command('ecSelLineEnd'); // select the line including the line ending
actEd.command('ecCut');
actEd.command('ecDeleteChar');//delete the immediately following line ending (if present)
}

function Init(){
addMenuItem("cut current line to clipboard", "", "cut_line", "Ctrl+Alt+A");// labels and shortcuts can be modified
}

hth,
vbr

Options: Reply | Quote | Up ^


#3 Re: trying my first extension

Posted by: Andreas | Date: 2014-10-02 11:34 | IP: IP Logged

Gorlash:
I want a "delete line" function which copies the deleted line to clipboard, so I can paste it elsewhere using Ctrl-V.

This function is already built-in.

Go to Program Settings... -> Editor (part 1) and activate 'Extended Cut and Copy'.

Now you can run the function with 'ctrl x'. Works with the line the cursor is in.

Options: Reply | Quote | Up ^


#4 Re: trying my first extension

Posted by: Gorlash | Date: 2014-10-02 15:15 | IP: IP Logged

Oh My Gosh!!! Thank you, Andreas!!! That does exactly what I want, for both Cut and Copy ...

Okay, now I'll do what I should have done in the first place, which is go through all the Editor options and look at the tooltips...

ITM, I think I'll test vbr's script and see how it works, in case I want to write other scripts in the future.

Options: Reply | Quote | Up ^






Editor PSPad - freeware editor, © 2001 - 2024 Jan Fiala, Hosted by Webhosting TOJEONO.CZ, design by WebDesign PAY & SOFT, code Petr Dvořák, Privacy policy and GDPR