search.c (195941) | search.c (221715) |
---|---|
1/* $FreeBSD: head/contrib/less/search.c 195941 2009-07-29 09:20:32Z delphij $ */ | 1/* $FreeBSD: head/contrib/less/search.c 221715 2011-05-09 21:51:59Z delphij $ */ |
2/* | 2/* |
3 * Copyright (C) 1984-2009 Mark Nudelman | 3 * Copyright (C) 1984-2011 Mark Nudelman |
4 * 5 * You may distribute under the terms of either the GNU General Public 6 * License or the Less License, as specified in the README file. 7 * 8 * For more information about less, or for information on how to 9 * contact the author, see the README file. 10 */ 11 --- 56 unchanged lines hidden (view full) --- 68 char* text; 69 int search_type; 70}; 71 72static struct pattern_info search_info; 73static struct pattern_info filter_info; 74 75/* | 4 * 5 * You may distribute under the terms of either the GNU General Public 6 * License or the Less License, as specified in the README file. 7 * 8 * For more information about less, or for information on how to 9 * contact the author, see the README file. 10 */ 11 --- 56 unchanged lines hidden (view full) --- 68 char* text; 69 int search_type; 70}; 71 72static struct pattern_info search_info; 73static struct pattern_info filter_info; 74 75/* |
76 * Are there any uppercase letters in this string? 77 */ 78 static int 79is_ucase(str) 80 char *str; 81{ 82 char *str_end = str + strlen(str); 83 LWCHAR ch; 84 85 while (str < str_end) 86 { 87 ch = step_char(&str, +1, str_end); 88 if (IS_UPPER(ch)) 89 return (1); 90 } 91 return (0); 92} 93 94/* |
|
76 * Compile and save a search pattern. 77 */ 78 static int 79set_pattern(info, pattern, search_type) 80 struct pattern_info *info; 81 char *pattern; 82 int search_type; 83{ --- 6 unchanged lines hidden (view full) --- 90 free(info->text); 91 info->text = NULL; 92 if (pattern != NULL) 93 { 94 info->text = (char *) ecalloc(1, strlen(pattern)+1); 95 strcpy(info->text, pattern); 96 } 97 info->search_type = search_type; | 95 * Compile and save a search pattern. 96 */ 97 static int 98set_pattern(info, pattern, search_type) 99 struct pattern_info *info; 100 char *pattern; 101 int search_type; 102{ --- 6 unchanged lines hidden (view full) --- 109 free(info->text); 110 info->text = NULL; 111 if (pattern != NULL) 112 { 113 info->text = (char *) ecalloc(1, strlen(pattern)+1); 114 strcpy(info->text, pattern); 115 } 116 info->search_type = search_type; |
117 118 /* 119 * Ignore case if -I is set OR 120 * -i is set AND the pattern is all lowercase. 121 */ 122 is_ucase_pattern = is_ucase(pattern); 123 if (is_ucase_pattern && caseless != OPT_ONPLUS) 124 is_caseless = 0; 125 else 126 is_caseless = caseless; |
|
98 return 0; 99} 100 101/* 102 * Discard a saved pattern. 103 */ 104 static void 105clear_pattern(info) --- 47 unchanged lines hidden (view full) --- 153 ops |= CVT_CRLF; 154 } 155 if (ctldisp == OPT_ONPLUS) 156 ops |= CVT_ANSI; 157 return (ops); 158} 159 160/* | 127 return 0; 128} 129 130/* 131 * Discard a saved pattern. 132 */ 133 static void 134clear_pattern(info) --- 47 unchanged lines hidden (view full) --- 182 ops |= CVT_CRLF; 183 } 184 if (ctldisp == OPT_ONPLUS) 185 ops |= CVT_ANSI; 186 return (ops); 187} 188 189/* |
161 * Are there any uppercase letters in this string? 162 */ 163 static int 164is_ucase(str) 165 char *str; 166{ 167 char *str_end = str + strlen(str); 168 LWCHAR ch; 169 170 while (str < str_end) 171 { 172 ch = step_char(&str, +1, str_end); 173 if (IS_UPPER(ch)) 174 return (1); 175 } 176 return (0); 177} 178 179/* | |
180 * Is there a previous (remembered) search pattern? 181 */ 182 static int 183prev_pattern(info) 184 struct pattern_info *info; 185{ 186 if (info->search_type & SRCH_NO_REGEX) 187 return (info->text != NULL); --- 38 unchanged lines hidden (view full) --- 226 pos = position(slinenum); 227 if (pos == NULL_POSITION) 228 continue; 229 epos = position(slinenum+1); 230 (void) forw_line(pos); 231 goto_line(slinenum); 232 put_line(); 233 } | 190 * Is there a previous (remembered) search pattern? 191 */ 192 static int 193prev_pattern(info) 194 struct pattern_info *info; 195{ 196 if (info->search_type & SRCH_NO_REGEX) 197 return (info->text != NULL); --- 38 unchanged lines hidden (view full) --- 236 pos = position(slinenum); 237 if (pos == NULL_POSITION) 238 continue; 239 epos = position(slinenum+1); 240 (void) forw_line(pos); 241 goto_line(slinenum); 242 put_line(); 243 } |
234 lower_left(); // if !oldbot | 244 lower_left(); |
235 hide_hilite = save_hide_hilite; 236} 237 238/* 239 * Clear the attn hilite. 240 */ 241 public void 242clear_attn() --- 353 unchanged lines hidden (view full) --- 596 * Start at the beginning (or end) of the file. 597 * The empty_screen() case is mainly for 598 * command line initiated searches; 599 * for example, "+/xyz" on the command line. 600 * Also for multi-file (SRCH_PAST_EOF) searches. 601 */ 602 if (search_type & SRCH_FORW) 603 { | 245 hide_hilite = save_hide_hilite; 246} 247 248/* 249 * Clear the attn hilite. 250 */ 251 public void 252clear_attn() --- 353 unchanged lines hidden (view full) --- 606 * Start at the beginning (or end) of the file. 607 * The empty_screen() case is mainly for 608 * command line initiated searches; 609 * for example, "+/xyz" on the command line. 610 * Also for multi-file (SRCH_PAST_EOF) searches. 611 */ 612 if (search_type & SRCH_FORW) 613 { |
604 return (ch_zero()); | 614 pos = ch_zero(); |
605 } else 606 { 607 pos = ch_length(); 608 if (pos == NULL_POSITION) 609 { 610 (void) ch_end_seek(); 611 pos = ch_length(); 612 } | 615 } else 616 { 617 pos = ch_length(); 618 if (pos == NULL_POSITION) 619 { 620 (void) ch_end_seek(); 621 pos = ch_length(); 622 } |
613 return (pos); | |
614 } | 623 } |
615 } 616 if (how_search) | 624 linenum = 0; 625 } else |
617 { | 626 { |
618 /* 619 * Search does not include current screen. 620 */ 621 if (search_type & SRCH_FORW) 622 linenum = BOTTOM_PLUS_ONE; 623 else 624 linenum = TOP; 625 pos = position(linenum); 626 } else 627 { 628 /* 629 * Search includes current screen. 630 * It starts at the jump target (if searching backwards), 631 * or at the jump target plus one (if forwards). 632 */ 633 linenum = adjsline(jump_sline); 634 pos = position(linenum); 635 if (search_type & SRCH_FORW) | 627 int add_one = 0; 628 629 if (how_search == OPT_ON) |
636 { | 630 { |
637 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL); 638 while (pos == NULL_POSITION) 639 { 640 if (++linenum >= sc_height) 641 break; 642 pos = position(linenum); 643 } | 631 /* 632 * Search does not include current screen. 633 */ 634 if (search_type & SRCH_FORW) 635 linenum = BOTTOM_PLUS_ONE; 636 else 637 linenum = TOP; 638 } else if (how_search == OPT_ONPLUS && !(search_type & SRCH_AFTER_TARGET)) 639 { 640 /* 641 * Search includes all of displayed screen. 642 */ 643 if (search_type & SRCH_FORW) 644 linenum = TOP; 645 else 646 linenum = BOTTOM_PLUS_ONE; |
644 } else 645 { | 647 } else 648 { |
646 while (pos == NULL_POSITION) 647 { 648 if (--linenum < 0) 649 break; 650 pos = position(linenum); 651 } | 649 /* 650 * Search includes the part of current screen beyond the jump target. 651 * It starts at the jump target (if searching backwards), 652 * or at the jump target plus one (if forwards). 653 */ 654 linenum = jump_sline; 655 if (search_type & SRCH_FORW) 656 add_one = 1; |
652 } | 657 } |
658 linenum = adjsline(linenum); 659 pos = position(linenum); 660 if (add_one) 661 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL); |
|
653 } | 662 } |
663 664 /* 665 * If the line is empty, look around for a plausible starting place. 666 */ 667 if (search_type & SRCH_FORW) 668 { 669 while (pos == NULL_POSITION) 670 { 671 if (++linenum >= sc_height) 672 break; 673 pos = position(linenum); 674 } 675 } else 676 { 677 while (pos == NULL_POSITION) 678 { 679 if (--linenum < 0) 680 break; 681 pos = position(linenum); 682 } 683 } |
|
654 return (pos); 655} 656 657/* 658 * Search a subset of the file, specified by start/end position. 659 */ 660 static int 661search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos) --- 124 unchanged lines hidden (view full) --- 786 /* 787 * Test the next line to see if we have a match. 788 * We are successful if we either want a match and got one, 789 * or if we want a non-match and got one. 790 */ 791 if (prev_pattern(&search_info)) 792 { 793 line_match = match_pattern(search_info.compiled, search_info.text, | 684 return (pos); 685} 686 687/* 688 * Search a subset of the file, specified by start/end position. 689 */ 690 static int 691search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos) --- 124 unchanged lines hidden (view full) --- 816 /* 817 * Test the next line to see if we have a match. 818 * We are successful if we either want a match and got one, 819 * or if we want a non-match and got one. 820 */ 821 if (prev_pattern(&search_info)) 822 { 823 line_match = match_pattern(search_info.compiled, search_info.text, |
794 cline, line_len, &sp, &ep, 0, search_type); //FIXME search_info.search_type | 824 cline, line_len, &sp, &ep, 0, search_type); |
795 if (line_match) 796 { 797 /* 798 * Got a match. 799 */ 800 if (search_type & SRCH_FIND_ALL) 801 { 802#if HILITE_SEARCH --- 47 unchanged lines hidden (view full) --- 850 set_mlist(ml_search, 0); 851 pattern = cmd_lastpattern(); 852 if (pattern == NULL) 853 return (0); 854 855 if (set_pattern(&search_info, pattern, search_type) < 0) 856 return (0); 857 | 825 if (line_match) 826 { 827 /* 828 * Got a match. 829 */ 830 if (search_type & SRCH_FIND_ALL) 831 { 832#if HILITE_SEARCH --- 47 unchanged lines hidden (view full) --- 880 set_mlist(ml_search, 0); 881 pattern = cmd_lastpattern(); 882 if (pattern == NULL) 883 return (0); 884 885 if (set_pattern(&search_info, pattern, search_type) < 0) 886 return (0); 887 |
858 is_ucase_pattern = is_ucase(pattern); 859 if (is_ucase_pattern && caseless != OPT_ONPLUS) 860 is_caseless = 0; 861 else 862 is_caseless = caseless; 863 | |
864#if HILITE_SEARCH 865 if (hilite_search == OPT_ONPLUS && !hide_hilite) 866 hilite_screen(); 867#endif 868 869 return (1); 870#else /* CMD_HISTORY */ 871 return (0); --- 17 unchanged lines hidden (view full) --- 889{ 890 POSITION pos; 891 892 if (pattern == NULL || *pattern == '\0') 893 { 894 /* 895 * A null pattern means use the previously compiled pattern. 896 */ | 888#if HILITE_SEARCH 889 if (hilite_search == OPT_ONPLUS && !hide_hilite) 890 hilite_screen(); 891#endif 892 893 return (1); 894#else /* CMD_HISTORY */ 895 return (0); --- 17 unchanged lines hidden (view full) --- 913{ 914 POSITION pos; 915 916 if (pattern == NULL || *pattern == '\0') 917 { 918 /* 919 * A null pattern means use the previously compiled pattern. 920 */ |
921 search_type |= SRCH_AFTER_TARGET; |
|
897 if (!prev_pattern(&search_info) && !hist_pattern(search_type)) 898 { 899 error("No previous regular expression", NULL_PARG); 900 return (-1); 901 } 902 if ((search_type & SRCH_NO_REGEX) != 903 (search_info.search_type & SRCH_NO_REGEX)) 904 { --- 22 unchanged lines hidden (view full) --- 927#endif 928 } else 929 { 930 /* 931 * Compile the pattern. 932 */ 933 if (set_pattern(&search_info, pattern, search_type) < 0) 934 return (-1); | 922 if (!prev_pattern(&search_info) && !hist_pattern(search_type)) 923 { 924 error("No previous regular expression", NULL_PARG); 925 return (-1); 926 } 927 if ((search_type & SRCH_NO_REGEX) != 928 (search_info.search_type & SRCH_NO_REGEX)) 929 { --- 22 unchanged lines hidden (view full) --- 952#endif 953 } else 954 { 955 /* 956 * Compile the pattern. 957 */ 958 if (set_pattern(&search_info, pattern, search_type) < 0) 959 return (-1); |
935 /* 936 * Ignore case if -I is set OR 937 * -i is set AND the pattern is all lowercase. 938 */ 939 is_ucase_pattern = is_ucase(pattern); 940 if (is_ucase_pattern && caseless != OPT_ONPLUS) 941 is_caseless = 0; 942 else 943 is_caseless = caseless; | |
944#if HILITE_SEARCH 945 if (hilite_search) 946 { 947 /* 948 * Erase the highlights currently on screen. 949 * Also permanently delete them from the hilite list. 950 */ 951 repaint_hilite(0); --- 246 unchanged lines hidden --- | 960#if HILITE_SEARCH 961 if (hilite_search) 962 { 963 /* 964 * Erase the highlights currently on screen. 965 * Also permanently delete them from the hilite list. 966 */ 967 repaint_hilite(0); --- 246 unchanged lines hidden --- |