about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-09-25 02:08:33 +0200
committerbptato <nincsnevem662@gmail.com>2023-09-25 02:08:33 +0200
commit463499131a1b4ca41fea56030f77f4794cdca071 (patch)
tree54f2efea637c9762711db4140ff42ae833a0900e
parent790e937795746563246808abaf4b0b25f4a5d0fe (diff)
downloadchawan-463499131a1b4ca41fea56030f77f4794cdca071.tar.gz
Add M-p as "load page on clipboard"
-rw-r--r--doc/config.md8
-rw-r--r--res/chawan.html2
-rw-r--r--res/config.toml20
-rw-r--r--src/extern/runproc.nim13
-rw-r--r--src/local/pager.nim11
5 files changed, 47 insertions, 7 deletions
diff --git a/doc/config.md b/doc/config.md
index ca7924b9..8796ccfe 100644
--- a/doc/config.md
+++ b/doc/config.md
@@ -902,6 +902,14 @@ is true. Redirect to /dev/null in the command if this is not desired. (This
 will be fixed in the future.)</td>
 </tr>
 
+<tr>
+<td>`pager.externCapture(cmd)`
+</td>
+<td>Like extern(), but redirect the command's stdout string into the
+result. null is returned if the command wasn't executed successfully, or if
+the command returned a non-zero exit value.</td>
+</tr>
+
 </table>
 
 
diff --git a/res/chawan.html b/res/chawan.html
index c2ba5b61..81f9bbea 100644
--- a/res/chawan.html
+++ b/res/chawan.html
@@ -34,6 +34,8 @@ the meta key may be called Alt or Escape.)
 <li><b>U</b>: reload page
 <li><b>,</b> (comma), <b>.</b> (period): previous/next buffer
 <li><b>D</b>: discard current buffer
+<li><b>M-y</b>: copy current buffer's URL to clipboard (needs xsel)
+<li><b>M-p</b>: go to the URL currently on the clipboard (needs xsel)
 <li><b>[</b>, <b>]</b>: move cursor to the previous/next hyperlink
 <li><b>C-d</b>, <b>C-u</b>: scroll up/down by half a page
 <li><b>C-f</b>, <b>C-b</b> (or <b>PgDn</b>, <b>PgUp</b>)</b>: scroll up/down by an entire page
diff --git a/res/config.toml b/res/config.toml
index 3374e7d8..89b42cbe 100644
--- a/res/config.toml
+++ b/res/config.toml
@@ -132,10 +132,22 @@ config.search.wrap = !config.search.wrap;
 pager.alert("Wrap search " + (config.search.wrap ? "on" : "off"));
 '''
 M-y = '''
-pager.extern('printf \'%s\\n\' "$CHA_URL" | xsel -bi',
-		{suspend: false, setenv: true}) ?
-	pager.alert("Copied URL to clipboard.") :
-	pager.alert("Failed to copy URL to clipboard. (Is xsel installed?)");
+() => {
+	if (pager.extern('printf \'%s\\n\' "$CHA_URL" | xsel -bi',
+			{suspend: false, setenv: true}))
+		pager.alert("Copied URL to clipboard.");
+	else
+		pager.alert("Failed to copy URL to clipboard. (Is xsel installed?)");
+}
+'''
+M-p = '''
+() => {
+	const s = pager.externCapture('xsel -bo');
+	if (s === null)
+		pager.alert("Failed to read URL from clipboard. (Is xsel installed?)");
+	else
+		pager.load(s + '\n');
+}
 '''
 
 [line]
diff --git a/src/extern/runproc.nim b/src/extern/runproc.nim
index c49d98db..fa1ed9bc 100644
--- a/src/extern/runproc.nim
+++ b/src/extern/runproc.nim
@@ -1,3 +1,4 @@
+import streams
 import posix
 
 import display/term
@@ -27,3 +28,15 @@ proc runProcess*(term: Terminal, cmd: string, wait = false): bool =
   if wait:
     term.anyKey()
   term.restart()
+
+# Run process, and capture its output.
+proc runProcessCapture*(term: Terminal, cmd: string, outs: var string): bool =
+  let file = popen(cmd, "r")
+  if file == nil:
+    return false
+  let fs = newFileStream(file)
+  outs = fs.readAll()
+  let rv = pclose(file)
+  if rv == -1:
+    return false
+  return rv == 0
diff --git a/src/local/pager.nim b/src/local/pager.nim
index d4853d31..dce66505 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -854,9 +854,7 @@ type ExternDict = object of JSDict
   suspend: Opt[bool]
   wait: bool
 
-#TODO this could be handled much better.
-# * suspend, setenv, wait as dict flags
-# * retval as int?
+#TODO we should have versions with retval as int?
 proc extern(pager: Pager, cmd: string, t = ExternDict()): bool {.jsfunc.} =
   if t.setenv.get(true):
     pager.setEnvVars()
@@ -865,6 +863,13 @@ proc extern(pager: Pager, cmd: string, t = ExternDict()): bool {.jsfunc.} =
   else:
     return runProcess(cmd)
 
+proc externCapture(pager: Pager, cmd: string): Opt[string] {.jsfunc.} =
+  pager.setEnvVars()
+  var s: string
+  if not runProcessCapture(pager.term, cmd, s):
+    return err()
+  return ok(s)
+
 proc authorize(pager: Pager) =
   pager.setLineEdit("Username: ", USERNAME)