Skip to content

Commit b02bf8a

Browse files
committed
Merge branch 'feature/FindAll' of https://github.com/daddel80/notepadpp-multireplace into feature/FindAll
2 parents 04dfdab + 62da329 commit b02bf8a

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

README.md

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ MultiReplace is a Notepad++ plugin that allows users to create, store, and manag
2626
- [lvars](#lvarsfilepath)
2727
- [lkp](#lkpkey-hpath-inner)
2828
- [fmtN](#fmtnnum-maxdecimals-fixeddecimals)
29-
- [Preloading Variables](#preloading-variables)
29+
- [Preload variables & helpers](#preload-variables--helpers)
3030
- [Operators](#operators)
3131
- [If-Then Logic](#if-then-logic)
3232
- [DEBUG option](#debug-option)
@@ -237,7 +237,7 @@ Initializes custom variables for use in various commands, extending beyond stand
237237

238238
Custom variables maintain their values throughout a single Replace-All or within a list of multiple Replace operations. Thus, they can transfer values from one list entry to subsequent ones. They reset at the start of each new document in **'Replace All in All Open Documents'**.
239239

240-
> **Tip**: To learn how to preload variables using an empty Find field before the main replacement process starts, see [Preloading Variables](#preloading-variables).
240+
**Init usage:** can be used as an init entry (empty Find) to preload before replacements; not mandatory. See [Preload variables & helpers](#preload-variables--helpers) for workflow and examples.
241241

242242
| **Find** | **Replace** | **Before** | **After** | **Regex** | **Scope CSV** | **Description** |
243243
|-----------------|----------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------|--------------------------------------------------------|----------|--------------|------------------------------------------------------------------------------------------------------------------------------|
@@ -269,7 +269,7 @@ return {
269269
}
270270
```
271271

272-
> **Tip**: To learn how to preload variables using an empty Find field before the main replacement process starts, see [Preloading Variables](#preloading-variables).
272+
**Init usage:** can be used as an init entry (empty Find) to preload before replacements; not mandatory. See [Preload variables & helpers](#preload-variables--helpers) for workflow and examples.
273273

274274
| Find | Replace | Regex | Scope CSV | Description |
275275
|---------------|-------------------------------------------------------------------------------|-------|-----------|------------------------------------------------------------------------------------------------------|
@@ -348,21 +348,65 @@ Formats numbers based on precision (maxDecimals) and whether the number of decim
348348

349349
<br>
350350

351-
### **Preloading Variables**
352-
MultiReplace supports **predefining or loading variables** before any replacements occur. By separating initialization from the actual replacements, operations stay clean and maintainable.
351+
#### **lcmd(path)**
353352

354-
#### 🔹 **How it works:**
355-
- **Place `vars()` or `lvars()` next to an empty Find field.**
356-
- This entry does **not** search for matches but runs before replacements begin.
357-
- It ensures that **variables are loaded once**, regardless of their position in the list.
353+
Load user-defined helper functions from a Lua file. The file **must** `return` a table of functions. `lcmd` registers those functions as globals for the current run.
358354

359-
**Examples**
355+
**Purpose:** add reusable helper functions (formatters, slugifiers, padding, small logic). Helpers **must return a string or number** and are intended to be called from **action** commands (e.g. `set(...)`, `cond(...)`).
356+
**Init usage:** can be used as an init entry (empty Find) to preload before replacements; not mandatory. See [Preload variables & helpers](#preload-variables--helpers) for workflow and examples.
360357

361-
| **Find** | **Replace** | **Description** |
362-
|--------------|------------------------------------------------------------|----------------|
363-
| *(empty)* | `vars({prefix = "ID_"})` | Sets `prefix = "ID_"` before replacements. |
364-
| *(empty)* | `lvars([[C:\path\to\myVars.vars]])` | Loads external variables from a file. |
365-
| `(\d+)` | `set(prefix .. CAP1)` | Uses `prefix` from initialization (e.g., `123``ID_123`). |
358+
| Find | Replace | Regex | Description |
359+
|-----------|----------------------------------------|-------|-------------|
360+
| *(empty)* | `lcmd([[C:\tmp\mycmds.lcmd]])` | No | Load helpers from file (init row — no replacement). |
361+
| `(\d+)` | `set(padLeft(CAP1, 6, '0'))` | Yes | Zero-pad captured number to width 6 using `padLeft`. |
362+
| `(.+)` | `set(slug(CAP1))` | Yes | Create a URL-safe slug from the whole line using `slug`. |
363+
364+
**File format:**
365+
```lua
366+
return {
367+
-- padLeft: left-pad to width
368+
padLeft = function(s, w, ch)
369+
s = tostring(s or "")
370+
ch = ch or " "
371+
if #s >= w then return s end
372+
return string.rep(ch, w - #s) .. s
373+
end,
374+
375+
-- slug: make URL-friendly
376+
slug = function(s)
377+
s = tostring(s or ""):lower()
378+
s = s:gsub("%s+", "-"):gsub("[^%w%-]", "")
379+
return s
380+
end
381+
}
382+
```
383+
384+
<br>
385+
386+
### **Preload variables & helpers**
387+
Use init entries (empty Find) to preload variables or helper functions before any replacements run. Init entries run once per Replace-All (or per list pass) and do not change text directly.
388+
389+
#### **How it works**
390+
- **Place `vars()`, `lvars()` or `lcmd()` next to an empty Find field.**
391+
- This entry does **not** search for matches but runs before replacements begin.
392+
- It ensures that **variables and helpers are loaded once**, regardless of their position in the list.
393+
- Use **Use Variables = ON** for init rows so loaded variables/helpers are available to later rows.
394+
395+
#### Examples
396+
397+
| **Find** | **Replace** | **Description** |
398+
|-------------|----------------------------------------------|-----------------|
399+
| *(empty)* | `vars({prefix = "ID_"})` | Set `prefix` before replacements. |
400+
| *(empty)* | `lvars([[C:\path\to\myVars.vars]])` | Load variables from file (file must `return { ... }`). |
401+
| *(empty)* | `lcmd([[C:\tmp\mycmds.lcmd]])` | Load helpers from file (e.g. `padLeft`, `slug`). |
402+
| `(\d+)` | `set(prefix .. CAP1)` | Uses `prefix` from init (`123``ID_123`). |
403+
| `(\d+)` | `set(padLeft(CAP1, 6, '0'))` | Use helper loaded by `lcmd` to zero-pad (`123``000123`). |
404+
| `(.+)` | `set(slug(CAP1))` | Use helper loaded by `lcmd` to create a slug (`Hello World!``hello-world`). |
405+
406+
#### File notes
407+
- `lvars` / `lcmd` files must **return a table**. Recommended extension: `*.lua` (e.g. `myVars.vars`, `mycmds.lcmd`).
408+
- `lcmd` registers helper functions globally for the run and **errors on name collisions** (no overrides).
409+
- Errors from loading are reported to the user (loader returns `(false, errMsg)` on failure).
366410

367411
<br>
368412

@@ -560,6 +604,9 @@ The MultiReplace plugin provides several configuration options, including transp
560604
- **Default**: `GroupResults=0` (disabled).
561605
- **Description**: This option changes how 'Find All' results are presented. When enabled (`1`), results are grouped by their source list entry, creating a categorized view. When disabled (`0`), all results are displayed as a single, flat list, sorted by their position in the document, without any categorization.
562606

607+
- **SafeMode**: Controls which standard Lua libraries are available.
608+
- **Default**: `SafeMode=1` (enabled).
609+
- **Description**: When enabled (`1`), some libraries are disabled (`os`, `io`, `package`, `debug`, and functions like `dofile`, `require`, `load`). Common libraries such as `string`, `table`, `math`, `utf8`, `coroutine` remain available. When disabled (`0`), all standard libraries are loaded.
563610

564611
### Multilingual UI Support
565612

0 commit comments

Comments
 (0)