about summary refs log tree commit diff stats
path: root/src/ips
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-11-28 19:52:10 +0100
committerbptato <nincsnevem662@gmail.com>2022-11-28 23:00:06 +0100
commiteb2e57c97eb67eec19f068e294a8f6d1375c82f5 (patch)
tree87156c515f6ee9a63f58dc080184bd3127ce6836 /src/ips
parent8af10b8b74fd29fe4c9debcd5cbecfaddf53a7b5 (diff)
downloadchawan-eb2e57c97eb67eec19f068e294a8f6d1375c82f5.tar.gz
Add textarea
Editing is implemented using an external editor (like vi).
Diffstat (limited to 'src/ips')
-rw-r--r--src/ips/editor.nim59
-rw-r--r--src/ips/forkserver.nim16
-rw-r--r--src/ips/serialize.nim17
-rw-r--r--src/ips/serversocket.nim8
-rw-r--r--src/ips/socketstream.nim2
5 files changed, 83 insertions, 19 deletions
diff --git a/src/ips/editor.nim b/src/ips/editor.nim
new file mode 100644
index 00000000..3bce0fa9
--- /dev/null
+++ b/src/ips/editor.nim
@@ -0,0 +1,59 @@
+import os
+
+import config/config
+import display/term
+
+func formatEditorName(editor, file: string, line: int): string =
+  result = newStringOfCap(editor.len + file.len)
+  var i = 0
+  var filefound = false
+  while i < editor.len:
+    if editor[i] == '%' and i < editor.high:
+      if editor[i + 1] == 's':
+        result &= file
+        filefound = true
+        i += 2
+        continue
+      elif editor[i + 1] == 'd':
+        result &= $line
+        i += 2
+        continue
+      elif editor[i + 1] == '%':
+        result &= '%'
+        i += 2
+        continue
+    result &= editor[i]
+    inc i
+  if editor.len == 0:
+    result = "vi"
+  if not filefound:
+    if result[^1] != ' ':
+      result &= ' '
+    result &= file
+
+proc openEditor*(term: Terminal, config: Config, file: string, line = 0): bool =
+  var editor = config.editor
+  if editor == "":
+    editor = getEnv("EDITOR")
+    if editor == "":
+      editor = "vi %s +%d"
+  let cmd = formatEditorName(editor, file, line)
+  term.quit()
+  result = execShellCmd(cmd) == 0
+  term.restart()
+
+var tmpf_seq: int
+proc openInEditor*(term: Terminal, config: Config, input: var string): bool =
+  try:
+    let tmpdir = config.tmpdir
+    if not dirExists(tmpdir):
+      createDir(tmpdir)
+    let tmpf = tmpdir / "chatmp" & $tmpf_seq
+    inc tmpf_seq
+    writeFile(tmpf, input)
+    if openEditor(term, config, tmpf):
+      input = readFile(tmpf)
+      removeFile(tmpf)
+      return true
+  except IOError:
+    discard
diff --git a/src/ips/forkserver.nim b/src/ips/forkserver.nim
index 1c29b120..6e95fc8e 100644
--- a/src/ips/forkserver.nim
+++ b/src/ips/forkserver.nim
@@ -4,16 +4,18 @@ when defined(posix):
   import posix
 
 import buffer/buffer
-import config/bufferconfig
+import config/config
 import io/loader
 import io/request
 import io/window
 import ips/serialize
+import ips/serversocket
 import types/buffersource
+import utils/twtstr
 
 type
   ForkCommand* = enum
-    FORK_BUFFER, FORK_LOADER, REMOVE_CHILD
+    FORK_BUFFER, FORK_LOADER, REMOVE_CHILD, LOAD_CONFIG
 
   ForkServer* = ref object
     process*: Pid
@@ -31,6 +33,11 @@ proc newFileLoader*(forkserver: ForkServer, defaultHeaders: HeaderList = Default
   forkserver.ostream.flush()
   forkserver.istream.sread(result)
 
+proc loadForkServerConfig*(forkserver: ForkServer, config: Config) =
+  forkserver.ostream.swrite(LOAD_CONFIG)
+  forkserver.ostream.swrite(config.getForkServerConfig())
+  forkserver.ostream.flush()
+
 proc removeChild*(forkserver: Forkserver, pid: Pid) =
   forkserver.ostream.swrite(REMOVE_CHILD)
   forkserver.ostream.swrite(pid)
@@ -103,6 +110,11 @@ proc runForkServer() =
         let loader = ctx.forkLoader(defaultHeaders)
         ctx.ostream.swrite(loader)
         ctx.children.add((loader.process, Pid(-1)))
+      of LOAD_CONFIG:
+        var config: ForkServerConfig
+        ctx.istream.sread(config)
+        width_table = makewidthtable(config.ambiguous_double)
+        SocketDirectory = config.tmpdir
       ctx.ostream.flush()
     except IOError:
       # EOF
diff --git a/src/ips/serialize.nim b/src/ips/serialize.nim
index 2647db2f..6eeaaca9 100644
--- a/src/ips/serialize.nim
+++ b/src/ips/serialize.nim
@@ -5,7 +5,6 @@ import streams
 import tables
 
 import buffer/cell
-import config/bufferconfig
 import io/request
 import js/regex
 import types/buffersource
@@ -92,7 +91,7 @@ proc slen*[T](o: T): int =
 template swrite*[T](stream: Stream, o: T) =
   stream.write(o)
 
-proc swrite*(stream: Stream, s: string, maxlen = 8192) =
+proc swrite*(stream: Stream, s: string) =
   stream.swrite(s.len)
   stream.write(s)
 
@@ -182,9 +181,6 @@ proc swrite*(stream: Stream, source: BufferSource) =
   stream.swrite(source.location)
   stream.swrite(source.contenttype)
 
-proc swrite*(stream: Stream, bconfig: BufferConfig) =
-  stream.swrite(bconfig.userstyle)
-
 proc swrite*(stream: Stream, tup: tuple) =
   for f in tup.fields:
     stream.swrite(f)
@@ -196,11 +192,11 @@ proc swrite*(stream: Stream, obj: object) =
 template sread*[T](stream: Stream, o: T) =
   stream.read(o)
 
-proc sread*(stream: Stream, s: var string, maxlen = 8192) =
+proc sread*(stream: Stream, s: var string) =
   var len: int
   stream.sread(len)
-  if maxlen != -1:
-    len = min(maxlen, len)
+  #if maxlen != -1:
+  #  len = min(maxlen, len)
   stream.readStr(len, s)
 
 proc sread*(stream: Stream, b: var bool) =
@@ -214,7 +210,7 @@ proc sread*(stream: Stream, b: var bool) =
 
 proc sread*(stream: Stream, url: var Url) =
   var s: string
-  stream.sread(s, 2048)
+  stream.sread(s)
   url = newURL(s)
 
 proc sread*(stream: Stream, headers: var HeaderList) =
@@ -323,9 +319,6 @@ proc sread*(stream: Stream, source: var BufferSource) =
   stream.sread(source.location)
   stream.sread(source.contenttype)
 
-proc sread*(stream: Stream, bconfig: var BufferConfig) =
-  stream.sread(bconfig.userstyle)
-
 proc sread*(stream: Stream, obj: var object) =
   for f in obj.fields:
     stream.sread(f)
diff --git a/src/ips/serversocket.nim b/src/ips/serversocket.nim
index 8ed79ab3..b476320e 100644
--- a/src/ips/serversocket.nim
+++ b/src/ips/serversocket.nim
@@ -7,10 +7,10 @@ type ServerSocket* = object
   sock*: Socket
   path*: string
 
-const SocketDirectory = "/tmp/cha/"
-const SocketPathPrefix = SocketDirectory & "cha_sock_"
-func getSocketPath*(pid: Pid): string =
-  SocketPathPrefix & $pid
+var SocketDirectory* = "/tmp/cha"
+const SocketPathPrefix = "cha_sock_"
+proc getSocketPath*(pid: Pid): string =
+  SocketDirectory / SocketPathPrefix & $pid
 
 proc initServerSocket*(buffered = true): ServerSocket =
   createDir(SocketDirectory)
diff --git a/src/ips/socketstream.nim b/src/ips/socketstream.nim
index df4bbf09..ff2d189e 100644
--- a/src/ips/socketstream.nim
+++ b/src/ips/socketstream.nim
@@ -18,7 +18,7 @@ proc sockReadData(s: Stream, buffer: pointer, len: int): int =
   result = s.source.recv(buffer, len)
   if result < 0:
     raise newException(IOError, "Failed to read data (code " & $osLastError() & ")")
-  elif result < len:
+  elif result == 0:
     s.isend = true
 
 proc sockWriteData(s: Stream, buffer: pointer, len: int) =