1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2012-2014 - TortoiseGit
4 // Copyright (C) 2003-2008,2012-2014 - 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"
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
)
80 , m_nAutoCompleteMinChars(3)
82 m_hModule
= ::LoadLibrary(_T("SciLexer_tgit.dll"));
85 CSciEdit::~CSciEdit(void)
87 m_personalDict
.Save();
89 ::FreeLibrary(m_hModule
);
96 static LPBYTE
Icon2Image(HICON hIcon
)
102 if (!GetIconInfo(hIcon
, &iconInfo
))
106 if (!GetObject(iconInfo
.hbmColor
, sizeof(BITMAP
), &bm
))
109 int width
= bm
.bmWidth
;
110 int height
= bm
.bmHeight
;
111 int bytesPerScanLine
= (width
* 3 + 3) & 0xFFFFFFFC;
112 int size
= bytesPerScanLine
* height
;
113 BITMAPINFO infoheader
;
114 infoheader
.bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
115 infoheader
.bmiHeader
.biWidth
= width
;
116 infoheader
.bmiHeader
.biHeight
= height
;
117 infoheader
.bmiHeader
.biPlanes
= 1;
118 infoheader
.bmiHeader
.biBitCount
= 24;
119 infoheader
.bmiHeader
.biCompression
= BI_RGB
;
120 infoheader
.bmiHeader
.biSizeImage
= size
;
122 std::unique_ptr
<BYTE
> ptrb(new BYTE
[(size
* 2 + height
* width
* 4)]);
123 LPBYTE pixelsIconRGB
= ptrb
.get();
124 LPBYTE alphaPixels
= pixelsIconRGB
+ size
;
125 HDC hDC
= CreateCompatibleDC(nullptr);
126 HBITMAP hBmpOld
= (HBITMAP
)SelectObject(hDC
, (HGDIOBJ
)iconInfo
.hbmColor
);
127 if (!GetDIBits(hDC
, iconInfo
.hbmColor
, 0, height
, (LPVOID
)pixelsIconRGB
, &infoheader
, DIB_RGB_COLORS
))
133 SelectObject(hDC
, hBmpOld
);
134 if (!GetDIBits(hDC
, iconInfo
.hbmMask
, 0,height
, (LPVOID
)alphaPixels
, &infoheader
, DIB_RGB_COLORS
))
141 UINT
* imagePixels
= new UINT
[height
* width
];
142 int lsSrc
= width
* 3;
143 int vsDest
= height
- 1;
144 for (int y
= 0; y
< height
; y
++)
146 int linePosSrc
= (vsDest
- y
) * lsSrc
;
147 int linePosDest
= y
* width
;
148 for (int x
= 0; x
< width
; x
++)
150 int currentDestPos
= linePosDest
+ x
;
151 int currentSrcPos
= linePosSrc
+ x
* 3;
152 imagePixels
[currentDestPos
] = (((UINT
)(
154 ((pixelsIconRGB
[currentSrcPos
+ 2] /*Red*/)
155 | (pixelsIconRGB
[currentSrcPos
+ 1] << 8 /*Green*/))
156 | pixelsIconRGB
[currentSrcPos
] << 16 /*Blue*/
158 | ((alphaPixels
[currentSrcPos
] ? 0 : 0xff) << 24))) & 0xffffffff);
161 return (LPBYTE
)imagePixels
;
164 void CSciEdit::Init(LONG lLanguage
, BOOL bLoadSpellCheck
)
166 //Setup the direct access data
167 m_DirectFunction
= SendMessage(SCI_GETDIRECTFUNCTION
, 0, 0);
168 m_DirectPointer
= SendMessage(SCI_GETDIRECTPOINTER
, 0, 0);
169 Call(SCI_SETMARGINWIDTHN
, 1, 0);
170 Call(SCI_SETUSETABS
, 0); //pressing TAB inserts spaces
171 Call(SCI_SETWRAPVISUALFLAGS
, SC_WRAPVISUALFLAG_END
);
172 Call(SCI_AUTOCSETIGNORECASE
, 1);
173 Call(SCI_SETLEXER
, SCLEX_CONTAINER
);
174 Call(SCI_SETCODEPAGE
, SC_CP_UTF8
);
175 Call(SCI_AUTOCSETFILLUPS
, 0, (LPARAM
)"\t([");
176 Call(SCI_AUTOCSETMAXWIDTH
, 0);
177 //Set the default windows colors for edit controls
178 Call(SCI_STYLESETFORE
, STYLE_DEFAULT
, ::GetSysColor(COLOR_WINDOWTEXT
));
179 Call(SCI_STYLESETBACK
, STYLE_DEFAULT
, ::GetSysColor(COLOR_WINDOW
));
180 Call(SCI_SETSELFORE
, TRUE
, ::GetSysColor(COLOR_HIGHLIGHTTEXT
));
181 Call(SCI_SETSELBACK
, TRUE
, ::GetSysColor(COLOR_HIGHLIGHT
));
182 Call(SCI_SETCARETFORE
, ::GetSysColor(COLOR_WINDOWTEXT
));
183 Call(SCI_SETMODEVENTMASK
, SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
| SC_PERFORMED_UNDO
| SC_PERFORMED_REDO
);
184 Call(SCI_INDICSETSTYLE
, INDIC_MISSPELLED
, INDIC_SQUIGGLE
);
185 Call(SCI_INDICSETFORE
, INDIC_MISSPELLED
, RGB(255,0,0));
187 CStringA sWhiteSpace
;
188 for (int i
=0; i
<255; ++i
)
190 if (i
== '\r' || i
== '\n')
192 else if (i
< 0x20 || i
== ' ')
193 sWhiteSpace
+= (char)i
;
194 else if (isalnum(i
) || i
== '\'' || i
== '_' || i
== '-')
195 sWordChars
+= (char)i
;
197 Call(SCI_SETWORDCHARS
, 0, (LPARAM
)(LPCSTR
)sWordChars
);
198 Call(SCI_SETWHITESPACECHARS
, 0, (LPARAM
)(LPCSTR
)sWhiteSpace
);
199 m_bDoStyle
= ((DWORD
)CRegStdDWORD(_T("Software\\TortoiseGit\\StyleCommitMessages"), TRUE
))==TRUE
;
200 m_nAutoCompleteMinChars
= (int)(DWORD
)CRegStdDWORD(_T("Software\\TortoiseGit\\AutoCompleteMinChars"), 3);
201 // look for dictionary files and use them if found
202 long langId
= GetUserDefaultLCID();
206 if ((lLanguage
!= 0)||(((DWORD
)CRegStdDWORD(_T("Software\\TortoiseGit\\Spellchecker"), FALSE
))==FALSE
))
208 if (!((lLanguage
)&&(!LoadDictionaries(lLanguage
))))
212 LoadDictionaries(langId
);
213 DWORD lid
= SUBLANGID(langId
);
217 langId
= MAKELANGID(PRIMARYLANGID(langId
), lid
);
219 else if (langId
== 1033)
223 } while ((langId
)&&((pChecker
==NULL
)||(pThesaur
==NULL
)));
227 Call(SCI_SETEDGEMODE
, EDGE_NONE
);
228 Call(SCI_SETWRAPMODE
, SC_WRAP_WORD
);
229 Call(SCI_ASSIGNCMDKEY
, SCK_END
, SCI_LINEENDWRAP
);
230 Call(SCI_ASSIGNCMDKEY
, SCK_END
+ (SCMOD_SHIFT
<< 16), SCI_LINEENDWRAPEXTEND
);
231 Call(SCI_ASSIGNCMDKEY
, SCK_HOME
, SCI_HOMEWRAP
);
232 Call(SCI_ASSIGNCMDKEY
, SCK_HOME
+ (SCMOD_SHIFT
<< 16), SCI_HOMEWRAPEXTEND
);
233 CRegStdDWORD
used2d(L
"Software\\TortoiseGit\\ScintillaDirect2D", FALSE
);
234 if (SysInfo::Instance().IsWin7OrLater() && DWORD(used2d
))
236 Call(SCI_SETTECHNOLOGY
, SC_TECHNOLOGY_DIRECTWRITERETAIN
);
237 Call(SCI_SETBUFFEREDDRAW
, 0);
242 void CSciEdit::Init(const ProjectProperties
& props
)
244 Init(props
.lProjectLanguage
);
245 m_sCommand
= CStringA(CUnicodeUtils::GetUTF8(props
.GetCheckRe()));
246 m_sBugID
= CStringA(CUnicodeUtils::GetUTF8(props
.GetBugIDRe()));
247 m_sUrl
= CStringA(CUnicodeUtils::GetUTF8(props
.sUrl
));
249 Call(SCI_SETMOUSEDWELLTIME
, 333);
251 if (props
.nLogWidthMarker
)
253 Call(SCI_SETWRAPMODE
, SC_WRAP_NONE
);
254 Call(SCI_SETEDGEMODE
, EDGE_LINE
);
255 Call(SCI_SETEDGECOLUMN
, props
.nLogWidthMarker
);
259 Call(SCI_SETEDGEMODE
, EDGE_NONE
);
260 Call(SCI_SETWRAPMODE
, SC_WRAP_WORD
);
264 void CSciEdit::SetIcon(const std::map
<int, UINT
> &icons
)
266 Call(SCI_RGBAIMAGESETWIDTH
, 16);
267 Call(SCI_RGBAIMAGESETHEIGHT
, 16);
268 for (auto icon
: icons
)
270 auto hIcon
= (HICON
)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(icon
.second
), IMAGE_ICON
, 16, 16, LR_DEFAULTCOLOR
);
271 std::unique_ptr
<BYTE
> bytes(Icon2Image(hIcon
));
273 Call(SCI_REGISTERRGBAIMAGE
, icon
.first
, (LPARAM
)bytes
.get());
277 BOOL
CSciEdit::LoadDictionaries(LONG lLanguageID
)
279 //Setup the spell checker and thesaurus
280 TCHAR buf
[6] = { 0 };
281 CString sFolder
= CPathUtils::GetAppDirectory();
282 CString sFolderUp
= CPathUtils::GetAppParentDirectory();
283 CString sFolderAppData
= CPathUtils::GetAppDataDirectory();
286 GetLocaleInfo(MAKELCID(lLanguageID
, SORT_DEFAULT
), LOCALE_SISO639LANGNAME
, buf
, _countof(buf
));
288 if (lLanguageID
== 2074)
289 sFile
+= _T("-Latn");
291 GetLocaleInfo(MAKELCID(lLanguageID
, SORT_DEFAULT
), LOCALE_SISO3166CTRYNAME
, buf
, _countof(buf
));
295 if ((PathFileExists(sFolderAppData
+ _T("dic\\") + sFile
+ _T(".aff"))) &&
296 (PathFileExists(sFolderAppData
+ _T("dic\\") + sFile
+ _T(".dic"))))
298 pChecker
= new Hunspell(CStringA(sFolderAppData
+ _T("dic\\") + sFile
+ _T(".aff")), CStringA(sFolderAppData
+ _T("dic\\") + sFile
+ _T(".dic")));
300 else if ((PathFileExists(sFolder
+ sFile
+ _T(".aff"))) &&
301 (PathFileExists(sFolder
+ sFile
+ _T(".dic"))))
303 pChecker
= new Hunspell(CStringA(sFolder
+ sFile
+ _T(".aff")), CStringA(sFolder
+ sFile
+ _T(".dic")));
305 else if ((PathFileExists(sFolder
+ _T("dic\\") + sFile
+ _T(".aff"))) &&
306 (PathFileExists(sFolder
+ _T("dic\\") + sFile
+ _T(".dic"))))
308 pChecker
= new Hunspell(CStringA(sFolder
+ _T("dic\\") + sFile
+ _T(".aff")), CStringA(sFolder
+ _T("dic\\") + sFile
+ _T(".dic")));
310 else if ((PathFileExists(sFolderUp
+ sFile
+ _T(".aff"))) &&
311 (PathFileExists(sFolderUp
+ sFile
+ _T(".dic"))))
313 pChecker
= new Hunspell(CStringA(sFolderUp
+ sFile
+ _T(".aff")), CStringA(sFolderUp
+ sFile
+ _T(".dic")));
315 else if ((PathFileExists(sFolderUp
+ _T("dic\\") + sFile
+ _T(".aff"))) &&
316 (PathFileExists(sFolderUp
+ _T("dic\\") + sFile
+ _T(".dic"))))
318 pChecker
= new Hunspell(CStringA(sFolderUp
+ _T("dic\\") + sFile
+ _T(".aff")), CStringA(sFolderUp
+ _T("dic\\") + sFile
+ _T(".dic")));
320 else if ((PathFileExists(sFolderUp
+ _T("Languages\\") + sFile
+ _T(".aff"))) &&
321 (PathFileExists(sFolderUp
+ _T("Languages\\") + sFile
+ _T(".dic"))))
323 pChecker
= new Hunspell(CStringA(sFolderUp
+ _T("Languages\\") + sFile
+ _T(".aff")), CStringA(sFolderUp
+ _T("Languages\\") + sFile
+ _T(".dic")));
329 if ((PathFileExists(sFolderAppData
+ _T("th_") + sFile
+ _T("_v2.idx"))) &&
330 (PathFileExists(sFolderAppData
+ _T("th_") + sFile
+ _T("_v2.dat"))))
332 pThesaur
= new MyThes(CStringA(sFolderAppData
+ sFile
+ _T("_v2.idx")), CStringA(sFolderAppData
+ sFile
+ _T("_v2.dat")));
334 else if ((PathFileExists(sFolder
+ _T("th_") + sFile
+ _T("_v2.idx"))) &&
335 (PathFileExists(sFolder
+ _T("th_") + sFile
+ _T("_v2.dat"))))
337 pThesaur
= new MyThes(CStringA(sFolder
+ sFile
+ _T("_v2.idx")), CStringA(sFolder
+ sFile
+ _T("_v2.dat")));
339 else if ((PathFileExists(sFolder
+ _T("dic\\th_") + sFile
+ _T("_v2.idx"))) &&
340 (PathFileExists(sFolder
+ _T("dic\\th_") + sFile
+ _T("_v2.dat"))))
342 pThesaur
= new MyThes(CStringA(sFolder
+ _T("dic\\") + sFile
+ _T("_v2.idx")), CStringA(sFolder
+ _T("dic\\") + sFile
+ _T("_v2.dat")));
344 else if ((PathFileExists(sFolderUp
+ _T("th_") + sFile
+ _T("_v2.idx"))) &&
345 (PathFileExists(sFolderUp
+ _T("th_") + sFile
+ _T("_v2.dat"))))
347 pThesaur
= new MyThes(CStringA(sFolderUp
+ _T("th_") + sFile
+ _T("_v2.idx")), CStringA(sFolderUp
+ _T("th_") + sFile
+ _T("_v2.dat")));
349 else if ((PathFileExists(sFolderUp
+ _T("dic\\th_") + sFile
+ _T("_v2.idx"))) &&
350 (PathFileExists(sFolderUp
+ _T("dic\\th_") + sFile
+ _T("_v2.dat"))))
352 pThesaur
= new MyThes(CStringA(sFolderUp
+ _T("dic\\th_") + sFile
+ _T("_v2.idx")), CStringA(sFolderUp
+ _T("dic\\th_") + sFile
+ _T("_v2.dat")));
354 else if ((PathFileExists(sFolderUp
+ _T("Languages\\th_") + sFile
+ _T("_v2.idx"))) &&
355 (PathFileExists(sFolderUp
+ _T("Languages\\th_") + sFile
+ _T("_v2.dat"))))
357 pThesaur
= new MyThes(CStringA(sFolderUp
+ _T("Languages\\th_") + sFile
+ _T("_v2.idx")), CStringA(sFolderUp
+ _T("Languages\\th_") + sFile
+ _T("_v2.dat")));
363 const char * encoding
= pChecker
->get_dic_encoding();
364 CTraceToOutputDebugString::Instance()(__FUNCTION__
": %s\n", encoding
);
365 int n
= _countof(enc2locale
);
367 for (int i
= 0; i
< n
; i
++)
369 if (strcmp(encoding
,enc2locale
[i
].def_enc
) == 0)
371 m_spellcodepage
= atoi(enc2locale
[i
].cp
);
374 m_personalDict
.Init(lLanguageID
);
376 if ((pThesaur
)||(pChecker
))
381 LRESULT
CSciEdit::Call(UINT message
, WPARAM wParam
, LPARAM lParam
)
383 ASSERT(::IsWindow(m_hWnd
)); //Window must be valid
384 ASSERT(m_DirectFunction
); //Direct function must be valid
385 return ((SciFnDirect
) m_DirectFunction
)(m_DirectPointer
, message
, wParam
, lParam
);
388 CString
CSciEdit::StringFromControl(const CStringA
& text
)
392 int codepage
= (int)Call(SCI_GETCODEPAGE
);
393 int reslen
= MultiByteToWideChar(codepage
, 0, text
, text
.GetLength(), 0, 0);
394 MultiByteToWideChar(codepage
, 0, text
, text
.GetLength(), sText
.GetBuffer(reslen
+1), reslen
+1);
395 sText
.ReleaseBuffer(reslen
);
402 CStringA
CSciEdit::StringForControl(const CString
& text
)
406 int codepage
= (int)SendMessage(SCI_GETCODEPAGE
);
407 int reslen
= WideCharToMultiByte(codepage
, 0, text
, text
.GetLength(), 0, 0, 0, 0);
408 WideCharToMultiByte(codepage
, 0, text
, text
.GetLength(), sTextA
.GetBuffer(reslen
), reslen
, 0, 0);
409 sTextA
.ReleaseBuffer(reslen
);
413 ATLTRACE("string length %d\n", sTextA
.GetLength());
417 void CSciEdit::SetText(const CString
& sText
)
419 CStringA sTextA
= StringForControl(sText
);
420 Call(SCI_SETTEXT
, 0, (LPARAM
)(LPCSTR
)sTextA
);
422 // Scintilla seems to have problems with strings that
423 // aren't terminated by a newline char. Once that char
424 // is there, it can be removed without problems.
425 // So we add here a newline, then remove it again.
426 Call(SCI_DOCUMENTEND
);
428 Call(SCI_DELETEBACK
);
431 void CSciEdit::InsertText(const CString
& sText
, bool bNewLine
)
433 CStringA sTextA
= StringForControl(sText
);
434 Call(SCI_REPLACESEL
, 0, (LPARAM
)(LPCSTR
)sTextA
);
436 Call(SCI_REPLACESEL
, 0, (LPARAM
)(LPCSTR
)"\n");
439 CString
CSciEdit::GetText()
441 LRESULT len
= Call(SCI_GETTEXT
, 0, 0);
443 Call(SCI_GETTEXT
, (WPARAM
)(len
+ 1), (LPARAM
)(LPCSTR
)sTextA
.GetBuffer((int)len
+ 1));
444 sTextA
.ReleaseBuffer();
445 return StringFromControl(sTextA
);
448 CString
CSciEdit::GetWordUnderCursor(bool bSelectWord
)
450 TEXTRANGEA textrange
;
451 int pos
= (int)Call(SCI_GETCURRENTPOS
);
452 textrange
.chrg
.cpMin
= (LONG
)Call(SCI_WORDSTARTPOSITION
, pos
, TRUE
);
453 if ((pos
== textrange
.chrg
.cpMin
)||(textrange
.chrg
.cpMin
< 0))
455 textrange
.chrg
.cpMax
= (LONG
)Call(SCI_WORDENDPOSITION
, textrange
.chrg
.cpMin
, TRUE
);
457 std::unique_ptr
<char[]> textbuffer(new char[textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 1]);
458 textrange
.lpstrText
= textbuffer
.get();
459 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
462 Call(SCI_SETSEL
, textrange
.chrg
.cpMin
, textrange
.chrg
.cpMax
);
464 CString sRet
= StringFromControl(textbuffer
.get());
468 void CSciEdit::SetFont(CString sFontName
, int iFontSizeInPoints
)
470 Call(SCI_STYLESETFONT
, STYLE_DEFAULT
, (LPARAM
)(LPCSTR
)CUnicodeUtils::GetUTF8(sFontName
).GetBuffer());
471 Call(SCI_STYLESETSIZE
, STYLE_DEFAULT
, iFontSizeInPoints
);
472 Call(SCI_STYLECLEARALL
);
474 LPARAM color
= (LPARAM
)GetSysColor(COLOR_HIGHLIGHT
);
475 // set the styles for the bug ID strings
476 Call(SCI_STYLESETBOLD
, STYLE_ISSUEBOLD
, (LPARAM
)TRUE
);
477 Call(SCI_STYLESETFORE
, STYLE_ISSUEBOLD
, color
);
478 Call(SCI_STYLESETBOLD
, STYLE_ISSUEBOLDITALIC
, (LPARAM
)TRUE
);
479 Call(SCI_STYLESETITALIC
, STYLE_ISSUEBOLDITALIC
, (LPARAM
)TRUE
);
480 Call(SCI_STYLESETFORE
, STYLE_ISSUEBOLDITALIC
, color
);
481 Call(SCI_STYLESETHOTSPOT
, STYLE_ISSUEBOLDITALIC
, (LPARAM
)TRUE
);
483 // set the formatted text styles
484 Call(SCI_STYLESETBOLD
, STYLE_BOLD
, (LPARAM
)TRUE
);
485 Call(SCI_STYLESETITALIC
, STYLE_ITALIC
, (LPARAM
)TRUE
);
486 Call(SCI_STYLESETUNDERLINE
, STYLE_UNDERLINED
, (LPARAM
)TRUE
);
488 // set the style for URLs
489 Call(SCI_STYLESETFORE
, STYLE_URL
, color
);
490 Call(SCI_STYLESETHOTSPOT
, STYLE_URL
, (LPARAM
)TRUE
);
492 Call(SCI_SETHOTSPOTACTIVEUNDERLINE
, (LPARAM
)TRUE
);
495 void CSciEdit::SetAutoCompletionList(const std::map
<CString
, int>& list
, TCHAR separator
, TCHAR typeSeparator
)
497 //copy the auto completion list.
499 //SK: instead of creating a copy of that list, we could accept a pointer
500 //to the list and use that instead. But then the caller would have to make
501 //sure that the list persists over the lifetime of the control!
504 m_separator
= separator
;
505 m_typeSeparator
= typeSeparator
;
508 BOOL
CSciEdit::IsMisspelled(const CString
& sWord
)
510 // convert the string from the control to the encoding of the spell checker module.
515 buf
= sWordA
.GetBuffer(sWord
.GetLength()*4 + 1);
516 int lengthIncTerminator
=
517 WideCharToMultiByte(m_spellcodepage
, 0, sWord
, -1, buf
, sWord
.GetLength()*4, NULL
, NULL
);
518 if (lengthIncTerminator
== 0)
519 return FALSE
; // converting to the codepage failed, assume word is spelled correctly
520 sWordA
.ReleaseBuffer(lengthIncTerminator
-1);
523 sWordA
= CStringA(sWord
);
524 sWordA
.Trim("\'\".,");
525 // words starting with a digit are treated as correctly spelled
526 if (_istdigit(sWord
.GetAt(0)))
528 // words in the personal dictionary are correct too
529 if (m_personalDict
.FindWord(sWord
))
532 // now we actually check the spelling...
533 if (!pChecker
->spell(sWordA
))
535 // the word is marked as misspelled, we now check whether the word
536 // is maybe a composite identifier
537 // a composite identifier consists of multiple words, with each word
538 // separated by a change in lower to uppercase letters
539 if (sWord
.GetLength() > 1)
543 while (wordend
< sWord
.GetLength())
545 while ((wordend
< sWord
.GetLength())&&(!_istupper(sWord
[wordend
])))
547 if ((wordstart
== 0)&&(wordend
== sWord
.GetLength()))
549 // words in the auto list are also assumed correctly spelled
550 if (m_autolist
.find(sWord
) != m_autolist
.end())
554 sWordA
= CStringA(sWord
.Mid(wordstart
, wordend
-wordstart
));
555 if ((sWordA
.GetLength() > 2)&&(!pChecker
->spell(sWordA
)))
567 void CSciEdit::CheckSpelling()
569 if (pChecker
== NULL
)
572 TEXTRANGEA textrange
;
574 LRESULT firstline
= Call(SCI_GETFIRSTVISIBLELINE
);
575 LRESULT lastline
= firstline
+ Call(SCI_LINESONSCREEN
);
576 textrange
.chrg
.cpMin
= (LONG
)Call(SCI_POSITIONFROMLINE
, firstline
);
577 textrange
.chrg
.cpMax
= (LONG
)textrange
.chrg
.cpMin
;
578 LRESULT lastpos
= Call(SCI_POSITIONFROMLINE
, lastline
) + Call(SCI_LINELENGTH
, lastline
);
580 lastpos
= Call(SCI_GETLENGTH
)-textrange
.chrg
.cpMin
;
581 Call(SCI_SETINDICATORCURRENT
, INDIC_MISSPELLED
);
582 while (textrange
.chrg
.cpMax
< lastpos
)
584 textrange
.chrg
.cpMin
= (LONG
)Call(SCI_WORDSTARTPOSITION
, textrange
.chrg
.cpMax
+1, TRUE
);
585 if (textrange
.chrg
.cpMin
< textrange
.chrg
.cpMax
)
587 textrange
.chrg
.cpMax
= (LONG
)Call(SCI_WORDENDPOSITION
, textrange
.chrg
.cpMin
, TRUE
);
588 if (textrange
.chrg
.cpMin
== textrange
.chrg
.cpMax
)
590 textrange
.chrg
.cpMax
++;
591 // since Scintilla squiggles to the end of the text even if told to stop one char before it,
592 // we have to clear here the squiggly lines to the end.
593 if (textrange
.chrg
.cpMin
)
594 Call(SCI_INDICATORCLEARRANGE
, textrange
.chrg
.cpMin
-1, textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 1);
597 ATLASSERT(textrange
.chrg
.cpMax
>= textrange
.chrg
.cpMin
);
598 std::unique_ptr
<char[]> textbuffer(new char[textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 2]);
599 SecureZeroMemory(textbuffer
.get(), textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 2);
600 textrange
.lpstrText
= textbuffer
.get();
601 textrange
.chrg
.cpMax
++;
602 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
603 int len
= (int)strlen(textrange
.lpstrText
);
606 textrange
.chrg
.cpMax
--;
607 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
608 len
= (int)strlen(textrange
.lpstrText
);
609 textrange
.chrg
.cpMax
++;
612 if (len
&& textrange
.lpstrText
[len
- 1] == '.')
614 // Try to ignore file names from the auto list.
615 // Do do this, for each word ending with '.' we extract next word and check
616 // whether the combined string is present in auto list.
618 twoWords
.chrg
.cpMin
= textrange
.chrg
.cpMin
;
619 twoWords
.chrg
.cpMax
= (LONG
)Call(SCI_WORDENDPOSITION
, textrange
.chrg
.cpMax
+ 1, TRUE
);
620 std::unique_ptr
<char[]> twoWordsBuffer(new char[twoWords
.chrg
.cpMax
- twoWords
.chrg
.cpMin
+ 1]);
621 twoWords
.lpstrText
= twoWordsBuffer
.get();
622 SecureZeroMemory(twoWords
.lpstrText
, twoWords
.chrg
.cpMax
- twoWords
.chrg
.cpMin
+ 1);
623 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&twoWords
);
624 CString sWord
= StringFromControl(twoWords
.lpstrText
);
625 if (m_autolist
.find(sWord
) != m_autolist
.end())
627 //mark word as correct (remove the squiggle line)
628 Call(SCI_INDICATORCLEARRANGE
, twoWords
.chrg
.cpMin
, twoWords
.chrg
.cpMax
- twoWords
.chrg
.cpMin
);
629 textrange
.chrg
.cpMax
= twoWords
.chrg
.cpMax
;
634 textrange
.lpstrText
[len
- 1] = 0;
635 textrange
.chrg
.cpMax
--;
636 if (strlen(textrange
.lpstrText
) > 0)
638 CString sWord
= StringFromControl(textrange
.lpstrText
);
639 if ((GetStyleAt(textrange
.chrg
.cpMin
) != STYLE_URL
) && IsMisspelled(sWord
))
641 //mark word as misspelled
642 Call(SCI_INDICATORFILLRANGE
, textrange
.chrg
.cpMin
, textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
);
646 //mark word as correct (remove the squiggle line)
647 Call(SCI_INDICATORCLEARRANGE
, textrange
.chrg
.cpMin
, textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
);
648 Call(SCI_INDICATORCLEARRANGE
, textrange
.chrg
.cpMin
, textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 1);
654 void CSciEdit::SuggestSpellingAlternatives()
656 if (pChecker
== NULL
)
658 CString word
= GetWordUnderCursor(true);
659 Call(SCI_SETCURRENTPOS
, Call(SCI_WORDSTARTPOSITION
, Call(SCI_GETCURRENTPOS
), TRUE
));
662 char ** wlst
= nullptr;
663 int ns
= pChecker
->suggest(&wlst
, CStringA(word
));
667 for (int i
=0; i
< ns
; i
++)
669 suggestions
.AppendFormat(_T("%s%c%d%c"), CString(wlst
[i
]), m_typeSeparator
, AUTOCOMPLETE_SPELLING
, m_separator
);
673 suggestions
.TrimRight(m_separator
);
674 if (suggestions
.IsEmpty())
676 Call(SCI_AUTOCSETSEPARATOR
, (WPARAM
)CStringA(m_separator
).GetAt(0));
677 Call(SCI_AUTOCSETTYPESEPARATOR
, (WPARAM
)m_typeSeparator
);
678 Call(SCI_AUTOCSETDROPRESTOFWORD
, 1);
679 Call(SCI_AUTOCSHOW
, 0, (LPARAM
)(LPCSTR
)StringForControl(suggestions
));
685 void CSciEdit::DoAutoCompletion(int nMinPrefixLength
)
687 if (m_autolist
.empty())
689 if (Call(SCI_AUTOCACTIVE
))
691 CString word
= GetWordUnderCursor();
692 if (word
.GetLength() < nMinPrefixLength
)
693 return; //don't auto complete yet, word is too short
694 int pos
= (int)Call(SCI_GETCURRENTPOS
);
695 if (pos
!= Call(SCI_WORDENDPOSITION
, pos
, TRUE
))
696 return; //don't auto complete if we're not at the end of a word
697 CString sAutoCompleteList
;
699 std::vector
<CString
> words
;
701 pos
= word
.Find('-');
703 CString wordLower
= word
;
704 wordLower
.MakeLower();
705 CString wordHigher
= word
;
706 wordHigher
.MakeUpper();
708 words
.push_back(wordLower
);
709 words
.push_back(wordHigher
);
713 CString s
= wordLower
.Left(pos
);
714 if (s
.GetLength() >= nMinPrefixLength
)
716 s
= wordLower
.Mid(pos
+1);
717 if (s
.GetLength() >= nMinPrefixLength
)
719 s
= wordHigher
.Left(pos
);
720 if (s
.GetLength() >= nMinPrefixLength
)
721 words
.push_back(wordHigher
.Left(pos
));
722 s
= wordHigher
.Mid(pos
+1);
723 if (s
.GetLength() >= nMinPrefixLength
)
724 words
.push_back(wordHigher
.Mid(pos
+1));
727 std::map
<CString
, int> wordset
;
728 for (const auto& w
: words
)
730 for (auto lowerit
= m_autolist
.lower_bound(w
);
731 lowerit
!= m_autolist
.end(); ++lowerit
)
733 int compare
= w
.CompareNoCase(lowerit
->first
.Left(w
.GetLength()));
736 else if (compare
== 0)
738 wordset
.insert(std::make_pair(lowerit
->first
, lowerit
->second
));
747 for (const auto& w
: wordset
)
748 sAutoCompleteList
.AppendFormat(_T("%s%c%d%c"), w
.first
, m_typeSeparator
, w
.second
, m_separator
);
750 sAutoCompleteList
.TrimRight(m_separator
);
751 if (sAutoCompleteList
.IsEmpty())
754 Call(SCI_AUTOCSETSEPARATOR
, (WPARAM
)CStringA(m_separator
).GetAt(0));
755 Call(SCI_AUTOCSETTYPESEPARATOR
, (WPARAM
)m_typeSeparator
);
756 Call(SCI_AUTOCSHOW
, word
.GetLength(), (LPARAM
)(LPCSTR
)StringForControl(sAutoCompleteList
));
759 BOOL
CSciEdit::OnChildNotify(UINT message
, WPARAM wParam
, LPARAM lParam
, LRESULT
* pLResult
)
761 if (message
!= WM_NOTIFY
)
762 return CWnd::OnChildNotify(message
, wParam
, lParam
, pLResult
);
764 LPNMHDR lpnmhdr
= (LPNMHDR
) lParam
;
765 SCNotification
* lpSCN
= (SCNotification
*)lParam
;
767 if(lpnmhdr
->hwndFrom
==m_hWnd
)
769 switch(lpnmhdr
->code
)
773 if ((lpSCN
->ch
< 32)&&(lpSCN
->ch
!= 13)&&(lpSCN
->ch
!= 10))
774 Call(SCI_DELETEBACK
);
777 DoAutoCompletion(m_nAutoCompleteMinChars
);
782 case SCN_AUTOCSELECTION
:
784 CString text
= StringFromControl(lpSCN
->text
);
785 if (m_autolist
[text
] == AUTOCOMPLETE_SNIPPET
)
787 Call(SCI_AUTOCCANCEL
);
788 for (INT_PTR handlerindex
= 0; handlerindex
< m_arContextHandlers
.GetCount(); ++handlerindex
)
790 CSciEditContextMenuInterface
* pHandler
= m_arContextHandlers
.GetAt(handlerindex
);
791 pHandler
->HandleSnippet(m_autolist
[text
], text
, this);
796 case SCN_STYLENEEDED
:
798 int startstylepos
= (int)Call(SCI_GETENDSTYLED
);
799 int endstylepos
= ((SCNotification
*)lpnmhdr
)->position
;
800 MarkEnteredBugID(startstylepos
, endstylepos
);
802 StyleEnteredText(startstylepos
, endstylepos
);
803 StyleURLs(startstylepos
, endstylepos
);
805 WrapLines(startstylepos
, endstylepos
);
810 case SCN_HOTSPOTCLICK
:
812 TEXTRANGEA textrange
;
813 textrange
.chrg
.cpMin
= lpSCN
->position
;
814 textrange
.chrg
.cpMax
= lpSCN
->position
;
815 DWORD style
= GetStyleAt(lpSCN
->position
);
816 if (style
!= STYLE_ISSUEBOLDITALIC
&& style
!= STYLE_URL
)
818 while (GetStyleAt(textrange
.chrg
.cpMin
- 1) == style
)
819 --textrange
.chrg
.cpMin
;
820 while (GetStyleAt(textrange
.chrg
.cpMax
+ 1) == style
)
821 ++textrange
.chrg
.cpMax
;
822 ++textrange
.chrg
.cpMax
;
823 std::unique_ptr
<char[]> textbuffer(new char[textrange
.chrg
.cpMax
- textrange
.chrg
.cpMin
+ 1]);
824 textrange
.lpstrText
= textbuffer
.get();
825 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
827 if (style
== STYLE_URL
)
828 url
= StringFromControl(textbuffer
.get());
832 url
.Replace(L
"%BUGID%", StringFromControl(textbuffer
.get()));
836 if (lpnmhdr
->code
== SCN_HOTSPOTCLICK
)
837 ShellExecute(GetParent()->GetSafeHwnd(), _T("open"), url
, NULL
, NULL
, SW_SHOWDEFAULT
);
840 CStringA sTextA
= StringForControl(url
);
841 Call(SCI_CALLTIPSHOW
, lpSCN
->position
+ 3, (LPARAM
)(LPCSTR
)sTextA
);
847 Call(SCI_CALLTIPCANCEL
);
851 return CWnd::OnChildNotify(message
, wParam
, lParam
, pLResult
);
854 BEGIN_MESSAGE_MAP(CSciEdit
, CWnd
)
859 void CSciEdit::OnKeyDown(UINT nChar
, UINT nRepCnt
, UINT nFlags
)
865 if ((Call(SCI_AUTOCACTIVE
)==0)&&(Call(SCI_CALLTIPACTIVE
)==0))
866 ::SendMessage(GetParent()->GetSafeHwnd(), WM_CLOSE
, 0, 0);
870 CWnd::OnKeyDown(nChar
, nRepCnt
, nFlags
);
873 BOOL
CSciEdit::PreTranslateMessage(MSG
* pMsg
)
875 if (pMsg
->message
== WM_KEYDOWN
)
877 switch (pMsg
->wParam
)
881 if (GetKeyState(VK_CONTROL
) & 0x8000)
889 // The TAB cannot be handled in OnKeyDown because it is too late by then.
891 if (GetKeyState(VK_CONTROL
)&0x8000)
893 //Ctrl-Tab was pressed, this means we should provide the user with
894 //a list of possible spell checking alternatives to the word under
896 SuggestSpellingAlternatives();
899 else if (!Call(SCI_AUTOCACTIVE
))
901 ::PostMessage(GetParent()->GetSafeHwnd(), WM_NEXTDLGCTL
, GetKeyState(VK_SHIFT
)&0x8000, 0);
908 return CWnd::PreTranslateMessage(pMsg
);
911 void CSciEdit::OnContextMenu(CWnd
* /*pWnd*/, CPoint point
)
913 int anchor
= (int)Call(SCI_GETANCHOR
);
914 int currentpos
= (int)Call(SCI_GETCURRENTPOS
);
915 int selstart
= (int)Call(SCI_GETSELECTIONSTART
);
916 int selend
= (int)Call(SCI_GETSELECTIONEND
);
918 if ((point
.x
== -1) && (point
.y
== -1))
921 GetClientRect(&rect
);
922 ClientToScreen(&rect
);
923 point
= rect
.CenterPoint();
924 pointpos
= (int)Call(SCI_GETCURRENTPOS
);
928 // change the cursor position to the point where the user
930 CPoint clientpoint
= point
;
931 ScreenToClient(&clientpoint
);
932 pointpos
= (int)Call(SCI_POSITIONFROMPOINT
, clientpoint
.x
, clientpoint
.y
);
934 CString sMenuItemText
;
936 bool bRestoreCursor
= true;
937 if (popup
.CreatePopupMenu())
939 bool bCanUndo
= !!Call(SCI_CANUNDO
);
940 bool bCanRedo
= !!Call(SCI_CANREDO
);
941 bool bHasSelection
= (selend
-selstart
> 0);
942 bool bCanPaste
= !!Call(SCI_CANPASTE
);
943 bool bIsReadOnly
= !!Call(SCI_GETREADONLY
);
944 UINT uEnabledMenu
= MF_STRING
| MF_ENABLED
;
945 UINT uDisabledMenu
= MF_STRING
| MF_GRAYED
;
947 // find the word under the cursor
951 // setting the cursor clears the selection
952 Call(SCI_SETANCHOR
, pointpos
);
953 Call(SCI_SETCURRENTPOS
, pointpos
);
954 sWord
= GetWordUnderCursor();
955 // restore the selection
956 Call(SCI_SETSELECTIONSTART
, selstart
);
957 Call(SCI_SETSELECTIONEND
, selend
);
960 sWord
= GetWordUnderCursor();
961 CStringA worda
= CStringA(sWord
);
963 int nCorrections
= 1;
964 bool bSpellAdded
= false;
965 // check if the word under the cursor is spelled wrong
966 if ((pChecker
)&&(!worda
.IsEmpty()) && !bIsReadOnly
)
968 char ** wlst
= nullptr;
969 // get the spell suggestions
970 int ns
= pChecker
->suggest(&wlst
,worda
);
973 // add the suggestions to the context menu
974 for (int i
=0; i
< ns
; i
++)
977 CString sug
= CString(wlst
[i
]);
978 popup
.InsertMenu((UINT
)-1, 0, nCorrections
++, sug
);
986 // only add a separator if spelling correction suggestions were added
988 popup
.AppendMenu(MF_SEPARATOR
);
990 // also allow the user to add the word to the custom dictionary so
991 // it won't show up as misspelled anymore
992 if ((sWord
.GetLength()<PDICT_MAX_WORD_LENGTH
)&&((pChecker
)&&(m_autolist
.find(sWord
) == m_autolist
.end())&&(!pChecker
->spell(worda
)))&&
993 (!_istdigit(sWord
.GetAt(0)))&&(!m_personalDict
.FindWord(sWord
)) && !bIsReadOnly
)
995 sMenuItemText
.Format(IDS_SCIEDIT_ADDWORD
, sWord
);
996 popup
.AppendMenu(uEnabledMenu
, SCI_ADDWORD
, sMenuItemText
);
998 popup
.AppendMenu(MF_SEPARATOR
);
1001 // add the 'default' entries
1002 sMenuItemText
.LoadString(IDS_SCIEDIT_UNDO
);
1003 popup
.AppendMenu(bCanUndo
? uEnabledMenu
: uDisabledMenu
, SCI_UNDO
, sMenuItemText
);
1004 sMenuItemText
.LoadString(IDS_SCIEDIT_REDO
);
1005 popup
.AppendMenu(bCanRedo
? uEnabledMenu
: uDisabledMenu
, SCI_REDO
, sMenuItemText
);
1007 popup
.AppendMenu(MF_SEPARATOR
);
1009 sMenuItemText
.LoadString(IDS_SCIEDIT_CUT
);
1010 popup
.AppendMenu(bHasSelection
? uEnabledMenu
: uDisabledMenu
, SCI_CUT
, sMenuItemText
);
1011 sMenuItemText
.LoadString(IDS_SCIEDIT_COPY
);
1012 popup
.AppendMenu(bHasSelection
? uEnabledMenu
: uDisabledMenu
, SCI_COPY
, sMenuItemText
);
1013 sMenuItemText
.LoadString(IDS_SCIEDIT_PASTE
);
1014 popup
.AppendMenu(bCanPaste
? uEnabledMenu
: uDisabledMenu
, SCI_PASTE
, sMenuItemText
);
1016 popup
.AppendMenu(MF_SEPARATOR
);
1018 sMenuItemText
.LoadString(IDS_SCIEDIT_SELECTALL
);
1019 popup
.AppendMenu(uEnabledMenu
, SCI_SELECTALL
, sMenuItemText
);
1021 popup
.AppendMenu(MF_SEPARATOR
);
1023 sMenuItemText
.LoadString(IDS_SCIEDIT_SPLITLINES
);
1024 popup
.AppendMenu(bHasSelection
? uEnabledMenu
: uDisabledMenu
, SCI_LINESSPLIT
, sMenuItemText
);
1026 if (m_arContextHandlers
.GetCount() > 0)
1027 popup
.AppendMenu(MF_SEPARATOR
);
1029 int nCustoms
= nCorrections
;
1030 // now add any custom context menus
1031 for (INT_PTR handlerindex
= 0; handlerindex
< m_arContextHandlers
.GetCount(); ++handlerindex
)
1033 CSciEditContextMenuInterface
* pHandler
= m_arContextHandlers
.GetAt(handlerindex
);
1034 pHandler
->InsertMenuItems(popup
, nCustoms
);
1037 if (nCustoms
> nCorrections
)
1039 // custom menu entries present, so add another separator
1040 popup
.AppendMenu(MF_SEPARATOR
);
1043 // add found thesauri to sub menu's
1046 CPtrArray menuArray
;
1047 if (thesaurs
.CreatePopupMenu())
1049 if ((pThesaur
)&&(!worda
.IsEmpty()))
1053 int count
= pThesaur
->Lookup(worda
, worda
.GetLength(),&pmean
);
1056 mentry
* pm
= pmean
;
1057 for (int i
=0; i
< count
; i
++)
1059 CMenu
* submenu
= new CMenu();
1060 menuArray
.Add(submenu
);
1061 submenu
->CreateMenu();
1062 for (int j
=0; j
< pm
->count
; j
++)
1064 CString sug
= CString(pm
->psyns
[j
]);
1065 submenu
->InsertMenu((UINT
)-1, 0, nCorrections
+ nCustoms
+ (nThesaurs
++), sug
);
1067 thesaurs
.InsertMenu((UINT
)-1, MF_POPUP
, (UINT_PTR
)(submenu
->m_hMenu
), CString(pm
->defn
));
1071 if ((count
> 0)&&(point
.x
>= 0))
1073 #ifdef IDS_SPELLEDIT_THESAURUS
1074 sMenuItemText
.LoadString(IDS_SPELLEDIT_THESAURUS
);
1075 popup
.InsertMenu((UINT
)-1, MF_POPUP
, (UINT_PTR
)thesaurs
.m_hMenu
, sMenuItemText
);
1077 popup
.InsertMenu((UINT
)-1, MF_POPUP
, (UINT_PTR
)thesaurs
.m_hMenu
, _T("Thesaurus"));
1079 nThesaurs
= nCustoms
;
1083 sMenuItemText
.LoadString(IDS_SPELLEDIT_NOTHESAURUS
);
1084 popup
.AppendMenu(MF_DISABLED
| MF_GRAYED
| MF_STRING
, 0, sMenuItemText
);
1087 pThesaur
->CleanUpAfterLookup(&pmean
, count
);
1091 sMenuItemText
.LoadString(IDS_SPELLEDIT_NOTHESAURUS
);
1092 popup
.AppendMenu(MF_DISABLED
| MF_GRAYED
| MF_STRING
, 0, sMenuItemText
);
1096 int cmd
= popup
.TrackPopupMenu(TPM_RETURNCMD
| TPM_LEFTALIGN
| TPM_NONOTIFY
, point
.x
, point
.y
, this, 0);
1100 break; // no command selected
1102 bRestoreCursor
= false;
1112 m_personalDict
.AddWord(sWord
);
1115 case SCI_LINESSPLIT
:
1117 int marker
= (int)(Call(SCI_GETEDGECOLUMN
) * Call(SCI_TEXTWIDTH
, 0, (LPARAM
)" "));
1120 Call(SCI_TARGETFROMSELECTION
);
1121 Call(SCI_LINESJOIN
);
1122 Call(SCI_LINESSPLIT
, marker
);
1127 if (cmd
< nCorrections
)
1129 Call(SCI_SETANCHOR
, pointpos
);
1130 Call(SCI_SETCURRENTPOS
, pointpos
);
1131 GetWordUnderCursor(true);
1133 popup
.GetMenuString(cmd
, temp
, 0);
1134 // setting the cursor clears the selection
1135 Call(SCI_REPLACESEL
, 0, (LPARAM
)(LPCSTR
)StringForControl(temp
));
1137 else if (cmd
< (nCorrections
+nCustoms
))
1139 for (INT_PTR handlerindex
= 0; handlerindex
< m_arContextHandlers
.GetCount(); ++handlerindex
)
1141 CSciEditContextMenuInterface
* pHandler
= m_arContextHandlers
.GetAt(handlerindex
);
1142 if (pHandler
->HandleMenuItemClick(cmd
, this))
1147 else if (cmd
<= (nThesaurs
+nCorrections
+nCustoms
))
1149 Call(SCI_SETANCHOR
, pointpos
);
1150 Call(SCI_SETCURRENTPOS
, pointpos
);
1151 GetWordUnderCursor(true);
1153 thesaurs
.GetMenuString(cmd
, temp
, 0);
1154 Call(SCI_REPLACESEL
, 0, (LPARAM
)(LPCSTR
)StringForControl(temp
));
1159 for (INT_PTR index
= 0; index
< menuArray
.GetCount(); ++index
)
1161 CMenu
* pMenu
= (CMenu
*)menuArray
[index
];
1168 // restore the anchor and cursor position
1169 Call(SCI_SETCURRENTPOS
, currentpos
);
1170 Call(SCI_SETANCHOR
, anchor
);
1174 bool CSciEdit::StyleEnteredText(int startstylepos
, int endstylepos
)
1176 bool bStyled
= false;
1177 const int line
= (int)Call(SCI_LINEFROMPOSITION
, startstylepos
);
1178 const int line_number_end
= (int)Call(SCI_LINEFROMPOSITION
, endstylepos
);
1179 for (int line_number
= line
; line_number
<= line_number_end
; ++line_number
)
1181 int offset
= (int)Call(SCI_POSITIONFROMLINE
, line_number
);
1182 int line_len
= (int)Call(SCI_LINELENGTH
, line_number
);
1183 std::unique_ptr
<char[]> linebuffer(new char[line_len
+1]);
1184 Call(SCI_GETLINE
, line_number
, (LPARAM
)linebuffer
.get());
1185 linebuffer
[line_len
] = 0;
1188 while (FindStyleChars(linebuffer
.get(), '*', start
, end
))
1190 Call(SCI_STARTSTYLING
, start
+offset
, STYLE_MASK
);
1191 Call(SCI_SETSTYLING
, end
-start
, STYLE_BOLD
);
1197 while (FindStyleChars(linebuffer
.get(), '^', start
, end
))
1199 Call(SCI_STARTSTYLING
, start
+offset
, STYLE_MASK
);
1200 Call(SCI_SETSTYLING
, end
-start
, STYLE_ITALIC
);
1206 while (FindStyleChars(linebuffer
.get(), '_', start
, end
))
1208 Call(SCI_STARTSTYLING
, start
+offset
, STYLE_MASK
);
1209 Call(SCI_SETSTYLING
, end
-start
, STYLE_UNDERLINED
);
1217 bool CSciEdit::WrapLines(int startpos
, int endpos
)
1219 int markerX
= (int)(Call(SCI_GETEDGECOLUMN
) * Call(SCI_TEXTWIDTH
, 0, (LPARAM
)" "));
1222 Call(SCI_SETTARGETSTART
, startpos
);
1223 Call(SCI_SETTARGETEND
, endpos
);
1224 Call(SCI_LINESSPLIT
, markerX
);
1230 void CSciEdit::AdvanceUTF8(const char * str
, int& pos
)
1232 if ((str
[pos
] & 0xE0)==0xC0)
1234 // utf8 2-byte sequence
1237 else if ((str
[pos
] & 0xF0)==0xE0)
1239 // utf8 3-byte sequence
1242 else if ((str
[pos
] & 0xF8)==0xF0)
1244 // utf8 4-byte sequence
1251 bool CSciEdit::FindStyleChars(const char * line
, char styler
, int& start
, int& end
)
1257 AdvanceUTF8(line
, i
);
1261 bool bFoundMarker
= false;
1262 CString sULine
= CUnicodeUtils::GetUnicode(line
);
1263 // find a starting marker
1264 while (line
[i
] != 0)
1266 if (line
[i
] == styler
)
1268 if ((line
[i
+1]!=0)&&(IsCharAlphaNumeric(sULine
[u
+1]))&&
1269 (((u
>0)&&(!IsCharAlphaNumeric(sULine
[u
-1]))) || (u
==0)))
1272 AdvanceUTF8(line
, i
);
1274 bFoundMarker
= true;
1278 AdvanceUTF8(line
, i
);
1283 // find ending marker
1284 bFoundMarker
= false;
1285 while (line
[i
] != 0)
1287 if (line
[i
] == styler
)
1289 if ((IsCharAlphaNumeric(sULine
[u
-1]))&&
1290 ((((u
+1)<sULine
.GetLength())&&(!IsCharAlphaNumeric(sULine
[u
+1]))) || ((u
+1) == sULine
.GetLength()))
1295 bFoundMarker
= true;
1299 AdvanceUTF8(line
, i
);
1302 return bFoundMarker
;
1305 BOOL
CSciEdit::MarkEnteredBugID(int startstylepos
, int endstylepos
)
1307 if (m_sCommand
.IsEmpty())
1309 // get the text between the start and end position we have to style
1310 const int line_number
= (int)Call(SCI_LINEFROMPOSITION
, startstylepos
);
1311 int start_pos
= (int)Call(SCI_POSITIONFROMLINE
, (WPARAM
)line_number
);
1312 int end_pos
= endstylepos
;
1314 if (start_pos
== end_pos
)
1316 if (start_pos
> end_pos
)
1318 int switchtemp
= start_pos
;
1319 start_pos
= end_pos
;
1320 end_pos
= switchtemp
;
1323 std::unique_ptr
<char[]> textbuffer(new char[end_pos
- start_pos
+ 2]);
1324 TEXTRANGEA textrange
;
1325 textrange
.lpstrText
= textbuffer
.get();
1326 textrange
.chrg
.cpMin
= start_pos
;
1327 textrange
.chrg
.cpMax
= end_pos
;
1328 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
1329 CStringA msg
= CStringA(textbuffer
.get());
1331 Call(SCI_STARTSTYLING
, start_pos
, STYLE_MASK
);
1335 if (!m_sBugID
.IsEmpty())
1337 // match with two regex strings (without grouping!)
1338 const std::tr1::regex
regCheck(m_sCommand
);
1339 const std::tr1::regex
regBugID(m_sBugID
);
1340 const std::tr1::sregex_iterator end
;
1341 std::string s
= msg
;
1344 // if start_pos is 0, we're styling from the beginning and let the ^ char match the beginning of the line
1345 // that way, the ^ matches the very beginning of the log message and not the beginning of further lines.
1346 // problem is: this only works *while* entering log messages. If a log message is pasted in whole or
1347 // multiple lines are pasted, start_pos can be 0 and styling goes over multiple lines. In that case, those
1348 // additional line starts also match ^
1349 for (std::tr1::sregex_iterator
it(s
.begin(), s
.end(), regCheck
, start_pos
!= 0 ? std::tr1::regex_constants::match_not_bol
: std::tr1::regex_constants::match_default
); it
!= end
; ++it
)
1351 // clear the styles up to the match position
1352 Call(SCI_SETSTYLING
, it
->position(0)-pos
, STYLE_DEFAULT
);
1354 // (*it)[0] is the matched string
1355 std::string matchedString
= (*it
)[0];
1356 LONG matchedpos
= 0;
1357 for (std::tr1::sregex_iterator
it2(matchedString
.begin(), matchedString
.end(), regBugID
); it2
!= end
; ++it2
)
1359 ATLTRACE("matched id : %s\n", std::string((*it2
)[0]).c_str());
1361 // bold style up to the id match
1362 ATLTRACE("position = %ld\n", it2
->position(0));
1363 if (it2
->position(0))
1364 Call(SCI_SETSTYLING
, it2
->position(0) - matchedpos
, STYLE_ISSUEBOLD
);
1365 // bold and recursive style for the bug ID itself
1366 if ((*it2
)[0].str().size())
1367 Call(SCI_SETSTYLING
, (*it2
)[0].str().size(), STYLE_ISSUEBOLDITALIC
);
1368 matchedpos
= (LONG
)(it2
->position(0) + (*it2
)[0].str().size());
1370 if ((matchedpos
)&&(matchedpos
< (LONG
)matchedString
.size()))
1372 Call(SCI_SETSTYLING
, matchedString
.size() - matchedpos
, STYLE_ISSUEBOLD
);
1374 pos
= (LONG
)(it
->position(0) + matchedString
.size());
1376 // bold style for the rest of the string which isn't matched
1378 Call(SCI_SETSTYLING
, s
.size()-pos
, STYLE_DEFAULT
);
1382 const std::tr1::regex
regCheck(m_sCommand
);
1383 const std::tr1::sregex_iterator end
;
1384 std::string s
= msg
;
1386 for (std::tr1::sregex_iterator
it(s
.begin(), s
.end(), regCheck
); it
!= end
; ++it
)
1388 // clear the styles up to the match position
1389 if (it
->position(0) - pos
>= 0)
1390 Call(SCI_SETSTYLING
, it
->position(0) - pos
, STYLE_DEFAULT
);
1391 pos
= (LONG
)it
->position(0);
1393 const std::tr1::smatch match
= *it
;
1394 // we define group 1 as the whole issue text and
1395 // group 2 as the bug ID
1396 if (match
.size() >= 2)
1398 ATLTRACE("matched id : %s\n", std::string(match
[1]).c_str());
1399 if (match
[1].first
- s
.begin() - pos
>= 0)
1400 Call(SCI_SETSTYLING
, match
[1].first
- s
.begin() - pos
, STYLE_ISSUEBOLD
);
1401 Call(SCI_SETSTYLING
, std::string(match
[1]).size(), STYLE_ISSUEBOLDITALIC
);
1402 pos
= (LONG
)(match
[1].second
-s
.begin());
1407 catch (std::exception
) {}
1412 bool CSciEdit::IsValidURLChar(unsigned char ch
)
1414 return isalnum(ch
) ||
1415 ch
== '_' || ch
== '/' || ch
== ';' || ch
== '?' || ch
== '&' || ch
== '=' ||
1416 ch
== '%' || ch
== ':' || ch
== '.' || ch
== '#' || ch
== '-' || ch
== '+' ||
1417 ch
== '|' || ch
== '>' || ch
== '<';
1420 void CSciEdit::StyleURLs(int startstylepos
, int endstylepos
)
1422 const int line_number
= (int)Call(SCI_LINEFROMPOSITION
, startstylepos
);
1423 startstylepos
= (int)Call(SCI_POSITIONFROMLINE
, (WPARAM
)line_number
);
1425 int len
= endstylepos
- startstylepos
+ 1;
1426 std::unique_ptr
<char[]> textbuffer(new char[len
+ 1]);
1427 TEXTRANGEA textrange
;
1428 textrange
.lpstrText
= textbuffer
.get();
1429 textrange
.chrg
.cpMin
= startstylepos
;
1430 textrange
.chrg
.cpMax
= endstylepos
;
1431 Call(SCI_GETTEXTRANGE
, 0, (LPARAM
)&textrange
);
1432 // we're dealing with utf8 encoded text here, which means one glyph is
1433 // not necessarily one byte/wchar_t
1434 // that's why we use CStringA to still get a correct char index
1435 CStringA msg
= textbuffer
.get();
1438 for (int i
= 0; i
<= msg
.GetLength(); AdvanceUTF8(msg
, i
))
1440 if ((i
< len
) && IsValidURLChar(msg
[i
]))
1450 if (msg
[starturl
] == '<' && i
< len
) // try to detect and do not strip URLs put within <>
1452 while (msg
[starturl
] == '<' && starturl
<= i
) // strip leading '<'
1456 while (i
< len
&& msg
[i
] != '\r' && msg
[i
] != '\n' && msg
[i
] != '>') // find first '>' or new line after resetting i to start position
1457 AdvanceUTF8(msg
, i
);
1459 if (!IsUrl(msg
.Mid(starturl
, i
- starturl
)))
1465 int skipTrailing
= 0;
1466 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] == '<'))
1468 ASSERT(startstylepos
+ i
- skipTrailing
<= endstylepos
);
1469 Call(SCI_STARTSTYLING
, startstylepos
+ starturl
, STYLE_MASK
);
1470 Call(SCI_SETSTYLING
, i
- starturl
- skipTrailing
, STYLE_URL
);
1477 bool CSciEdit::IsUrl(const CStringA
& sText
)
1479 if (!PathIsURLA(sText
))
1481 if (sText
.Find("://")>=0)
1486 bool CSciEdit::IsUTF8(LPVOID pBuffer
, size_t cb
)
1490 UINT16
* pVal
= (UINT16
*)pBuffer
;
1491 UINT8
* pVal2
= (UINT8
*)(pVal
+1);
1492 // scan the whole buffer for a 0x0000 sequence
1493 // if found, we assume a binary file
1494 for (size_t i
=0; i
<(cb
-2); i
=i
+2)
1496 if (0x0000 == *pVal
++)
1499 pVal
= (UINT16
*)pBuffer
;
1500 if (*pVal
== 0xFEFF)
1504 if (*pVal
== 0xBBEF)
1509 // check for illegal UTF8 chars
1510 pVal2
= (UINT8
*)pBuffer
;
1511 for (size_t i
=0; i
<cb
; ++i
)
1513 if ((*pVal2
== 0xC0)||(*pVal2
== 0xC1)||(*pVal2
>= 0xF5))
1517 pVal2
= (UINT8
*)pBuffer
;
1519 for (size_t i
=0; i
<(cb
-3); ++i
)
1521 if ((*pVal2
& 0xE0)==0xC0)
1524 if ((*pVal2
& 0xC0)!=0x80)
1528 if ((*pVal2
& 0xF0)==0xE0)
1531 if ((*pVal2
& 0xC0)!=0x80)
1534 if ((*pVal2
& 0xC0)!=0x80)
1538 if ((*pVal2
& 0xF8)==0xF0)
1541 if ((*pVal2
& 0xC0)!=0x80)
1544 if ((*pVal2
& 0xC0)!=0x80)
1547 if ((*pVal2
& 0xC0)!=0x80)
1558 void CSciEdit::SetAStyle(int style
, COLORREF fore
, COLORREF back
, int size
, const char *face
)
1560 Call(SCI_STYLESETFORE
, style
, fore
);
1561 Call(SCI_STYLESETBACK
, style
, back
);
1563 Call(SCI_STYLESETSIZE
, style
, size
);
1565 Call(SCI_STYLESETFONT
, style
, reinterpret_cast<LPARAM
>(face
));
1569 void CSciEdit::SetUDiffStyle()
1571 SetAStyle(STYLE_DEFAULT
, ::GetSysColor(COLOR_WINDOWTEXT
), ::GetSysColor(COLOR_WINDOW
),
1572 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffFontSize", 10),
1573 CUnicodeUtils::StdGetUTF8(CRegStdString(L
"Software\\TortoiseGit\\UDiffFontName", L
"Courier New")).c_str());
1574 Call(SCI_SETTABWIDTH
, CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffTabSize", 4));
1576 Call(SCI_SETTABWIDTH
, 4);
1577 Call(SCI_SETREADONLY
, TRUE
);
1578 //LRESULT pix = Call(SCI_TEXTWIDTH, STYLE_LINENUMBER, (LPARAM)"_99999");
1579 //Call(SCI_SETMARGINWIDTHN, 0, pix);
1580 //Call(SCI_SETMARGINWIDTHN, 1);
1581 //Call(SCI_SETMARGINWIDTHN, 2);
1582 //Set the default windows colors for edit controls
1583 Call(SCI_STYLESETFORE
, STYLE_DEFAULT
, ::GetSysColor(COLOR_WINDOWTEXT
));
1584 Call(SCI_STYLESETBACK
, STYLE_DEFAULT
, ::GetSysColor(COLOR_WINDOW
));
1585 Call(SCI_SETSELFORE
, TRUE
, ::GetSysColor(COLOR_HIGHLIGHTTEXT
));
1586 Call(SCI_SETSELBACK
, TRUE
, ::GetSysColor(COLOR_HIGHLIGHT
));
1587 Call(SCI_SETCARETFORE
, ::GetSysColor(COLOR_WINDOWTEXT
));
1589 //SendEditor(SCI_SETREADONLY, FALSE);
1591 Call(EM_EMPTYUNDOBUFFER
);
1592 Call(SCI_SETSAVEPOINT
);
1594 Call(SCI_SETUNDOCOLLECTION
, 0);
1596 Call(SCI_SETUNDOCOLLECTION
, 1);
1597 Call(SCI_SETWRAPMODE
,SC_WRAP_NONE
);
1599 //::SetFocus(m_hWndEdit);
1600 Call(EM_EMPTYUNDOBUFFER
);
1601 Call(SCI_SETSAVEPOINT
);
1602 Call(SCI_GOTOPOS
, 0);
1604 Call(SCI_CLEARDOCUMENTSTYLE
, 0, 0);
1605 Call(SCI_SETSTYLEBITS
, 5, 0);
1607 //SetAStyle(SCE_DIFF_DEFAULT, RGB(0, 0, 0));
1608 SetAStyle(SCE_DIFF_COMMAND
,
1609 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForeCommandColor", UDIFF_COLORFORECOMMAND
),
1610 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackCommandColor", UDIFF_COLORBACKCOMMAND
));
1611 SetAStyle(SCE_DIFF_POSITION
,
1612 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForePositionColor", UDIFF_COLORFOREPOSITION
),
1613 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackPositionColor", UDIFF_COLORBACKPOSITION
));
1614 SetAStyle(SCE_DIFF_HEADER
,
1615 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForeHeaderColor", UDIFF_COLORFOREHEADER
),
1616 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackHeaderColor", UDIFF_COLORBACKHEADER
));
1617 SetAStyle(SCE_DIFF_COMMENT
,
1618 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForeCommentColor", UDIFF_COLORFORECOMMENT
),
1619 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackCommentColor", UDIFF_COLORBACKCOMMENT
));
1620 Call(SCI_STYLESETBOLD
, SCE_DIFF_COMMENT
, TRUE
);
1621 SetAStyle(SCE_DIFF_ADDED
,
1622 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForeAddedColor", UDIFF_COLORFOREADDED
),
1623 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackAddedColor", UDIFF_COLORBACKADDED
));
1624 SetAStyle(SCE_DIFF_DELETED
,
1625 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffForeRemovedColor", UDIFF_COLORFOREREMOVED
),
1626 CRegStdDWORD(L
"Software\\TortoiseGit\\UDiffBackRemovedColor", UDIFF_COLORBACKREMOVED
));
1628 Call(SCI_SETLEXER
, SCLEX_DIFF
);
1629 Call(SCI_SETKEYWORDS
, 0, (LPARAM
)"revision");
1630 Call(SCI_COLOURISE
, 0, -1);
1633 int CSciEdit::LoadFromFile(CString
&filename
)
1636 _tfopen_s(&fp
, filename
, _T("rb"));
1640 char data
[4096] = { 0 };
1641 size_t lenFile
= fread(data
, 1, sizeof(data
), fp
);
1642 bool bUTF8
= IsUTF8(data
, lenFile
);
1645 Call(SCI_ADDTEXT
, lenFile
,
1646 reinterpret_cast<LPARAM
>(static_cast<char *>(data
)));
1647 lenFile
= fread(data
, 1, sizeof(data
), fp
);
1650 Call(SCI_SETCODEPAGE
, bUTF8
? SC_CP_UTF8
: GetACP());
1657 void CSciEdit::RestyleBugIDs()
1659 int endstylepos
= (int)Call(SCI_GETLENGTH
);
1661 Call(SCI_STARTSTYLING
, 0, STYLE_MASK
);
1662 Call(SCI_SETSTYLING
, endstylepos
, STYLE_DEFAULT
);
1663 // style the bug IDs
1664 MarkEnteredBugID(0, endstylepos
);