From 65b88963767b49e9a0829f0cee248b1725a973f7 Mon Sep 17 00:00:00 2001 From: Thomas Knoefel <53190206+daddel80@users.noreply.github.com> Date: Wed, 25 Dec 2024 13:47:33 +0100 Subject: [PATCH 1/3] added lvars() --- README.md | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8f920db..ea46239 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ MultiReplace is a Notepad++ plugin that allows users to create, store, and manag - [set](#setstrorcalc) - [cond](#condcondition-trueval-falseval) - [vars](#varsvariable1value1-variable2value2-) + - [lvars](#lvarsfilepath) - [lkp](#lkpkey-hpath-inner) - [fmtN](#fmtnnum-maxdecimals-fixeddecimals) - [Operators](#operators) @@ -143,17 +144,42 @@ Custom variables maintain their values throughout a single Replace-All or within | `(\d+)` | `vars({COL2=0,COL4=0}); cond(LCNT==4, COL2+COL4);`
`if COL==2 then COL2=CAP1 end;`
`if COL==4 then COL4=CAP1 end;` | `1,20,text,2,0`
`2,30,text,3,0`
`3,40,text,4,0` | `1,20,text,2,22.0`
`2,30,text,3,33.0`
`3,40,text,4,44.0` | Yes | Yes | Tracks values from columns 2 and 4, sums them, and updates the result for the 4th match in the current line. | | `\d{2}-[A-Z]{3}` | `vars({MATCH_PREV=''}); cond(LCNT==1,'Moved', MATCH_PREV); MATCH_PREV=MATCH;` | `12-POV,00-PLC`
`65-SUB,00-PLC`
`43-VOL,00-PLC` | `Moved,12-POV`
`Moved,65-SUB`
`Moved,43-VOL` | Yes | No | Uses MATCH_PREV to track the value of the first match in the line and shift it to the 2nd (LCNT) match during replacements. | -An empty Find string can be used to set variables for the entire Find and Replace list without being tied to a specific Find action. This entry does not match any text but is executed once at the beginning of the 'Replace' or 'Replace All' process when 'Use List' is enabled. It allows the Replace field to run initialization commands like `vars()` for the entire operation. The position of this entry in the list does not affect its behavior. +An empty Find string (`*(empty)*`) can be used to set variables for the entire Find and Replace list without being tied to a specific Find action. This entry does not match any text but is executed once at the beginning of the 'Replace' or 'Replace All' process when 'Use List' is enabled. It allows the Replace field to run initialization commands like `vars()` for the entire operation. The position of this entry in the list does not affect its behavior. | Find | Replace | Description/Expected Output | |-------------------|-------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------| -| | `vars({ `
`VpersonName = FNAME:sub(1, (FNAME:find(" - ", 1, true) or 0) - 1),`
`Vdepartment = FNAME:sub((FNAME:find(" - ", 1, true) or #FNAME + 1) + 3, (FNAME:find(".", 1, true) or 0) - 1) })` | Extracts `VpersonName` and `Vdepartment` from the filename of the active document in the format ` - .xml` using the `vars` action. Triggered only once at the start of the replace process when `Find` is empty. | +| *(empty)* | `vars({ `
`VpersonName = FNAME:sub(1, (FNAME:find(" - ", 1, true) or 0) - 1),`
`Vdepartment = FNAME:sub((FNAME:find(" - ", 1, true) or #FNAME + 1) + 3, (FNAME:find(".", 1, true) or 0) - 1) })` | Extracts `VpersonName` and `Vdepartment` from the filename of the active document in the format ` - .xml` using the `vars` action. Triggered only once at the start of the replace process when `Find` is empty. | | `personname` | `set(VpersonName)` | Replaces `personname` with the content of the variable `VpersonName`, previously initialized by the `vars` action. | | `department` | `set(Vdepartment)` | Replaces `department` with the content of the variable `Vdepartment`, previously initialized by the `vars` action. | -#### **lkp(key, hpath, inner)** +#### **lvars(filePath)** + +Loads custom variables from an external file. The file specifies variable names and their corresponding values. The loaded variables can then be used throughout the Replace process, similar to how variables defined with [`vars`](#varsvariable1value1-variable2value2-) work. + +The parameter **filePath** must specify a valid path to a file. Supported path formats include: +- Escaped Backslashes: `"C:\\path\\to\\file.vars"` +- Forward Slashes: `"C:/path/to/file.vars"` +- Long Bracket String: `[[C:\path\to\file.vars]]` + +**Example File:** +```lua +return { + userName = "Alice", + threshold = 10, + enableFeature = true +} +``` + +| Find | Replace | Regex | Scope CSV | Description | +|---------------|-------------------------------------------------------------------------------|-------|-----------|------------------------------------------------------------------------------------------------------| +| *(empty)* | `lvars([[C:/tmp/myVars.vars]])` | No | No | Loads variables such as `userName = "Alice"` and `threshold = 10` from `myVars.vars`. | +| `Hello` | `set(userName)` | No | No | Replaces `Hello` with the value of the variable `userName`, e.g., `"Alice"`. | +| `(\d+)` | `cond(tonumber(CAP1) > threshold, "Above", "Below")` | Yes | No | Replaces the match based on the condition evaluated using the variable `threshold`. | -##### Description +An empty Find string (`*(empty)*`) initializes variables globally at the start of the 'Replace' or 'Replace All' process when "Use List" is enabled. This initialization runs only once and is independent of specific matches or its position in the list. Alternatively, variables can be loaded conditionally by combining `lvars` or `vars` with a Find string, triggering the variable assignment only when the specified string is matched. + + +#### **lkp(key, hpath, inner)** Performs an external lookup of **key** against an indexed data file located at **hpath** and returns the corresponding value. By default, if the **key** is not found, `lkp()` simply reverts to the key itself. Setting **inner** to `true` instead yields a `nil` result when the key is missing, allowing for conditional checks or deeper nested logic. ##### Key and File Path From 829e739d8529be35fdf87f77c3c21a55f6036d70 Mon Sep 17 00:00:00 2001 From: Thomas Knoefel <53190206+daddel80@users.noreply.github.com> Date: Wed, 25 Dec 2024 13:52:44 +0100 Subject: [PATCH 2/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ea46239..c88906d 100644 --- a/README.md +++ b/README.md @@ -174,7 +174,7 @@ return { |---------------|-------------------------------------------------------------------------------|-------|-----------|------------------------------------------------------------------------------------------------------| | *(empty)* | `lvars([[C:/tmp/myVars.vars]])` | No | No | Loads variables such as `userName = "Alice"` and `threshold = 10` from `myVars.vars`. | | `Hello` | `set(userName)` | No | No | Replaces `Hello` with the value of the variable `userName`, e.g., `"Alice"`. | -| `(\d+)` | `cond(tonumber(CAP1) > threshold, "Above", "Below")` | Yes | No | Replaces the match based on the condition evaluated using the variable `threshold`. | +| `(\d+)` | `cond(threshold > 5, "Above", "Below")` | Yes | No | Replaces the match based on the condition evaluated using the variable `threshold`. | An empty Find string (`*(empty)*`) initializes variables globally at the start of the 'Replace' or 'Replace All' process when "Use List" is enabled. This initialization runs only once and is independent of specific matches or its position in the list. Alternatively, variables can be loaded conditionally by combining `lvars` or `vars` with a Find string, triggering the variable assignment only when the specified string is matched. From c1f3634e382ffa530e2c76a361d4fa98de7733d5 Mon Sep 17 00:00:00 2001 From: Thomas Knoefel <53190206+daddel80@users.noreply.github.com> Date: Wed, 25 Dec 2024 16:04:32 +0100 Subject: [PATCH 3/3] Update README.md --- README.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c88906d..684449e 100644 --- a/README.md +++ b/README.md @@ -135,22 +135,24 @@ Implements if-then-else logic, or if-then if falseVal is omitted. Evates the con | `cond(LINE<10, cond(LINE<5, cond(LINE>2, "3-4", "0-2"), "5-9"), "10+")` | "5-9" (Nested condition) | #### **vars({Variable1=Value1, Variable2=Value2, ...})** -Initializes custom variables for use in various commands, extending beyond standard variables like CNT, MATCH, CAP1. These variables can carry the status of previous find-and-replace operations to subsequent ones. +**Note:** This command was previously named `init(...)` and has been renamed to `vars(...)`. For compatibility, `init(...)` still works. -Custom variables maintain their values throughout a single Replace-All or within the list of multiple Replace operations. So they can transfer values from one list entry to the following ones. They reset at the start of each new document in 'Replace All in All Open Documents'. +Initializes custom variables for use in various commands, extending beyond standard variables like `CNT`, `MATCH`, `CAP1`. These variables can carry the status of previous find-and-replace operations to subsequent ones. -| Find | Replace | Before | After | Regex | Scope CSV | Description | -|-------------------|-----------------------------------------------------------------------------------------------------------|-------------------------------------|---------------------------------------|-------|-----------|-------------------------------------------------------------------------------------------------| -| `(\d+)` | `vars({COL2=0,COL4=0}); cond(LCNT==4, COL2+COL4);`
`if COL==2 then COL2=CAP1 end;`
`if COL==4 then COL4=CAP1 end;` | `1,20,text,2,0`
`2,30,text,3,0`
`3,40,text,4,0` | `1,20,text,2,22.0`
`2,30,text,3,33.0`
`3,40,text,4,44.0` | Yes | Yes | Tracks values from columns 2 and 4, sums them, and updates the result for the 4th match in the current line. | -| `\d{2}-[A-Z]{3}` | `vars({MATCH_PREV=''}); cond(LCNT==1,'Moved', MATCH_PREV); MATCH_PREV=MATCH;` | `12-POV,00-PLC`
`65-SUB,00-PLC`
`43-VOL,00-PLC` | `Moved,12-POV`
`Moved,65-SUB`
`Moved,43-VOL` | Yes | No | Uses MATCH_PREV to track the value of the first match in the line and shift it to the 2nd (LCNT) match during replacements. | +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'**. -An empty Find string (`*(empty)*`) can be used to set variables for the entire Find and Replace list without being tied to a specific Find action. This entry does not match any text but is executed once at the beginning of the 'Replace' or 'Replace All' process when 'Use List' is enabled. It allows the Replace field to run initialization commands like `vars()` for the entire operation. The position of this entry in the list does not affect its behavior. +| **Find** | **Replace** | **Before** | **After** | **Regex** | **Scope CSV** | **Description** | +|-----------------|----------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------|--------------------------------------------------------|----------|--------------|------------------------------------------------------------------------------------------------------------------------------| +| `(\d+)` | `vars({COL2=0,COL4=0}); cond(LCNT==4, COL2+COL4);`
`if COL==2 then COL2=CAP1 end;`
`if COL==4 then COL4=CAP1 end;` | `1,20,text,2,0`
`2,30,text,3,0`
`3,40,text,4,0` | `1,20,text,2,22.0`
`2,30,text,3,33.0`
`3,40,text,4,44.0` | Yes | Yes | Tracks values from columns 2 and 4, sums them, and updates the result for the 4th match in the current line. | +| `\d{2}-[A-Z]{3}`| `vars({MATCH_PREV=''}); cond(LCNT==1,'Moved', MATCH_PREV); MATCH_PREV=MATCH;` | `12-POV,00-PLC`
`65-SUB,00-PLC`
`43-VOL,00-PLC` | `Moved,12-POV`
`Moved,65-SUB`
`Moved,43-VOL` | Yes | No | Uses `MATCH_PREV` to track the first match in the line and shift it to the 2nd (`LCNT`) match during replacements. | -| Find | Replace | Description/Expected Output | -|-------------------|-------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------| -| *(empty)* | `vars({ `
`VpersonName = FNAME:sub(1, (FNAME:find(" - ", 1, true) or 0) - 1),`
`Vdepartment = FNAME:sub((FNAME:find(" - ", 1, true) or #FNAME + 1) + 3, (FNAME:find(".", 1, true) or 0) - 1) })` | Extracts `VpersonName` and `Vdepartment` from the filename of the active document in the format ` - .xml` using the `vars` action. Triggered only once at the start of the replace process when `Find` is empty. | -| `personname` | `set(VpersonName)` | Replaces `personname` with the content of the variable `VpersonName`, previously initialized by the `vars` action. | -| `department` | `set(Vdepartment)` | Replaces `department` with the content of the variable `Vdepartment`, previously initialized by the `vars` action. | +An empty Find string (`*(empty)*`) can be used to set variables for the entire Find and Replace list without being tied to a specific Find action. This entry does **not** match any text but is executed once at the beginning of the **'Replace'** or **'Replace All'** process when **'Use List'** is enabled. It allows the Replace field to run initialization commands like `vars()` for the entire operation. The position of this entry in the list does not affect its behavior. + +| **Find** | **Replace** | **Description/Expected Output** | +|--------------------|-------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------| +| *(empty)* | `vars({ `
`VpersonName = FNAME:sub(1, (FNAME:find(" - ", 1, true) or 0) - 1),`
`Vdepartment = FNAME:sub((FNAME:find(" - ", 1, true) or #FNAME + 1) + 3, (FNAME:find(".", 1, true) or 0) - 1) })` | Extracts `VpersonName` and `Vdepartment` from the active document’s filename in the format ` - .xml` using the `vars` action. Triggered only once at the start of the replace process when **Find** is empty. | +| `personname` | `set(VpersonName)` | Replaces `personname` with the content of the variable `VpersonName`, previously initialized by the `vars` action. | +| `department` | `set(Vdepartment)` | Replaces `department` with the content of the variable `Vdepartment`, previously initialized by the `vars` action. | #### **lvars(filePath)**