Macro search/replace scope based on whether any text is highlighted

Started by: Mr KT2 voices5 posts

Hello, Is there a way to run a search/replace type macro with "In the selection Only" selected if any text/columns are highlighted before the macro is run, but run on the entire file if no text is highlighted when the macro is called? Could this functionality be added otherwise?
Hello Mr. KT, You can replace text only in the selection by using (JavaScript):

document.selection.Replace( "find", "replace", eeReplaceSelOnly | eeReplaceAll );
For the "find", please use EmEditor v14.6.0 beta 18 or later, and you can do this:

document.selection.Find( "find", eeFindNext | eeFindReplaceSelOnly );
I hope this helps. Thank you!
Thanks Yutaka, so to clarify - if I have a comma separated file as follows: _______ A1,B1, A2,B2, A3,B3, etc. ________ is there a way to run a SINGLE macro so if no text is selected, it will run on the entire file & produce this (just a simple example, searching for regexp start of line ^, replace with "): _______ "A1,B1, "A2,B2, "A3,B3, etc. _______ BUT if I am in CSV Mode (so the file is now in columns), highlight Column "B" and run the same macro, it will produce this: _______ A1,"B1, A2,"B2, A3,"B3, etc. _______ It seems there needs to be some way for the single macro to know if text is highlighted or not, before it can run on the whole file or just selected text? Would an additional flag that checks if text is selected first, then run the macro and it will work on that selection only, but if text isn't selected, if will run on the whole file instead (e.g. eeReplaceSelOnly_else_eeReplaceall)? Thank you for your efforts.
Hello, You can use the following code to check whether the selection exists...
// check whether selection exists
if( !document.selection.IsEmpty() ) {
   // selection exists
   document.seleciton.Replace( ... );
}
Fantastic! Thank you Yutaka.

5 posts in this topic