Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic EOL conversion is needed. #12

Closed
username39 opened this issue Sep 25, 2011 · 4 comments
Closed

Automatic EOL conversion is needed. #12

username39 opened this issue Sep 25, 2011 · 4 comments

Comments

@username39
Copy link

Fingertext doesn't consider current document's EOL mode, so snippet insertion may cause mixed-EOLs problem.

Below is a quick patch to convert LF-ended snippets into current document's EOL at insertion time.

It doesn't check whether a snippet is CRLF-ended or not, so additional code is needed to make sure an user-created snippets always have LF-ended. (I can see that every imported snippets are converted to LF)

--- PluginDefinition.cpp    Thu Jan 15 18:14:12 1970
+++ PluginDefinition.cpp    Thu Jan 15 18:14:12 1970
@@ -2962,7 +2962,12 @@

 bool replaceTag(char *expanded, int &posCurrent, int &posBeforeTag)
 {
+    char *expanded_eolfix;
+    int eolmode = ::SendScintilla(SCI_GETEOLMODE, 0, 0);
+    char *eol[3] = {"\r\n","\r","\n"};

+    expanded_eolfix = replaceAll(expanded, "\n", eol[eolmode]);
+    
     //TODO: can use ::SendMessage(curScintilla, SCI_ENSUREVISIBLE, line-1, 0); to make sure that caret is visible after long snippet substitution.
     //TODO: should abandon this `[SnippetInserting] method
     int lineCurrent = ::SendScintilla(SCI_LINEFROMPOSITION, posCurrent, 0);
@@ -2972,7 +2977,9 @@

    ::SendScintilla(SCI_SETTARGETSTART, posBeforeTag, 0);
    ::SendScintilla(SCI_SETTARGETEND, posCurrent, 0);
-    ::SendScintilla(SCI_REPLACETARGET, strlen(expanded), reinterpret_cast<LPARAM>(expanded));
+    ::SendScintilla(SCI_REPLACETARGET, strlen(expanded_eolfix), reinterpret_cast<LPARAM>(expanded_eolfix));
+
+    delete [] expanded_eolfix;

     searchNext("`[SnippetInserting]");
     int posEndOfInsertedText = ::SendScintilla(SCI_GETCURRENTPOS,0,0)+19;
@@ -5247,3 +5254,34 @@

     //::SendMessage(nppData._nppHandle, NPPM_ACTIVATEDOC, MAIN_VIEW, 1);
 }
+
+char *replaceAll(char *src, const char *fromstr, const char *tostr) {
+    char *result, *sr;
+    size_t i, count = 0;
+    size_t fromlen = strlen(fromstr); if (fromlen < 1) return src;
+    size_t tolen = strlen(tostr);
+
+    if (tolen != fromlen) {
+        for (i = 0; src[i] != '\0';) {
+            if (memcmp(&src[i], fromstr, fromlen) == 0) count++, i += fromlen;
+        else i++;
+        }
+    } else i = strlen(src);
+
+
+    result = (char *) malloc(i + 1 + count * (tolen - fromlen));
+    if (result == NULL) return NULL;
+
+
+    sr = result;
+    while (*src) {
+        if (memcmp(src, fromstr, fromlen) == 0) {
+            memcpy(sr, tostr, tolen);
+            sr += tolen;
+            src  += fromlen;
+        } else *sr++ = *src++;
+    }
+    *sr = '\0';
+
+    return result;
+}
\ No newline at end of file
--- PluginDefinition.h  Thu Jan 15 18:14:12 1970
+++ PluginDefinition.h  Thu Jan 15 18:14:12 1970
@@ -168,7 +168,7 @@
 void testing();
 void testing2();

-
+char *replaceAll(char *src, const char *fromstr, const char *tostr);
@erinata
Copy link
Owner

erinata commented Sep 25, 2011

Thanks for your implementation. I will probably add code to the saveSnippet() function to ensure that all the snippets are using \n when they are in database.

(the "transformation to \n" in importSnippet() is not perfect and need rewrite actually)

Also, thanks for the replaceAll function!! I have tried to write a function like this and don't know how to write it. So I end up changing the char* to string and change it back..............I think I can use this replaceAll() function in many other places.

@username39
Copy link
Author

I'm happy to hear that it's useful :D
Rather than creation-time conversion, just putting "\n" at the first line of SnippetEditor.ftb might be sufficient, since every newline will then be terminated by LF unless user explicitly insert "\r".
Not a perfect solution though :)

@erinata
Copy link
Owner

erinata commented Oct 12, 2011

Fingertext 0.5.44 is released and this issue should be fixed in this new version

http://sourceforge.net/projects/fingertext/files/Alpha%20Releases/FingerText%20-%200.5.44.zip/download

@username39
Copy link
Author

Confirmed. Thank you very much ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants