zsh-workers
 help / color / mirror / code / Atom feed
* kill-region hook
@ 2009-01-30  0:07 Michal Maruska
  2009-01-30 10:02 ` Peter Stephenson
  2009-01-30 19:59 ` Stephane Chazelas
  0 siblings, 2 replies; 3+ messages in thread
From: Michal Maruska @ 2009-01-30  0:07 UTC (permalink / raw)
  To: Zsh Hackers' List

[-- Attachment #1: Type: text/plain, Size: 533 bytes --]


Hello,

I would like to have a hook (a shell function) run when "kill-buffer" is
populated.
Motivation: I would use it to promote the string to X selection (via
an Escape sequence of rxvt).

I changed the cuttext function to call callhookfunc.
I made a function to do WideChar->Locale-encoding conversion, but I'm not sure if that is a
zsh way to do it.

Then I found zlelineasstring, but I don't understand what metafy is about (it's
called in zlelineasstring to modify the C string somehow).

Can anybody help?  Patch enclosed.



[-- Attachment #2: selection --]
[-- Type: application/octet-stream, Size: 2476 bytes --]

Index: zsh-4.3.9/Src/Zle/zle_utils.c
===================================================================
--- zsh-4.3.9.orig/Src/Zle/zle_utils.c	2009-01-29 21:28:33.297880363 +0100
+++ zsh-4.3.9/Src/Zle/zle_utils.c	2009-01-30 00:58:23.437186220 +0100
@@ -494,6 +494,8 @@
   cuttext(zleline + i, ct, flags);
 }
 
+static size_t zstring_as_cstring(const ZLE_STRING_T zs, char** res, size_t len);
+
 /*
  * As cut, but explicitly supply the text together with its length.
  */
@@ -574,12 +576,68 @@
 	ZS_memcpy(cutbuf.buf + cutbuf.len, line, ct);
 	cutbuf.len += ct;
     }
+    {  /* Invoke kill_region_hook with the cutbuf text as the only argument. */
+        char* arg;
+        size_t size;
+        LinkList hookargs = newlinklist();
+
+        arg = zlelineasstring(cutbuf.buf, cutbuf.len, 0, NULL, NULL, 0);
+/*        size = zstring_as_cstring(cutbuf.buf, &arg, cutbuf.len); */
+
+        if (size < 0) {
+            zbeep();
+        } else {
+            addlinknode(hookargs, "kill_region_hook");
+            addlinknode(hookargs, arg);
+            int retval;
+            callhookfunc("kill_region_hook", hookargs, 0, &retval); /* NULL */
+            /* zfree(arg, size);*/
+            free(arg);
+        }
+    }
     if(vilinerange)
 	cutbuf.flags |= CUTBUFFER_LINE;
     else
 	cutbuf.flags &= ~CUTBUFFER_LINE;
 }
 
+/* Convert the (sub)zstring ZS of lenght LEN, to a C-string allocated in *RES;
+ * following the Locale setting.
+ * Returns the size (of the allocated memory) or -1 if conversion failed. */
+
+static size_t
+zstring_as_cstring(const ZLE_STRING_T zs, char** res, size_t len)
+{
+#ifdef MULTIBYTE_SUPPORT
+    /* mmc: there is a difference between: "const wchar_t*" and "const ZLE_STRING_T"
+     *      so I use the cur variable: */
+    const wchar_t* cur = zs;
+    size_t size = wcsnrtombs(NULL, &cur, len, 0, NULL) + 1;
+    cur = zs;
+    if (size < 0)
+            return -1;
+#else
+    size_t size = len + 1;
+#endif  /* MULTIBYTE_SUPPORT */
+
+    *res = zalloc(size);
+#ifdef MULTIBYTE_SUPPORT
+    size_t s = wcsnrtombs(*res, &cur, len, size -1, NULL);
+    if (s<0)                    /* impossible here? */
+        {
+            zfree(*res, size);
+            return -1;
+        }
+#else
+    memcpy(*res, zs, size);
+#endif  /* MULTIBYTE_SUPPORT */
+    (*res)[size]=0;
+
+    return size+1;
+}
+
+
+
 /*
  * Now we're back in the world of zlecs where we need to keep
  * track of whether we're on a combining character.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-01-30 19:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-30  0:07 kill-region hook Michal Maruska
2009-01-30 10:02 ` Peter Stephenson
2009-01-30 19:59 ` Stephane Chazelas

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).