From: Michal Maruska <michal@ruska.it>
To: "Zsh Hackers' List" <zsh-workers@sunsite.dk>
Subject: kill-region hook
Date: Fri, 30 Jan 2009 01:07:32 +0100 [thread overview]
Message-ID: <87fxj1psfv.fsf@linux2.maruska.tin.it> (raw)
[-- 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.
next reply other threads:[~2009-01-30 0:07 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-01-30 0:07 Michal Maruska [this message]
2009-01-30 10:02 ` Peter Stephenson
2009-01-30 19:59 ` Stephane Chazelas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87fxj1psfv.fsf@linux2.maruska.tin.it \
--to=michal@ruska.it \
--cc=zsh-workers@sunsite.dk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).