Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #8692
    chjfth
    Member

    Hello, Yukata,

    I want to know the return method of current active document from macro code in Emeditor. So I tried the following VBS snippet,


    Set WshShell = CreateObject( "WScript.Shell" )
    Set dc = document.config
    crlf_style = dc.FileSave.ReturnMethod
    WshShell.Popup "crlf_style " & crlf_style

    However, whether I run this macro on a Windows text file(CRLF) or Unix text file(LF), I always get “crlf_style 0”. Is there a way to work me out?

    Tried on EmEditor v8 and v10.0, same result.

    Thank you.

    #8693
    Yutaka Emura
    Keymaster

    Hello chjfth,

    If you just want to know the Return method of the current document, you can display that information on the Status Bar. Please go to Customize on the Tools menu, and select the “Status” tab, and check “Return Method”.

    Please let me know if you have further questions.

    Thank you for using EmEditor!

    #8697
    chjfth
    Member

    Oh, yes, I do have a further question.

    I’m writing a slash-cvt.vbee macro.


    ' This VBScript macro do the following:
    ' For a block of selected text,
    ' * if a / is found, replace all occurance of / to .
    ' * else if a is found, replace all to / .
    ' After the macro is executed, the converted text will remain at its select state(invert-colored).

    Set ds = document.selection
    slen = Len(ds.Text) ' get selected text length
    posSlash = InStr(ds.Text, "/")
    posBkSlash = InStr(ds.Text, "")

    If posSlash>0 and (posBkSlash=0 or posBkSlash>posSlash) Then
    ' / in selected text and / appears before
    ds.Text = Replace(ds.Text, "/", "")
    ds.CharLeft True, slen 'make the new text in selected state(inverted color)
    ElseIf posBkSlash>0 Then
    ' in selected text
    ds.Text = Replace(ds.Text, "", "/")
    ds.CharLeft True, slen
    Else
    Set WshShell = CreateObject( "WScript.Shell" )
    n = WshShell.Popup( "There is no / or in your selected text. This macro can do nothing.", 0, "EmEditor Macro running", 0 )
    Set dc = document.config
    crlf_style = dc.FileSave.ReturnMethod
    WshShell.Popup "abc" & crlf_style
    End If

    If the selection is partial line, or, multiple lines of a Unix text file, it works well. However, if the selection is multiple lines of a CRLF text file, there is something wrong: ds.CharLeft True, slen will move(select) visually more characters than I expect, –because, at end of a line, ds.CharLeft True, 1 will actually step 2 characters(CR and LF).

    So, If I can know whether a text file is CRLF-ed, I can replace all CRLF to LF before I count slen=Len(ds.Text) , so that the finally selection length will be correct.

    I hope you can add this property in future version. On the other hand, is there another way to achieve the same goal(implement the slash-cvt.vbee macro)?

    #8700
    Yutaka Emura
    Keymaster

    Hi,

    GetLine method (Document object) allows you to add new line character(s) (CR, LF, or CR+LF) at the end of the line.


    str = document.GetLine( yLine, eeGetLineWithNewLines )

    Then you can correctly count the line length.

    I hope this helps.

    #8703
    chjfth
    Member

    Excuse me, I cannot tell how document.GetLine() can help. In order to call GetLine, I have to know the line numbers of the selected text, which seems to stray away from my topic.

    This issue is not urgent. I can wait until next EmEditor release when you add a document property to tell current document’s return method.

    Regards.

    #8704
    Yutaka Emura
    Keymaster

    dc.FileSave.ReturnMethod usually always returns 0. This means the configuration will not alter the return method of a document. EmEditor supports files that have mixed return methods.

    If you want to find the return method of the current document and if you know the return method is the same in the entire document, you can write:

    str = document.GetLine( 0, eeGetLineWithNewLines )

    Then you can find what the return method of the current document is.

    I hope this helps.
    If you have further questions, please let me know.
    Thanks!

    #8705
    chjfth
    Member

    Many thanks to your final clear reply. Now I know that EmEditor supports mixed EOL mark. And thank you for pointing out


    str = document.GetLine( 1, eeGetLineWithNewLines )

    can find out the EOL of line 1. (Yes, I must use 1 as first parameter instead of 0. 0 will cause script error “Invalid procedure call or argument”.

    Now, I realize that I pratically don’t have to care if current text file has LF or CRLF as end-of-line marker; I can simply replace all occurence of “rn” to “n” before counting slen, then my goal is fulfilled.

    So my script becomes:


    ' This VBScript macro do the following:
    ' For a block of selected text,
    ' * if a / is found, replace all occurance of / to .
    ' * else if a is found, replace all to / .
    ' After the macro is executed, the converted text will remain at its select state(invert-colored).

    Set ds = document.selection
    TextCopy = Replace(ds.Text, chr(13)+chr(10), chr(10)) 'replace rn to be one n
    slen = Len(TextCopy) 'get selected text length, rn counted as only one char
    posSlash = InStr(ds.Text, "/")
    posBkSlash = InStr(ds.Text, "")

    If posSlash>0 and (posBkSlash=0 or posBkSlash>posSlash) Then
    ' / in selected text and / appears before
    ds.Text = Replace(ds.Text, "/", "")
    ds.CharLeft True, slen 'make the new text in selected state(inverted color)
    ElseIf posBkSlash>0 Then
    ' in selected text
    ds.Text = Replace(ds.Text, "", "/")
    ds.CharLeft True, slen
    Else
    Set WshShell = CreateObject( "WScript.Shell" )
    n = WshShell.Popup( "There is no / or in your selected text. This macro can do nothing.", 0, "EmEditor Macro running", 0 )
    End If

    EmEditor is so great with custom script.

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