/*mimir*/ #define FINALLY(ARG) catch(...){ARG;}ARG; inline void GUIDToStr(const GUID &g, char*a){ sprintf(a,"{%X-%X-%X-%X%X-%X%X%X%X%X%X}",g.Data1,g.Data2,g.Data3, g.Data4[0],g.Data4[1],g.Data4[2],g.Data4[3],g.Data4[4], g.Data4[5],g.Data4[6],g.Data4[7]); } bool SciTEBase::StartExpandAbbreviation() { int currentPos = GetCaretInLine(); int position = SendEditor(SCI_GETCURRENTPOS); // from the beginning char *linebuf = new char[currentPos + 2]; GetLine(linebuf, currentPos + 2); // Just get text to the left of the caret linebuf[currentPos] = '\0'; int abbrevPos = (currentPos > 32 ? currentPos - 32 : 0); const char *abbrev = linebuf + abbrevPos; SString data; size_t dataLength = 0; int abbrevLength = currentPos - abbrevPos; // Try each potential abbreviation from the first letter on a line // and expanding to the right. // We arbitrarily limits the length of an abbreviation (seems a reasonable value..), // and of course stop on the caret. while (abbrevLength > 0) { data = propsAbbrev.Get(abbrev); dataLength = data.length(); if (dataLength > 0) { break; /* Found */ } abbrev++; // One more letter to the right abbrevLength--; } if (dataLength == 0) { WarnUser(warnNotFound); // No need for a special warning return true; // returning if expanded abbreviation is empty } /*mimir*/ SString currentSelection = EncodeString(SelectionExtend(0, false)); SString clpBuffer; bool UseSel = false; BOOL IsOpen=OpenClipboard(0); if(IsOpen){ HANDLE Data; Data = GetClipboardData(CF_TEXT); try{ if(Data != 0){ try{ clpBuffer = static_cast<char*>(GlobalLock(Data)); } FINALLY(GlobalUnlock(Data)); } } FINALLY(CloseClipboard()); } SendEditorString(SCI_REPLACESEL, 0, ""); char *expbuf = new char[dataLength + 1]; strcpy(expbuf, data.c_str()); UnSlash(expbuf); size_t expbuflen = strlen(expbuf); int caret_pos = -1; // caret position int currentLineNumber = GetCurrentLineNumber(); int indent = 0; int indentExtra = 0; bool isIndent = true; char *pPerc=NULL; int eolMode = SendEditor(SCI_GETEOLMODE); if (props.GetInt("indent.automatic")) { indent = GetLineIndentation(currentLineNumber); } SendEditor(SCI_BEGINUNDOACTION); SendEditor(SCI_SETSEL, position - abbrevLength, position); // add the abbreviation one character at a time for (size_t i = 0; i < expbuflen; i++) { char c = expbuf[i]; SString abbrevText(""); if (isIndent && c == '\t') { indentExtra++; SetLineIndentation(currentLineNumber, indent + SendEditor(SCI_GETINDENT) * indentExtra); } else { switch (c) { case '|': // user may want to insert '|' instead of caret if (i < (dataLength - 1) && expbuf[i + 1] == '|') { // put '|' into the line abbrevText += c; i++; } else if (caret_pos == -1) { if (i == 0) { // when caret is set at the first place in abbreviation caret_pos = SendEditor(SCI_GETCURRENTPOS) - abbrevLength; } else { caret_pos = SendEditor(SCI_GETCURRENTPOS); } } break; case '\n': if (eolMode == SC_EOL_CRLF || eolMode == SC_EOL_CR) { abbrevText += '\r'; } if (eolMode == SC_EOL_CRLF || eolMode == SC_EOL_LF) { abbrevText += '\n'; } break; case '%':/*mimir*/ pPerc=strstr(&expbuf[i+1],"%"); if(pPerc){ int lenPerc = pPerc - expbuf-i+1; pPerc = new char[lenPerc+2]; strncpy(pPerc,&expbuf[i],lenPerc); pPerc[lenPerc] = '\0'; if(strcmp(pPerc,"%SEL%")==0){ abbrevText +=currentSelection; i+=lenPerc-1; UseSel = true; }else if(strcmp(pPerc,"%CLP%")==0){ abbrevText +=clpBuffer; i+=lenPerc-1; UseSel = true; }else if(strcmp(pPerc,"%GUID%")==0){ char chGUID[40]={0}; GUID guid; CoCreateGuid (&guid); GUIDToStr(guid,chGUID); abbrevText += chGUID; i+=strlen(chGUID)-1; }else abbrevText += c; delete pPerc; } break; default: abbrevText += c; break; } SendEditorString(SCI_REPLACESEL, 0, abbrevText.c_str()); if (c == '\n') { isIndent = true; indentExtra = 0; currentLineNumber++; SetLineIndentation(currentLineNumber, indent); } else { isIndent = false; } } } if(!UseSel)/*mimir*/ SendEditorString(SCI_REPLACESEL, 0, currentSelection.c_str()); // set the caret to the desired position if (caret_pos != -1) { SendEditor(SCI_GOTOPOS, caret_pos); } SendEditor(SCI_ENDUNDOACTION); delete []expbuf; delete []linebuf; return true; } |