TSE: LFind(): Search: Option: '+'

14 views
Skip to first unread message

knud van eeden

unread,
Aug 8, 2022, 9:22:31 PM8/8/22
to SemWare TSE Pro Text Editor
Hello,

I noticed that adding the '+' to the search option
e.g. when searching in a block will not find the 
first occurrence.

This because it forces the search to always start from the next character.

Thus if you move the cursor to the begin of the file
and the begin of the line, then it will first move the
cursor to the next character and only then start the search.

So it will not find the first occurrence, but the second
occurrence thus.

This might be an issue when you e.g. search in locally in
a block instead of a global search.

-------------------------
//test
//test

PROC Main()
 BegFile()
 BegLine()
 LFind( "//test", "+" )
END
-------------------------

If you run the program you will see that the cursor
stops at the 2nd occurrence of '//test' and not at
the first occurrence.

The '+' is thus a 'pre-operator', it acts first on the search position and only after that it starts the search.

So my choice would be to use e.g. the 'NextChar() at the end of the search to avoid re-finding the same string again and again, instead of using the '+' because that might lead to not finding all occurrences.

with friendly greetings
Knud van Eeden

zhong zhao

unread,
Aug 9, 2022, 1:22:23 AM8/9/22
to SemWare TSE Pro text editor
I use 'ag+','g+'  or 'lg+' to avoid not find the first occurrence in all opened file,current file,current marked block.

knud van eeden

unread,
Aug 9, 2022, 3:34:08 AM8/9/22
to TSE Pro Support
Yes, but if you use lg+ in a WHILE loop in a block then you get an infinite loop because at each loop it starts from the beginning of the block again because of that g. So in such very frequently occurring cases you must use l+ or l.
But if you use ll+ then it will not find the FIRST occurrence in that block and that is of course wrong and unexpected. So what is left is using l and then e.g. NextChar() arfter it to post move the cursor to avoid repeating the same search.

So including g is in general not the solution for blocks.

Sent from Yahoo Mail on Samsung Galaxy Note 9 black 512 gigabytes

--

---
You received this message because you are subscribed to the Google Groups "SemWare TSE Pro text editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to semware+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/semware/65d8dc97-f155-400f-aae9-cf8542bb562fn%40googlegroups.com.

zhong zhao

unread,
Aug 9, 2022, 10:40:24 PM8/9/22
to SemWare TSE Pro text editor
To avoid  an infinite loop,I save the position at the beginning of the search loop,then check the position of every lFind(),if it is same as beginning position ,exit loop.

knud van eeden

unread,
Aug 10, 2022, 5:27:50 AM8/10/22
to SemWare TSE Pro text editor
----------------------------------------------------------
//test
//test

PROC Main()
 IF ( NOT ( IsBlockInCurrFile() ) ) Warn( "Please mark a block" ) RETURN() ENDIF
 GotoBlockBegin()
 BegLine()
 WHILE LFind( "//test", "l" )
  Warn( GetFoundText() )
  NextChar()
 ENDWHILE
END
----------------------------------------------------------

This could be a simplest solution, goto the beginning of the block and also go to the beginning of the line
and using "l" and using NextChar() to avoid repetition of finding the same string.

===

Testing this, if you highlight the 2 lines with '//test' and run the macro with "l" you will see it shows it 2 times.

----------------------------------------------------------
//test
//test

PROC Main()
 INTEGER I = 0
 IF ( NOT ( IsBlockInCurrFile() ) ) Warn( "Please mark a block" ) RETURN() ENDIF
 GotoBlockBegin()
 BegLine()
 I = 1 - 1
 WHILE LFind( "//test", "l" )
  I = I + 1
  Warn( I, " ", GetFoundText() )
  NextChar()
 ENDWHILE
END
----------------------------------------------------------


On the other hand if you use "l+" you will see it only 1 time thus.

----------------------------------------------------------
//test
//test

PROC Main()
 INTEGER I = 0
 IF ( NOT ( IsBlockInCurrFile() ) ) Warn( "Please mark a block" ) RETURN() ENDIF
 GotoBlockBegin()
 BegLine()
 I = 1 - 1
 WHILE LFind( "//test", "l+" )
  I = I + 1
  Warn( I, " ", GetFoundText() )
  NextChar()
 ENDWHILE
END
----------------------------------------------------------

And if you should use "gl+" or "gl" (not recommended to test thus) then it should cause an infinite loop and should hang TSE thus.



zhong zhao

unread,
Aug 10, 2022, 6:15:37 AM8/10/22
to SemWare TSE Pro text editor
Try lg+ and Save Position at begin,check if found position is same as saved position,exit loop

knud van eeden

unread,
Aug 10, 2022, 9:06:59 AM8/10/22
to SemWare TSE Pro text editor
1. OK, thanks, could you possibly share your simplest source code?

I am usually looking for a simplest and smallest possible solution 

(e.g. creating a template from it afterwards, so that it quickly can be reused later).

Like the ones from 'the book' from Paul Erdos ;-)



zhong zhao

unread,
Aug 10, 2022, 10:27:24 PM8/10/22
to SemWare TSE Pro text editor
//test
//test

PROC Main()
   INTEGER I = 0,r,c
   IF ( NOT ( IsBlockInCurrFile() ) ) Warn( "Please mark first 2 line in this file" ) RETURN() ENDIF
   GotoBlockBegin()
   BegLine()
   if LFind("//test", "l")
      r=CurrLine()
      c=CurrCol()

      I = I + 1
      Warn( I, " ", GetFoundText() ,"at ",r," ",c)
      WHILE LRepeatFind()
          r=CurrLine()
          c=CurrCol()

          I = I + 1
          Warn( I, " ", GetFoundText() ,"at ",r," ",c)
     ENDWHILE
   endif
END

//Repeats the previous lFind() or lReplace() command, whichever was executed
//last.
//
//Syntax:     INTEGER lRepeatFind([INTEGER direction])
//
//               direction is an optional integer that forces the repeated find
//               or replace to proceed in the indicated direction.  Supported
//               values for direction are:
//
//                 _BACKWARD_ forces the search to proceed backward, towards
//                 the beginning of the file.
//
//                 _FORWARD_ forces the search to proceed forward, towards the
//                 end of the file.
//
//                 _REVERSE_ forces the search to proceed in the opposite
//                 direction than the last find or replace.
//
//                 _SEARCH_INCLUSIVE_ forces the search to proceed starting
//                 from the current position, rather than one character beyond
//                 the current cursor position, for a forward search (as is
//                 normally done).
//
// Returns:    Non-zero if the find string was found; otherwise, zero (FALSE).
//
// Notes:      For a forward search, the search starts one character beyond the
//             current cursor position.  For a backward search, the search
//             starts one character before the current cursor position.
//
//             lRepeatFind() is intended for use in macros.  It does not affect
//             the histories and options of the high-level Find(), Replace(), and
//             RepeatFind().
//
//             Unlike RepeatFind(), lRepeatFind() does not issue any messages and
//             does not highlight the found string.
//
// See Also:   lFind(), lReplace()
Reply all
Reply to author
Forward
0 new messages