From e2ef7d668c6d4526ddd1e1e21e60bc0a69e99c2e Mon Sep 17 00:00:00 2001
From: Přemysl Eric Janouch <p@janouch.name>
Date: Sun, 18 Sep 2022 01:09:41 +0200
Subject: xP: implement Readline's M-b and M-f

---
 xP/public/xP.js | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

(limited to 'xP/public')

diff --git a/xP/public/xP.js b/xP/public/xP.js
index 71c1b6e..aa2c39a 100644
--- a/xP/public/xP.js
+++ b/xP/public/xP.js
@@ -742,6 +742,33 @@ let Input = {
 		return true
 	},
 
+	backward: (b, textarea) => {
+		if (textarea.selectionStart !== textarea.selectionEnd)
+			return false
+
+		let point = textarea.selectionStart
+		if (point < 1)
+			return false
+		while (point && /\s/.test(textarea.value.charAt(--point))) {}
+		while (point-- && !/\s/.test(textarea.value.charAt(point))) {}
+		point++
+		textarea.setSelectionRange(point, point)
+		return true
+	},
+
+	forward: (b, textarea) => {
+		if (textarea.selectionStart !== textarea.selectionEnd)
+			return false
+
+		let point = textarea.selectionStart, len = textarea.value.length
+		if (point + 1 > len)
+			return false
+		while (point < len && /\s/.test(textarea.value.charAt(point))) point++
+		while (point < len && !/\s/.test(textarea.value.charAt(point))) point++
+		textarea.setSelectionRange(point, point)
+		return true
+	},
+
 	first: (b, textarea) => {
 		if (b.historyAt <= 0)
 			return false
@@ -796,6 +823,8 @@ let Input = {
 		if (hasShortcutModifiers(event)) {
 			handled = true
 			switch (event.key) {
+			case 'b': success = Input.backward(b, textarea); break
+			case 'f': success = Input.forward(b, textarea);  break
 			case '<': success = Input.first(b, textarea);    break
 			case '>': success = Input.last(b, textarea);     break
 			case 'p': success = Input.previous(b, textarea); break
-- 
cgit v1.2.3-70-g09d2