summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2019-02-19 01:12:57 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2019-02-19 01:12:57 +0900
commit6c32148f9020e222bae335fa823229715e067815 (patch)
tree402d0735161514145b32ca64e7962727d060a963 /src
parent315e568de006e80138f79c77d5508c7e4853e6b2 (diff)
Add placeholder expression for zero-based item index: {n} and {+n}
Close #1482
Diffstat (limited to 'src')
-rw-r--r--src/terminal.go19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/terminal.go b/src/terminal.go
index b2b38254..c807a4ba 100644
--- a/src/terminal.go
+++ b/src/terminal.go
@@ -24,7 +24,7 @@ import (
var placeholder *regexp.Regexp
func init() {
- placeholder = regexp.MustCompile("\\\\?(?:{[+s]*[0-9,-.]*}|{q})")
+ placeholder = regexp.MustCompile("\\\\?(?:{[+s]*[0-9,-.]*}|{q}|{\\+?n})")
}
type jumpMode int
@@ -227,6 +227,7 @@ const (
type placeholderFlags struct {
plus bool
preserveSpace bool
+ number bool
query bool
}
@@ -1198,6 +1199,9 @@ func parsePlaceholder(match string) (bool, string, placeholderFlags) {
case 's':
flags.preserveSpace = true
skipChars++
+ case 'n':
+ flags.number = true
+ skipChars++
case 'q':
flags.query = true
default:
@@ -1253,7 +1257,16 @@ func replacePlaceholder(template string, stripAnsi bool, delimiter Delimiter, fo
if match == "{}" {
for idx, item := range items {
- replacements[idx] = quoteEntry(item.AsString(stripAnsi))
+ if flags.number {
+ n := int(item.text.Index)
+ if n < 0 {
+ replacements[idx] = ""
+ } else {
+ replacements[idx] = strconv.Itoa(n)
+ }
+ } else {
+ replacements[idx] = quoteEntry(item.AsString(stripAnsi))
+ }
}
return strings.Join(replacements, " ")
}
@@ -1351,7 +1364,7 @@ func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item
// 2. or it contains {+} and we have more than one item already selected.
// To do so, we pass an empty Item instead of nil to trigger an update.
if current == nil {
- current = &Item{}
+ current = &minItem
}
var sels []*Item