@@ -1099,117 +1099,67 @@ int Editor::contextHelp()
10991099 return 1 ;
11001100}
11011101
1102- void Editor::highlightBlock (QTextCursor *cur)
1103- {
1104- // anchor and position vary depending on select up or down
1105- int beg = cur->position ();
1106- int end = cur->anchor ();
1107-
1108- // swap begin and end?
1109- if (beg > end) {
1110- beg = cur->anchor ();
1111- end = cur->position ();
1112- }
1113-
1114- // find new begin and end
1115- cur->setPosition (beg, QTextCursor::MoveAnchor);
1116- cur->movePosition (QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
1117- beg = cur->anchor ();
1118- cur->setPosition (end, QTextCursor::MoveAnchor);
1119- cur->movePosition (QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
1120- end = cur->anchor ();
1121-
1122- // set new cursor positions
1123- cur->setPosition (beg, QTextCursor::MoveAnchor);
1124- cur->setPosition (end, QTextCursor::KeepAnchor);
1125- }
1126-
11271102int Editor::tabBlockShift ()
11281103{
1104+ /* make tabs based on user preference - set by mainwindow */
11291105 int tabSpaces = propDialog->getTabSpaces ();
1106+ QString tab (tabSpaces, ' ' );
11301107
11311108 QTextCursor cur = this ->textCursor ();
1132- int curbeg = cur.selectionStart ();
11331109
11341110 /* do we have shift ? */
1135- bool shift = false ;
1136- if ((QApplication::keyboardModifiers () & Qt::SHIFT))
1137- shift = true ;
1138-
1139- /* make tabs based on user preference - set by mainwindow */
1140- QString tab = " " ;
1141- for (int n = tabSpaces; n > 0 ; n--) tab+=" " ;
1142-
1143- QString text = cur.selectedText ();
1111+ bool shiftTab = QApplication::keyboardModifiers () & Qt::SHIFT;
11441112
1145- /* if a block is selected */
1146- if (cur.selectedText ().length () > 0 && cur.selectedText ().contains (QChar::ParagraphSeparator) == true ) {
1113+ /* block is selected */
1114+ if (cur.hasSelection () && cur.selectedText ().contains (QChar::ParagraphSeparator)) {
1115+ /* determine current selection */
1116+ int curbeg = cur.selectionStart ();
1117+ int curend = cur.selectionEnd ();
11471118
1148- highlightBlock (&cur);
1149- curbeg = cur.selectionStart ();
1119+ if (curbeg > curend) std::swap (curbeg, curend);
11501120
1151- QStringList mylist;
1121+ /* create workable selection */
1122+ cur.setPosition (curbeg, QTextCursor::MoveAnchor);
1123+ cur.movePosition (QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
1124+ cur.setPosition (curend, QTextCursor::KeepAnchor);
11521125
1153- /* highlight block from beginning of the first line to the last line */
1154- int column = cur.columnNumber ();
1155- #if 0
1156- // I found this method had some troubles.
1157- // Using highlightBlock() instead.
1158- if(column > 0) {
1159- cur.setPosition(curbeg-column,QTextCursor::MoveAnchor);
1160- cur.movePosition(QTextCursor::Right,QTextCursor::KeepAnchor, text.length()+column);
1161- text = cur.selectedText();
1162- }
1163- #endif
1164- text = cur.selectedText ();
1165- if (text.length () == 0 )
1166- return 0 ;
1167-
1168- /* get a list of the selected block. keep empty lines */
1169- mylist = text.split (QChar::ParagraphSeparator);
1126+ /* get a list of lines in the selected block. keep empty lines */
1127+ QStringList mylist = cur.selectedText ().split (QChar::ParagraphSeparator);
11701128
11711129 /* start a single undo/redo operation */
11721130 cur.beginEditBlock ();
11731131
1174- /* get rid of old block */
1175- cur.removeSelectedText ();
1176-
11771132 /* indent list */
1178- text = " " ;
1179- for (int n = 0 ; n < mylist.length (); n++) {
1180- QString s = mylist.at (n);
1181- if (s.length () == 0 && n+1 == mylist.length ())
1182- break ;
1183-
1184- if (shift == false ) {
1185- for (int n = tabSpaces; n > 0 ; n--) s = " " + s;
1186- }
1187- else {
1188- if (s.indexOf (tab) == 0 || (n == 0 && column >= tabSpaces && s.at (0 ) == ' ' )) {
1189- s = s.mid (tabSpaces);
1190- }
1191- /* Sometimes code isn't aligned according to tabSpaces. Fix it.
1192- * This means that some indented lines can get unindented though.
1193- */
1194- else {
1195- for (int j = tabSpaces; s.length () > 0 && j > 0 ; j--) {
1196- if (s.at (0 ) == ' ' ) {
1197- s = s.mid (1 );
1198- }
1199- }
1200- }
1201- }
1133+ QString text;
1134+
1135+ for (int n = 1 ; n <= mylist.length (); n++) {
1136+ QString s = mylist[n-1 ];
1137+ int size = s.length ();
1138+
1139+ /* ignore empty last line */
1140+ if (size == 0 && n == mylist.length ()) break ;
1141+
1142+ if (!shiftTab) s.insert (0 , tab); // indent line
1143+ else if (s.startsWith (tab)) s.remove (0 , tabSpaces); // decrease line indent
1144+ else s.replace (QRegExp (" ^ *" ), " " ); // remove leading spaces
1145+
1146+ /* adjust selection */
1147+ if (n == 1 ) {
1148+ curbeg -= size - s.length (); // only first line
1149+ curbeg = std::max (curbeg, cur.selectionStart ()); // avoid underflow
1150+ }
1151+ curend -= size - s.length (); // all but an empty last line
1152+
1153+ /* rebuild block */
12021154 text += s;
1203- if (n+1 < mylist.length ())
1204- text += " \n " ;
1155+ if (n < mylist.length ()) text += QChar::ParagraphSeparator;
12051156 }
12061157 /* insert new block */
12071158 cur.insertText (text);
12081159
1209- cur.setPosition (curbeg,QTextCursor::MoveAnchor);
1210- cur.movePosition (QTextCursor::Right,QTextCursor::MoveAnchor, text.length ());
1211- cur.movePosition (QTextCursor::Left,QTextCursor::KeepAnchor, text.length ());
1212- this ->setTextCursor (cur);
1160+ /* update selection */
1161+ cur.setPosition (curbeg, QTextCursor::MoveAnchor);
1162+ cur.setPosition (curend, QTextCursor::KeepAnchor);
12131163
12141164 /* end single undo/redo operation */
12151165 cur.endEditBlock ();
@@ -1224,15 +1174,14 @@ int Editor::tabBlockShift()
12241174 int cursorPos = cur.position (); /* cursor position within text */
12251175
12261176 if (cursorPos > -1 ) {
1227- if (shift == false ) {
1177+ if (!shiftTab ) {
12281178 for (int n = column % tabSpaces; n < tabSpaces; n++) {
12291179 cur.insertText (" " );
12301180 cursorPos++;
12311181 }
12321182 cur.setPosition (cursorPos,QTextCursor::MoveAnchor); /* reset selection */
12331183 cur.movePosition (QTextCursor::Right,QTextCursor::MoveAnchor, selectLength);
12341184 cur.movePosition (QTextCursor::Left,QTextCursor::KeepAnchor, selectLength);
1235- this ->setTextCursor (cur);
12361185 }
12371186 else if (cur.columnNumber () != 0 ) {
12381187 QString st;
@@ -1250,11 +1199,11 @@ int Editor::tabBlockShift()
12501199 /* reset selection */
12511200 cur.movePosition (QTextCursor::Right,QTextCursor::MoveAnchor, selectLength);
12521201 cur.movePosition (QTextCursor::Left,QTextCursor::KeepAnchor, selectLength);
1253- this ->setTextCursor (cur);
12541202 }
12551203 }
1256- this ->setTextCursor (cur);
12571204 }
1205+ this ->setTextCursor (cur);
1206+
12581207 return 1 ;
12591208}
12601209
0 commit comments