You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This script is not really sensible in real life, as for large files, it will take some time to save the file, and it will be saved after every key press.
4236
4242
4237
-
Simple regular expression search and replace (using Notepad++/Scintilla regular expressions).
4238
-
4243
+
Note that ``Editor`` callbacks are processed *asynchronously* by default. What this means in practice is that your event handler function
4244
+
(saveCurrentDoc in the previous example) is called just after the event has fired. If the callback handler is slow, and the callbacks occur quickly, you
4245
+
could get "behind". Callbacks are placed in a queue and processed in the order they arrived. If you need to do something before letting the user continue, you
4246
+
can use :method:`Editor.callbackSync`, which adds a synchronous callback.
Adds a *synchronous* handler for an ``Editor`` (Scintilla) event. The events list is a list of events to respond to, from the :class:`SCINTILLANOTIFICATION` enum.
4252
+
4253
+
What this means is that the handler function is called, and must complete, before control is returned to the user. If you perform a slow operation in your handler
4254
+
function, this will have an effect on the speed of Notepad++ for the user (i.e. Notepad++ may appear to have "locked up", whilst your event processes).
4255
+
4256
+
Synchronous callbacks are mostly used for calling :method:`Editor.autoCCancel` in response to :class:`SCINTILLANOTIFICATION.AUTOCSELECTION`, but could be used for
4257
+
anything where the timing of the handler function is critical.
The main search and replace method, using regular expressions. The regular expression syntax in use is
4294
+
that from Notepad++, which is actually the `Boost::Regex <http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/index.html>`
4295
+
implementation (specifically the Perl regular expression syntax).
4296
+
4297
+
4245
4298
``flags`` are from the re module (e.g. ``re.IGNORECASE``), so ``import re`` if you use the flags.
4246
-
A maximum of ``count`` replacements are made, if zero or not given, then all replacements are made.
4247
-
4248
-
``startLine`` and ``endLine`` are optional, and if provided refer the first and last (zero indexed)
4249
-
lines to perform the replace on.
4250
4299
4251
-
*Note that as ``$`` only matches ``\n``, for Windows line ending files (i.e. ``\r\n``) if you want to match
4252
-
the end of the line, you need to use ``\r$`` - see note about ``Editor.INCLUDELINEENDINGS``*
4253
-
4254
-
As each line is searched individually, if you want to remove the line breaks, for instance to join lines together,
4255
-
you need to add a flag ``Editor.INCLUDELINEENDINGS``. This includes the line endings in the search string, and also
4256
-
in the replacement.
4257
-
4258
-
To just add a star (*) to the end of every line, you'd just use::
4300
+
The ``re.MULTILINE`` flag is automatically set, so ``^`` matches the start of each line of the document, and
4301
+
``$`` the end of each line. If you want to ``^`` and ``$`` to match the start and end of the whole document,
4302
+
you can override the behaviour by adding in the ``editor.WHOLEDOC`` flag.
4303
+
4304
+
Note that line endings are now handled automatically.
4305
+
4306
+
``search`` can be a string, a unicode string, or an object. An object will be converted to a string using it's __str__ method.
4307
+
For a unicode string, the current document encoding is checked, and an attempt is made at a conversion. If the conversion cannot be
4308
+
successfully performed, an error occurs. When a standard Python string is used, no conversion takes place. If you need to replace
4309
+
strings in documents in both UTF-8 and ANSI (or other single byte encodings), then it's best to pass unicode strings.
4310
+
4311
+
``replace`` follows the same conversion rules as ``search``. However, you can also pass a function or lambda expression
4312
+
as the ``replace`` parameter. This function receives a single parameter, which is an object resembling a re.MatchObject instance.
4313
+
It only resembles an re.MatchObject because it doesn't support all the methods. Specifically, ``groupdict()``, ``pos``, ``endpos``, ``re`` and ``string``
4314
+
methods and properties are not supported. ``expand()``, ``group()`` and ``groups()`` (for example) all work identically. The function should
4315
+
return the string to use as the replacement.
4316
+
4317
+
A simple function replacement::
4318
+
4319
+
def add_1(m):
4320
+
return 'Y' + str(number(m.group(1)) + 1)
4321
+
4322
+
# replace X followed by numbers by an incremented number
4323
+
# e.g. X56 X39 X999
4324
+
# becomes
4325
+
# Y57 Y40 Y1000
4326
+
4327
+
editor.rereplace('X([0-9]+)', add_1);
4328
+
4329
+
``startPosition`` is the binary position to start the search. Use :method:`Editor.positionFromLine`
4330
+
to get the binary position from the (zero indexed) line number.
4331
+
4332
+
``endPosition`` is the binary position to end the search. Use :method:`Editor.positionFromLine`
4333
+
to get the binary position from the (zero indexed) line number.
4334
+
4335
+
A maximum of ``count`` replacements are made, if zero or None, then all replacements are made.
4259
4336
4260
-
editor.pyreplace("$", "*")
4261
4337
4262
-
However, to join lines together, and put a pipe character (|) in between each one::
4338
+
An small point to note, is that the replacements are first searched, and then all replacements are made.
4339
+
This is done for performance and reliability reasons. Generally this will have no side effects, however there
4340
+
may be cases where it makes a difference. (Author's note: If you have such a case, please post a note on the forums
4341
+
such that it can be added to the documentation, or corrected).
The ``re.MULTILINE`` flag is automatically set, so ``^`` matches the start of each line of the document, and
4352
+
``$`` the end of each line. If you want to ``^`` and ``$`` to match the start and end of the whole document,
4353
+
you can override the behaviour by adding in the ``editor.WHOLEDOC`` flag.
4354
+
4355
+
Note that line endings are now handled automatically.
4356
+
4357
+
``search`` can be a string, a unicode string, or an object. An object will be converted to a string using it's __str__ method.
4358
+
For a unicode string, the current document encoding is checked, and an attempt is made at a conversion. If the conversion cannot be
4359
+
successfully performed, an error occurs. When a standard Python string is used, no conversion takes place. If you need to replace
4360
+
strings in documents in both UTF-8 and ANSI (or other single byte encodings), then it's best to pass unicode strings.
4361
+
4362
+
``matchFunction`` is a function that gets callled with each match. This function receives a single parameter, which is an object resembling a re.MatchObject instance.
4363
+
It only resembles an re.MatchObject because it doesn't support all the methods. Specifically, ``groupdict()``, ``pos``, ``endpos``, ``re`` and ``string``
4364
+
methods and properties are not supported. ``expand()``, ``group()`` and ``groups()`` (for example) all work identically. The function should
4365
+
return the string to use as the replacement.
4366
+
4367
+
A simple function replacement::
4368
+
4369
+
matches = []
4370
+
def match_found(m):
4371
+
# append the match (start, end) positions to the matches array
4372
+
matches.append(m.span(0))
4373
+
4374
+
# find X followed by numbers
4375
+
# e.g. X56 X39 X999
4376
+
4377
+
editor.research('X([0-9]+)', match_found)
4378
+
4379
+
You can do the same thing with a lambda expression::
0 commit comments