Changeset View
Changeset View
Standalone View
Standalone View
src/ScreenWindow.cpp
Context not available. | |||||
22 | 22 | | |||
---|---|---|---|---|---|
23 | // Konsole | 23 | // Konsole | ||
24 | #include "Screen.h" | 24 | #include "Screen.h" | ||
25 | #include "ExtendedCharTable.h" | ||||
26 | #include <QDebug> | ||||
27 | | ||||
28 | #include <memory> | ||||
25 | 29 | | |||
26 | using namespace Konsole; | 30 | using namespace Konsole; | ||
27 | 31 | | |||
Context not available. | |||||
102 | Screen::fillWithDefaultChar(_windowBuffer + _windowBufferSize - charsToFill, charsToFill); | 106 | Screen::fillWithDefaultChar(_windowBuffer + _windowBufferSize - charsToFill, charsToFill); | ||
103 | } | 107 | } | ||
104 | 108 | | |||
109 | int ScreenWindow::loc(int x, int y) const | ||||
110 | { | ||||
111 | Q_ASSERT(y >= 0 && y < lineCount()); | ||||
112 | Q_ASSERT(x >= 0 && x < columnCount()); | ||||
113 | x = qBound(0, x, columnCount() - 1); | ||||
114 | y = qBound(0, y, lineCount() - 1); | ||||
115 | | ||||
116 | return y * columnCount() + x; | ||||
117 | } | ||||
118 | | ||||
105 | // return the index of the line at the end of this window, or if this window | 119 | // return the index of the line at the end of this window, or if this window | ||
106 | // goes beyond the end of the screen, the index of the line at the end | 120 | // goes beyond the end of the screen, the index of the line at the end | ||
107 | // of the screen. | 121 | // of the screen. | ||
Context not available. | |||||
131 | return _screen->selectedText(options); | 145 | return _screen->selectedText(options); | ||
132 | } | 146 | } | ||
133 | 147 | | |||
148 | QPoint ScreenWindow::findLineStart(const QPoint &pnt) | ||||
149 | { | ||||
150 | QVector<LineProperty> lineProperties = getLineProperties(); | ||||
151 | | ||||
152 | const int visibleScreenLines = lineProperties.size(); | ||||
153 | const int topVisibleLine = currentLine(); | ||||
154 | int line = pnt.y(); | ||||
155 | int lineInHistory= line + topVisibleLine; | ||||
156 | | ||||
157 | | ||||
158 | while (lineInHistory > 0) { | ||||
159 | for (; line > 0; line--, lineInHistory--) { | ||||
160 | // Does previous line wrap around? | ||||
161 | if ((lineProperties[line - 1] & LINE_WRAPPED) == 0) { | ||||
162 | return {0, lineInHistory - topVisibleLine}; | ||||
163 | } | ||||
164 | } | ||||
165 | | ||||
166 | if (lineInHistory < 1) { | ||||
167 | break; | ||||
168 | } | ||||
169 | | ||||
170 | // _lineProperties is only for the visible screen, so grab new data | ||||
171 | int newRegionStart = qMax(0, lineInHistory - visibleScreenLines); | ||||
172 | lineProperties = _screen->getLineProperties(newRegionStart, lineInHistory - 1); | ||||
173 | line = lineInHistory - newRegionStart; | ||||
174 | } | ||||
175 | | ||||
176 | return {0, lineInHistory - topVisibleLine}; | ||||
177 | } | ||||
178 | | ||||
179 | QPoint ScreenWindow::findLineEnd(const QPoint &pnt) | ||||
180 | { | ||||
181 | QVector<LineProperty> lineProperties = getLineProperties(); | ||||
182 | | ||||
183 | const int visibleScreenLines = lineProperties.size(); | ||||
184 | const int topVisibleLine = currentLine(); | ||||
185 | const int maxY = lineCount() - 1; | ||||
186 | int line = pnt.y(); | ||||
187 | int lineInHistory= line + topVisibleLine; | ||||
188 | | ||||
189 | while (lineInHistory < maxY) { | ||||
190 | for (; line < lineProperties.count() && lineInHistory < maxY; line++, lineInHistory++) { | ||||
191 | // Does current line wrap around? | ||||
192 | if ((lineProperties[line] & LINE_WRAPPED) == 0) { | ||||
193 | return {columnCount() - 1, lineInHistory - topVisibleLine}; | ||||
194 | } | ||||
195 | } | ||||
196 | | ||||
197 | line = 0; | ||||
198 | lineProperties = _screen->getLineProperties(lineInHistory, qMin(lineInHistory + visibleScreenLines, maxY)); | ||||
199 | } | ||||
200 | return {columnCount() - 1, lineInHistory - topVisibleLine}; | ||||
201 | } | ||||
202 | | ||||
203 | QPoint ScreenWindow::findWordStart(const QPoint &pnt) | ||||
204 | { | ||||
205 | const int regSize = qMax(windowLines(), 10); | ||||
206 | const int firstVisibleLine = currentLine(); | ||||
207 | | ||||
208 | Character *image = getImage(); | ||||
209 | std::unique_ptr<Character[]> tempImage; | ||||
210 | | ||||
211 | int imgLine = pnt.y(); | ||||
212 | | ||||
213 | if (imgLine < 0 || imgLine >= lineCount()) { | ||||
214 | return pnt; | ||||
215 | } | ||||
216 | | ||||
217 | int x = pnt.x(); | ||||
218 | int y = imgLine + firstVisibleLine; | ||||
219 | int imgLoc = loc(x, imgLine); | ||||
220 | QVector<LineProperty> lineProperties = getLineProperties(); | ||||
221 | const QChar selClass = charClass(image[imgLoc]); | ||||
222 | const int imageSize = regSize * columnCount(); | ||||
223 | | ||||
224 | while (imgLoc >= 0) { | ||||
225 | for (; imgLoc > 0 && imgLine >= 0; imgLoc--, x--) { | ||||
226 | const QChar &curClass = charClass(image[imgLoc - 1]); | ||||
227 | if (curClass != selClass) { | ||||
228 | return {x, y - firstVisibleLine}; | ||||
229 | } | ||||
230 | | ||||
231 | // has previous char on this line | ||||
232 | if (x > 0) { | ||||
233 | continue; | ||||
234 | } | ||||
235 | | ||||
236 | // not the first line in the session | ||||
237 | if ((lineProperties[imgLine - 1] & LINE_WRAPPED) == 0) { | ||||
238 | return {x, y - firstVisibleLine}; | ||||
239 | } | ||||
240 | | ||||
241 | // have continuation on prev line | ||||
242 | x = columnCount(); | ||||
243 | imgLine--; | ||||
244 | y--; | ||||
245 | } | ||||
246 | | ||||
247 | if (y <= 0) { | ||||
248 | return {x, y - firstVisibleLine}; | ||||
249 | } | ||||
250 | | ||||
251 | // Fetch new region | ||||
252 | const int newRegStart = qMax(y - regSize + 1, 0); | ||||
253 | lineProperties = _screen->getLineProperties(newRegStart, y - 1); | ||||
254 | imgLine = y - newRegStart; | ||||
255 | | ||||
256 | if (!tempImage) { | ||||
257 | tempImage.reset(new Character[imageSize]); | ||||
258 | image = tempImage.get(); | ||||
259 | } | ||||
260 | | ||||
261 | _screen->getImage(image, imageSize, newRegStart, y - 1); | ||||
262 | imgLoc = loc(x, imgLine); | ||||
263 | } | ||||
264 | | ||||
265 | return {x, y - firstVisibleLine}; | ||||
266 | | ||||
267 | } | ||||
268 | | ||||
269 | QPoint ScreenWindow::findWordEnd(const QPoint &pnt) | ||||
270 | { | ||||
271 | const int regSize = qMax(windowLines(), 10); | ||||
272 | const int curLine = currentLine(); | ||||
273 | int line = pnt.y(); | ||||
274 | | ||||
275 | // The selection is already scrolled out of view, so assume it is already at a boundary | ||||
276 | if (line < 0 || line >= lineCount()) { | ||||
277 | return pnt; | ||||
278 | } | ||||
279 | | ||||
280 | int x = pnt.x(); | ||||
281 | int y = line + curLine; | ||||
282 | QVector<LineProperty> lineProperties = getLineProperties(); | ||||
283 | Character *image = getImage(); | ||||
284 | std::unique_ptr<Character[]> tempImage; | ||||
285 | | ||||
286 | int imgPos = loc(x, line); | ||||
287 | const QChar selClass = charClass(image[imgPos]); | ||||
288 | QChar curClass; | ||||
289 | QChar nextClass; | ||||
290 | | ||||
291 | const int imageSize = regSize * columnCount(); | ||||
292 | const int maxY = lineCount(); | ||||
293 | const int maxX = columnCount() - 1; | ||||
294 | | ||||
295 | while (x >= 0 && line >= 0) { | ||||
296 | imgPos = loc(x, line); | ||||
297 | | ||||
298 | const int visibleLinesCount = lineProperties.count(); | ||||
299 | bool changedClass = false; | ||||
300 | | ||||
301 | for (;y < maxY && line < visibleLinesCount; imgPos++, x++) { | ||||
302 | curClass = charClass(image[imgPos + 1]); | ||||
303 | nextClass = charClass(image[imgPos + 2]); | ||||
304 | | ||||
305 | changedClass = curClass != selClass && | ||||
306 | // A colon right before whitespace is never part of a word | ||||
307 | !(image[imgPos + 1].character == ':' && nextClass == QLatin1Char(' ')); | ||||
308 | | ||||
309 | if (changedClass) { | ||||
310 | break; | ||||
311 | } | ||||
312 | | ||||
313 | if (x >= maxX) { | ||||
314 | if ((lineProperties[line] & LINE_WRAPPED) == 0) { | ||||
315 | break; | ||||
316 | } | ||||
317 | | ||||
318 | line++; | ||||
319 | y++; | ||||
320 | x = -1; | ||||
321 | } | ||||
322 | } | ||||
323 | | ||||
324 | if (changedClass) { | ||||
325 | break; | ||||
326 | } | ||||
327 | | ||||
328 | if (line < visibleLinesCount && ((lineProperties[line] & LINE_WRAPPED) == 0)) { | ||||
329 | break; | ||||
330 | } | ||||
331 | | ||||
332 | const int newRegEnd = qMin(y + regSize - 1, maxY - 1); | ||||
333 | lineProperties = _screen->getLineProperties(y, newRegEnd); | ||||
334 | if (!tempImage) { | ||||
335 | tempImage.reset(new Character[imageSize]); | ||||
336 | image = tempImage.get(); | ||||
337 | } | ||||
338 | _screen->getImage(tempImage.get(), imageSize, y, newRegEnd); | ||||
339 | | ||||
340 | line = 0; | ||||
341 | } | ||||
342 | | ||||
343 | y -= curLine; | ||||
344 | // In word selection mode don't select @ (64) if at end of word. | ||||
345 | if (((image[imgPos].rendition & RE_EXTENDED_CHAR) == 0) && | ||||
346 | (QChar(image[imgPos].character) == QLatin1Char('@')) && | ||||
347 | (y > pnt.y() || x > pnt.x())) { | ||||
348 | if (x > 0) { | ||||
349 | x--; | ||||
350 | } else { | ||||
351 | y--; | ||||
352 | } | ||||
353 | } | ||||
354 | | ||||
355 | return {x, y}; | ||||
356 | | ||||
357 | } | ||||
358 | | ||||
359 | QChar ScreenWindow::charClass(const Character &ch) const | ||||
360 | { | ||||
361 | if ((ch.rendition & RE_EXTENDED_CHAR) != 0) { | ||||
362 | ushort extendedCharLength = 0; | ||||
363 | const uint* chars = ExtendedCharTable::instance.lookupExtendedChar(ch.character, extendedCharLength); | ||||
364 | if ((chars != nullptr) && extendedCharLength > 0) { | ||||
365 | const QString s = QString::fromUcs4(chars, extendedCharLength); | ||||
366 | if (_wordCharacters.contains(s, Qt::CaseInsensitive)) { | ||||
367 | return QLatin1Char('a'); | ||||
368 | } | ||||
369 | bool letterOrNumber = false; | ||||
370 | for (int i = 0; !letterOrNumber && i < s.size(); ++i) { | ||||
371 | letterOrNumber = s.at(i).isLetterOrNumber(); | ||||
372 | } | ||||
373 | return letterOrNumber ? QLatin1Char('a') : s.at(0); | ||||
374 | } | ||||
375 | return 0; | ||||
376 | } else { | ||||
377 | const QChar qch(ch.character); | ||||
378 | if (qch.isSpace()) { | ||||
379 | return QLatin1Char(' '); | ||||
380 | } | ||||
381 | | ||||
382 | if (qch.isLetterOrNumber() || _wordCharacters.contains(qch, Qt::CaseInsensitive)) { | ||||
383 | return QLatin1Char('a'); | ||||
384 | } | ||||
385 | | ||||
386 | return qch; | ||||
387 | } | ||||
388 | } | ||||
389 | | ||||
390 | void ScreenWindow::setWordCharacters(const QString &wc) | ||||
391 | { | ||||
392 | _wordCharacters = wc; | ||||
393 | } | ||||
394 | | ||||
395 | void ScreenWindow::extendSelection(const QPoint &rawPosition, const ScreenWindow::SelectionMode selectionMode) | ||||
396 | { | ||||
397 | QPoint cursorPosition = rawPosition; | ||||
398 | | ||||
399 | const QRect characterBounds(0, 0, windowColumns(), windowLines()); | ||||
400 | | ||||
401 | int linesBeyondWidget = 0; | ||||
402 | if (!characterBounds.contains(cursorPosition)) { | ||||
403 | cursorPosition.setX(qBound(0, cursorPosition.x(), windowColumns() - 1)); | ||||
404 | cursorPosition.setY(qBound(0, cursorPosition.y(), windowLines() - 1)); | ||||
405 | | ||||
406 | linesBeyondWidget = rawPosition.y() - cursorPosition.y(); | ||||
407 | if (linesBeyondWidget < 0) { | ||||
408 | scrollTo(currentLine() + linesBeyondWidget); | ||||
409 | } else if (linesBeyondWidget > 0) { | ||||
410 | scrollTo(currentLine() + linesBeyondWidget); | ||||
411 | } | ||||
412 | } | ||||
413 | | ||||
414 | QPoint selectionEnd = cursorPosition; | ||||
415 | QPoint selectionStart; | ||||
416 | QPoint initialSelectionBegin = _initialSelectionPoint; | ||||
417 | initialSelectionBegin.ry() -= currentLine(); | ||||
418 | QPoint initialSelectionEnd = _initialSelectionEnd; | ||||
419 | initialSelectionEnd.ry() -= currentLine(); | ||||
420 | QPoint previousSelectionPoint = _currentSelectionPoint; | ||||
421 | previousSelectionPoint.ry() -= currentLine(); | ||||
422 | bool swapping = false; | ||||
423 | | ||||
424 | int offset = 0; | ||||
425 | | ||||
426 | if (selectionMode == SelectionMode::Word) { | ||||
427 | // Extend to word boundaries | ||||
428 | const bool extendingLeft = (selectionEnd.y() < initialSelectionBegin.y() || | ||||
429 | (selectionEnd.y() == initialSelectionBegin.y() && selectionEnd.x() < initialSelectionBegin.x())); | ||||
430 | const bool wasExtendingLeft = (previousSelectionPoint.y() < initialSelectionBegin.y() || | ||||
431 | (previousSelectionPoint.y() == initialSelectionBegin.y() && previousSelectionPoint.x() < initialSelectionBegin.x())); | ||||
432 | swapping = extendingLeft != wasExtendingLeft; | ||||
433 | | ||||
434 | if (extendingLeft) { | ||||
435 | selectionEnd = findWordStart(cursorPosition); | ||||
436 | selectionStart = initialSelectionEnd; | ||||
437 | } else { | ||||
438 | selectionEnd = findWordEnd(cursorPosition); | ||||
439 | selectionStart = initialSelectionBegin; | ||||
440 | } | ||||
441 | | ||||
442 | selectionStart.rx()++; | ||||
443 | } | ||||
444 | | ||||
445 | if (selectionMode == SelectionMode::Line) { | ||||
446 | // Extend to complete line | ||||
447 | const bool extendingUpwards = (selectionEnd.y() < initialSelectionBegin.y()); | ||||
448 | const bool wasExtendingUpwards = (previousSelectionPoint.y() < initialSelectionBegin.y()); | ||||
449 | swapping = extendingUpwards != wasExtendingUpwards; | ||||
450 | if (extendingUpwards) { | ||||
451 | selectionEnd = findLineStart(selectionEnd); | ||||
452 | selectionStart = initialSelectionEnd; | ||||
453 | } else { | ||||
454 | selectionStart = initialSelectionBegin; | ||||
455 | selectionEnd = findLineEnd(selectionEnd); | ||||
456 | } | ||||
457 | | ||||
458 | _tripleSelBegin = selectionStart; | ||||
459 | | ||||
460 | selectionStart.rx()++; | ||||
461 | } | ||||
462 | | ||||
463 | if (selectionMode == SelectionMode::Normal) { | ||||
464 | QChar selClass; | ||||
465 | | ||||
466 | const bool extendingLeft = (selectionEnd.y() < initialSelectionBegin.y() || | ||||
467 | (selectionEnd.y() == initialSelectionBegin.y() && selectionEnd.x() < initialSelectionBegin.x())); | ||||
468 | const bool wasExtendingLeft = (previousSelectionPoint.y() < initialSelectionBegin.y() || | ||||
469 | (previousSelectionPoint.y() == initialSelectionBegin.y() && previousSelectionPoint.x() < initialSelectionBegin.x())); | ||||
470 | swapping = extendingLeft != wasExtendingLeft; | ||||
471 | | ||||
472 | // Find left (left_not_right ? from here : from start) | ||||
473 | const QPoint left = extendingLeft ? selectionEnd : initialSelectionBegin; | ||||
474 | | ||||
475 | Character *image = getImage(); | ||||
476 | // Find left (left_not_right ? from start : from here) | ||||
477 | QPoint right = extendingLeft ? initialSelectionBegin : selectionEnd; | ||||
478 | if (right.x() > 0 && selectionMode != SelectionMode::Column) { | ||||
479 | if (right.x() - 1 < columnCount() && right.y() < windowLines()) { | ||||
480 | selClass = charClass(image[loc(right.x() - 1, right.y())]); | ||||
481 | } | ||||
482 | } | ||||
483 | | ||||
484 | // Pick which is start (ohere) and which is extension (here) | ||||
485 | if (extendingLeft) { | ||||
486 | selectionEnd = left; | ||||
487 | selectionStart = right; | ||||
488 | offset = 0; | ||||
489 | } else { | ||||
490 | selectionEnd = right; | ||||
491 | selectionStart = left; | ||||
492 | offset = -1; | ||||
493 | } | ||||
494 | } | ||||
495 | | ||||
496 | if ((selectionEnd == previousSelectionPoint) && linesBeyondWidget == 0) { | ||||
497 | return; // not moved | ||||
498 | } | ||||
499 | | ||||
500 | if (selectionEnd == selectionStart) { | ||||
501 | return; // It's not left, it's not right. | ||||
502 | } | ||||
503 | | ||||
504 | if (_actSel < 2 || swapping) { | ||||
505 | if (selectionMode == SelectionMode::Column) { | ||||
506 | setSelectionStart(selectionStart.x() , selectionStart.y() , true); | ||||
507 | } else { | ||||
508 | setSelectionStart(selectionStart.x() - 1 - offset , selectionStart.y() , false); | ||||
509 | } | ||||
510 | } | ||||
511 | | ||||
512 | _actSel = 2; // within selection | ||||
513 | _currentSelectionPoint = selectionEnd; | ||||
514 | _currentSelectionPoint.ry() += currentLine(); | ||||
515 | | ||||
516 | if (selectionMode == SelectionMode::Column) { | ||||
517 | setSelectionEnd(selectionEnd.x() , selectionEnd.y()); | ||||
518 | } else { | ||||
519 | setSelectionEnd(selectionEnd.x() + offset , selectionEnd.y()); | ||||
520 | } | ||||
521 | | ||||
522 | } | ||||
523 | | ||||
524 | void ScreenWindow::selectLine(QPoint pos, bool entireLine) | ||||
525 | { | ||||
526 | _initialSelectionPoint = pos; | ||||
527 | | ||||
528 | clearSelection(); | ||||
529 | | ||||
530 | _actSel = 2; // within selection | ||||
531 | | ||||
532 | if (!entireLine) { // Select from cursor to end of line | ||||
533 | _tripleSelBegin = findWordStart(pos); | ||||
534 | setSelectionStart(_tripleSelBegin.x(), | ||||
535 | _tripleSelBegin.y() , false); | ||||
536 | } else { | ||||
537 | _tripleSelBegin = findLineStart(pos); | ||||
538 | setSelectionStart(0 , _tripleSelBegin.y() , false); | ||||
539 | } | ||||
540 | | ||||
541 | _initialSelectionPoint = findLineStart(_initialSelectionPoint); | ||||
542 | _initialSelectionEnd = findLineEnd(pos); | ||||
543 | | ||||
544 | setSelectionEnd(_initialSelectionEnd.x() , _initialSelectionEnd.y()); | ||||
545 | | ||||
546 | _initialSelectionPoint.ry() += currentLine(); | ||||
547 | _initialSelectionEnd.ry() += currentLine(); | ||||
548 | | ||||
549 | } | ||||
550 | | ||||
551 | void ScreenWindow::selectWord(QPoint pos) | ||||
552 | { | ||||
553 | clearSelection(); | ||||
554 | | ||||
555 | _currentSelectionPoint = pos; | ||||
556 | _currentSelectionPoint.ry() += currentLine(); | ||||
557 | | ||||
558 | _actSel = 2; // within selection | ||||
559 | | ||||
560 | // find word boundaries... | ||||
561 | // find the start of the word | ||||
562 | const QPoint bgnSel = findWordStart(pos); | ||||
563 | const QPoint endSel = findWordEnd(pos); | ||||
564 | | ||||
565 | _actSel = 2; // within selection | ||||
566 | | ||||
567 | setSelectionStart(bgnSel.x() , bgnSel.y() , false); | ||||
568 | setSelectionEnd(endSel.x() , endSel.y()); | ||||
569 | | ||||
570 | _initialSelectionPoint = bgnSel; | ||||
571 | _initialSelectionPoint.ry() += currentLine(); | ||||
572 | | ||||
573 | _initialSelectionEnd = endSel; | ||||
574 | _initialSelectionEnd.ry() += currentLine(); | ||||
575 | | ||||
576 | } | ||||
577 | | ||||
134 | void ScreenWindow::getSelectionStart(int &column, int &line) | 578 | void ScreenWindow::getSelectionStart(int &column, int &line) | ||
135 | { | 579 | { | ||
136 | _screen->getSelectionStart(column, line); | 580 | _screen->getSelectionStart(column, line); | ||
Context not available. | |||||
329 | 773 | | |||
330 | emit outputChanged(); | 774 | emit outputChanged(); | ||
331 | } | 775 | } | ||
776 | | ||||
777 | void ScreenWindow::startNormalSelection(QPoint pos) | ||||
778 | { | ||||
779 | clearSelection(); | ||||
780 | _initialSelectionPoint = _currentSelectionPoint = pos; | ||||
781 | _actSel = 1; // left mouse button pressed but nothing selected yet. | ||||
782 | } | ||||
Context not available. |