1/* Part of Refactoring Tools for SWI-Prolog 2 3 Author: Edison Mera 4 E-mail: efmera@gmail.com 5 WWW: https://github.com/edisonm/refactor 6 Copyright (C): 2013, Process Design Center, Breda, The Netherlands. 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module(seek_text, 36 [ comment_bound/2, 37 comment_bound/3, 38 seek1_char_left/4, 39 seekn_char_right/6, 40 seek_sub_string/6 41 ]). 42 43:- use_module(library(lists)). 44:- use_module(library(ref_context)). 45 46comment_bound(CommentL, From, To) :- 47 member(Pos-Text, CommentL), 48 stream_position_data(char_count, Pos, From), 49 string_length(Text, Length), 50 nl_mark_end(Text, Delta), 51 To is Length + Delta + From. 52 53nl_mark_end(_, 0 ). 54 55/* 56nl_mark_end(Text, Delta) :- 57 ( string_code(1, Text, 0'%) 58 ->Delta = 1 59 ; Delta = 0 60 ). 61*/ 62 63comment_bound(From, To) :- 64 refactor_context(comments, CommentL), 65 comment_bound(CommentL, From, To). 66 67comment_bound(From1, To1, From, To) :- 68 comment_bound(FromC, ToC), 69 ( FromC < To1 70 ->true 71 ; FromC = To1 72 ->! 73 ; !, 74 fail 75 ), 76 ( ToC < To1 77 ->To = ToC 78 ; !, 79 To = To1 80 ), 81 ( From1 =< FromC 82 ->From = FromC 83 ; From1 =< ToC 84 ->From = From1 85 ). 86 87seek_sub_string(Text, SubText, SubTextN, F, T1, T) :- 88 S = s(T1), 89 ( comment_bound(T1, F, FC, TC) 90 ; FC = F, 91 TC = F 92 ), 93 arg(1, S, T2), 94 ( D1 is FC - T2, 95 D1 > 0, 96 sub_string(Text, T2, D1, _, Frame), 97 sub_string(Frame, D, SubTextN, _, SubText), 98 T is T2 + D 99 ; nb_setarg(1, S, TC), 100 fail 101 ). 102 103seek1_char_left(Text, Char, F1, F) :- 104 succ(F2, F1), 105 ( sub_string(Text, F2, _, _, Char) 106 ->F = F2 107 ; seek1_char_left(Text, Char, F2, F) 108 ). 109 110seekn_char_right(0, _, _, _, T, T) :- !. 111seekn_char_right(N, Text, L, Char, T1, T) :- 112 S = s(0), 113 ( seek_sub_string(Text, Char, 1, L, T1, T2), 114 arg(1, S, N1), 115 succ(N1, N2), 116 ( N2 = N 117 ->!, 118 succ(T2, T) 119 ; nb_setarg(1, S, N2), 120 fail 121 ) 122 )