st-newterm-0.8.2.diff 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. From a7eedc85e0609177cdb1ed3f6203fa37e6420012 Mon Sep 17 00:00:00 2001
  2. From: Matias Lang <yo@matiaslang.me>
  3. Date: Wed, 17 Jul 2019 01:10:44 -0300
  4. Subject: [PATCH] Add shortcut to spawn new terminal in the current dir
  5. Ctrl-Shift-Return now creates a new ST terminal, whose CWD is the same
  6. as the parent st's CWD
  7. ---
  8. config.def.h | 1 +
  9. st.c | 21 +++++++++++++++++++++
  10. st.h | 1 +
  11. 3 files changed, 23 insertions(+)
  12. diff --git a/config.def.h b/config.def.h
  13. index 0e01717..31f26d8 100644
  14. --- a/config.def.h
  15. +++ b/config.def.h
  16. @@ -178,6 +178,7 @@ static Shortcut shortcuts[] = {
  17. { TERMMOD, XK_Y, selpaste, {.i = 0} },
  18. { ShiftMask, XK_Insert, selpaste, {.i = 0} },
  19. { TERMMOD, XK_Num_Lock, numlock, {.i = 0} },
  20. + { TERMMOD, XK_Return, newterm, {.i = 0} },
  21. };
  22. /*
  23. diff --git a/st.c b/st.c
  24. index b8e6077..044e29b 100644
  25. --- a/st.c
  26. +++ b/st.c
  27. @@ -153,6 +153,7 @@ typedef struct {
  28. } STREscape;
  29. static void execsh(char *, char **);
  30. +static char *getcwd_by_pid(pid_t pid);
  31. static void stty(char **);
  32. static void sigchld(int);
  33. static void ttywriteraw(const char *, size_t);
  34. @@ -1059,6 +1060,26 @@ tswapscreen(void)
  35. tfulldirt();
  36. }
  37. +void
  38. +newterm(const Arg* a)
  39. +{
  40. + switch (fork()) {
  41. + case -1:
  42. + die("fork failed: %s\n", strerror(errno));
  43. + break;
  44. + case 0:
  45. + chdir(getcwd_by_pid(pid));
  46. + execlp("st", "./st", NULL);
  47. + break;
  48. + }
  49. +}
  50. +
  51. +static char *getcwd_by_pid(pid_t pid) {
  52. + char buf[32];
  53. + snprintf(buf, sizeof buf, "/proc/%d/cwd", pid);
  54. + return realpath(buf, NULL);
  55. +}
  56. +
  57. void
  58. tscrolldown(int orig, int n)
  59. {
  60. diff --git a/st.h b/st.h
  61. index 38c61c4..54d4a43 100644
  62. --- a/st.h
  63. +++ b/st.h
  64. @@ -80,6 +80,7 @@ void die(const char *, ...);
  65. void redraw(void);
  66. void draw(void);
  67. +void newterm(const Arg *);
  68. void printscreen(const Arg *);
  69. void printsel(const Arg *);
  70. void sendbreak(const Arg *);
  71. --
  72. 2.19.2