Assistance Required: Inserting Numbered Rows in an EmEditor Document Using a Macro

Assistance Required: Inserting Numbered Rows in an EmEditor Document Using a Macro

Richard Lv8

Assistance Required: Inserting Numbered Rows in an EmEditor Document Using a Macro

Viewing 10 posts - 1 through 10 (of 10 total)

  • Author
    Posts

  • March 30, 2010 at 7:18 pm #8240
    zhouzh2
    Participant
    I wrote this for inserting number row. both in this kind:
    a=1
    b=2
    c=3
    ..
    and:
    a = 1 somethinghere
    b = 2 somethinghere
    c = 3 somethinghere
    ..
    It is supposed to work from number 0 to 99 or 99 to 0.
    /insertNumbers.jsee/
    /Insert numbers in a row/

    var i = prompt(“Please enter the start number (not larger than 99)”, 0);
    var n = prompt(“Please enter the end number (not larger than 99)”, 1); //promote

    if ((i == “”)|| (n == “”)){Quit();};
    if ((i > 99)|| (i < 0) || (n > 99)||(n < 0)) {alert(“Error!”); Quit();}; //errors

    cfg = document.Config;
    var userVSsetting = cfg.General.VirtualSpace;
    cfg.General.VirtualSpace = ‘true’;
    cfg.Save(); //enable VirtualSpace so that when inserting at ‘^a=nb=n…’ will run normally.

    status = “Inserting - Please Wait…”;

    if (i < n) {
    n++;
    while (i < 10){
    document.selection.Text=i;
    document.selection.LineDown(false,1);
    document.selection.CharLeft(false,1);
    i++;
    if (i == n) {insertEnd();};
    };
    while (i <= 99){
    document.selection.Text=i;
    document.selection.LineDown(false,1);
    document.selection.CharLeft(false,1);
    document.selection.CharLeft(false,1);
    i++;
    if (i == n) {insertEnd();};
    };
    }; //ascending

    else {
    n–;
    while (i >= 10){
    document.selection.Text=i;
    document.selection.LineDown(false,1);
    document.selection.CharLeft(false,1);
    document.selection.CharLeft(false,1);
    i–;
    if (i == n) {insertEnd();};
    };
    while (i < 10){
    document.selection.Text=i;
    document.selection.LineDown(false,1);
    document.selection.CharLeft(false,1);
    i–;
    if (i == n) {insertEnd();};
    };
    }; //descending

    function insertEnd(){
    cfg.General.VirtualSpace = userVSsetting;
    cfg.Save();
    status = “Inserting Completed”;
    Quit();
    };
    However, this macro will be trapped in a loop under a certain situation, i.e. when 1<i<10 and=”” n=””>10, or when 10<i<=99 and=”” n<10=”” (however=”” the=”” latter=”” will=”” not=”” always=”” appear.)=”” but=”” i=”” cannot=”” find=”” out=”” why.=”” anyone=”” please=”” help=”” me=”” with=”” that=”” :-(<=”” p=””></i<=99></i<10>

April 3, 2010 at 11:34 am #8269
zhouzh2
Participant
no one interested?:-(
April 6, 2010 at 5:46 am #8304
ToadLoadin
Member
Hi zhouzh2,
If your “somethinghere” in every line is exactly the same:

a=1

b=2

c=3

..

and:

a = 1 somethinghere

b = 2 somethinghere

c = 3 somethinghere

..
perhaps you could use “Insert Number Sequence” plugin, the Simplified Chinese version is here.
April 6, 2010 at 4:13 pm #8307
zhouzh2
Participant
Thanks ToadLoadin :-)
I know there is such a plug-in.
However, a 32bit plug-in will not work on a 64bit Emeditor.
Furthermore, I would prefer a macro if the macro can do what I want, since it saves hardware recourse.
“somethinghere” is not what I want to insert. This is just one of the situation when inserting numbers. Under the above two situation a macro should behave accoradingly to insert numbers correctly. To surpass this I temporarily enables the VirtualSpace and restores it when finished.
The problem here is when entering a start number greater than 2, the macro will stack in a infinite loop, but I can’t find out why…from I point of view, this macro should run correctly :-?
April 21, 2010 at 12:49 pm #8390
ToadLoadin
Member
Hi zhouzh2,
I modified your macro to this:
cfg = document.Config;
var userVSsetting = cfg.General.VirtualSpace;
cfg.General.VirtualSpace = ‘true’;
cfg.Save(); //enable VirtualSpace so that when inserting at ‘^a=nb=n…’ will run normally.

var i = prompt("Please enter the start number (not larger than 99)", 0);  
var n = prompt("Please enter the end number (not larger than 99)", 1); //promote  
    
if ((i == "")|| (n == "")){Quit();};  
if ((i > 99)|| (i < 0) || (n > 99)||(n < 0)) {alert("Error!"); Quit();}; //errors  
    
// Get current cursor position  
    var iCol = document.selection.GetActivePointX(eePosLogicalA);  
    var iLine = document.selection.GetActivePointY(eePosLogical);  
// Caculate the number of lines  
    var nLines = Math.abs(i-n)+1;  
    
status = "Inserting - Please Wait...";  
    
for (var j=0; j < nLines; j++) {  
    document.selection.SetActivePoint(eePosLogicalA, iCol, iLine+j);  
    if (i < n) { //ascending  
    	document.selection.Text = i;  
    	i++;  
    } else { //descending  
    	document.selection.Text = i;  
    	i--;  

    
cfg.General.VirtualSpace = userVSsetting;  
cfg.Save();  
status = "Inserting Completed";  
Quit();  

And observed an interesting issue, as you noticed:

when 1<i10, or when 10<i<=99 and n<10
The first number inserted will be the biggest one, I guess that’s why your macro fall into a infinite loop, but I can not figure out why this happens, perhaps a bug? 8-)
April 22, 2010 at 2:36 pm #8395
zhouzh2
Participant
Hi ToadLoadin,
Thanks a lot for your help. Your macro is a lot elegant than my silly one :-)
Yeah I agree with you that this is strange and might be a bug, not so sure though.
Any other JavaScript expert please come to our rescue :-?
(Yutaka? 8-))
April 23, 2010 at 4:58 pm #8401
tonne
Participant
Try
if (Math.abs(i) < Math.abs(n)) { //ascending
April 24, 2010 at 1:49 am #8402
ToadLoadin
Member
tonne wrote:
Try
if (Math.abs(i) < Math.abs(n)) { //ascending
That works!~~ :-) But…why that works? :-(
April 24, 2010 at 8:22 am #8403
tonne
Participant
prompt returns a string; is “2” < “10” ?
April 24, 2010 at 9:55 am #8404
ToadLoadin
Member
tonne wrote:
prompt returns a string; is “2” < “10” ?
OIC, it’s a string comparison instead of integer one.
Thank you! :-D

  • Author
    Posts

Viewing 10 posts - 1 through 10 (of 10 total)

  • You must be logged in to reply to this topic.

Also read:

https://techidaily.com
  • Title: Assistance Required: Inserting Numbered Rows in an EmEditor Document Using a Macro
  • Author: Richard
  • Created at : 2024-10-13 16:59:26
  • Updated at : 2024-10-14 16:56:56
  • Link: https://win-reviews.techidaily.com/assistance-required-inserting-numbered-rows-in-an-emeditor-document-using-a-macro/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
Assistance Required: Inserting Numbered Rows in an EmEditor Document Using a Macro