[NEW_FEATURE] (Author: Loreia) Enhance TAB2SPACE and SPACE2TAB features.

git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@944 f5eea248-9336-0410-98b8-ebc06183d4e3
This commit is contained in:
Don Ho 2012-08-25 19:23:16 +00:00
parent e08e566e19
commit e86940390f
6 changed files with 483 additions and 331 deletions

View File

@ -1033,42 +1033,177 @@ bool Notepad_plus::matchInList(const TCHAR *fileName, const vector<generic_strin
return false;
}
void Notepad_plus::wsTabConvert(bool tab2ws)
void Notepad_plus::wsTabConvert(spaceTab whichWay)
{
generic_string tab = TEXT(" ");
generic_string blank2search = tab;
generic_string blank2replace = tab;
// Get tab size (ws length)
int tabWidth = _pEditView->execute(SCI_GETTABWIDTH);
generic_string ws(tabWidth, ' ');
int docLength = int(_pEditView->execute(SCI_GETLENGTH)) + 1;
if (docLength < 2)
return;
int count = 0;
int column = 0;
int counter = 0;
int tabStop = tabWidth - 1; // remember, counting from zero !
bool onlyLeading = false;
bool nonSpaceFound = false;
// tab2ws or ws2tab ?
if (tab2ws)
char * source = new char[docLength];
if (source == NULL)
return;
_pEditView->execute(SCI_GETTEXT, docLength, (LPARAM)source);
if (whichWay == tab2Space)
{
blank2replace = ws;
// count how many tabs are there
for (const char * ch=source; *ch; ++ch)
{
if (*ch == '\t')
++count;
}
if (count == 0)
{
delete [] source;
return;
}
}
else
// allocate tabwidth-1 chars extra per tab, just to be safe
size_t newlen = docLength + count * (tabWidth - 1) + 1;
char * destination = new char[newlen];
if (destination == NULL)
{
blank2search= ws;
delete [] source;
return;
}
char * dest = destination;
switch (whichWay)
{
case tab2Space:
{
// rip through each line in the file
for (const char * ch = source; *ch; ++ch)
{
if (*ch == '\t')
{
size_t insertTabs = tabWidth - (column % tabWidth);
for (size_t i = 0; i<insertTabs; ++i)
{
*dest++ = ' ';
}
column += insertTabs;
}
else
{
*dest++ = *ch;
if ((*ch == '\n') || (*ch == '\r'))
column = 0;
else
++column;
}
}
*dest = '\0';
break;
}
case space2TabLeading:
{
onlyLeading = true;
}
case space2TabAll:
{
bool nextChar = false;
for (const char * ch=source; *ch; ++ch)
{
if (nonSpaceFound == false)
{
while (*(ch + counter) == ' ')
{
if ((column + counter) == tabStop)
{
tabStop += tabWidth;
if (counter >= 1) // counter is counted from 0, so counter >= max -1
{
*dest++ = '\t';
ch += counter;
column += counter + 1;
counter = 0;
nextChar = true;
break;
}
else if (*(ch+1) == ' ' || *(ch+1) == '\t') // if followed by space or TAB, convert even a single space to TAB
{
*dest++ = '\t';
ch++;
column += 1;
counter = 0;
}
else // single space, don't convert it to TAB
{
*dest++ = *ch;
column += 1;
counter = 0;
nextChar = true;
break;
}
}
else
++counter;
}
if (nextChar == true)
{
nextChar = false;
continue;
}
if (*ch == ' ' && *(ch + counter) == '\t') // spaces "absorbed" by a TAB on the right
{
*dest++ = '\t';
ch += counter;
column = tabStop + 1;
tabStop += tabWidth;
counter = 0;
continue;
}
}
if (onlyLeading == true && nonSpaceFound == false)
nonSpaceFound = true;
if (*ch == '\n' || *ch == '\r')
{
*dest++ = *ch;
column = 0;
tabStop = tabWidth - 1;
nonSpaceFound = false;
}
else if (*ch == '\t')
{
*dest++ = *ch;
column = tabStop + 1;
tabStop += tabWidth;
counter = 0;
}
else
{
*dest++ = *ch;
++column;
counter = 0;
if (column > 0 && column % tabWidth == 0)
tabStop += tabWidth;
}
}
*dest = '\0';
break;
}
}
FindOption env;
env._str2Search = blank2search;
env._str4Replace = blank2replace;
env._searchType = FindRegex;
// do the replacement
_pEditView->execute(SCI_BEGINUNDOACTION);
_findReplaceDlg.processAll(ProcessReplaceAll, &env, true);
// if white space to TAB, we replace the remain white spaces by TAB
if (!tab2ws)
{
env._str2Search = TEXT(" +");
_findReplaceDlg.processAll(ProcessReplaceAll, &env, true);
}
_pEditView->execute(SCI_SETTEXT, 0, (LPARAM)destination);
_pEditView->execute(SCI_ENDUNDOACTION);
// clean up
delete [] source;
delete [] destination;
}
void Notepad_plus::doTrim(trimOp whichPart)
@ -1484,6 +1619,7 @@ void Notepad_plus::checkClipboard()
bool canPaste = (_pEditView->execute(SCI_CANPASTE) != 0);
enableCommand(IDM_EDIT_CUT, hasSelection, MENU | TOOLBAR);
enableCommand(IDM_EDIT_COPY, hasSelection, MENU | TOOLBAR);
enableCommand(IDM_EDIT_PASTE, canPaste, MENU | TOOLBAR);
enableCommand(IDM_EDIT_DELETE, hasSelection, MENU | TOOLBAR);
enableCommand(IDM_EDIT_UPPERCASE, hasSelection, MENU);
@ -4488,6 +4624,7 @@ int Notepad_plus::getLangFromMenuName(const TCHAR * langName)
generic_string Notepad_plus::getLangFromMenu(const Buffer * buf)
{
int id;
generic_string userLangName;
const int nbChar = 32;

View File

@ -151,6 +151,12 @@ enum trimOp {
lineEol = 2
};
enum spaceTab {
tab2Space = 0,
space2TabLeading = 1,
space2TabAll = 2
};
struct TaskListInfo;
struct VisibleGUIConf {
@ -599,7 +605,7 @@ private:
bool goToNextIndicator(int indicID2Search, bool isWrap = true) const;
int wordCount();
void wsTabConvert(bool whichWay);
void wsTabConvert(spaceTab whichWay);
void doTrim(trimOp whichPart);
void launchAnsiCharPanel();
void launchClipboardHistoryPanel();

View File

@ -271,31 +271,32 @@ BEGIN
MENUITEM "UNIX Format", IDM_FORMAT_TOUNIX
MENUITEM "Mac Format", IDM_FORMAT_TOMAC
END
POPUP "Blank Operations"
BEGIN
MENUITEM "Trim Trailing Space", IDM_EDIT_TRIMTRAILING
MENUITEM "Trim Leading Space", IDM_EDIT_TRIMLINEHEAD
MENUITEM "Trim Leading and Trailing Space", IDM_EDIT_TRIM_BOTH
MENUITEM "EOL to Space", IDM_EDIT_EOL2WS
MENUITEM "Remove Unnecessary Blank and EOL", IDM_EDIT_TRIMALL
MENUITEM SEPARATOR
MENUITEM "TAB to Space", IDM_EDIT_TAB2SW
MENUITEM "Space to TAB", IDM_EDIT_SW2TAB
END
POPUP "Paste Special"
BEGIN
MENUITEM "Paste HTML Content", IDM_EDIT_PASTE_AS_HTML
MENUITEM "Paste RTF Content", IDM_EDIT_PASTE_AS_RTF
MENUITEM SEPARATOR
MENUITEM "Copy Binary Content", IDM_EDIT_COPY_BINARY
MENUITEM "Cut Binary Content", IDM_EDIT_CUT_BINARY
MENUITEM "Paste Binary Content", IDM_EDIT_PASTE_BINARY
END
POPUP "Blank Operations"
BEGIN
MENUITEM "Trim Trailing Space", IDM_EDIT_TRIMTRAILING
MENUITEM "Trim Leading Space", IDM_EDIT_TRIMLINEHEAD
MENUITEM "Trim Leading and Trailing Space", IDM_EDIT_TRIM_BOTH
MENUITEM "EOL to Space", IDM_EDIT_EOL2WS
MENUITEM "Remove Unnecessary Blank and EOL", IDM_EDIT_TRIMALL
MENUITEM SEPARATOR
MENUITEM "TAB to Space", IDM_EDIT_TAB2SW
MENUITEM "Space to TAB (All)", IDM_EDIT_SW2TAB_ALL
MENUITEM "Space to TAB (Leading)", IDM_EDIT_SW2TAB_LEADING
END
POPUP "Paste Special"
BEGIN
MENUITEM "Paste HTML Content", IDM_EDIT_PASTE_AS_HTML
MENUITEM "Paste RTF Content", IDM_EDIT_PASTE_AS_RTF
MENUITEM SEPARATOR
MENUITEM "Copy Binary Content", IDM_EDIT_COPY_BINARY
MENUITEM "Cut Binary Content", IDM_EDIT_CUT_BINARY
MENUITEM "Paste Binary Content", IDM_EDIT_PASTE_BINARY
END
MENUITEM SEPARATOR
MENUITEM "Column Mode...", IDM_EDIT_COLUMNMODETIP
MENUITEM "Column Editor...", IDM_EDIT_COLUMNMODE
MENUITEM "Character Panel", IDM_EDIT_CHAR_PANEL
MENUITEM "Clipboard History", IDM_EDIT_CLIPBOARDHISTORY_PANEL
MENUITEM "Column Editor...", IDM_EDIT_COLUMNMODE
MENUITEM "Character Panel", IDM_EDIT_CHAR_PANEL
MENUITEM "Clipboard History", IDM_EDIT_CLIPBOARDHISTORY_PANEL
MENUITEM SEPARATOR
MENUITEM "Set Read-Only", IDM_EDIT_SETREADONLY
MENUITEM "Clear Read-Only Flag", IDM_EDIT_CLEARREADONLY
@ -356,22 +357,22 @@ BEGIN
MENUITEM "5th Style", IDM_SEARCH_GONEXTMARKER5
MENUITEM "Find Style", IDM_SEARCH_GONEXTMARKER_DEF
END
MENUITEM SEPARATOR
MENUITEM SEPARATOR
POPUP "Bookmark"
BEGIN
MENUITEM "Toggle Bookmark" , IDM_SEARCH_TOGGLE_BOOKMARK
MENUITEM "Next Bookmark", IDM_SEARCH_NEXT_BOOKMARK
MENUITEM "Previous Bookmark", IDM_SEARCH_PREV_BOOKMARK
MENUITEM "Clear All Bookmarks", IDM_SEARCH_CLEAR_BOOKMARKS
MENUITEM "Cut Bookmarked Lines", IDM_SEARCH_CUTMARKEDLINES
MENUITEM "Copy Bookmarked Lines", IDM_SEARCH_COPYMARKEDLINES
MENUITEM "Paste to (Replace) Bookmarked Lines", IDM_SEARCH_PASTEMARKEDLINES
MENUITEM "Remove Bookmarked Lines", IDM_SEARCH_DELETEMARKEDLINES
MENUITEM "Remove Unmarked Lines", IDM_SEARCH_DELETEUNMARKEDLINES
MENUITEM "Inverse Bookmark", IDM_SEARCH_INVERSEMARKS
MENUITEM "Toggle Bookmark" , IDM_SEARCH_TOGGLE_BOOKMARK
MENUITEM "Next Bookmark", IDM_SEARCH_NEXT_BOOKMARK
MENUITEM "Previous Bookmark", IDM_SEARCH_PREV_BOOKMARK
MENUITEM "Clear All Bookmarks", IDM_SEARCH_CLEAR_BOOKMARKS
MENUITEM "Cut Bookmarked Lines", IDM_SEARCH_CUTMARKEDLINES
MENUITEM "Copy Bookmarked Lines", IDM_SEARCH_COPYMARKEDLINES
MENUITEM "Paste to (Replace) Bookmarked Lines", IDM_SEARCH_PASTEMARKEDLINES
MENUITEM "Remove Bookmarked Lines", IDM_SEARCH_DELETEMARKEDLINES
MENUITEM "Remove Unmarked Lines", IDM_SEARCH_DELETEUNMARKEDLINES
MENUITEM "Inverse Bookmark", IDM_SEARCH_INVERSEMARKS
END
MENUITEM SEPARATOR
MENUITEM "Find characters in range...", IDM_SEARCH_FINDCHARINRANGE
MENUITEM SEPARATOR
MENUITEM "Find characters in range...", IDM_SEARCH_FINDCHARINRANGE
END
POPUP "&View"
@ -436,18 +437,18 @@ BEGIN
END
MENUITEM SEPARATOR
MENUITEM "Summary...", IDM_VIEW_SUMMARY
MENUITEM "Summary...", IDM_VIEW_SUMMARY
MENUITEM SEPARATOR
POPUP "Project"
POPUP "Project"
BEGIN
MENUITEM "Project Panel 1", IDM_VIEW_PROJECT_PANEL_1
MENUITEM "Project Panel 2", IDM_VIEW_PROJECT_PANEL_2
MENUITEM "Project Panel 3", IDM_VIEW_PROJECT_PANEL_3
MENUITEM "Project Panel 2", IDM_VIEW_PROJECT_PANEL_2
MENUITEM "Project Panel 3", IDM_VIEW_PROJECT_PANEL_3
END
#ifdef UNICODE
MENUITEM "Document Map", IDM_VIEW_DOC_MAP
MENUITEM "Document Map", IDM_VIEW_DOC_MAP
#endif
MENUITEM SEPARATOR
MENUITEM SEPARATOR
MENUITEM "Synchronize Vertical Scrolling", IDM_VIEW_SYNSCROLLV
MENUITEM "Synchronize Horizontal Scrolling", IDM_VIEW_SYNSCROLLH
MENUITEM SEPARATOR
@ -534,7 +535,7 @@ BEGIN
POPUP "Korean"
BEGIN
MENUITEM "Windows 949", IDM_FORMAT_KOREAN_WIN
MENUITEM "EUC-KR", IDM_FORMAT_EUC_KR
MENUITEM "EUC-KR", IDM_FORMAT_EUC_KR
END
POPUP "North European"
@ -585,60 +586,60 @@ BEGIN
POPUP "&Language"
BEGIN
MENUITEM "Ada", IDM_LANG_ADA
MENUITEM "ASP", IDM_LANG_ASP
MENUITEM "Assembly", IDM_LANG_ASM
MENUITEM "AutoIt", IDM_LANG_AU3
MENUITEM "Batch", IDM_LANG_BATCH
MENUITEM "C", IDM_LANG_C
MENUITEM "C#", IDM_LANG_CS
MENUITEM "C++", IDM_LANG_CPP
MENUITEM "Caml", IDM_LANG_CAML
MENUITEM "Cmake", IDM_LANG_CMAKE
MENUITEM "COBOL", IDM_LANG_COBOL
MENUITEM "CSS", IDM_LANG_CSS
MENUITEM "D", IDM_LANG_D
MENUITEM "Diff", IDM_LANG_DIFF
MENUITEM "Flash actionscript", IDM_LANG_FLASH
MENUITEM "Fortran", IDM_LANG_FORTRAN
MENUITEM "Gui4Cli", IDM_LANG_GUI4CLI
MENUITEM "Haskell", IDM_LANG_HASKELL
MENUITEM "HTML", IDM_LANG_HTML
MENUITEM "INNO", IDM_LANG_INNO
MENUITEM "Java", IDM_LANG_JAVA
MENUITEM "Javascript", IDM_LANG_JS
MENUITEM "JSP", IDM_LANG_JSP
MENUITEM "KIXtart", IDM_LANG_KIX
MENUITEM "LISP", IDM_LANG_LISP
MENUITEM "Lua", IDM_LANG_LUA
MENUITEM "Makefile", IDM_LANG_MAKEFILE
MENUITEM "Matlab", IDM_LANG_MATLAB
MENUITEM "MS INI file", IDM_LANG_INI
MENUITEM "MS-DOS Style", IDM_LANG_ASCII
MENUITEM "Normal Text", IDM_LANG_TEXT
MENUITEM "NSIS", IDM_LANG_NSIS
MENUITEM "Objective-C", IDM_LANG_OBJC
MENUITEM "Pascal", IDM_LANG_PASCAL
MENUITEM "Perl", IDM_LANG_PERL
MENUITEM "PHP", IDM_LANG_PHP
MENUITEM "Postscript", IDM_LANG_PS
MENUITEM "PowerShell", IDM_LANG_POWERSHELL
MENUITEM "Properties", IDM_LANG_PROPS
MENUITEM "Python", IDM_LANG_PYTHON
MENUITEM "R", IDM_LANG_R
MENUITEM "Resource file", IDM_LANG_RC
MENUITEM "Ruby", IDM_LANG_RUBY
MENUITEM "Shell", IDM_LANG_BASH
MENUITEM "Scheme", IDM_LANG_SCHEME
MENUITEM "Smalltalk", IDM_LANG_SMALLTALK
MENUITEM "SQL", IDM_LANG_SQL
MENUITEM "TCL", IDM_LANG_TCL
MENUITEM "TeX", IDM_LANG_TEX
MENUITEM "VB", IDM_LANG_VB
MENUITEM "VHDL", IDM_LANG_VHDL
MENUITEM "Verilog", IDM_LANG_VERILOG
MENUITEM "XML", IDM_LANG_XML
MENUITEM "YAML", IDM_LANG_YAML
MENUITEM "Ada", IDM_LANG_ADA
MENUITEM "ASP", IDM_LANG_ASP
MENUITEM "Assembly", IDM_LANG_ASM
MENUITEM "AutoIt", IDM_LANG_AU3
MENUITEM "Batch", IDM_LANG_BATCH
MENUITEM "C", IDM_LANG_C
MENUITEM "C#", IDM_LANG_CS
MENUITEM "C++", IDM_LANG_CPP
MENUITEM "Caml", IDM_LANG_CAML
MENUITEM "Cmake", IDM_LANG_CMAKE
MENUITEM "COBOL", IDM_LANG_COBOL
MENUITEM "CSS", IDM_LANG_CSS
MENUITEM "D", IDM_LANG_D
MENUITEM "Diff", IDM_LANG_DIFF
MENUITEM "Flash actionscript", IDM_LANG_FLASH
MENUITEM "Fortran", IDM_LANG_FORTRAN
MENUITEM "Gui4Cli", IDM_LANG_GUI4CLI
MENUITEM "Haskell", IDM_LANG_HASKELL
MENUITEM "HTML", IDM_LANG_HTML
MENUITEM "INNO", IDM_LANG_INNO
MENUITEM "Java", IDM_LANG_JAVA
MENUITEM "Javascript", IDM_LANG_JS
MENUITEM "JSP", IDM_LANG_JSP
MENUITEM "KIXtart", IDM_LANG_KIX
MENUITEM "LISP", IDM_LANG_LISP
MENUITEM "Lua", IDM_LANG_LUA
MENUITEM "Makefile", IDM_LANG_MAKEFILE
MENUITEM "Matlab", IDM_LANG_MATLAB
MENUITEM "MS INI file", IDM_LANG_INI
MENUITEM "MS-DOS Style", IDM_LANG_ASCII
MENUITEM "Normal Text", IDM_LANG_TEXT
MENUITEM "NSIS", IDM_LANG_NSIS
MENUITEM "Objective-C", IDM_LANG_OBJC
MENUITEM "Pascal", IDM_LANG_PASCAL
MENUITEM "Perl", IDM_LANG_PERL
MENUITEM "PHP", IDM_LANG_PHP
MENUITEM "Postscript", IDM_LANG_PS
MENUITEM "PowerShell", IDM_LANG_POWERSHELL
MENUITEM "Properties", IDM_LANG_PROPS
MENUITEM "Python", IDM_LANG_PYTHON
MENUITEM "R", IDM_LANG_R
MENUITEM "Resource file", IDM_LANG_RC
MENUITEM "Ruby", IDM_LANG_RUBY
MENUITEM "Shell", IDM_LANG_BASH
MENUITEM "Scheme", IDM_LANG_SCHEME
MENUITEM "Smalltalk", IDM_LANG_SMALLTALK
MENUITEM "SQL", IDM_LANG_SQL
MENUITEM "TCL", IDM_LANG_TCL
MENUITEM "TeX", IDM_LANG_TEX
MENUITEM "VB", IDM_LANG_VB
MENUITEM "VHDL", IDM_LANG_VHDL
MENUITEM "Verilog", IDM_LANG_VERILOG
MENUITEM "XML", IDM_LANG_XML
MENUITEM "YAML", IDM_LANG_YAML
MENUITEM SEPARATOR
MENUITEM "User-Defined", IDM_LANG_USER
END
@ -757,7 +758,7 @@ BEGIN
MENUITEM "Import style theme(s)...", IDM_SETTING_IMPORTSTYLETHEMS
END
MENUITEM SEPARATOR
MENUITEM "Edit Popup ContextMenu", IDM_SETTING_EDITCONTEXTMENU
MENUITEM "Edit Popup ContextMenu", IDM_SETTING_EDITCONTEXTMENU
END
POPUP "Macro"

View File

@ -879,11 +879,15 @@ void Notepad_plus::command(int id)
break;
case IDM_EDIT_TAB2SW:
wsTabConvert(true);
wsTabConvert(tab2Space);
break;
case IDM_EDIT_SW2TAB:
wsTabConvert(false);
case IDM_EDIT_SW2TAB_LEADING:
wsTabConvert(space2TabLeading);
break;
case IDM_EDIT_SW2TAB_ALL:
wsTabConvert(space2TabAll);
break;
case IDM_EDIT_SETREADONLY:
@ -2376,7 +2380,8 @@ void Notepad_plus::command(int id)
case IDM_EDIT_EOL2WS:
case IDM_EDIT_TRIMALL:
case IDM_EDIT_TAB2SW:
case IDM_EDIT_SW2TAB:
case IDM_EDIT_SW2TAB_ALL:
case IDM_EDIT_SW2TAB_LEADING:
case IDM_EDIT_SETREADONLY :
case IDM_EDIT_FULLPATHTOCLIP :
case IDM_EDIT_FILENAMETOCLIP :

View File

@ -103,7 +103,8 @@ WinMenuKeyDefinition winKeyDefs[] = {
{VK_NULL, IDM_EDIT_EOL2WS, false, false, false, NULL},
{VK_NULL, IDM_EDIT_TRIMALL, false, false, false, NULL},
{VK_NULL, IDM_EDIT_TAB2SW, false, false, false, NULL},
{VK_NULL, IDM_EDIT_SW2TAB, false, false, false, NULL},
{VK_NULL, IDM_EDIT_SW2TAB_ALL, false, false, false, NULL},
{VK_NULL, IDM_EDIT_SW2TAB_LEADING, false, false, false, NULL},
{VK_C, IDM_EDIT_COLUMNMODE, false, true, false, NULL},
{VK_U, IDM_EDIT_UPPERCASE, true, false, true, NULL},

View File

@ -88,7 +88,9 @@
#define IDM_EDIT_EOL2WS (IDM_EDIT + 44)
#define IDM_EDIT_TRIMALL (IDM_EDIT + 45)
#define IDM_EDIT_TAB2SW (IDM_EDIT + 46)
#define IDM_EDIT_SW2TAB (IDM_EDIT + 47)
#define IDM_EDIT_SW2TAB_LEADING (IDM_EDIT + 53)
#define IDM_EDIT_SW2TAB_ALL (IDM_EDIT + 54)
//#define IDM_EDIT_SW2TAB (IDM_EDIT + 47)
// Menu macro
#define IDM_MACRO_SAVECURRENTMACRO (IDM_EDIT + 25)