1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
/*
* This file is part of GtkSourceView
*
* Copyright (C) 2014, 2016 - Sébastien Wilmet <swilmet@gnome.org>
*
* GtkSourceView is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* GtkSourceView is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gtksourceiter.h"
/* GtkTextIter functions. Contains forward/backward functions for word
* movements, with custom word boundaries that are used for word selection
* (double-click) and cursor movements (Ctrl+left, Ctrl+right, etc). The
* initial idea was to use those word boundaries directly in GTK+, for all text
* widgets. But in the end only the GtkTextView::extend-selection signal has
* been added to be able to customize the boundaries for double- and
* triple-click (the ::move-cursor and ::delete-from-cursor signals were already
* present to customize boundaries for cursor movements). The GTK+ developers
* didn't want to change the word boundaries for text widgets. More information:
* https://mail.gnome.org/archives/gtk-devel-list/2014-September/msg00019.html
* https://bugzilla.gnome.org/show_bug.cgi?id=111503
*/
/* TODO: the following Pango bug is now mostly done, see if the code here can be
* simplified.
* https://bugzilla.gnome.org/show_bug.cgi?id=97545
* "Make pango_default_break follow Unicode TR #29"
*/
/* Go to the end of the next or current "full word". A full word is a group of
* non-blank chars.
* In other words, this function is the same as the 'E' Vim command.
*
* Examples ('|' is the iter position):
* "|---- abcd" -> "----| abcd"
* "| ---- abcd" -> " ----| abcd"
* "--|-- abcd" -> "----| abcd"
* "---- a|bcd" -> "---- abcd|"
*/
void
_gtk_source_iter_forward_full_word_end (GtkTextIter *iter)
{
GtkTextIter pos;
gboolean non_blank_found = FALSE;
/* It would be better to use gtk_text_iter_forward_visible_char(), but
* it doesn't exist. So move by cursor position instead, it should be
* equivalent here.
*/
pos = *iter;
while (g_unichar_isspace (gtk_text_iter_get_char (&pos)))
{
gtk_text_iter_forward_visible_cursor_position (&pos);
}
while (!gtk_text_iter_is_end (&pos) &&
!g_unichar_isspace (gtk_text_iter_get_char (&pos)))
{
non_blank_found = TRUE;
gtk_text_iter_forward_visible_cursor_position (&pos);
}
if (non_blank_found)
{
*iter = pos;
}
}
/* Symmetric of iter_forward_full_word_end(). */
void
_gtk_source_iter_backward_full_word_start (GtkTextIter *iter)
{
GtkTextIter pos;
GtkTextIter prev;
gboolean non_blank_found = FALSE;
pos = *iter;
while (!gtk_text_iter_is_start (&pos))
{
prev = pos;
gtk_text_iter_backward_visible_cursor_position (&prev);
if (!g_unichar_isspace (gtk_text_iter_get_char (&prev)))
{
break;
}
pos = prev;
}
while (!gtk_text_iter_is_start (&pos))
{
prev = pos;
gtk_text_iter_backward_visible_cursor_position (&prev);
if (g_unichar_isspace (gtk_text_iter_get_char (&prev)))
{
break;
}
non_blank_found = TRUE;
pos = prev;
}
if (non_blank_found)
{
*iter = pos;
}
}
gboolean
_gtk_source_iter_starts_full_word (const GtkTextIter *iter)
{
GtkTextIter prev = *iter;
if (gtk_text_iter_is_end (iter))
{
return FALSE;
}
if (!gtk_text_iter_backward_visible_cursor_position (&prev))
{
return !g_unichar_isspace (gtk_text_iter_get_char (iter));
}
return (g_unichar_isspace (gtk_text_iter_get_char (&prev)) &&
!g_unichar_isspace (gtk_text_iter_get_char (iter)));
}
gboolean
_gtk_source_iter_ends_full_word (const GtkTextIter *iter)
{
GtkTextIter prev = *iter;
if (!gtk_text_iter_backward_visible_cursor_position (&prev))
{
return FALSE;
}
return (!g_unichar_isspace (gtk_text_iter_get_char (&prev)) &&
(gtk_text_iter_is_end (iter) ||
g_unichar_isspace (gtk_text_iter_get_char (iter))));
}
/* Extends the definition of a natural-language word used by Pango. The
* underscore is added to the possible characters of a natural-language word.
*/
void
_gtk_source_iter_forward_extra_natural_word_end (GtkTextIter *iter)
{
GtkTextIter next_word_end = *iter;
GtkTextIter next_underscore_end = *iter;
GtkTextIter *limit = NULL;
gboolean found;
if (gtk_text_iter_forward_visible_word_end (&next_word_end))
{
limit = &next_word_end;
}
found = gtk_text_iter_forward_search (iter,
"_",
GTK_TEXT_SEARCH_VISIBLE_ONLY | GTK_TEXT_SEARCH_TEXT_ONLY,
NULL,
&next_underscore_end,
limit);
if (found)
{
*iter = next_underscore_end;
}
else
{
*iter = next_word_end;
}
while (TRUE)
{
if (gtk_text_iter_get_char (iter) == '_')
{
gtk_text_iter_forward_visible_cursor_position (iter);
}
else if (gtk_text_iter_starts_word (iter))
{
gtk_text_iter_forward_visible_word_end (iter);
}
else
{
break;
}
}
}
/* Symmetric of iter_forward_extra_natural_word_end(). */
void
_gtk_source_iter_backward_extra_natural_word_start (GtkTextIter *iter)
{
GtkTextIter prev_word_start = *iter;
GtkTextIter prev_underscore_start = *iter;
GtkTextIter *limit = NULL;
gboolean found;
if (gtk_text_iter_backward_visible_word_start (&prev_word_start))
{
limit = &prev_word_start;
}
found = gtk_text_iter_backward_search (iter,
"_",
GTK_TEXT_SEARCH_VISIBLE_ONLY | GTK_TEXT_SEARCH_TEXT_ONLY,
&prev_underscore_start,
NULL,
limit);
if (found)
{
*iter = prev_underscore_start;
}
else
{
*iter = prev_word_start;
}
while (!gtk_text_iter_is_start (iter))
{
GtkTextIter prev = *iter;
gtk_text_iter_backward_visible_cursor_position (&prev);
if (gtk_text_iter_get_char (&prev) == '_')
{
*iter = prev;
}
else if (gtk_text_iter_ends_word (iter))
{
gtk_text_iter_backward_visible_word_start (iter);
}
else
{
break;
}
}
}
static gboolean
backward_cursor_position (GtkTextIter *iter,
gboolean visible)
{
if (visible)
{
return gtk_text_iter_backward_visible_cursor_position (iter);
}
return gtk_text_iter_backward_cursor_position (iter);
}
gboolean
_gtk_source_iter_starts_extra_natural_word (const GtkTextIter *iter,
gboolean visible)
{
gboolean starts_word;
GtkTextIter prev;
starts_word = gtk_text_iter_starts_word (iter);
prev = *iter;
if (!backward_cursor_position (&prev, visible))
{
return starts_word || gtk_text_iter_get_char (iter) == '_';
}
if (starts_word)
{
return gtk_text_iter_get_char (&prev) != '_';
}
return (gtk_text_iter_get_char (iter) == '_' &&
gtk_text_iter_get_char (&prev) != '_' &&
!gtk_text_iter_ends_word (iter));
}
gboolean
_gtk_source_iter_ends_extra_natural_word (const GtkTextIter *iter,
gboolean visible)
{
GtkTextIter prev;
gboolean ends_word;
prev = *iter;
if (!backward_cursor_position (&prev, visible))
{
return FALSE;
}
ends_word = gtk_text_iter_ends_word (iter);
if (gtk_text_iter_is_end (iter))
{
return ends_word || gtk_text_iter_get_char (&prev) == '_';
}
if (ends_word)
{
return gtk_text_iter_get_char (iter) != '_';
}
return (gtk_text_iter_get_char (&prev) == '_' &&
gtk_text_iter_get_char (iter) != '_' &&
!gtk_text_iter_starts_word (iter));
}
/* Similar to gtk_text_iter_forward_visible_word_end, but with a custom
* definition of "word".
*
* It is normally the same word boundaries as in Vim. This function is the same
* as the 'e' command.
*
* With the custom word definition, a word can be:
* - a natural-language word as defined by Pango, plus the underscore. The
* underscore is added because it is often used in programming languages.
* - a group of contiguous non-blank characters.
*/
gboolean
_gtk_source_iter_forward_visible_word_end (GtkTextIter *iter)
{
GtkTextIter orig = *iter;
GtkTextIter farthest = *iter;
GtkTextIter next_word_end = *iter;
GtkTextIter word_start;
/* 'farthest' is the farthest position that this function can return. Example:
* "|---- aaaa" -> "----| aaaa"
*/
_gtk_source_iter_forward_full_word_end (&farthest);
/* Go to the next extra-natural word end. It can be farther than
* 'farthest':
* "|---- aaaa" -> "---- aaaa|"
*
* Or it can remain at the same place:
* "aaaa| ----" -> "aaaa| ----"
*/
_gtk_source_iter_forward_extra_natural_word_end (&next_word_end);
if (gtk_text_iter_compare (&farthest, &next_word_end) < 0 ||
gtk_text_iter_equal (iter, &next_word_end))
{
*iter = farthest;
goto end;
}
/* From 'next_word_end', go to the previous extra-natural word start.
*
* Example 1:
* iter: "ab|cd"
* next_word_end: "abcd|" -> the good one
* word_start: "|abcd"
*
* Example 2:
* iter: "| abcd()"
* next_word_end: " abcd|()" -> the good one
* word_start: " |abcd()"
*
* Example 3:
* iter: "abcd|()efgh"
* next_word_end: "abcd()efgh|"
* word_start: "abcd()|efgh" -> the good one, at the end of the word "()".
*/
word_start = next_word_end;
_gtk_source_iter_backward_extra_natural_word_start (&word_start);
/* Example 1 */
if (gtk_text_iter_compare (&word_start, iter) <= 0)
{
*iter = next_word_end;
}
/* Example 2 */
else if (_gtk_source_iter_starts_full_word (&word_start))
{
*iter = next_word_end;
}
/* Example 3 */
else
{
*iter = word_start;
}
end:
return !gtk_text_iter_equal (&orig, iter) && !gtk_text_iter_is_end (iter);
}
/* Symmetric of _gtk_source_iter_forward_visible_word_end(). */
gboolean
_gtk_source_iter_backward_visible_word_start (GtkTextIter *iter)
{
GtkTextIter orig = *iter;
GtkTextIter farthest = *iter;
GtkTextIter prev_word_start = *iter;
GtkTextIter word_end;
/* 'farthest' is the farthest position that this function can return. Example:
* "aaaa ----|" -> "aaaa |----"
*/
_gtk_source_iter_backward_full_word_start (&farthest);
/* Go to the previous extra-natural word start. It can be farther than
* 'farthest':
* "aaaa ----|" -> "|aaaa ----"
*
* Or it can remain at the same place:
* "---- |aaaa" -> "---- |aaaa"
*/
_gtk_source_iter_backward_extra_natural_word_start (&prev_word_start);
if (gtk_text_iter_compare (&prev_word_start, &farthest) < 0 ||
gtk_text_iter_equal (iter, &prev_word_start))
{
*iter = farthest;
goto end;
}
/* From 'prev_word_start', go to the next extra-natural word end.
*
* Example 1:
* iter: "ab|cd"
* prev_word_start: "|abcd" -> the good one
* word_end: "abcd|"
*
* Example 2:
* iter: "()abcd |"
* prev_word_start: "()|abcd " -> the good one
* word_end: "()abcd| "
*
* Example 3:
* iter: "abcd()|"
* prev_word_start: "|abcd()"
* word_end: "abcd|()" -> the good one, at the start of the word "()".
*/
word_end = prev_word_start;
_gtk_source_iter_forward_extra_natural_word_end (&word_end);
/* Example 1 */
if (gtk_text_iter_compare (iter, &word_end) <= 0)
{
*iter = prev_word_start;
}
/* Example 2 */
else if (_gtk_source_iter_ends_full_word (&word_end))
{
*iter = prev_word_start;
}
/* Example 3 */
else
{
*iter = word_end;
}
end:
return !gtk_text_iter_equal (&orig, iter) && !gtk_text_iter_is_end (iter);
}
/* Similar to gtk_text_iter_forward_visible_word_ends(). */
gboolean
_gtk_source_iter_forward_visible_word_ends (GtkTextIter *iter,
gint count)
{
GtkTextIter orig = *iter;
gint i;
if (count < 0)
{
return _gtk_source_iter_backward_visible_word_starts (iter, -count);
}
for (i = 0; i < count; i++)
{
if (!_gtk_source_iter_forward_visible_word_end (iter))
{
break;
}
}
return !gtk_text_iter_equal (&orig, iter) && !gtk_text_iter_is_end (iter);
}
/* Similar to gtk_text_iter_backward_visible_word_starts(). */
gboolean
_gtk_source_iter_backward_visible_word_starts (GtkTextIter *iter,
gint count)
{
GtkTextIter orig = *iter;
gint i;
if (count < 0)
{
return _gtk_source_iter_forward_visible_word_ends (iter, -count);
}
for (i = 0; i < count; i++)
{
if (!_gtk_source_iter_backward_visible_word_start (iter))
{
break;
}
}
return !gtk_text_iter_equal (&orig, iter) && !gtk_text_iter_is_end (iter);
}
gboolean
_gtk_source_iter_starts_word (const GtkTextIter *iter)
{
if (_gtk_source_iter_starts_full_word (iter) ||
_gtk_source_iter_starts_extra_natural_word (iter, TRUE))
{
return TRUE;
}
/* Example: "abcd|()", at the start of the word "()". */
return (!_gtk_source_iter_ends_full_word (iter) &&
_gtk_source_iter_ends_extra_natural_word (iter, TRUE));
}
gboolean
_gtk_source_iter_ends_word (const GtkTextIter *iter)
{
if (_gtk_source_iter_ends_full_word (iter) ||
_gtk_source_iter_ends_extra_natural_word (iter, TRUE))
{
return TRUE;
}
/* Example: "abcd()|efgh", at the end of the word "()". */
return (!_gtk_source_iter_starts_full_word (iter) &&
_gtk_source_iter_starts_extra_natural_word (iter, TRUE));
}
gboolean
_gtk_source_iter_inside_word (const GtkTextIter *iter)
{
GtkTextIter prev_word_start;
GtkTextIter word_end;
if (_gtk_source_iter_starts_word (iter))
{
return TRUE;
}
prev_word_start = *iter;
if (!_gtk_source_iter_backward_visible_word_start (&prev_word_start))
{
return FALSE;
}
word_end = prev_word_start;
_gtk_source_iter_forward_visible_word_end (&word_end);
return (gtk_text_iter_compare (&prev_word_start, iter) <= 0 &&
gtk_text_iter_compare (iter, &word_end) < 0);
}
/* Used for the GtkTextView::extend-selection signal. */
void
_gtk_source_iter_extend_selection_word (const GtkTextIter *location,
GtkTextIter *start,
GtkTextIter *end)
{
/* Exactly the same algorithm as in GTK+, but with our custom word
* boundaries.
*/
*start = *location;
*end = *location;
if (_gtk_source_iter_inside_word (start))
{
if (!_gtk_source_iter_starts_word (start))
{
_gtk_source_iter_backward_visible_word_start (start);
}
if (!_gtk_source_iter_ends_word (end))
{
_gtk_source_iter_forward_visible_word_end (end);
}
}
else
{
GtkTextIter tmp;
tmp = *start;
if (_gtk_source_iter_backward_visible_word_start (&tmp))
{
_gtk_source_iter_forward_visible_word_end (&tmp);
}
if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
{
*start = tmp;
}
else
{
gtk_text_iter_set_line_offset (start, 0);
}
tmp = *end;
if (!_gtk_source_iter_forward_visible_word_end (&tmp))
{
gtk_text_iter_forward_to_end (&tmp);
}
if (_gtk_source_iter_ends_word (&tmp))
{
_gtk_source_iter_backward_visible_word_start (&tmp);
}
if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
{
*end = tmp;
}
else
{
gtk_text_iter_forward_to_line_end (end);
}
}
}
/* Get the boundary, on @iter's line, between leading spaces (indentation) and
* the text.
*/
void
_gtk_source_iter_get_leading_spaces_end_boundary (const GtkTextIter *iter,
GtkTextIter *leading_end)
{
g_return_if_fail (iter != NULL);
g_return_if_fail (leading_end != NULL);
*leading_end = *iter;
gtk_text_iter_set_line_offset (leading_end, 0);
while (!gtk_text_iter_ends_line (leading_end))
{
gunichar ch = gtk_text_iter_get_char (leading_end);
if (!g_unichar_isspace (ch))
{
break;
}
gtk_text_iter_forward_char (leading_end);
}
}
/* Get the boundary, on @iter's line, between the end of the text and trailing
* spaces.
*/
void
_gtk_source_iter_get_trailing_spaces_start_boundary (const GtkTextIter *iter,
GtkTextIter *trailing_start)
{
g_return_if_fail (iter != NULL);
g_return_if_fail (trailing_start != NULL);
*trailing_start = *iter;
if (!gtk_text_iter_ends_line (trailing_start))
{
gtk_text_iter_forward_to_line_end (trailing_start);
}
while (!gtk_text_iter_starts_line (trailing_start))
{
GtkTextIter prev;
gunichar ch;
prev = *trailing_start;
gtk_text_iter_backward_char (&prev);
ch = gtk_text_iter_get_char (&prev);
if (!g_unichar_isspace (ch))
{
break;
}
*trailing_start = prev;
}
}
|