Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #18702
    Shavok
    Participant

    Hi

    Is there an option to ensure there is a new line at the end of the file when saving? Similar to trimming spaces. If not, could it be put forward as a suggestion?

    Thanks!

    #18704
    Stefan
    Participant

    There is nothing like that on the default settings dialog:
    http://www.emeditor.com/forums/topic/automatic-saving-of-all-documents-as-lf-only/

     

     

    Me guess, the only way currently available, is to write a macro and enable the event “Before Saving” for that macro.

    See a how-to >> http://www.emeditor.com/forums/topic/automatically-determine-xml-documents/

     
    HTH? :D

    #18705
    Shavok
    Participant

    Thanks for the reply!

    So I’ve started on a macro but my coding on this is very weak…

    The intention:

    • Check if there’s a blank line at the end of the document
    • If not, then make a new line

    I’ve only got the last part nailed so far…

    document.selection.EndOfDocument();
    document.selection.NewLine();
    

    Can you please assist on the “if” part?

    #18706
    Yutaka Emura
    Keymaster

    Hello,

    This code should work assuming the Auto Indent is disabled.

    nLines = document.GetLines();
    str = document.GetLine( nLines );
    if( str.length != 0 ) {
    	document.selection.EndOfDocument();
    	document.selection.NewLine();
    }
    
    #18707
    Shavok
    Participant

    Great! This works very well (and looking at the code, can understand how it works).

    The indent “problem” would only occur now and then and the main point of this auto-eof-line is a general good practice.

    May I put this forward as a suggestion?

    Thanks!

    #18708
    Stefan
    Participant

    Oh, you was faster than I?
     

    I have just this moment worked on this too :

    //JavaScript Macro for EmEditor, v0.01 by Stefan
    //Purpose: Ensure that last line is a blank one, by adding one if need.
    
    //EmEditor Help - EmEditor Macro Reference - Document Object
    //Retrieves the number of the lines in the document:
    nLines = document.GetLines();
    
    //Retrieves the text on the specified line:
    str = document.GetLine(nLines);
    
    if(str.length != 0){
    //If last line is NOT empty, add one blank line:
    	document.selection.EndOfDocument();
    	document.selection.NewLine();
    	
    	//Workaround: Delete automatically added indenting from line above,// 
    	//if line was not empty but indented by white space//
    	//Selects a line at the cursor:
    	document.selection.SelectLine();
    	//Deletes the selected text:
    	document.selection.Delete();	
    }

     

     

    #18709
    Shavok
    Participant

    I like! Thank you :)

    #18710
    Stefan
    Participant

    Sorry, update again.

     
    Better logic:
    if last line have whitespace only, just remove these blanks and done.
    Else, if last line is not empty, but not whitespace only, add a new line.
    (This way we not end up with too “visible blank” lines)

     
    I also added cursor handling to went back from EOF to origin cursor place.
    (Not needed for this thread purpose as event script, but if script is used on demand)

    Not asked for, but just done for the fun:
     

    //JavaScript Macro for EmEditor, v0.02 by Stefan
    //Purpose: Ensure that last line is a blank one, by adding one if need.
    
    //// Don't show the cursor move////
    //EmEditor Help - EmEditor Macro Reference - Window Object
    //prevents changes in EmEditor from being redrawn:
    Redraw = false;
    
    ////Store cursor position////
    //EmEditor Help - EmEditor Macro Reference - Selection Object
    //Returns the column number of the cursor position.
    xPos = document.selection.GetActivePointX(eePosLogical);
    //Returns the line number of the cursor position:
    yPos = document.selection.GetActivePointY(eePosLogical);
    
    ////Check status of last line////
    //EmEditor Help - EmEditor Macro Reference - Document Object
    //Retrieves the number of the lines in the document:
    nLines = document.GetLines();
    //Retrieves the text on the specified line:
    str = document.GetLine(nLines);
    //Moves the cursor to the end of the document:
    document.selection.EndOfDocument();
    //If last line only contains indenting white space, just remove the whitespace//
    if(str.match(/^\s+$/) != null){
    	//Selects a line at the cursor:
    	document.selection.SelectLine();
    	//Deletes the selected text:
    	document.selection.Delete();
    }else if (str.length != 0){
    //If last line is NOT empty, but not whitespace only, add one blank line//
    	document.selection.NewLine();	
    }
    
    ////Restore cursor position////
    //Sets the cursor position:
    bExtendSelection = false;
    document.selection.SetActivePoint(eePosLogical, xPos, yPos, bExtendSelection);
    
    //<EOF>
    #18711
    Shavok
    Participant

    I like the addition of keeping the cursor where it should be, however I tested your 0.02 version and it does not work with the Auto Indentation activated (unlike the previous one 0.01).

    #18713
    Stefan
    Participant

    Shavok

    it does not work with the Auto Indentation activated

     
    WHAT? do not work? I don’t see what you want to tell.

    The v0.02 just deletes auto-indented-whitespace from last line and use that last line as trailing empty line.
    V0.01 adds an additionally blank last line first and deletes the auto-indented-whitespace from that new last line, ending up in two empty last lines.

     
    Test with v0.02
    Code:

    der Test
    der Test
    der Test

    A blank line is added.

     
    Code:

    der Test
    der Test
    der Test
     

    No line is added.

     
    Code:

    der Test
    der Test
    der Test
    ~~~

    Trailing blanks are removed, left a blank line.

     
    Code:

    der Test
    der Test
    der Test
    ~~~der Test
    ~~~der Test
    ~~~

    Trailing blanks are removed, left a blank line.

     
    “~~~” indicates whitespace here.
     
    “”does not work”” helps nobody. Tell what you get and what you had expect instead. That could led to improvement.

    #18714
    Shavok
    Participant

    Okay, okay…fair point :)

    The error I picked up with your 0.02 was using the Python syntax (so with Auto Intent in place).

    With the following command:

    for i in range(1, 10, 1):
        print i  # line ends here

    If I pressed enter again it would create a tabbed space. Using your macro – it too create a space in the last line then, so:

    for i in range(1, 10, 1):
        print i
        ~~~~ # where the line starts

    As mentioned – this was not the case with the previous 0.01 code.

    #18715
    Stefan
    Participant

    Why are there space right under “print”?
    That indention should be only till the “p” of print.

    for i in range(1, 10, 1):
    ~~~~print i
    ~~~~ # where the line starts

     
    I have tried, v0.01 adds an additionally blank line, making two of them.
    V0.02 remove the indenting whitespace, leaving one blank line only.

     
    Did you have enabled line numbers? I had got unintentional additional empty line too on pressing enter; the indented one and a additional line break.
    Maybe that is what you see too?

     
    Sorry, right now I see no issue here. But I will test some more later on…..

    #18716
    Shavok
    Participant

    This is the modified macro I’m working with that covers things quite nicely:

    Redraw = false;
    
    xPos = document.selection.GetActivePointX(eePosLogical);
    yPos = document.selection.GetActivePointY(eePosLogical);
    
    nLines = document.GetLines();
    str = document.GetLine(nLines);
    if(str.length != 0) {
    	document.selection.EndOfDocument();
        document.selection.NewLine();
        document.selection.SelectLine();
        document.selection.Delete();
    }
    
    bExtendSelection = false;
    document.selection.SetActivePoint(eePosLogical, xPos, yPos, bExtendSelection);

    I wasn’t clear enough with the example I showed – it should be:

    for i in range(1, 10, 1):
    ~~~~print i
    ~~~~<----where the line starts

    This indentation is perfectly natural if you are going to have additional commands in Python. But on saving the expectation should be:

    for i in range(1, 10, 1):
    ~~~~print i
    <----where the line starts

    Answering your question – yes, I do have line numbers enabled.

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.