1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2016 - TortoiseGit
4 // Copyright (C) 2003-2008,2012-2016 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "LoglistCommonResource.h"
22 #include "PathUtils.h"
23 #include "UnicodeUtils.h"
27 #include "SmartHandle.h"
28 #include "../../TortoiseUDiff/UDiffColors.h"
30 void CSciEditContextMenuInterface::InsertMenuItems(CMenu
&, int&) {return;}
31 bool CSciEditContextMenuInterface::HandleMenuItemClick(int, CSciEdit
*) {return false;}
32 void CSciEditContextMenuInterface::HandleSnippet(int, const CString
&, CSciEdit
*) { return; }
35 #define STYLE_ISSUEBOLD 11
36 #define STYLE_ISSUEBOLDITALIC 12
38 #define STYLE_ITALIC 15
39 #define STYLE_UNDERLINED 16
41 #define INDIC_MISSPELLED 18
43 #define STYLE_MASK 0x1f
45 #define SCI_ADDWORD 2000
52 struct loc_map enc2locale
[] = {
53 {"28591","ISO8859-1"},
54 {"28592","ISO8859-2"},
55 {"28593","ISO8859-3"},
56 {"28594","ISO8859-4"},
57 {"28595","ISO8859-5"},
58 {"28596","ISO8859-6"},
59 {"28597","ISO8859-7"},
60 {"28598","ISO8859-8"},
61 {"28599","ISO8859-9"},
62 {"28605","ISO8859-15"},
65 {"1251","microsoft-cp1251"},
70 IMPLEMENT_DYNAMIC(CSciEdit
, CWnd
)
72 CSciEdit::CSciEdit(void) : m_DirectFunction(NULL
)
73 , m_DirectPointer(NULL
)
78 , m_nAutoCompleteMinChars(3)
79 , m_SpellingCache(2000)
81 m_hModule
= ::LoadLibrary(_T("SciLexer_tgit.dll"));
84 CSciEdit::~CSciEdit(void)
86 m_personalDict
.Save();
89 static std::unique_ptr
<UINT
[]> Icon2Image(HICON hIcon
)
95 if (!GetIconInfo(hIcon
, &iconInfo
))
99 if (!GetObject(iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
))
102 int width
= bm
.bmWidth
;
103 int height
= bm
.bmHeight
;
104 int bytesPerScanLine
= (width
* 3 + 3) & 0xFFFFFFFC;
105 int size
= bytesPerScanLine
* height
;
106 BITMAPINFO infoheader
;
107 infoheader
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
108 infoheader
.bmiHeader
.biWidth
= width
;
109 infoheader
.bmiHeader
.biHeight
= height
;
110 infoheader
.bmiHeader
.biPlanes
= 1;
111 infoheader
.bmiHeader
.biBitCount
= 24;
112 infoheader
.bmiHeader
.biCompression
= BI_RGB
;
113 infoheader
.bmiHeader
.biSizeImage
= size
;
115 auto ptrb
= std::make_unique
<BYTE
[]>(size
* 2 + height
* width
* 4);
116 LPBYTE pixelsIconRGB
= ptrb
.get();
117 LPBYTE alphaPixels
= pixelsIconRGB
+ size
;
118 HDC hDC
= CreateCompatibleDC(nullptr);
119 SCOPE_EXIT
{ DeleteDC(hDC
); };
120 HBITMAP hBmpOld
= (HBITMAP
)SelectObject(hDC
, (HGDIOBJ
)iconInfo
.hbmColor
);
121 if (!GetDIBits(hDC
, iconInfo
.hbmColor
, 0, height
, (LPVOID
)pixelsIconRGB
, &infoheader
, DIB_RGB_COLORS
))
124 SelectObject(hDC
, hBmpOld
);
125 if (!GetDIBits(hDC
, iconInfo
.hbmMask
, 0,height
, (LPVOID
)alphaPixels
, &infoheader
, DIB_RGB_COLORS
))
128 auto imagePixels
= std::make_unique
<UINT
[]>(height
* width
);
129 int lsSrc
= width
* 3;
130 int vsDest
= height
- 1;
131 for (int y
= 0; y
< height
; y
++)
133 int linePosSrc
= (vsDest
- y
) * lsSrc
;
134 int linePosDest
= y
* width
;
135 for (int x
= 0; x
< width
; x
++)
137 int currentDestPos
= linePosDest
+ x
;
138 int currentSrcPos
= linePosSrc
+ x
* 3;
139 imagePixels
[currentDestPos
] = (((UINT
)(
141 ((pixelsIconRGB
[currentSrcPos
+ 2] /*Red*/)
142 | (pixelsIconRGB
[currentSrcPos
+ 1] << 8 /*Green*/))
143 | pixelsIconRGB
[currentSrcPos
] << 16 /*Blue*/
145 | ((alphaPixels
[currentSrcPos
] ? 0 : 0xff) << 24))) & 0xffffffff);
151 void CSciEdit::Init(LONG lLanguage
)
153 //Setup the direct access data
154 m_DirectFunction
= SendMessage(SCI_GETDIRECTFUNCTION
, 0, 0);
155 m_DirectPointer
= SendMessage(SCI_GETDIRECTPOINTER
, 0, 0);
156 Call(SCI_SETMARGINWIDTHN
, 1, 0);
157 Call(SCI_SETUSETABS
, 0); //pressing TAB inserts spaces
158 Call(SCI_SETWRAPVISUALFLAGS
, SC_WRAPVISUALFLAG_END
);
159 Call(SCI_AUTOCSETIGNORECASE
, 1);
160 Call(SCI_SETLEXER
, SCLEX_CONTAINER
);
161 Call(SCI_SETCODEPAGE
, SC_CP_UTF8
);
162 Call(SCI_AUTOCSETFILLUPS
, 0, (LPARAM
)"\t([");
163 Call(SCI_AUTOCSETMAXWIDTH
, 0);
164 //Set the default windows colors for edit controls
165 Call(SCI_STYLESETFORE
, STYLE_DEFAULT
, ::GetSysColor(COLOR_WINDOWTEXT
));
166 Call(SCI_STYLESETBACK
, STYLE_DEFAULT
, ::GetSysColor(COLOR_WINDOW
));
167 Call(SCI_SETSELFORE
, TRUE
, ::GetSysColor(COLOR_HIGHLIGHTTEXT
));
168 Call(SCI_SETSELBACK
, TRUE
, ::GetSysColor(COLOR_HIGHLIGHT
));
169 Call(SCI_SETCARETFORE
, ::GetSysColor(COLOR_WINDOWTEXT
));
170 Call(SCI_SETMODEVENTMASK
, SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
| SC_PERFORMED_UNDO
| SC_PERFORMED_REDO
);
171 Call(SCI_INDICSETSTYLE
, INDIC_MISSPELLED
, INDIC_SQUIGGLE
);
172 Call(SCI_INDICSETFORE
, INDIC_MISSPELLED
, RGB(255,0,0));
174 CStringA sWhiteSpace
;
175 for (int i
=0; i
<255; ++i
)
177 if (i
== '\r' || i
== '\n')
179 else if (i
< 0x20 || i
== ' ')
180 sWhiteSpace
+= (char)i
;
181 else if (isalnum(i
) || i
== '\'' || i
== '_' || i
== '-')
182 sWordChars
+= (char)i
;
184 Call(SCI_SETWORDCHARS
, 0, (LPARAM
)(LPCSTR
)sWordChars
);
185 Call(SCI_SETWHITESPACECHARS
, 0, (LPARAM
)(LPCSTR
)sWhiteSpace
);
186 m_bDoStyle
= ((DWORD
)CRegStdDWORD(_T("Software\\TortoiseGit\\StyleCommitMessages"), TRUE
))==TRUE
;
187 m_nAutoCompleteMinChars
= (int)(DWORD
)CRegStdDWORD(_T("Software\\TortoiseGit\\AutoCompleteMinChars"), 3);
188 // look for dictionary files and use them if found
189 if ((lLanguage
>= 0) && (((DWORD
)CRegStdDWORD(L
"Software\\TortoiseGit\\Spellchecker", TRUE
)) == TRUE
))
191 if (!lLanguage
|| (lLanguage
&& !LoadDictionaries(lLanguage
)))
193 long langId
= GetUserDefaultLCID();
196 LoadDictionaries(langId
);
197 DWORD lid
= SUBLANGID(langId
);
200 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
201 else if (langId
== 1033)
205 } while (langId
&& (!pChecker
|| !pThesaur
));
209 Call(SCI_SETEDGEMODE
, EDGE_NONE
);
210 Call(SCI_SETWRAPMODE
, SC_WRAP_WORD
);
211 Call(SCI_ASSIGNCMDKEY
, SCK_END
, SCI_LINEENDWRAP
);
212 Call(SCI_ASSIGNCMDKEY
, SCK_END
+ (SCMOD_SHIFT
<< 16), SCI_LINEENDWRAPEXTEND
);
213 Call(SCI_ASSIGNCMDKEY
, SCK_HOME
, SCI_HOMEWRAP
);
214 Call(SCI_ASSIGNCMDKEY
, SCK_HOME
+ (SCMOD_SHIFT
<< 16), SCI_HOMEWRAPEXTEND
);
215 if (CRegStdDWORD(L
"Software\\TortoiseGit\\ScintillaDirect2D", FALSE
) != FALSE
)
217 Call(SCI_SETTECHNOLOGY
, SC_TECHNOLOGY_DIRECTWRITERETAIN
);
218 Call(SCI_SETBUFFEREDDRAW
, 0);
223 void CSciEdit::Init(const ProjectProperties
& props
)
225 Init(props
.lProjectLanguage
);
226 m_sCommand
= CStringA(CUnicodeUtils::GetUTF8(props
.GetCheckRe()));
227 m_sBugID
= CStringA(CUnicodeUtils::GetUTF8(props
.GetBugIDRe()));
228 m_sUrl
= CStringA(CUnicodeUtils::GetUTF8(props
.sUrl
));
230 Call(SCI_SETMOUSEDWELLTIME
, 333);
232 if (props
.nLogWidthMarker
)
234 Call(SCI_SETWRAPMODE
, SC_WRAP_NONE
);
235 Call(SCI_SETEDGEMODE
, EDGE_LINE
);
236 Call(SCI_SETEDGECOLUMN
, props
.nLogWidthMarker
);
237 Call(SCI_SETSCROLLWIDTHTRACKING
, TRUE
);
238 Call(SCI_SETSCROLLWIDTH
, 1);
242 Call(SCI_SETEDGEMODE
, EDGE_NONE
);
243 Call(SCI_SETWRAPMODE
, SC_WRAP_WORD
);
247 void CSciEdit::SetIcon(const std::map
<int, UINT
> &icons
)
249 Call(SCI_RGBAIMAGESETWIDTH
, 16);
250 Call(SCI_RGBAIMAGESETHEIGHT
, 16);
251 for (auto icon
: icons
)
253 auto hIcon
= (HICON
)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(icon
.second
), IMAGE_ICON
, 16, 16, LR_DEFAULTCOLOR
);
254 auto bytes
= Icon2Image(hIcon
);
256 Call(SCI_REGISTERRGBAIMAGE
, icon
.first
, (LPARAM
)bytes
.get());
260 BOOL
CSciEdit::LoadDictionaries(LONG lLanguageID
)
262 //Setup the spell checker and thesaurus
263 TCHAR buf
[6] = { 0 };
264 CString sFolderUp
= CPathUtils::GetAppParentDirectory();
265 CString sFolderAppData
= CPathUtils::GetAppDataDirectory();
268 GetLocaleInfo(MAKELCID(lLanguageID
, SORT_DEFAULT
), LOCALE_SISO639LANGNAME
, buf
, _countof(buf
));
270 if (lLanguageID
== 2074)
271 sFile
+= _T("-Latn");
273 GetLocaleInfo(MAKELCID(lLanguageID
, SORT_DEFAULT
), LOCALE_SISO3166CTRYNAME
, buf
, _countof(buf
));
277 if ((PathFileExists(sFolderAppData
+ _T("dic\\") + sFile
+ _T(".aff"))) &&
278 (PathFileExists(sFolderAppData
+ _T("dic\\") + sFile
+ _T(".dic"))))
280 pChecker
= std::make_unique
<Hunspell
>(CStringA(sFolderAppData
+ _T("dic\\") + sFile
+ _T(".aff")), CStringA(sFolderAppData
+ _T("dic\\") + sFile
+ _T(".dic")));
282 else if ((PathFileExists(sFolderUp
+ _T("Languages\\") + sFile
+ _T(".aff"))) &&
283 (PathFileExists(sFolderUp
+ _T("Languages\\") + sFile
+ _T(".dic"))))
285 pChecker
= std::make_unique
<Hunspell
>(CStringA(sFolderUp
+ _T("Languages\\") + sFile
+ _T(".aff")), CStringA(sFolderUp
+ _T("Languages\\") + sFile
+ _T(".dic")));
289 const char* encoding
= pChecker
->get_dic_encoding();
290 CTraceToOutputDebugString::Instance()(__FUNCTION__
": %s\n", encoding
);
292 for (int i
= 0; i
< _countof(enc2locale
); ++i
)
294 if (strcmp(encoding
, enc2locale
[i
].def_enc
) == 0)
295 m_spellcodepage
= atoi(enc2locale
[i
].cp
);
297 m_personalDict
.Init(lLanguageID
);
303 if ((PathFileExists(sFolderAppData
+ _T("dic\\th_") + sFile
+ _T("_v2.idx"))) &&
304 (PathFileExists(sFolderAppData
+ _T("dic\\th_") + sFile
+ _T("_v2.dat"))))
306 pThesaur
= std::make_unique
<MyThes
>(CStringA(sFolderAppData
+ _T("dic\\th_") + sFile
+ _T("_v2.idx")), CStringA(sFolderAppData
+ _T("dic\\th_") + sFile
+ _T("_v2.dat")));
308 else if ((PathFileExists(sFolderUp
+ _T("Languages\\th_") + sFile
+ _T("_v2.idx"))) &&
309 (PathFileExists(sFolderUp
+ _T("Languages\\th_") + sFile
+ _T("_v2.dat"))))
311 pThesaur
= std::make_unique
<MyThes
>(CStringA(sFolderUp
+ _T("Languages\\th_") + sFile
+ _T("_v2.idx")), CStringA(sFolderUp
+ _T("Languages\\th_") + sFile
+ _T("_v2.dat")));
315 if ((pThesaur
)||(pChecker
))
320 LRESULT
CSciEdit::Call(UINT message
, WPARAM wParam
, LPARAM lParam
)
322 ASSERT(::IsWindow(m_hWnd
)); //Window must be valid
323 ASSERT(m_DirectFunction
); //Direct function must be valid
324 return ((SciFnDirect
) m_DirectFunction
)(m_DirectPointer
, message
, wParam
, lParam
);
327 CString
CSciEdit::StringFromControl(const CStringA
& text
)
331 int codepage
= (int)Call(SCI_GETCODEPAGE
);
332 int reslen
= MultiByteToWideChar(codepage
, 0, text
, text
.GetLength(), 0, 0);
333 MultiByteToWideChar(codepage
, 0, text
, text
.GetLength(), sText
.GetBuffer(reslen
+1), reslen
+1);
334 sText
.ReleaseBuffer(reslen
);
341 CStringA
CSciEdit::StringForControl(const CString
& text
)
345 int codepage
= (int)SendMessage(SCI_GETCODEPAGE
);
346 int reslen
= WideCharToMultiByte(codepage
, 0, text
, text
.GetLength(), 0, 0, 0, 0);
347 WideCharToMultiByte(codepage
, 0, text
, text
.GetLength(), sTextA
.GetBuffer(reslen
), reslen
, 0, 0);
348 sTextA
.ReleaseBuffer(reslen
);
352 ATLTRACE("string length %d\n", sTextA
.GetLength());
356 void CSciEdit::SetText(const CString
& sText
)
358 CStringA sTextA
= StringForControl(sText
);
359 Call(SCI_SETTEXT
, 0, (LPARAM
)(LPCSTR
)sTextA
);
361 if (Call(SCI_GETSCROLLWIDTHTRACKING
) != 0)
362 Call(SCI_SETSCROLLWIDTH
, 1);
364 // Scintilla seems to have problems with strings that
365 // aren't terminated by a newline char. Once that char
366 // is there, it can be removed without problems.
367 // So we add here a newline, then remove it again.
368 Call(SCI_DOCUMENTEND
);
370 Call(SCI_DELETEBACK
);
373 void CSciEdit::InsertText(const CString
& sText
, bool bNewLine
)
375 CStringA sTextA
= StringForControl(sText
);
376 Call(SCI_REPLACESEL
, 0, (LPARAM
)(LPCSTR
)sTextA
);
378 Call(SCI_REPLACESEL
, 0, (LPARAM
)(LPCSTR
)"\n");
381 CString
CSciEdit::GetText()
383 LRESULT len
= Call(SCI_GETTEXT
, 0, 0);
385 Call(SCI_GETTEXT
, (WPARAM
)(len
+ 1), (LPARAM
)(LPCSTR
)sTextA
.GetBuffer((int)len
+ 1));
386 sTextA
.ReleaseBuffer();
387 return StringFromControl(sTextA
);
390 CString
CSciEdit::GetWordUnderCursor(bool bSelectWord
)
392 TEXTRANGEA textrange
;
393 int pos
= (int)Call(SCI_GETCURRENTPOS
);
394 textrange
.chrg
.cpMin
= (LONG
)Call(SCI_WORDSTARTPOSITION
, pos
, TRUE
);
395 if ((pos
== textrange
.chrg
.cpMin
)||(textrange
.chrg
.cpMin
< 0))
397 textrange
.chrg
.cpMax
= (LONG
)Call(SCI_WORDENDPOSITION
, textrange
.chrg
.cpMin
, TRUE
);
399 auto textbuffer
= std::make_unique
<char[]>(textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 1);
400 textrange
.lpstrText
= textbuffer
.get();
401 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
403 Call(SCI_SETSEL
, textrange
.chrg
.cpMin
, textrange
.chrg
.cpMax
);
404 CString sRet
= StringFromControl(textbuffer
.get());
408 void CSciEdit::SetFont(CString sFontName
, int iFontSizeInPoints
)
410 Call(SCI_STYLESETFONT
, STYLE_DEFAULT
, (LPARAM
)(LPCSTR
)CUnicodeUtils::GetUTF8(sFontName
).GetBuffer());
411 Call(SCI_STYLESETSIZE
, STYLE_DEFAULT
, iFontSizeInPoints
);
412 Call(SCI_STYLECLEARALL
);
414 LPARAM color
= (LPARAM
)GetSysColor(COLOR_HIGHLIGHT
);
415 // set the styles for the bug ID strings
416 Call(SCI_STYLESETBOLD
, STYLE_ISSUEBOLD
, (LPARAM
)TRUE
);
417 Call(SCI_STYLESETFORE
, STYLE_ISSUEBOLD
, color
);
418 Call(SCI_STYLESETBOLD
, STYLE_ISSUEBOLDITALIC
, (LPARAM
)TRUE
);
419 Call(SCI_STYLESETITALIC
, STYLE_ISSUEBOLDITALIC
, (LPARAM
)TRUE
);
420 Call(SCI_STYLESETFORE
, STYLE_ISSUEBOLDITALIC
, color
);
421 Call(SCI_STYLESETHOTSPOT
, STYLE_ISSUEBOLDITALIC
, (LPARAM
)TRUE
);
423 // set the formatted text styles
424 Call(SCI_STYLESETBOLD
, STYLE_BOLD
, (LPARAM
)TRUE
);
425 Call(SCI_STYLESETITALIC
, STYLE_ITALIC
, (LPARAM
)TRUE
);
426 Call(SCI_STYLESETUNDERLINE
, STYLE_UNDERLINED
, (LPARAM
)TRUE
);
428 // set the style for URLs
429 Call(SCI_STYLESETFORE
, STYLE_URL
, color
);
430 Call(SCI_STYLESETHOTSPOT
, STYLE_URL
, (LPARAM
)TRUE
);
432 Call(SCI_SETHOTSPOTACTIVEUNDERLINE
, (LPARAM
)TRUE
);
435 void CSciEdit::SetAutoCompletionList(const std::map
<CString
, int>& list
, TCHAR separator
, TCHAR typeSeparator
)
437 //copy the auto completion list.
439 //SK: instead of creating a copy of that list, we could accept a pointer
440 //to the list and use that instead. But then the caller would have to make
441 //sure that the list persists over the lifetime of the control!
444 m_separator
= separator
;
445 m_typeSeparator
= typeSeparator
;
448 BOOL
CSciEdit::IsMisspelled(const CString
& sWord
)
450 // convert the string from the control to the encoding of the spell checker module.
451 CStringA sWordA
= GetWordForSpellChecker(sWord
);
453 // words starting with a digit are treated as correctly spelled
454 if (_istdigit(sWord
.GetAt(0)))
456 // words in the personal dictionary are correct too
457 if (m_personalDict
.FindWord(sWord
))
460 // Check spell checking cache first.
461 const BOOL
*cacheResult
= m_SpellingCache
.try_get(std::wstring(sWord
, sWord
.GetLength()));
465 // now we actually check the spelling...
466 BOOL misspelled
= !pChecker
->spell(sWordA
);
469 // the word is marked as misspelled, we now check whether the word
470 // is maybe a composite identifier
471 // a composite identifier consists of multiple words, with each word
472 // separated by a change in lower to uppercase letters
474 if (sWord
.GetLength() > 1)
478 while (wordend
< sWord
.GetLength())
480 while ((wordend
< sWord
.GetLength())&&(!_istupper(sWord
[wordend
])))
482 if ((wordstart
== 0)&&(wordend
== sWord
.GetLength()))
484 // words in the auto list are also assumed correctly spelled
485 if (m_autolist
.find(sWord
) != m_autolist
.end())
491 sWordA
= GetWordForSpellChecker(sWord
.Mid(wordstart
, wordend
- wordstart
));
492 if ((sWordA
.GetLength() > 2) && (!pChecker
->spell(sWordA
)))
504 m_SpellingCache
.insert_or_assign(std::wstring(sWord
, sWord
.GetLength()), misspelled
);
508 void CSciEdit::CheckSpelling(int startpos
, int endpos
)
513 TEXTRANGEA textrange
;
514 textrange
.chrg
.cpMin
= startpos
;
515 textrange
.chrg
.cpMax
= (LONG
)textrange
.chrg
.cpMin
;
516 LRESULT lastpos
= endpos
;
518 lastpos
= Call(SCI_GETLENGTH
)-textrange
.chrg
.cpMin
;
519 Call(SCI_SETINDICATORCURRENT
, INDIC_MISSPELLED
);
520 while (textrange
.chrg
.cpMax
< lastpos
)
522 textrange
.chrg
.cpMin
= (LONG
)Call(SCI_WORDSTARTPOSITION
, textrange
.chrg
.cpMax
+1, TRUE
);
523 if (textrange
.chrg
.cpMin
< textrange
.chrg
.cpMax
)
525 textrange
.chrg
.cpMax
= (LONG
)Call(SCI_WORDENDPOSITION
, textrange
.chrg
.cpMin
, TRUE
);
526 if (textrange
.chrg
.cpMin
== textrange
.chrg
.cpMax
)
528 textrange
.chrg
.cpMax
++;
529 // since Scintilla squiggles to the end of the text even if told to stop one char before it,
530 // we have to clear here the squiggly lines to the end.
531 if (textrange
.chrg
.cpMin
)
532 Call(SCI_INDICATORCLEARRANGE
, textrange
.chrg
.cpMin
-1, textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 1);
535 ATLASSERT(textrange
.chrg
.cpMax
>= textrange
.chrg
.cpMin
);
536 auto textbuffer
= std::make_unique
<char[]>(textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 2);
537 SecureZeroMemory(textbuffer
.get(), textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 2);
538 textrange
.lpstrText
= textbuffer
.get();
539 textrange
.chrg
.cpMax
++;
540 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
541 int len
= (int)strlen(textrange
.lpstrText
);
544 textrange
.chrg
.cpMax
--;
545 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
546 len
= (int)strlen(textrange
.lpstrText
);
547 textrange
.chrg
.cpMax
++;
550 if (len
&& textrange
.lpstrText
[len
- 1] == '.')
552 // Try to ignore file names from the auto list.
553 // Do do this, for each word ending with '.' we extract next word and check
554 // whether the combined string is present in auto list.
556 twoWords
.chrg
.cpMin
= textrange
.chrg
.cpMin
;
557 twoWords
.chrg
.cpMax
= (LONG
)Call(SCI_WORDENDPOSITION
, textrange
.chrg
.cpMax
+ 1, TRUE
);
558 auto twoWordsBuffer
= std::make_unique
<char[]>(twoWords
.chrg
.cpMax
- twoWords
.chrg
.cpMin
+ 1);
559 twoWords
.lpstrText
= twoWordsBuffer
.get();
560 SecureZeroMemory(twoWords
.lpstrText
, twoWords
.chrg
.cpMax
- twoWords
.chrg
.cpMin
+ 1);
561 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&twoWords
);
562 CString sWord
= StringFromControl(twoWords
.lpstrText
);
563 if (m_autolist
.find(sWord
) != m_autolist
.end())
565 //mark word as correct (remove the squiggle line)
566 Call(SCI_INDICATORCLEARRANGE
, twoWords
.chrg
.cpMin
, twoWords
.chrg
.cpMax
- twoWords
.chrg
.cpMin
);
567 textrange
.chrg
.cpMax
= twoWords
.chrg
.cpMax
;
572 textrange
.lpstrText
[len
- 1] = 0;
573 textrange
.chrg
.cpMax
--;
574 if (strlen(textrange
.lpstrText
) > 0)
576 CString sWord
= StringFromControl(textrange
.lpstrText
);
577 if ((GetStyleAt(textrange
.chrg
.cpMin
) != STYLE_URL
) && IsMisspelled(sWord
))
579 //mark word as misspelled
580 Call(SCI_INDICATORFILLRANGE
, textrange
.chrg
.cpMin
, textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
);
584 //mark word as correct (remove the squiggle line)
585 Call(SCI_INDICATORCLEARRANGE
, textrange
.chrg
.cpMin
, textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
);
586 Call(SCI_INDICATORCLEARRANGE
, textrange
.chrg
.cpMin
, textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 1);
592 void CSciEdit::SuggestSpellingAlternatives()
596 CString word
= GetWordUnderCursor(true);
597 Call(SCI_SETCURRENTPOS
, Call(SCI_WORDSTARTPOSITION
, Call(SCI_GETCURRENTPOS
), TRUE
));
600 char ** wlst
= nullptr;
601 int ns
= pChecker
->suggest(&wlst
, GetWordForSpellChecker(word
));
605 for (int i
=0; i
< ns
; i
++)
607 suggestions
.AppendFormat(_T("%s%c%d%c"), (LPCTSTR
)GetWordFromSpellChecker(wlst
[i
]), m_typeSeparator
, AUTOCOMPLETE_SPELLING
, m_separator
);
611 suggestions
.TrimRight(m_separator
);
612 if (suggestions
.IsEmpty())
614 Call(SCI_AUTOCSETSEPARATOR
, (WPARAM
)CStringA(m_separator
).GetAt(0));
615 Call(SCI_AUTOCSETTYPESEPARATOR
, (WPARAM
)m_typeSeparator
);
616 Call(SCI_AUTOCSETDROPRESTOFWORD
, 1);
617 Call(SCI_AUTOCSHOW
, 0, (LPARAM
)(LPCSTR
)StringForControl(suggestions
));
623 void CSciEdit::DoAutoCompletion(int nMinPrefixLength
)
625 if (m_autolist
.empty())
627 if (Call(SCI_AUTOCACTIVE
))
629 CString word
= GetWordUnderCursor();
630 if (word
.GetLength() < nMinPrefixLength
)
631 return; //don't auto complete yet, word is too short
632 int pos
= (int)Call(SCI_GETCURRENTPOS
);
633 if (pos
!= Call(SCI_WORDENDPOSITION
, pos
, TRUE
))
634 return; //don't auto complete if we're not at the end of a word
635 CString sAutoCompleteList
;
637 std::vector
<CString
> words
;
639 pos
= word
.Find('-');
641 CString wordLower
= word
;
642 wordLower
.MakeLower();
643 CString wordHigher
= word
;
644 wordHigher
.MakeUpper();
646 words
.push_back(word
);
647 words
.push_back(wordLower
);
648 words
.push_back(wordHigher
);
652 CString s
= wordLower
.Left(pos
);
653 if (s
.GetLength() >= nMinPrefixLength
)
655 s
= wordLower
.Mid(pos
+1);
656 if (s
.GetLength() >= nMinPrefixLength
)
658 s
= wordHigher
.Left(pos
);
659 if (s
.GetLength() >= nMinPrefixLength
)
660 words
.push_back(wordHigher
.Left(pos
));
661 s
= wordHigher
.Mid(pos
+1);
662 if (s
.GetLength() >= nMinPrefixLength
)
663 words
.push_back(wordHigher
.Mid(pos
+1));
666 std::map
<CString
, int> wordset
;
667 for (const auto& w
: words
)
669 for (auto lowerit
= m_autolist
.lower_bound(w
);
670 lowerit
!= m_autolist
.end(); ++lowerit
)
672 int compare
= w
.CompareNoCase(lowerit
->first
.Left(w
.GetLength()));
675 else if (compare
== 0)
676 wordset
.emplace(lowerit
->first
, lowerit
->second
);
682 for (const auto& w
: wordset
)
683 sAutoCompleteList
.AppendFormat(_T("%s%c%d%c"), (LPCTSTR
)w
.first
, m_typeSeparator
, w
.second
, m_separator
);
685 sAutoCompleteList
.TrimRight(m_separator
);
686 if (sAutoCompleteList
.IsEmpty())
689 Call(SCI_AUTOCSETSEPARATOR
, (WPARAM
)CStringA(m_separator
).GetAt(0));
690 Call(SCI_AUTOCSETTYPESEPARATOR
, (WPARAM
)m_typeSeparator
);
691 auto sForControl
= StringForControl(sAutoCompleteList
);
692 Call(SCI_AUTOCSHOW
, StringForControl(word
).GetLength(), (LPARAM
)(LPCSTR
)sForControl
);
695 BOOL
CSciEdit::OnChildNotify(UINT message
, WPARAM wParam
, LPARAM lParam
, LRESULT
* pLResult
)
697 if (message
!= WM_NOTIFY
)
698 return CWnd::OnChildNotify(message
, wParam
, lParam
, pLResult
);
700 LPNMHDR lpnmhdr
= (LPNMHDR
) lParam
;
701 SCNotification
* lpSCN
= (SCNotification
*)lParam
;
703 if(lpnmhdr
->hwndFrom
==m_hWnd
)
705 switch(lpnmhdr
->code
)
709 if ((lpSCN
->ch
< 32)&&(lpSCN
->ch
!= 13)&&(lpSCN
->ch
!= 10))
710 Call(SCI_DELETEBACK
);
712 DoAutoCompletion(m_nAutoCompleteMinChars
);
716 case SCN_AUTOCSELECTION
:
718 CString text
= StringFromControl(lpSCN
->text
);
719 if (m_autolist
[text
] == AUTOCOMPLETE_SNIPPET
)
721 Call(SCI_AUTOCCANCEL
);
722 for (INT_PTR handlerindex
= 0; handlerindex
< m_arContextHandlers
.GetCount(); ++handlerindex
)
724 CSciEditContextMenuInterface
* pHandler
= m_arContextHandlers
.GetAt(handlerindex
);
725 pHandler
->HandleSnippet(m_autolist
[text
], text
, this);
730 case SCN_STYLENEEDED
:
732 int startpos
= (int)Call(SCI_GETENDSTYLED
);
733 int endpos
= ((SCNotification
*)lpnmhdr
)->position
;
735 int startwordpos
= (int)Call(SCI_WORDSTARTPOSITION
, startpos
, true);
736 int endwordpos
= (int)Call(SCI_WORDENDPOSITION
, endpos
, true);
738 MarkEnteredBugID(startwordpos
, endwordpos
);
740 StyleEnteredText(startwordpos
, endwordpos
);
742 StyleURLs(startwordpos
, endwordpos
);
743 CheckSpelling(startwordpos
, endwordpos
);
745 // Tell scintilla editor that we styled all requested range.
746 Call(SCI_STARTSTYLING
, endwordpos
);
747 Call(SCI_SETSTYLING
, 0, 0);
752 if (lpSCN
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
))
754 LRESULT firstline
= Call(SCI_GETFIRSTVISIBLELINE
);
755 LRESULT lastline
= firstline
+ Call(SCI_LINESONSCREEN
);
756 int firstpos
= (int)Call(SCI_POSITIONFROMLINE
, firstline
);
757 int lastpos
= (int)Call(SCI_GETLINEENDPOSITION
, lastline
);
758 int pos1
= lpSCN
->position
;
759 int pos2
= pos1
+ lpSCN
->length
;
760 // always use the bigger range
761 firstpos
= min(firstpos
, pos1
);
762 lastpos
= max(lastpos
, pos2
);
764 WrapLines(firstpos
, lastpos
);
769 case SCN_HOTSPOTCLICK
:
771 TEXTRANGEA textrange
;
772 textrange
.chrg
.cpMin
= lpSCN
->position
;
773 textrange
.chrg
.cpMax
= lpSCN
->position
;
774 DWORD style
= GetStyleAt(lpSCN
->position
);
775 if (style
!= STYLE_ISSUEBOLDITALIC
&& style
!= STYLE_URL
)
777 while (GetStyleAt(textrange
.chrg
.cpMin
- 1) == style
)
778 --textrange
.chrg
.cpMin
;
779 while (GetStyleAt(textrange
.chrg
.cpMax
+ 1) == style
)
780 ++textrange
.chrg
.cpMax
;
781 ++textrange
.chrg
.cpMax
;
782 auto textbuffer
= std::make_unique
<char[]>(textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 1);
783 textrange
.lpstrText
= textbuffer
.get();
784 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
786 if (style
== STYLE_URL
)
788 url
= StringFromControl(textbuffer
.get());
789 if (url
.Find(L
'@') > 0 && !PathIsURL(url
))
790 url
= L
"mailto:" + url
;
795 url
.Replace(L
"%BUGID%", StringFromControl(textbuffer
.get()));
799 if (lpnmhdr
->code
== SCN_HOTSPOTCLICK
)
800 ShellExecute(GetParent()->GetSafeHwnd(), _T("open"), url
, nullptr, nullptr, SW_SHOWDEFAULT
);
803 CStringA sTextA
= StringForControl(url
);
804 Call(SCI_CALLTIPSHOW
, lpSCN
->position
+ 3, (LPARAM
)(LPCSTR
)sTextA
);
810 Call(SCI_CALLTIPCANCEL
);
814 return CWnd::OnChildNotify(message
, wParam
, lParam
, pLResult
);
817 BEGIN_MESSAGE_MAP(CSciEdit
, CWnd
)
822 void CSciEdit::OnKeyDown(UINT nChar
, UINT nRepCnt
, UINT nFlags
)
828 if ((Call(SCI_AUTOCACTIVE
)==0)&&(Call(SCI_CALLTIPACTIVE
)==0))
829 ::SendMessage(GetParent()->GetSafeHwnd(), WM_CLOSE
, 0, 0);
833 CWnd::OnKeyDown(nChar
, nRepCnt
, nFlags
);
836 BOOL
CSciEdit::PreTranslateMessage(MSG
* pMsg
)
838 if (pMsg
->message
== WM_KEYDOWN
)
840 switch (pMsg
->wParam
)
844 if ((GetKeyState(VK_CONTROL
) & 0x8000) && ((GetKeyState(VK_MENU
) & 0x8000) == 0))
852 // The TAB cannot be handled in OnKeyDown because it is too late by then.
854 if ((GetKeyState(VK_CONTROL
) & 0x8000) && ((GetKeyState(VK_MENU
) & 0x8000) == 0))
856 //Ctrl-Tab was pressed, this means we should provide the user with
857 //a list of possible spell checking alternatives to the word under
859 SuggestSpellingAlternatives();
862 else if (!Call(SCI_AUTOCACTIVE
))
864 ::PostMessage(GetParent()->GetSafeHwnd(), WM_NEXTDLGCTL
, GetKeyState(VK_SHIFT
)&0x8000, 0);
871 return CWnd::PreTranslateMessage(pMsg
);
874 void CSciEdit::OnContextMenu(CWnd
* /*pWnd*/, CPoint point
)
876 int anchor
= (int)Call(SCI_GETANCHOR
);
877 int currentpos
= (int)Call(SCI_GETCURRENTPOS
);
878 int selstart
= (int)Call(SCI_GETSELECTIONSTART
);
879 int selend
= (int)Call(SCI_GETSELECTIONEND
);
881 if ((point
.x
== -1) && (point
.y
== -1))
884 GetClientRect(&rect
);
885 ClientToScreen(&rect
);
886 point
= rect
.CenterPoint();
887 pointpos
= (int)Call(SCI_GETCURRENTPOS
);
891 // change the cursor position to the point where the user
893 CPoint clientpoint
= point
;
894 ScreenToClient(&clientpoint
);
895 pointpos
= (int)Call(SCI_POSITIONFROMPOINT
, clientpoint
.x
, clientpoint
.y
);
897 CString sMenuItemText
;
899 bool bRestoreCursor
= true;
900 if (popup
.CreatePopupMenu())
902 bool bCanUndo
= !!Call(SCI_CANUNDO
);
903 bool bCanRedo
= !!Call(SCI_CANREDO
);
904 bool bHasSelection
= (selend
-selstart
> 0);
905 bool bCanPaste
= !!Call(SCI_CANPASTE
);
906 bool bIsReadOnly
= !!Call(SCI_GETREADONLY
);
907 UINT uEnabledMenu
= MF_STRING
| MF_ENABLED
;
908 UINT uDisabledMenu
= MF_STRING
| MF_GRAYED
;
910 // find the word under the cursor
914 // setting the cursor clears the selection
915 Call(SCI_SETANCHOR
, pointpos
);
916 Call(SCI_SETCURRENTPOS
, pointpos
);
917 sWord
= GetWordUnderCursor();
918 // restore the selection
919 Call(SCI_SETSELECTIONSTART
, selstart
);
920 Call(SCI_SETSELECTIONEND
, selend
);
923 sWord
= GetWordUnderCursor();
924 CStringA worda
= GetWordForSpellChecker(sWord
);
926 int nCorrections
= 1;
927 bool bSpellAdded
= false;
928 // check if the word under the cursor is spelled wrong
929 if ((pChecker
)&&(!worda
.IsEmpty()) && !bIsReadOnly
)
931 char ** wlst
= nullptr;
932 // get the spell suggestions
933 int ns
= pChecker
->suggest(&wlst
,worda
);
936 // add the suggestions to the context menu
937 for (int i
=0; i
< ns
; i
++)
940 CString sug
= GetWordFromSpellChecker(wlst
[i
]);
941 popup
.InsertMenu((UINT
)-1, 0, nCorrections
++, sug
);
949 // only add a separator if spelling correction suggestions were added
951 popup
.AppendMenu(MF_SEPARATOR
);
953 // also allow the user to add the word to the custom dictionary so
954 // it won't show up as misspelled anymore
955 if ((sWord
.GetLength()<PDICT_MAX_WORD_LENGTH
)&&((pChecker
)&&(m_autolist
.find(sWord
) == m_autolist
.end())&&(!pChecker
->spell(worda
)))&&
956 (!_istdigit(sWord
.GetAt(0)))&&(!m_personalDict
.FindWord(sWord
)) && !bIsReadOnly
)
958 sMenuItemText
.Format(IDS_SCIEDIT_ADDWORD
, (LPCTSTR
)sWord
);
959 popup
.AppendMenu(uEnabledMenu
, SCI_ADDWORD
, sMenuItemText
);
961 popup
.AppendMenu(MF_SEPARATOR
);
964 // add the 'default' entries
965 sMenuItemText
.LoadString(IDS_SCIEDIT_UNDO
);
966 popup
.AppendMenu(bCanUndo
? uEnabledMenu
: uDisabledMenu
, SCI_UNDO
, sMenuItemText
);
967 sMenuItemText
.LoadString(IDS_SCIEDIT_REDO
);
968 popup
.AppendMenu(bCanRedo
? uEnabledMenu
: uDisabledMenu
, SCI_REDO
, sMenuItemText
);
970 popup
.AppendMenu(MF_SEPARATOR
);
972 sMenuItemText
.LoadString(IDS_SCIEDIT_CUT
);
973 popup
.AppendMenu(!bIsReadOnly
&& bHasSelection
? uEnabledMenu
: uDisabledMenu
, SCI_CUT
, sMenuItemText
);
974 sMenuItemText
.LoadString(IDS_SCIEDIT_COPY
);
975 popup
.AppendMenu(bHasSelection
? uEnabledMenu
: uDisabledMenu
, SCI_COPY
, sMenuItemText
);
976 sMenuItemText
.LoadString(IDS_SCIEDIT_PASTE
);
977 popup
.AppendMenu(bCanPaste
? uEnabledMenu
: uDisabledMenu
, SCI_PASTE
, sMenuItemText
);
979 popup
.AppendMenu(MF_SEPARATOR
);
981 sMenuItemText
.LoadString(IDS_SCIEDIT_SELECTALL
);
982 popup
.AppendMenu(uEnabledMenu
, SCI_SELECTALL
, sMenuItemText
);
984 popup
.AppendMenu(MF_SEPARATOR
);
986 sMenuItemText
.LoadString(IDS_SCIEDIT_SPLITLINES
);
987 popup
.AppendMenu(bHasSelection
? uEnabledMenu
: uDisabledMenu
, SCI_LINESSPLIT
, sMenuItemText
);
989 if (m_arContextHandlers
.GetCount() > 0)
990 popup
.AppendMenu(MF_SEPARATOR
);
992 int nCustoms
= nCorrections
;
993 // now add any custom context menus
994 for (INT_PTR handlerindex
= 0; handlerindex
< m_arContextHandlers
.GetCount(); ++handlerindex
)
996 CSciEditContextMenuInterface
* pHandler
= m_arContextHandlers
.GetAt(handlerindex
);
997 pHandler
->InsertMenuItems(popup
, nCustoms
);
1000 if (nCustoms
> nCorrections
)
1002 // custom menu entries present, so add another separator
1003 popup
.AppendMenu(MF_SEPARATOR
);
1006 // add found thesauri to sub menu's
1009 CPtrArray menuArray
;
1010 if (thesaurs
.CreatePopupMenu())
1012 if ((pThesaur
)&&(!worda
.IsEmpty()))
1016 int count
= pThesaur
->Lookup(worda
, worda
.GetLength(),&pmean
);
1019 mentry
* pm
= pmean
;
1020 for (int i
=0; i
< count
; i
++)
1022 CMenu
* submenu
= new CMenu();
1023 menuArray
.Add(submenu
);
1024 submenu
->CreateMenu();
1025 for (int j
=0; j
< pm
->count
; j
++)
1027 CString sug
= CString(pm
->psyns
[j
]);
1028 submenu
->InsertMenu((UINT
)-1, 0, nCorrections
+ nCustoms
+ (nThesaurs
++), sug
);
1030 thesaurs
.InsertMenu((UINT
)-1, MF_POPUP
, (UINT_PTR
)(submenu
->m_hMenu
), CString(pm
->defn
));
1034 if ((count
> 0)&&(point
.x
>= 0))
1036 #ifdef IDS_SPELLEDIT_THESAURUS
1037 sMenuItemText
.LoadString(IDS_SPELLEDIT_THESAURUS
);
1038 popup
.InsertMenu((UINT
)-1, MF_POPUP
, (UINT_PTR
)thesaurs
.m_hMenu
, sMenuItemText
);
1040 popup
.InsertMenu((UINT
)-1, MF_POPUP
, (UINT_PTR
)thesaurs
.m_hMenu
, _T("Thesaurus"));
1042 nThesaurs
= nCustoms
;
1046 sMenuItemText
.LoadString(IDS_SPELLEDIT_NOTHESAURUS
);
1047 popup
.AppendMenu(MF_DISABLED
| MF_GRAYED
| MF_STRING
, 0, sMenuItemText
);
1050 pThesaur
->CleanUpAfterLookup(&pmean
, count
);
1054 sMenuItemText
.LoadString(IDS_SPELLEDIT_NOTHESAURUS
);
1055 popup
.AppendMenu(MF_DISABLED
| MF_GRAYED
| MF_STRING
, 0, sMenuItemText
);
1059 int cmd
= popup
.TrackPopupMenu(TPM_RETURNCMD
| TPM_LEFTALIGN
| TPM_NONOTIFY
, point
.x
, point
.y
, this);
1063 break; // no command selected
1065 bRestoreCursor
= false;
1075 m_personalDict
.AddWord(sWord
);
1076 CheckSpelling((int)Call(SCI_POSITIONFROMLINE
, Call(SCI_GETFIRSTVISIBLELINE
)), (int)Call(SCI_POSITIONFROMLINE
, Call(SCI_GETFIRSTVISIBLELINE
) + Call(SCI_LINESONSCREEN
)));
1078 case SCI_LINESSPLIT
:
1080 int marker
= (int)(Call(SCI_GETEDGECOLUMN
) * Call(SCI_TEXTWIDTH
, 0, (LPARAM
)" "));
1083 Call(SCI_TARGETFROMSELECTION
);
1084 Call(SCI_LINESJOIN
);
1085 Call(SCI_LINESSPLIT
, marker
);
1090 if (cmd
< nCorrections
)
1092 Call(SCI_SETANCHOR
, pointpos
);
1093 Call(SCI_SETCURRENTPOS
, pointpos
);
1094 GetWordUnderCursor(true);
1096 popup
.GetMenuString(cmd
, temp
, 0);
1097 // setting the cursor clears the selection
1098 Call(SCI_REPLACESEL
, 0, (LPARAM
)(LPCSTR
)StringForControl(temp
));
1100 else if (cmd
< (nCorrections
+nCustoms
))
1102 for (INT_PTR handlerindex
= 0; handlerindex
< m_arContextHandlers
.GetCount(); ++handlerindex
)
1104 CSciEditContextMenuInterface
* pHandler
= m_arContextHandlers
.GetAt(handlerindex
);
1105 if (pHandler
->HandleMenuItemClick(cmd
, this))
1110 else if (cmd
<= (nThesaurs
+nCorrections
+nCustoms
))
1112 Call(SCI_SETANCHOR
, pointpos
);
1113 Call(SCI_SETCURRENTPOS
, pointpos
);
1114 GetWordUnderCursor(true);
1116 thesaurs
.GetMenuString(cmd
, temp
, 0);
1117 Call(SCI_REPLACESEL
, 0, (LPARAM
)(LPCSTR
)StringForControl(temp
));
1122 for (INT_PTR index
= 0; index
< menuArray
.GetCount(); ++index
)
1124 CMenu
* pMenu
= (CMenu
*)menuArray
[index
];
1131 // restore the anchor and cursor position
1132 Call(SCI_SETCURRENTPOS
, currentpos
);
1133 Call(SCI_SETANCHOR
, anchor
);
1137 bool CSciEdit::StyleEnteredText(int startstylepos
, int endstylepos
)
1139 bool bStyled
= false;
1140 const int line
= (int)Call(SCI_LINEFROMPOSITION
, startstylepos
);
1141 const int line_number_end
= (int)Call(SCI_LINEFROMPOSITION
, endstylepos
);
1142 for (int line_number
= line
; line_number
<= line_number_end
; ++line_number
)
1144 int offset
= (int)Call(SCI_POSITIONFROMLINE
, line_number
);
1145 int line_len
= (int)Call(SCI_LINELENGTH
, line_number
);
1146 auto linebuffer
= std::make_unique
<char[]>(line_len
+ 1);
1147 Call(SCI_GETLINE
, line_number
, (LPARAM
)linebuffer
.get());
1148 linebuffer
[line_len
] = 0;
1151 while (FindStyleChars(linebuffer
.get(), '*', start
, end
))
1153 Call(SCI_STARTSTYLING
, start
+ offset
, STYLE_BOLD
);
1154 Call(SCI_SETSTYLING
, end
-start
, STYLE_BOLD
);
1160 while (FindStyleChars(linebuffer
.get(), '^', start
, end
))
1162 Call(SCI_STARTSTYLING
, start
+ offset
, STYLE_ITALIC
);
1163 Call(SCI_SETSTYLING
, end
-start
, STYLE_ITALIC
);
1169 while (FindStyleChars(linebuffer
.get(), '_', start
, end
))
1171 Call(SCI_STARTSTYLING
, start
+ offset
, STYLE_UNDERLINED
);
1172 Call(SCI_SETSTYLING
, end
-start
, STYLE_UNDERLINED
);
1180 bool CSciEdit::WrapLines(int startpos
, int endpos
)
1182 int markerX
= (int)(Call(SCI_GETEDGECOLUMN
) * Call(SCI_TEXTWIDTH
, 0, (LPARAM
)" "));
1185 Call(SCI_SETTARGETSTART
, startpos
);
1186 Call(SCI_SETTARGETEND
, endpos
);
1187 Call(SCI_LINESSPLIT
, markerX
);
1193 void CSciEdit::AdvanceUTF8(const char * str
, int& pos
)
1195 if ((str
[pos
] & 0xE0)==0xC0)
1197 // utf8 2-byte sequence
1200 else if ((str
[pos
] & 0xF0)==0xE0)
1202 // utf8 3-byte sequence
1205 else if ((str
[pos
] & 0xF8)==0xF0)
1207 // utf8 4-byte sequence
1214 bool CSciEdit::FindStyleChars(const char * line
, char styler
, int& start
, int& end
)
1220 AdvanceUTF8(line
, i
);
1224 bool bFoundMarker
= false;
1225 CString sULine
= CUnicodeUtils::GetUnicode(line
);
1226 // find a starting marker
1227 while (line
[i
] != 0)
1229 if (line
[i
] == styler
)
1231 if ((line
[i
+1]!=0)&&(IsCharAlphaNumeric(sULine
[u
+1]))&&
1232 (((u
>0)&&(!IsCharAlphaNumeric(sULine
[u
-1]))) || (u
==0)))
1235 AdvanceUTF8(line
, i
);
1237 bFoundMarker
= true;
1241 AdvanceUTF8(line
, i
);
1246 // find ending marker
1247 bFoundMarker
= false;
1250 if (line
[i
] == styler
)
1252 if ((IsCharAlphaNumeric(sULine
[u
-1]))&&
1253 ((((u
+1)<sULine
.GetLength())&&(!IsCharAlphaNumeric(sULine
[u
+1]))) || ((u
+1) == sULine
.GetLength()))
1258 bFoundMarker
= true;
1262 AdvanceUTF8(line
, i
);
1265 return bFoundMarker
;
1268 BOOL
CSciEdit::MarkEnteredBugID(int startstylepos
, int endstylepos
)
1270 if (m_sCommand
.IsEmpty())
1272 // get the text between the start and end position we have to style
1273 const int line_number
= (int)Call(SCI_LINEFROMPOSITION
, startstylepos
);
1274 int start_pos
= (int)Call(SCI_POSITIONFROMLINE
, (WPARAM
)line_number
);
1275 int end_pos
= endstylepos
;
1277 if (start_pos
== end_pos
)
1279 if (start_pos
> end_pos
)
1281 int switchtemp
= start_pos
;
1282 start_pos
= end_pos
;
1283 end_pos
= switchtemp
;
1286 auto textbuffer
= std::make_unique
<char[]>(end_pos
- start_pos
+ 2);
1287 TEXTRANGEA textrange
;
1288 textrange
.lpstrText
= textbuffer
.get();
1289 textrange
.chrg
.cpMin
= start_pos
;
1290 textrange
.chrg
.cpMax
= end_pos
;
1291 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
1292 CStringA msg
= CStringA(textbuffer
.get());
1294 Call(SCI_STARTSTYLING
, start_pos
, STYLE_MASK
);
1298 if (!m_sBugID
.IsEmpty())
1300 // match with two regex strings (without grouping!)
1301 const std::tr1::regex
regCheck(m_sCommand
);
1302 const std::tr1::regex
regBugID(m_sBugID
);
1303 const std::tr1::sregex_iterator end
;
1304 std::string s
= msg
;
1307 // if start_pos is 0, we're styling from the beginning and let the ^ char match the beginning of the line
1308 // that way, the ^ matches the very beginning of the log message and not the beginning of further lines.
1309 // problem is: this only works *while* entering log messages. If a log message is pasted in whole or
1310 // multiple lines are pasted, start_pos can be 0 and styling goes over multiple lines. In that case, those
1311 // additional line starts also match ^
1312 for (std::tr1::sregex_iterator
it(s
.cbegin(), s
.cend(), regCheck
, start_pos
!= 0 ? std::tr1::regex_constants::match_not_bol
: std::tr1::regex_constants::match_default
); it
!= end
; ++it
)
1314 // clear the styles up to the match position
1315 Call(SCI_SETSTYLING
, it
->position(0)-pos
, STYLE_DEFAULT
);
1317 // (*it)[0] is the matched string
1318 std::string matchedString
= (*it
)[0];
1319 LONG matchedpos
= 0;
1320 for (std::tr1::sregex_iterator
it2(matchedString
.cbegin(), matchedString
.cend(), regBugID
); it2
!= end
; ++it2
)
1322 ATLTRACE("matched id : %s\n", std::string((*it2
)[0]).c_str());
1324 // bold style up to the id match
1325 ATLTRACE("position = %ld\n", it2
->position(0));
1326 if (it2
->position(0))
1327 Call(SCI_SETSTYLING
, it2
->position(0) - matchedpos
, STYLE_ISSUEBOLD
);
1328 // bold and recursive style for the bug ID itself
1329 if ((*it2
)[0].str().size())
1330 Call(SCI_SETSTYLING
, (*it2
)[0].str().size(), STYLE_ISSUEBOLDITALIC
);
1331 matchedpos
= (LONG
)(it2
->position(0) + (*it2
)[0].str().size());
1333 if ((matchedpos
)&&(matchedpos
< (LONG
)matchedString
.size()))
1335 Call(SCI_SETSTYLING
, matchedString
.size() - matchedpos
, STYLE_ISSUEBOLD
);
1337 pos
= (LONG
)(it
->position(0) + matchedString
.size());
1339 // bold style for the rest of the string which isn't matched
1341 Call(SCI_SETSTYLING
, s
.size()-pos
, STYLE_DEFAULT
);
1345 const std::tr1::regex
regCheck(m_sCommand
);
1346 const std::tr1::sregex_iterator end
;
1347 std::string s
= msg
;
1349 for (std::tr1::sregex_iterator
it(s
.cbegin(), s
.cend(), regCheck
); it
!= end
; ++it
)
1351 // clear the styles up to the match position
1352 if (it
->position(0) - pos
>= 0)
1353 Call(SCI_SETSTYLING
, it
->position(0) - pos
, STYLE_DEFAULT
);
1354 pos
= (LONG
)it
->position(0);
1356 const std::tr1::smatch match
= *it
;
1357 // we define group 1 as the whole issue text and
1358 // group 2 as the bug ID
1359 if (match
.size() >= 2)
1361 ATLTRACE("matched id : %s\n", std::string(match
[1]).c_str());
1362 if (match
[1].first
- s
.cbegin() - pos
>= 0)
1363 Call(SCI_SETSTYLING
, match
[1].first
- s
.cbegin() - pos
, STYLE_ISSUEBOLD
);
1364 Call(SCI_SETSTYLING
, std::string(match
[1]).size(), STYLE_ISSUEBOLDITALIC
);
1365 pos
= (LONG
)(match
[1].second
- s
.cbegin());
1370 catch (std::exception
) {}
1375 //similar code in AppUtils.cpp
1376 bool CSciEdit::IsValidURLChar(unsigned char ch
)
1378 return isalnum(ch
) ||
1379 ch
== '_' || ch
== '/' || ch
== ';' || ch
== '?' || ch
== '&' || ch
== '=' ||
1380 ch
== '%' || ch
== ':' || ch
== '.' || ch
== '#' || ch
== '-' || ch
== '+' ||
1381 ch
== '|' || ch
== '>' || ch
== '<' || ch
== '!' || ch
== '@';
1384 //similar code in AppUtils.cpp
1385 void CSciEdit::StyleURLs(int startstylepos
, int endstylepos
)
1387 const int line_number
= (int)Call(SCI_LINEFROMPOSITION
, startstylepos
);
1388 startstylepos
= (int)Call(SCI_POSITIONFROMLINE
, (WPARAM
)line_number
);
1390 int len
= endstylepos
- startstylepos
+ 1;
1391 auto textbuffer
= std::make_unique
<char[]>(len
+ 1);
1392 TEXTRANGEA textrange
;
1393 textrange
.lpstrText
= textbuffer
.get();
1394 textrange
.chrg
.cpMin
= startstylepos
;
1395 textrange
.chrg
.cpMax
= endstylepos
;
1396 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
1397 // we're dealing with utf8 encoded text here, which means one glyph is
1398 // not necessarily one byte/wchar_t
1399 // that's why we use CStringA to still get a correct char index
1400 CStringA msg
= textbuffer
.get();
1403 for (int i
= 0; i
<= msg
.GetLength(); AdvanceUTF8(msg
, i
))
1405 if ((i
< len
) && IsValidURLChar(msg
[i
]))
1415 if (msg
[starturl
] == '<' && i
< len
) // try to detect and do not strip URLs put within <>
1417 while (starturl
<= i
&& msg
[starturl
] == '<') // strip leading '<'
1421 while (i
< len
&& msg
[i
] != '\r' && msg
[i
] != '\n' && msg
[i
] != '>') // find first '>' or new line after resetting i to start position
1422 AdvanceUTF8(msg
, i
);
1424 if (!IsUrlOrEmail(msg
.Mid(starturl
, i
- starturl
)))
1430 int skipTrailing
= 0;
1431 while (strip
&& i
- skipTrailing
- 1 > starturl
&& (msg
[i
- skipTrailing
- 1] == '.' || msg
[i
- skipTrailing
- 1] == '-' || msg
[i
- skipTrailing
- 1] == '?' || msg
[i
- skipTrailing
- 1] == ';' || msg
[i
- skipTrailing
- 1] == ':' || msg
[i
- skipTrailing
- 1] == '>' || msg
[i
- skipTrailing
- 1] == '<' || msg
[i
- skipTrailing
- 1] == '!'))
1433 ASSERT(startstylepos
+ i
- skipTrailing
<= endstylepos
);
1434 Call(SCI_STARTSTYLING
, startstylepos
+ starturl
, STYLE_URL
);
1435 Call(SCI_SETSTYLING
, i
- starturl
- skipTrailing
, STYLE_URL
);
1442 bool CSciEdit::IsUrlOrEmail(const CStringA
& sText
)
1444 if (!PathIsURLA(sText
))
1446 auto atpos
= sText
.Find('@');
1449 if (sText
.ReverseFind('.') > atpos
)
1453 for (const CStringA
& prefix
: { "http://", "https://", "git://", "ftp://", "file://", "mailto:" })
1455 if (strncmp(sText
, prefix
, prefix
.GetLength()) == 0 && sText
.GetLength() != prefix
.GetLength())
1461 CStringA
CSciEdit::GetWordForSpellChecker(const CString
& sWord
)
1463 // convert the string from the control to the encoding of the spell checker module.
1465 if (m_spellcodepage
)
1467 char * buf
= sWordA
.GetBuffer(sWord
.GetLength() * 4 + 1);
1468 int lengthIncTerminator
= WideCharToMultiByte(m_spellcodepage
, 0, sWord
, -1, buf
, sWord
.GetLength() * 4, nullptr, nullptr);
1469 if (lengthIncTerminator
== 0)
1470 return ""; // converting to the codepage failed
1471 sWordA
.ReleaseBuffer(lengthIncTerminator
- 1);
1474 sWordA
= CStringA(sWord
);
1476 sWordA
.Trim("\'\".,");
1481 CString
CSciEdit::GetWordFromSpellChecker(const CStringA
& sWordA
)
1484 if (m_spellcodepage
)
1486 wchar_t * buf
= sWord
.GetBuffer(sWordA
.GetLength() * 2);
1487 int lengthIncTerminator
= MultiByteToWideChar(m_spellcodepage
, 0, sWordA
, -1, buf
, sWordA
.GetLength() * 2);
1488 if (lengthIncTerminator
== 0)
1490 sWord
.ReleaseBuffer(lengthIncTerminator
- 1);
1493 sWord
= CString(sWordA
);
1495 sWord
.Trim(L
"\'\".,");
1500 bool CSciEdit::IsUTF8(LPVOID pBuffer
, size_t cb
)
1504 UINT16
* pVal
= (UINT16
*)pBuffer
;
1505 UINT8
* pVal2
= (UINT8
*)(pVal
+1);
1506 // scan the whole buffer for a 0x0000 sequence
1507 // if found, we assume a binary file
1508 for (size_t i
=0; i
<(cb
-2); i
=i
+2)
1510 if (0x0000 == *pVal
++)
1513 pVal
= (UINT16
*)pBuffer
;
1514 if (*pVal
== 0xFEFF)
1518 if (*pVal
== 0xBBEF)
1523 // check for illegal UTF8 chars
1524 pVal2
= (UINT8
*)pBuffer
;
1525 for (size_t i
=0; i
<cb
; ++i
)
1527 if ((*pVal2
== 0xC0)||(*pVal2
== 0xC1)||(*pVal2
>= 0xF5))
1531 pVal2
= (UINT8
*)pBuffer
;
1533 for (size_t i
=0; i
<(cb
-3); ++i
)
1535 if ((*pVal2
& 0xE0)==0xC0)
1538 if ((*pVal2
& 0xC0)!=0x80)
1542 if ((*pVal2
& 0xF0)==0xE0)
1545 if ((*pVal2
& 0xC0)!=0x80)
1548 if ((*pVal2
& 0xC0)!=0x80)
1552 if ((*pVal2
& 0xF8)==0xF0)
1555 if ((*pVal2
& 0xC0)!=0x80)
1558 if ((*pVal2
& 0xC0)!=0x80)
1561 if ((*pVal2
& 0xC0)!=0x80)
1572 void CSciEdit::SetAStyle(int style
, COLORREF fore
, COLORREF back
, int size
, const char *face
)
1574 Call(SCI_STYLESETFORE
, style
, fore
);
1575 Call(SCI_STYLESETBACK
, style
, back
);
1577 Call(SCI_STYLESETSIZE
, style
, size
);
1579 Call(SCI_STYLESETFONT
, style
, reinterpret_cast<LPARAM
>(face
));
1582 void CSciEdit::SetUDiffStyle()
1585 SetAStyle(STYLE_DEFAULT
, ::GetSysColor(COLOR_WINDOWTEXT
), ::GetSysColor(COLOR_WINDOW
),
1586 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffFontSize", 10),
1587 CUnicodeUtils::StdGetUTF8(CRegStdString(L
"Software\\TortoiseGit\\UDiffFontName", L
"Courier New")).c_str());
1588 Call(SCI_SETTABWIDTH
, CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffTabSize", 4));
1590 Call(SCI_SETREADONLY
, TRUE
);
1591 //LRESULT pix = Call(SCI_TEXTWIDTH, STYLE_LINENUMBER, (LPARAM)"_99999");
1592 //Call(SCI_SETMARGINWIDTHN, 0, pix);
1593 //Call(SCI_SETMARGINWIDTHN, 1);
1594 //Call(SCI_SETMARGINWIDTHN, 2);
1595 //Set the default windows colors for edit controls
1596 Call(SCI_STYLESETFORE
, STYLE_DEFAULT
, ::GetSysColor(COLOR_WINDOWTEXT
));
1597 Call(SCI_STYLESETBACK
, STYLE_DEFAULT
, ::GetSysColor(COLOR_WINDOW
));
1598 Call(SCI_SETSELFORE
, TRUE
, ::GetSysColor(COLOR_HIGHLIGHTTEXT
));
1599 Call(SCI_SETSELBACK
, TRUE
, ::GetSysColor(COLOR_HIGHLIGHT
));
1600 Call(SCI_SETCARETFORE
, ::GetSysColor(COLOR_WINDOWTEXT
));
1602 //SendEditor(SCI_SETREADONLY, FALSE);
1604 Call(EM_EMPTYUNDOBUFFER
);
1605 Call(SCI_SETSAVEPOINT
);
1607 Call(SCI_SETUNDOCOLLECTION
, 0);
1609 Call(SCI_SETUNDOCOLLECTION
, 1);
1610 Call(SCI_SETWRAPMODE
,SC_WRAP_NONE
);
1612 //::SetFocus(m_hWndEdit);
1613 Call(EM_EMPTYUNDOBUFFER
);
1614 Call(SCI_SETSAVEPOINT
);
1615 Call(SCI_GOTOPOS
, 0);
1617 Call(SCI_CLEARDOCUMENTSTYLE
, 0, 0);
1618 Call(SCI_SETSTYLEBITS
, 5, 0);
1620 HIGHCONTRAST highContrast
= { 0 };
1621 highContrast
.cbSize
= sizeof(HIGHCONTRAST
);
1622 if (SystemParametersInfo(SPI_GETHIGHCONTRAST
, 0, &highContrast
, 0) == TRUE
&& (highContrast
.dwFlags
& HCF_HIGHCONTRASTON
))
1625 //SetAStyle(SCE_DIFF_DEFAULT, RGB(0, 0, 0));
1626 SetAStyle(SCE_DIFF_COMMAND
,
1627 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForeCommandColor", UDIFF_COLORFORECOMMAND
),
1628 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackCommandColor", UDIFF_COLORBACKCOMMAND
));
1629 SetAStyle(SCE_DIFF_POSITION
,
1630 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForePositionColor", UDIFF_COLORFOREPOSITION
),
1631 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackPositionColor", UDIFF_COLORBACKPOSITION
));
1632 SetAStyle(SCE_DIFF_HEADER
,
1633 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForeHeaderColor", UDIFF_COLORFOREHEADER
),
1634 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackHeaderColor", UDIFF_COLORBACKHEADER
));
1635 SetAStyle(SCE_DIFF_COMMENT
,
1636 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForeCommentColor", UDIFF_COLORFORECOMMENT
),
1637 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackCommentColor", UDIFF_COLORBACKCOMMENT
));
1638 Call(SCI_STYLESETBOLD
, SCE_DIFF_COMMENT
, TRUE
);
1639 SetAStyle(SCE_DIFF_ADDED
,
1640 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForeAddedColor", UDIFF_COLORFOREADDED
),
1641 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackAddedColor", UDIFF_COLORBACKADDED
));
1642 SetAStyle(SCE_DIFF_DELETED
,
1643 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForeRemovedColor", UDIFF_COLORFOREREMOVED
),
1644 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackRemovedColor", UDIFF_COLORBACKREMOVED
));
1646 Call(SCI_SETLEXER
, SCLEX_DIFF
);
1647 Call(SCI_SETKEYWORDS
, 0, (LPARAM
)"revision");
1648 Call(SCI_COLOURISE
, 0, -1);
1651 int CSciEdit::LoadFromFile(CString
&filename
)
1653 CAutoFILE fp
= _tfsopen(filename
, _T("rb"), _SH_DENYWR
);
1657 char data
[4096] = { 0 };
1658 size_t lenFile
= fread(data
, 1, sizeof(data
), fp
);
1659 bool bUTF8
= IsUTF8(data
, lenFile
);
1662 Call(SCI_ADDTEXT
, lenFile
, reinterpret_cast<LPARAM
>(static_cast<char *>(data
)));
1663 lenFile
= fread(data
, 1, sizeof(data
), fp
);
1665 Call(SCI_SETCODEPAGE
, bUTF8
? SC_CP_UTF8
: GetACP());
1669 void CSciEdit::RestyleBugIDs()
1671 int endstylepos
= (int)Call(SCI_GETLENGTH
);
1673 Call(SCI_STARTSTYLING
, 0, STYLE_MASK
);
1674 Call(SCI_SETSTYLING
, endstylepos
, STYLE_DEFAULT
);
1675 // style the bug IDs
1676 MarkEnteredBugID(0, endstylepos
);