tools/stpaster
2016-10-05 12:08:35 +02:00

95 lines
2 KiB
C
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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 fulllines, 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;
}