MemoEdit Search feature via UserFunc?

173 views
Skip to first unread message

cgallery

unread,
Apr 7, 2026, 9:58:59 AMApr 7
to Harbour Users
Hello.

Does anyone have some code I can use to add a Search feature to MemoEdit?

Some of my memos are quite long, and I'd like to do something like press Ctrl-F and be prompted for a search string, and then have the cursor positioned at the next occurrence of that string within the buffer (while I'm editing within MemoEdit).

I tried some of the code suggested by AI but the code is incomplete and there are two stumbling blocks.  First is, I can't see how to access the current buffer MemoEdit is using.

The second is that the AI-generated code never really tells you how to reposition the cursor at the line/col where the search phrase was found.  It always suggests this is complex.  I haven't tried to figure this out yet because I'm stuck at figuring out my first problem in the paragraph above, but...

Then I figured maybe already has code that does this, or will tell me "can't be done" in which case I won't try.

Thanks for any help.

cgallery

unread,
Apr 8, 2026, 10:55:42 AMApr 8
to Harbour Users
Well I never found anything that worked, so sort of came-up with a kludge.

Basically it relies on a combination of reading the screen (via SaveScreen) and stuffing down-arrows and Ctrl-L's (the keyboard shortcut I use for "Locate text.").

It isn't pretty code, but I figured I'd post it here as someone may be able to use it.

// ==========================================
// User Defined Function (UDF) for MemoEdit
// ==========================================
FUNCTION MemoEditFind( nMode, nLine, nCol )
   LOCAL nKey := LASTKEY()
   LOCAL nRetVal := ME_DEFAULT // Default action
   LOCAL cSearch, nFoundPos

   if nMode == ME_INIT
      return ME_DEFAULT
   endif
   
   if (nMode == ME_UNKEY .or. nMode == ME_UNKEYX) .and. nKey = K_CTRL_L
   
      if SearchGet
         PushRow = Row()
         PushCol = Col()
         @22,0 clear to 22,79
         clear gets
         @22,0 say " Search: " get SearchString pict "XXXXXXXXXXXXXXXXXXXXXXXXX"
         read
         * @22,0 say ""
         * accept "Search string? " to SearchString
         @PushRow,PushCol say ""
         SearchGet = .f.
      endif
     
      * @22,0 say " Ctrl-L to locate text "

      ThisLine = SaveScreen(row(),col(),row(),78)
      ThisLine = CharOdd(ThisLine)
      ThisLine = trim(CharOdd(ThisLine))

      if ThisLine = LastLine
         LastLineCount = LastLineCount + 1
         if LastLineCount < 10
            * tone(1000,1)
            * ? "Duplicate Line!"
            keyboard chr(24) + chr(12)
            return ME_DEFAULT
         else
            LastLineCount = 0
            SearchGet = .t.
            return ME_DEFAULT
         endif
      else
         LastLine = ThisLine
      endif
         
      LastLineCount = 0
      FindIt = at(upper(trim(SearchString)),upper(ThisLine))
      if FindIt > 0
         * tone(2000,2)
         keyboard replicate(chr(4),FindIt)
         SearchGet = .t.
         return ME_DEFAULT
      endif
         
      * tone(3000,3)
      keyboard chr(1) + chr(24) + chr(12)
      return ME_DEFAULT
   endif
   
RETURN nRetVal

cgallery

unread,
Apr 8, 2026, 10:57:15 AMApr 8
to Harbour Users
Oh and there are some public variables declared outside of this function.

   public LastLine := ""
   public LastLineCount := 0
   public SearchString := replicate(" ",20)
   public SearchGet := .t.

Francesco Perillo

unread,
Apr 8, 2026, 4:58:26 PMApr 8
to harbou...@googlegroups.com
I spent a bit browsing the source code of memoedit... it actually creates a LOCAL oEd instance of HBMemoEditor that is a subclass of HBEditor.

Browsing the source code I found several references to places where text is extracted and cursor positioned. My first idea was to clone memoedit and make the oEd variable PRIVATE so that it could be accessed from a SET KEY.

Then I wanted to better understand when the use function was called and found this post where you may find, in the attached file, the solution for your problem.
Please note that the solution provided subclass HBEditor directly and bypass MemoEdit/HBMemoEditor. You know what you need.

Please let us know if you find a better solution.

--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-users/a1d81e7b-c2dc-4de3-9e54-cac02fe8a7c6n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages