MCPcopy Create free account
hub / github.com/ProgerXP/Notepad2e / EditEncloseSelection

Function EditEncloseSelection

src/Edit.c:2958–3029  ·  view source on GitHub ↗

============================================================================= EditEncloseSelection()

Source from the content-addressed store, hash-verified

2956// EditEncloseSelection()
2957//
2958void EditEncloseSelection ( HWND hwnd, LPCWSTR pwszOpen, LPCWSTR pwszClose )
2959{
2960 char mszOpen[256 * 3] = "";
2961 char mszClose[256 * 3] = "";
2962 char skip[] = "\r\n\t ";
2963 int mbcp;
2964 int iSelStart = ( int ) SendMessage ( hwnd, SCI_GETSELECTIONSTART, 0, 0 );
2965 int iSelEnd = ( int ) SendMessage ( hwnd, SCI_GETSELECTIONEND, 0, 0 );
2966 //////////////
2967 char ch = 0;
2968 while (iSelStart < iSelEnd) {
2969 ch = (char)SendMessage(hwnd, SCI_GETCHARAT, iSelStart, 0);
2970 if (strchr(skip, ch)) {
2971 ++iSelStart;
2972 }
2973 else {
2974 break;
2975 }
2976 }
2977 while (iSelStart < iSelEnd) {
2978 ch = (char)SendMessage(hwnd, SCI_GETCHARAT, iSelEnd-1, 0);
2979 if (strchr(skip, ch)) {
2980 --iSelEnd;
2981 }
2982 else {
2983 break;
2984 }
2985 }
2986 //////////////
2987 if ( SendMessage ( hwnd, SCI_GETCODEPAGE, 0, 0 ) == SC_CP_UTF8 ) {
2988 mbcp = CP_UTF8;
2989 } else {
2990 mbcp = CP_ACP;
2991 }
2992 if ( lstrlen ( pwszOpen ) ) {
2993 WideCharToMultiByte ( mbcp, 0, pwszOpen, -1, mszOpen, COUNTOF ( mszOpen ), NULL, NULL );
2994 }
2995 if ( lstrlen ( pwszClose ) ) {
2996 WideCharToMultiByte ( mbcp, 0, pwszClose, -1, mszClose, COUNTOF ( mszClose ), NULL, NULL );
2997 }
2998 if ( SC_SEL_RECTANGLE != SendMessage ( hwnd, SCI_GETSELECTIONMODE, 0, 0 ) ) {
2999 SendMessage ( hwnd, SCI_BEGINUNDOACTION, 0, 0 );
3000 if ( lstrlenA ( mszOpen ) ) {
3001 SendMessage ( hwnd, SCI_SETTARGETSTART, ( WPARAM ) iSelStart, 0 );
3002 SendMessage ( hwnd, SCI_SETTARGETEND, ( WPARAM ) iSelStart, 0 );
3003 SendMessage ( hwnd, SCI_REPLACETARGET, ( WPARAM ) lstrlenA ( mszOpen ), ( LPARAM ) mszOpen );
3004 }
3005 if ( lstrlenA ( mszClose ) ) {
3006 SendMessage ( hwnd, SCI_SETTARGETSTART, ( WPARAM ) iSelEnd + lstrlenA ( mszOpen ), 0 );
3007 SendMessage ( hwnd, SCI_SETTARGETEND, ( WPARAM ) iSelEnd + lstrlenA ( mszOpen ), 0 );
3008 SendMessage ( hwnd, SCI_REPLACETARGET, ( WPARAM ) lstrlenA ( mszClose ), ( LPARAM ) mszClose );
3009 }
3010 SendMessage ( hwnd, SCI_ENDUNDOACTION, 0, 0 );
3011 // Fix selection
3012 if ( iSelStart == iSelEnd ) {
3013 SendMessage ( hwnd, SCI_SETSEL, ( WPARAM ) iSelStart + lstrlenA ( mszOpen ), ( WPARAM ) iSelStart + lstrlenA ( mszOpen ) );
3014 } else {
3015 int iCurPos = ( int ) SendMessage ( hwnd, SCI_GETCURRENTPOS, 0, 0 );

Callers 1

MsgCommandFunction · 0.85

Calls 1

MsgBoxFunction · 0.85

Tested by

no test coverage detected