Macro for Open, Find and Replace and Save
-
Can’t you just accept that it WAS unclear?
I don’t want to insult your intelligence by asking you to clarify what this part of my answer means.
Nebulous? Maybe, I give you the credit!!!.*
And maybe means that anyone involved could be right. Or not?
-
@José-Luis-Montero-Castellanos ,
It’s the language, obviously. It made me wonder if you were being insulting or not, also, the way it was phrased. So let’s not make it worse by making more assumptions. :) I like your idea, so using the Python Plugin might be a good vehicle for you to try and implement it, but it has to be worth the needed learning and time to be worth it to you. We only work on stuff when not getting paid, because we are interested or passionate about it.
-
Hi, @josé-luis-montero-castellanos, @alan-kilborn, @peterjones and all,
Ah… OK, I do understand the regex problem ;-))
I thought about a very simple way to achive it !
To verify my assumption, I made a test with the
markdown._preinstalled.udl.xml
file, within theUserDefineLangs
folder-
First, I copied this file with a new name
markdown._new.udl.xml
, left in theUserDefineLangs
folder, too -
I opened this new file and change the line below, accordingly :
<UserLang name="Markdown (new)" ext="md markdown" udlVersion="2.1">
- Then, before the line
<Styles>
, I inserted the comment line<!-- Light mode -->
giving :
<!-- Light mode --> <Styles>
-
I copied all the
<Styles>......</styles
section -
I pasted all these lines at the end of the
markdown._new.udl.xml
file-
Preceded with a comment line
<!-- Dark mode -->
-
Followed with the two final lines
</userLang>
and</NotepadPlus>
-
=> So, the final contents of the
markdown._new.udl.xml
file are :<!--// Markdown-plus-plus is a project to support Markdown syntax in Notepad++. All UDLs are carefully designed by human, rather than generated by machine. Want an UDL for different theme? Want to change its preference? Want to create your own? Go get it: https://github.com/Edditoria/markdown-plus-plus Please read the README file for details. Welcome issues and pull requests via the above link. Copyright (c) Edditoria. Open source under the MIT license: https://github.com/Edditoria/markdown-plus-plus/blob/master/LICENSE.txt //--> <NotepadPlus> <UserLang name="Markdown (new)" ext="md markdown" udlVersion="2.1"> <Settings> <Global caseIgnored="yes" allowFoldOfComments="no" foldCompact="no" forcePureLC="2" decimalSeparator="0" /> <Prefix Keywords1="yes" Keywords2="yes" Keywords3="yes" Keywords4="yes" Keywords5="yes" Keywords6="yes" Keywords7="yes" Keywords8="no" /> </Settings> <KeywordLists> <Keywords name="Comments">00# 01 02((EOL)) 03<!-- 04--></Keywords> <Keywords name="Numbers, prefix1"></Keywords> <Keywords name="Numbers, prefix2"></Keywords> <Keywords name="Numbers, extras1"></Keywords> <Keywords name="Numbers, extras2"></Keywords> <Keywords name="Numbers, suffix1">.</Keywords> <Keywords name="Numbers, suffix2">.</Keywords> <Keywords name="Numbers, range"></Keywords> <Keywords name="Operators1">@ < > \< \> \\ \` \* \_ \{ \} \[ \] \( \) \# \+ \- \. \! \~ \| |:-: |:--: |:---: | :-: :--: :---: :|: |: :|</Keywords> <Keywords name="Operators2">- + :- :-- :--- :</Keywords> <Keywords name="Folders in code1, open"></Keywords> <Keywords name="Folders in code1, middle"></Keywords> <Keywords name="Folders in code1, close"></Keywords> <Keywords name="Folders in code2, open"></Keywords> <Keywords name="Folders in code2, middle"></Keywords> <Keywords name="Folders in code2, close"></Keywords> <Keywords name="Folders in comment, open"></Keywords> <Keywords name="Folders in comment, middle"></Keywords> <Keywords name="Folders in comment, close"></Keywords> <Keywords name="Keywords1">http:// (http:// https:// (https:// mailto: (mailto: ftp:// (ftp:// ftps:// (ftps:// (/ /</Keywords> <Keywords name="Keywords2">==== ----</Keywords> <Keywords name="Keywords3">*** ___</Keywords> <Keywords name="Keywords4">** __</Keywords> <Keywords name="Keywords5">* _</Keywords> <Keywords name="Keywords6">** __</Keywords> <Keywords name="Keywords7">* _</Keywords> <Keywords name="Keywords8"></Keywords> <Keywords name="Delimiters">00![ 00[ 01\ 02] 02] 03``` 03` 03~~~ 04\ 05``` 05((EOL `)) 05~~~ 06*** 07\ 08((EOL ***)) 09** 10\ 11((EOL **)) 12* 13\ 14((EOL *)) 15** 16\ 17((EOL **)) 18* 19\ 20((EOL *)) 21 22 23</Keywords> </KeywordLists> <!-- Light mode --> <Styles> <WordsStyle name="DEFAULT" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="COMMENTS" fgColor="808080" bgColor="FFFFFF" fontName="" fontStyle="2" nesting="0" /> <WordsStyle name="LINE COMMENTS" fgColor="FF8000" bgColor="FFFFFF" fontName="" fontStyle="1" nesting="0" /> <WordsStyle name="NUMBERS" fgColor="0080FF" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="KEYWORDS1" fgColor="8000FF" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="KEYWORDS2" fgColor="FF8000" bgColor="FFFFFF" fontName="" fontStyle="1" nesting="0" /> <WordsStyle name="KEYWORDS3" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="KEYWORDS4" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="1" nesting="0" /> <WordsStyle name="KEYWORDS5" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="2" nesting="0" /> <WordsStyle name="KEYWORDS6" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="KEYWORDS7" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="KEYWORDS8" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="OPERATORS" fgColor="8080FF" bgColor="FFFFFF" fontName="" fontStyle="1" nesting="0" /> <WordsStyle name="FOLDER IN CODE1" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="FOLDER IN CODE2" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="FOLDER IN COMMENT" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="DELIMITERS1" fgColor="8000FF" bgColor="FFFFFF" fontName="" fontStyle="2" nesting="0" /> <WordsStyle name="DELIMITERS2" fgColor="008000" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="DELIMITERS3" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="DELIMITERS4" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="1" nesting="65600" /> <WordsStyle name="DELIMITERS5" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="2" nesting="32800" /> <WordsStyle name="DELIMITERS6" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="DELIMITERS7" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="DELIMITERS8" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> </Styles> <!-- Dark mode --> <Styles> <WordsStyle name="DEFAULT" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="COMMENTS" fgColor="808080" bgColor="FFFFFF" fontName="" fontStyle="2" nesting="0" /> <WordsStyle name="LINE COMMENTS" fgColor="FF8000" bgColor="FFFFFF" fontName="" fontStyle="1" nesting="0" /> <WordsStyle name="NUMBERS" fgColor="0080FF" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="KEYWORDS1" fgColor="8000FF" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="KEYWORDS2" fgColor="FF8000" bgColor="FFFFFF" fontName="" fontStyle="1" nesting="0" /> <WordsStyle name="KEYWORDS3" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="KEYWORDS4" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="1" nesting="0" /> <WordsStyle name="KEYWORDS5" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="2" nesting="0" /> <WordsStyle name="KEYWORDS6" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="KEYWORDS7" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="KEYWORDS8" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="OPERATORS" fgColor="8080FF" bgColor="FFFFFF" fontName="" fontStyle="1" nesting="0" /> <WordsStyle name="FOLDER IN CODE1" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="FOLDER IN CODE2" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="FOLDER IN COMMENT" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="DELIMITERS1" fgColor="8000FF" bgColor="FFFFFF" fontName="" fontStyle="2" nesting="0" /> <WordsStyle name="DELIMITERS2" fgColor="008000" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> <WordsStyle name="DELIMITERS3" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="DELIMITERS4" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="1" nesting="65600" /> <WordsStyle name="DELIMITERS5" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="2" nesting="32800" /> <WordsStyle name="DELIMITERS6" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="DELIMITERS7" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="3" nesting="0" /> <WordsStyle name="DELIMITERS8" fgColor="333333" bgColor="FFFFFF" fontName="" fontStyle="0" nesting="0" /> </Styles> </UserLang> </NotepadPlus>
-
Thus, the problem came down to the simple question : how to comment out all the
Light Mode
section, in one go ? As usual, regular expressions are the solution ! -
Open or switch to the
markdown._new.udl.xml
file -
Open the Replace dialog (
Ctrl + H
)
(?xs-i) ^ [\t\x20]* <!--[ ]Light[ ]mode[ ]--> \R \K [\t\x20]* <Styles> \R .+? ^ [\t\x20]* </Styles> \R # ( Search ) ( )<!--\r\n$0( )-->\r\n
-
Untick all box options
-
Tick the
Wrap around
option -
Select the
Regular expression
search mode -
Click once on the
Replace All
button ( Do not use theReplace
button ! ) -
Close the Replace dialog (
Esc
) -
Save the new contents, relative to the
Dark mode
-
After stopping and re-starting Notepad++, I did verify that this new UDL was functional and identical to the
Mardown (Preinstalled)
UDL
=> From now on, all the
light mode
section is commented out between two lines<!--
and-->
So, you just have to modify the
fgcolor
andbgcolor
attibutes of theDark mode
section, as you like ;-))
Notes :
-
The search regex uses the free-spacing mode
(?x)
to better identify the main parts of the search regex -
When the
(?x)
modifier is used, the search regex may lie on one or several lines
For instance, my previous
single-line
regex :(?xs-i) ^ [\t\x20]* <!--[ ]Light[ ]mode[ ]--> \R \K [\t\x20]* <Styles> \R .+? ^ [\t\x20]* </Styles> \R # ( Search )
Could also be expressed as :
(?xs-i) # FREE-SPACING (x) , SINGLE-LINE (s) , SENSIBLE to CASE (-i) ( 'in-line' modifiers ) ^ [\t\x20]* # ANY range of leading SPACE or TABULATION characters, possibly NULL, at BEGINNING of CURRENT line <!--[ ]Light[ ]mode[ ]--> \R # String '<!-- Light mode -->' with that EXACT case and the LINE-BREAK chars ( \R ) \K # Resets any MATCHED text, so far and resets the REGEX ENGINE position to 0 [\t\x20]* # ANY range of leading SPACE or TABULATION characters, possibly NULL, at BEGINNING of CURRENT line <Styles> \R # String '<Styles>' with that EXACT case and the LINE-BREAK chars ( \R ) .+? # The SHORTEST NON-NULL range of ANY char ( standard or EOL ) till... ^ [\t\x20]* # ANY range of leading SPACE or TABULATION characters, possibly NULL, at BEGINNING of CURRENT line </Styles> \R # String '</Styles>' with that EXACT case and the LINE-BREAK chars ( \R )
-
In this mode,
space
andline-break
chars are never part of the regex AND anything after a first#
character is a comment. Thus :-
Any
space
character, part of the regex, must be changed as[ ]
or\x20
or preceded with an anti-slash\
-
Any
#
character, part of the regex, must be changed as[#]
or\x23
or preceded with an anti-slash\
-
-
Note that the appropriate replacement regex cannot benefit from the
(?x)
mode. So all its text is necessarily linked in one (possibly long ) line
( )<!--\r\n$0( )-->\r\n
Best Regards
guy038
-
-
@guy038
Good morning - Thanks for your kind reply:I forgot to mention that the RegEx that I made, not only deactivates one section while activating the other, and
vice versa
, that is, it alternates (toggles) between the two modes or serves as a “selector” that is why the?1
as a conditional selector which means that if capturing group 1 matches the first part of the toggle is replaced, if not then the second.?ℕYesPattern:NoPattern
(from the user manual).It all boils down to changing the tags positions (part of them)
<St
yle>,</S
tyle>,<Xt
yle>,</X
tyle> (the spaces in the previous tags are effect of the ` character).Regarding your suggestions, I haven’t tried it yet, but I’m about to do it. The first thing to see in detail each expression and understand its purpose. It seems elegant and obviously more understandable (digestible) especially with the use of
(?x)
. (Don’t forget I’m new to Npp and its RegEX engine.) Although I already knew something about RegEx.As soon as I have tested the functionality, and deduced that it can be useful to me, I will tell you.
Have a good day :)
-
@Lycan-Thrope
Hello:
The idea is that if I’m going to use Npp, it’s natural that I want to take advantage of its resources without resorting to a new learning curve. That’s why the natural option seemed to me to be the Npp macros.With respect to
So let’s not make it worse by making more assumptions
I like that smile :), with which the comment ends, And the comment itself, I subscribe.
Have a a Good Day and success.
-
@José-Luis-Montero-Castellanos ,
I suggested the Python option because I’m not quite sure changing the files contents would be able to effectively be changed and reloaded simply by using the regex, because essentially the file has to be saved, I believe, to be implemented, but after thinking about it, I remember that changing the User Defined Language keyword and stylers would interactively change them in real time, so in hindsight, you might be on to something.
The Python option was suggested because even though I’m not good at it, it seems that the stuff that @Alan-Kilborn has posted that I’ve seen goes to the heart of using the Scintilla directly via NPP, so the changes would be able to be changed real time, is what I was figuring…so I may have been wrong to suggest that in light of the possiblity you point out. I’m not that involved in the actual code that runs NPP, so I’m more of a user seeking more advanced abilities of my language without needing to learn to code NPP itself…and Python plugin was the closest thing I cold think of short of actual NPP coding that might be able to accomplish the task you had in mind.
So in closing, good luck with your experimentation. I hope it proves usable as I’d like to not have to do the multiple steps to change from Light mode to Dark Mode as well. I just didn’t want to see the different lanuages we speak, become a problem in our communications. :-)
-
This post is deleted! -
@Lycan-Thrope
Greetings:I edited this post, (deleting the previous one, since the editing time had passed.) highlighting the parts that were changed or added that could be misinterpreted…
just didn’t want to see the different lanuages we speak, become a problem in our communications. :-)
By the way, my Qtranslator works great, I’m a Spanish speaker (greetings from Colombia, remember?). But that if I were to blame the translator it would be dishonest on my side, and of low esteem on my part, because I too have much to complain about and point out, but I reserve that for the efficient running of the forum. Wasn’t what seemed offensive really a mockery of myself? (3 or 4 times in this thread) (a good preventative habit against hubris!).
About python, well I have no choice, and your suggestion is very appropriate, I think @PeterJones also mentioned it. I’ll give it a try. Or I could program it in some language that I know.
Well, opening a file and changing four words is not a “feat” that requires a lot of code. :)
Closing! - Have a good evening…
-
@José-Luis-Montero-Castellanos ,
I understood what you meant, without needing to delete, especially once I realized that you weren’t speaking as if with broken english , but rather because you ARE using a translator, which are not as good at translating your language using the nuances an english speaking person is used to. I realize, with the few languages I have some familiarity with, that most foreign (to English speakers) languages don’t have the same directness or ability for description that English allows speakers, so even though your tranlator seems to be working good from your end…it has a few failings on our end…and at first, I wasn’t sure if it was your personal lack of a wider understanding of English, or if you were using a translator, which can’t compare to the language usage. :)
Now that we know the issue, we at least can understand now, that it wasn’t your statements, but that of a translator’s inability to properly relay your meaning. :)
-
@Lycan-Thrope :
I will take your comments into account.
Greetings :-) -
@guy038 said in Macro for Open, Find and Replace and Save:
Hello, @josé-luis-montero-castellanos, @aln-kilborn and all,
@josé-luis-montero-castellanos, regarding your regex S/R :
-
SEARCH
(?s)(<St)(.)(/S)(.)(<Xt)(.)(/X)|(<Xt)(.)(/X)(.)(<St)(.)(/S)
-
REPLACE
?1\5\2\7\4\1\6\3:${12}${9}${14}${11}${8}${13}${10}
Why do you need the leading in-line modifier
(?s)
, in the search part ? Do you mean that the single char.
in group2
,4
,6
,9
,11
and13
may also represent a line-break char (\r
or\n
) ?I suppose not ! Then, your correct search regex is rather :
(?-s)(<St)(.)(/S)(.)(<Xt)(.)(/X)|(<Xt)(.)(/X)(.)(<St)(.)(/S)
where any
.
single char will represent a non-break char, only !
Now, I noticed that the groups used, in replacement, for each alternative of the search regex, are in the same order ! Then, you can use, preferably, a specific feature of the Boost Regex engine, called
Branch Reset
For instance, in your regex, the numbers of each group are :
(?-s)(<St)(.)(/S)(.)(<Xt)(.)(/X)|(<Xt)(.)(/X)(.)(<St)(.)(/S) Gr : 1 2 3 4 5 6 7 |8 9 10 11 12 13 14
If we use the
Branch Reset
feature the search regex becomes :(?-s)(?|(<St)(.)(/S)(.)(<Xt)(.)(/X)|(<Xt)(.)(/X)(.)(<St)(.)(/S)) Gr: 1 2 3 4 5 6 7 |1 2 3 4 5 6 7
Thus, your entire regex S/R can be simplified as below :
-
SEARCH
(?-s)(?|(<St)(.)(/S)(.)(<Xt)(.)(/X)|(<Xt)(.)(/X)(.)(<St)(.)(/S))
-
REPLACE
\5\2\7\4\1\6\3
For instance, given this INPUT text :
<St2/S4<Xt6/X <Xt9/XB<StD/S
You would get the OUTPUT text, below :
<Xt2/X4<St6/S <St9/SB<XtD/X
Notes :
-
The
Regular expression
option must be selected -
Preferably, tick the
[Wrap around](https://www.ahijoy.com/)
option
Best Regards,
guy038
I tried it but didn’t work
-