Search for inconsistent line endings with a regex? (part 2)

  • Hello @guy038 !

    Well, I didn’t really explain my goal, did I?

    Recently I’ve been manipulating some data where I can get mixed line-endings in a file (initially). So I want to be “made aware” of the situation without having to do anything. So I’ve created a script whereby if mixed line-endings occur in one area (between start_pos and end_pos, see script segment above) then I will, in code, turn visible line-endings ON. This way it hits me in the face, the current situation.

    Anyway, I don’t know the file type (Windows/Linux, Mac really isn’t happening) in advance so I can’t do stuff based upon that. And I can’t ask the “notepad” Python object what the current file format is anyway, because if I happen to have a WIndows file open in editor1/view0 and a Linux file open in editor2/view1, bad things happen to the logic.

    Probably the above makes no real sense, but I’m just trying to say that I don’t think I’m making it hard on myself, I’m just doing what it takes for a solution. And I did arrive at that solution today; stuff is working fine now. :)

  • Hi, @alan-kilborn and All,

    Yeeeeaaaah ! I succeeded to built a search regex which grabs all character(s) from the first EOL character(s) found to the nearest other EOL character(s), excluded, which is /are different from the first one ;-))

    I’m using the free-spacing mode for a best comprehension !

      \r\n     .*?  (?=  (?<!\r)\n  |  \r(?!\n)  )  |  #  From a TRUE \r\ TRUE (\n|\r)   EXCLUDED
    (?<!\r)\n  .*?  (?=    \r\n     |  \r(?!\n)  )  |  #  From a TRUE \ TRUE (\r\n|\r) EXCLUDED
     \r(?!\n)  .*?  (?=    \r\n     | (?<!\r)\n  )     #  From a TRUE \ TRUE (\r\n|\n) EXCLUDED

    To test my regex, just paste, in a new N++ tab, the sample text, below ( the v7.6 change.log, slightly modified ) :

    Notepad++ v7.6 new feature and bug-fixes:<CRLF>
    1.  Add Built-in Plugins Admins. Users can install, update and remove plugins by some clicks via Plugins Admin:<CRLF><LF>
    2.  Change plugin loading method: Remove the legacy plugin loading way and apply only the new plugin loading method.<CRLF>
    3.  Add new message NPPM_GETPLUGINHOMEPATH in Notepad++ API for plugin, so plugin can get its path easily.<CR>
    4.  Fix a regression of performance issue while word wrap option is enable.<CRLF>
    5.  Fix a performance issue for switching back to folded document.<LF>
    6.  Fix crash issue due to Unix style path input in Open file dialog.<CR>
    7.  Fix UTF-8 detection problem: 4 byte characters UTF-8 character can be detected now.<CR>
    8.  Enhance/Fix encoding detection/problem.<CRLF>
    9.  Fix auto-indent issue by typing Enter on empty line.<LF>
    10. Fix "Close all but this" behaviour if multiple views are present and some files are dirty.<CR>
    11. Fix tool tip in document switcher showing the old name issue (after being renamed).<CRLF>
    12. Add autoit and lua autoCompletion<CRLF>
    Included plugins:<CRLF>
    1.  NppExport v0.2.8 (32-bit x86 only)<LF>
    2.  Converter 4.2.1<LF>
    3.  Mime Tool 2.1<CRLF>
    4.  DSpellCheck 1.4.6<LF>
    Updater (Installer only):<CRLF>
    * WinGup (for Notepad++) v5.0.4<CRLF>

    Then, with that regex S/R, below, we are going to change, first, in this new tab, the EOL characters to get a final text with all forms of line-breaks :

    SEARCH (?-i)(?:(<CRLF>)|(<LF>)|(<CR>))\R

    REPLACE (?1\r\n)(?2\n)(?3\r)

    Now, you can play around, with my free-spacing regex above ;-))



    P.S. :

    If a file do not contain inconsistent EOL ( i.e. if all the line-breaks, of current file, have the same form ) NO match occurred, as expected !!

  • @guy038


    Glad you had some fun with it!

  • @Alan-Kilborn

    slightly different approach

    regex_dict = {0:'\r[^\n]|[^\r]\n',
    def check_eol(match):
        notepad.messageBox('Different EOLS detected','EOL Missmatch', 0)
    editor.research(regex_dict[editor.getEOLMode()],    # regex
                    check_eol,                          # function to call
                    0,                                  # re flags
                    0,                                  # start
                    editor.getTextLength(),             # end
                    1)                                  # count

  • @Ekopalypse

    I either forgot about or never knew about editor.getEOLMode(). Perhaps I could have used that knowledge YESTERDAY before I finished my design the way I did. :(

    but Thank you.

  • @Ekopalypse

    So it appears the reason (maybe) that I never noticed editor.getEOLMode() before is that I “grew up” here on Community sample scripts where it seems that notepad.getFormatType() was used much more frequently for a very similar purpose. On an integer basis the functions even return the same number values!

    I suppose the notepad.getFormatType() function is for what Notepad++ thinks the setting is for a file upon loading, and after that it follows the current user setting for “EOL conversion”…and the editor.getEOLMode() function usually follows the notepad.getFormatType() setting, but could be set independently (via PS code call to editor.setEOLMode()).

    I did verify this, the editor.getEOLMode() value follows the notepad.getFormatType() value, and if you editor.setEOLMode() to something different than the Notepad++ EOL setting, and then switch the active tab and then come back to the original tab, editor.getEOLMode() will again be back at the notepad.getFormatType() setting. [A fair number of settings work this way: You can change them via PS editor functions, but a switch of tabs and a return will find them reset to original Notepad++ controlling values.]

    For my purposes, however, the editor function is valuable to know the setting for editor1 and editor2, without, say, having to make editor2 the active editor–when it isn’t currently–and then calling notepad.getFormatType().

    …if that all makes any kind of sense to you. :)

  • @Alan-Kilborn

    You are absolutely right and this is something one needs to keep in mind.
    Whenever possible, a notepad object method should be used to stay in sync with npp. Npp itself does, as far as I understand the code, use SCI_SETEOLMODE and SCI_GETEOLMODE to set/get the current eol and as far as I have understood,
    scintilla only checks the first line to determine the eol mode.

    I would say, to get a value it is safe to use editor object methods but, as said, if one wants to change something then notepad object methods should be preferred.

  • @Alan-Kilborn

    My guess is that it was locked by “Father Time”! (i.e., age + inactivity)

    very funny … nooot

    • no: topics don’t get locked automatically, when marked as solved.
    • yes: topics have to be locked manually.
    • no: this topic was not locked, to prevent follow up posts, in order to preserve it’s extraordinary state for eternity, like keeping an ancient ming vase empty.
    • no: there was no content reason to lock this topic.
    • no: the community place does not need a clean up, as the separate information exchange does not interfere with the issue tracker readability for developers.
    • maybe: this was one of those topics, that got spammed back then, and was locked to contain it a bit.

  • @Ekopalypse said:

    Whenever possible, a notepad object method should be used to stay in sync with npp

    if one wants to change something then notepad object methods should be preferred

    Very much agree. Usually the notepad object provides only get access, e.g. in this case notepad.getFormatType() has no corresponding notepad.setFormatType(). In order to do the set, one must do `notepad.menuCommand(MENUCOMMAND.FORMAT_TOUNIX) as an example. This is nice because it keeps the Notepad++ user interface consistent.

    Note a very similar discussion involving View -> Show Symbol -> … menu items via script control is found here:

  • A stretch for staying on-topic, but I found a great way to set up a scenario for inconsistent line-endings, from the hand of @donho himself:

    • Open a file with Unix (Linux) line-endings
    • Select all (ctrl+a)
    • Invoke Plugins -> Mime Tools -> Quoted-printable Encode

    Boom. A very mixed line-endings file (Unix and Windows ends) now results.

    (I discovered this when I was needing to mime a short file. I decided I don’t like how the Mime Tools plugin does its thing–not just this line-ending thing–and will resort to WinZip’s mime for my future miming needs.)


Log in to reply