apply right-click-to-plumb - st - [fork] simple terminal
 (HTM) git clone https://git.drkhsh.at/st.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 89a26d5dfcde75768cf3711b93bbb7fa4fe06fc0
 (DIR) parent ae62f06cbdf4d2e3ce940719d41172c2a8c03147
 (HTM) Author: drkhsh <me@drkhsh.at>
       Date:   Tue,  1 Jul 2025 20:14:15 +0200
       
       apply right-click-to-plumb
       
       this patch allows you to right-click on some selected text to send it to
       the plumbing program of choice, e.g. open a file, view an image, open a
       URL
       
       https://st.suckless.org/patches/right_click_to_plumb/
       
       Diffstat:
         M config.def.h                        |       6 ++++++
         A patch/rightclicktoplumb.h           |       2 ++
         A patch/rightclicktoplumb_st.c        |      19 +++++++++++++++++++
         A patch/rightclicktoplumb_x.c         |      25 +++++++++++++++++++++++++
         M st.c                                |       4 +++-
         M x.c                                 |       4 ++++
       
       6 files changed, 59 insertions(+), 1 deletion(-)
       ---
 (DIR) diff --git a/config.def.h b/config.def.h
       @@ -482,3 +482,9 @@ static char ascii_printable[] =
                "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
                "`abcdefghijklmnopqrstuvwxyz{|}~";
        
       +/*
       + * plumb_cmd is run on mouse button 3 click, with argument set to
       + * current selection and with cwd set to the cwd of the active shell
       + */
       +static char *plumb_cmd = "plumb";
       +
 (DIR) diff --git a/patch/rightclicktoplumb.h b/patch/rightclicktoplumb.h
       @@ -0,0 +1,2 @@
       +void plumb(char *);
       +int subprocwd(char *);
 (DIR) diff --git a/patch/rightclicktoplumb_st.c b/patch/rightclicktoplumb_st.c
       @@ -0,0 +1,19 @@
       +#if defined(__OpenBSD__)
       + #include <sys/sysctl.h>
       +#endif
       +
       +int
       +subprocwd(char *path)
       +{
       +#if   defined(__linux)
       +        if (snprintf(path, PATH_MAX, "/proc/%d/cwd", pid) < 0)
       +                return -1;
       +        return 0;
       +#elif defined(__OpenBSD__)
       +        size_t sz = PATH_MAX;
       +        int name[3] = {CTL_KERN, KERN_PROC_CWD, pid};
       +        if (sysctl(name, 3, path, &sz, 0, 0) == -1)
       +                return -1;
       +        return 0;
       +#endif
       +}
 (DIR) diff --git a/patch/rightclicktoplumb_x.c b/patch/rightclicktoplumb_x.c
       @@ -0,0 +1,25 @@
       +#include <sys/wait.h>
       +
       +void
       +plumb(char *sel)
       +{
       +        if (sel == NULL)
       +                return;
       +
       +        char cwd[PATH_MAX];
       +        pid_t child;
       +
       +        if (subprocwd(cwd) != 0)
       +                return;
       +
       +        switch (child = fork()) {
       +                case -1:
       +                        return;
       +                case 0:
       +                        if (chdir(cwd) != 0)
       +                                exit(1);
       +                        if (execvp(plumb_cmd, (char *const []){plumb_cmd, sel, 0}) == -1)
       +                                exit(1);
       +                        exit(0);
       +        }
       +}
 (DIR) diff --git a/st.c b/st.c
       @@ -216,6 +216,7 @@ static const Rune utfmin[UTF_SIZ + 1] = {       0,    0,  0x80,  0x800,  0x10000
        static const Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
        
        #include "patch/reflow.h"
       +#include "patch/rightclicktoplumb.h"
        
        ssize_t
        xwrite(int fd, const char *s, size_t len)
       @@ -641,7 +642,7 @@ ttynew(const char *line, char *cmd, const char *out, char **args)
                        break;
                default:
        #ifdef __OpenBSD__
       -                if (pledge("stdio rpath tty proc", NULL) == -1)
       +                if (pledge("stdio rpath tty proc ps exec", NULL) == -1)
                                die("pledge\n");
        #endif
                        close(s);
       @@ -2556,6 +2557,7 @@ drawregion(int x1, int y1, int x2, int y2)
        }
        
        #include "patch/reflow.c"
       +#include "patch/rightclicktoplumb_st.c"
        
        void
        draw(void)
 (DIR) diff --git a/x.c b/x.c
       @@ -40,6 +40,7 @@ static void ttysend(const Arg *);
        
        #include "patch/alpha.h"
        #include "patch/reflow.h"
       +#include "patch/rightclicktoplumb.h"
        
        /* config.h for applying patches and the configuration. */
        #include "config.h"
       @@ -171,6 +172,7 @@ static char *opt_title = NULL;
        static uint buttons; /* bit field of pressed buttons */
        
        #include "patch/alpha.c"
       +#include "patch/rightclicktoplumb_x.c"
        
        void
        clipcopy(const Arg *dummy)
       @@ -649,6 +651,8 @@ brelease(XEvent *e)
                        return;
                if (btn == Button1)
                        mousesel(e, 1);
       +        else if (btn == Button3)
       +                plumb(xsel.primary);
        }
        
        void