-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstpaster
More file actions
executable file
·95 lines (82 loc) · 1.97 KB
/
stpaster
File metadata and controls
executable file
·95 lines (82 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/tcc -run
/* vim: set syn=c: */
/*
* stpaster intends to pass st(1) terminal content to dmenu(1) for rapid
* selection and finaly in xclip(1) for pasting easily.
*
* Remove empty lines from stdin and output full‐lines, WORDS splited by
* space/tab, words splited by work breakers.
*
* see st & externalpipe patch.
* http://st.suckless.org/
* http://st.suckless.org/patches/externalpipe
*
* is_word_break() comes from :
* http://st.suckless.org/patches/configwordbreak
*
* Add to config.h shortcuts :
* { MODKEY, 'u', externalpipe, {.v = "trim | stpaster | sort -u | dmenu -i -l 5 | removenl | xclip -in -selection c" } },
*
* memo :
* 0x09 - horizontal tab
* 0x0a - linefeed
* 0x0b - vertical tab
* 0x0c - form feed
* 0x0d - carriage return
* 0x20 - space
**/
#include <stdlib.h>
#include <stdio.h>
#define WORD_BREAK " ()<>[]\",/:'."
static unsigned int is_word_break(char c);
int
main() {
char *line = NULL;
size_t len = 0;
ssize_t nread = 0;
unsigned int i = 0;
unsigned int wasblank = 1;
unsigned int emptyline = 1;
while ((nread = getline(&line, &len, stdin)) != -1) {
/* remove empty lines */
emptyline = 1;
for (i = 0; i < nread; ++i)
if(line[i] != 0x20 && line[i] != 0x09 && line[i] != 0x0a)
emptyline = 0;
if (emptyline)
continue;
/* print each lines as is*/
printf("%s", line);
/* split each lines on space/tab */
for (i = 0; i < nread; ++i)
if (line[i] != 0x20 && line[i] != 0x09) {
wasblank = 0;
printf("%c", line[i]);
} else if (!wasblank) {
wasblank = 1;
printf("\n");
}
wasblank = 1;
/* split each lines on word breaker */
for (i = 0; i < nread; ++i)
if (!is_word_break(line[i])) {
wasblank = 0;
printf("%c", line[i]);
} else if (!wasblank) {
wasblank = 1;
printf("\n");
}
}
return 0;
}
unsigned int
is_word_break(char c) {
static char *word_break = WORD_BREAK;
char *s = word_break;
while (*s) {
if (*s == c)
return 1;
s++;
}
return 0;
}