Improve html dictionaries
This commit is contained in:
parent
ef1d1658b6
commit
16bdce0c6f
27
Makefile
27
Makefile
|
@ -1,9 +1,9 @@
|
||||||
include config.mk
|
include config.mk
|
||||||
|
|
||||||
SRC = popup.c
|
SRC = popup.c format_output.c
|
||||||
OBJ = $(SRC:.c=.o)
|
OBJ = $(SRC:.c=.o)
|
||||||
|
|
||||||
all: options popup
|
all: options dictpopup
|
||||||
|
|
||||||
options:
|
options:
|
||||||
@echo popup build options:
|
@echo popup build options:
|
||||||
|
@ -11,20 +11,33 @@ options:
|
||||||
@echo "LDFLAGS = $(LDFLAGS)"
|
@echo "LDFLAGS = $(LDFLAGS)"
|
||||||
@echo "CC = $(CC)"
|
@echo "CC = $(CC)"
|
||||||
|
|
||||||
|
config.h:
|
||||||
|
cp config.def.h config.h
|
||||||
|
|
||||||
.c.o:
|
.c.o:
|
||||||
$(CC) -c $(CFLAGS) $<
|
$(CC) -c $(CFLAGS) $<
|
||||||
|
|
||||||
popup: popup.o
|
popup.o: config.h
|
||||||
$(CC) -o $@ popup.o $(LDFLAGS)
|
|
||||||
|
$(OBJ): config.h config.mk
|
||||||
|
|
||||||
|
dictpopup: $(OBJ)
|
||||||
|
$(CC) -o format_output format_output.o
|
||||||
|
$(CC) -o popup popup.o $(LDFLAGS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f popup $(OBJ) format_output
|
||||||
|
|
||||||
install: all
|
install: all
|
||||||
mkdir -p ${DESTDIR}${PREFIX}/bin
|
mkdir -p ${DESTDIR}${PREFIX}/bin
|
||||||
cp -f popup ${DESTDIR}${PREFIX}/bin
|
cp -f popup ${DESTDIR}${PREFIX}/bin
|
||||||
|
cp -f dictpopup ${DESTDIR}${PREFIX}/bin
|
||||||
|
cp -f format_output ${DESTDIR}${PREFIX}/bin
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
rm -f ${DESTDIR}${PREFIX}/bin/popup
|
rm -f ${DESTDIR}${PREFIX}/bin/popup
|
||||||
|
rm -f ${DESTDIR}${PREFIX}/bin/dictpopup
|
||||||
|
rm -f ${DESTDIR}${PREFIX}/bin/format_output
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -f popup $(OBJ)
|
|
||||||
|
|
||||||
.PHONY: all install uninstall clean
|
.PHONY: all options clean install uninstall
|
||||||
|
|
20
README.md
20
README.md
|
@ -5,17 +5,21 @@ This is a very lightweight program to show a popup with the translation of the s
|
||||||
![image](https://github.com/GenjiFujimoto/dictpopup/assets/50422430/c4a3663b-fd91-4a66-95ad-f1528071c932)
|
![image](https://github.com/GenjiFujimoto/dictpopup/assets/50422430/c4a3663b-fd91-4a66-95ad-f1528071c932)
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
xclip, sdcv
|
sselp (can be replaced with xclip -o), sdcv. Optional: lynx for html support
|
||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
First setup [sdcv](https://github.com/Dushistov/sdcv) according to their github page.
|
First setup [sdcv](https://github.com/Dushistov/sdcv) according to their github page.
|
||||||
Then compile with `make` and place `dictpopup` as well as `popup` in your PATH.
|
Then install with `sudo make install`. \
|
||||||
|
Uninstall with `sudo make uninstall`
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
Call `dictpopup` to translate and display the popup of the selected text. The
|
Call `dictpopup [html] [<WORD>]`.\
|
||||||
popup can be dismissed by clicking on it.
|
If no word as an argument is supplied, the selection is used.\
|
||||||
The styling can be changed in `config.h`, but it has the be recompiled.
|
If the string `html` is supplied as the first argument, then html support will be enabled.\
|
||||||
There is also a xresources patch from [herbe](https://github.com/dudik/herbe)
|
The popup can be dismissed by clicking on it.
|
||||||
that you could try to apply.
|
|
||||||
|
|
||||||
You can also use `popup` like `popup "Hello"` for other things if you want.
|
The styling can be changed in `config.h` and then recompile to apply.
|
||||||
|
There is also a xresources patch from [herbe](https://github.com/dudik/herbe)
|
||||||
|
that you could try to apply if you like.
|
||||||
|
|
||||||
|
`popup` can also be used as a standalone program to show the contents of stdin.
|
||||||
|
|
|
@ -5,10 +5,13 @@ static const char *font_pattern = "Noto Sans Mono CJK JP:size=12";
|
||||||
static const unsigned line_spacing = 5;
|
static const unsigned line_spacing = 5;
|
||||||
static const unsigned int padding = 15;
|
static const unsigned int padding = 15;
|
||||||
|
|
||||||
static const unsigned int width = 450;
|
static const unsigned int width = 480;
|
||||||
static const unsigned int border_size = 1;
|
static const unsigned int border_size = 1;
|
||||||
|
|
||||||
static const unsigned int duration = 5; /* in seconds */
|
static const unsigned int duration = 5; /* in seconds */
|
||||||
|
|
||||||
#define DISMISS_BUTTON Button1
|
#define MIN_BORDER_DISTANCE 3 /* The minimum distance to the monitor boundary */
|
||||||
#define ACTION_BUTTON Button3
|
|
||||||
|
#define DISMISS_BUTTON Button1 /* left click */
|
||||||
|
#define ACTION1_BUTTON Button4 /* scroll up */
|
||||||
|
#define ACTION2_BUTTON Button5 /* scroll down */
|
26
dictpopup
26
dictpopup
|
@ -1,10 +1,24 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# Looks up argument instead, if provided
|
# Depends on:
|
||||||
word=${1:-$(sselp)}
|
# - sdcv
|
||||||
[ -z $word ] && exit 1
|
# - sselp (for selection support)
|
||||||
|
# - lynx (for html support)
|
||||||
|
# invocation: dictpopup [html] [word]
|
||||||
|
# - If no argument provided, the selection is used
|
||||||
|
# - If supplied with the html option, the dictionary output will be piped through lynx
|
||||||
|
# in order to format html as plain text
|
||||||
|
|
||||||
sdcv -n --utf8-output -e "$word" | tail -n +5 | sed 's|<br>|\n|g' | sed 's|<[^>]*>||g' | popup
|
# TODO: Switch between dictionaries / dictionary results.
|
||||||
|
# Implementation idea (in C): Store each dictionary entry in an array. Invoke popup with a dictionary entry.
|
||||||
|
# Increment, decrement array and reinvoke or quit according to exit code of popup
|
||||||
|
|
||||||
# Instead of sed also possible:
|
if [ "$1" = "html" ]; then
|
||||||
# `lynx -dump -stdin -assume_charset=UTF-8 -display_charset=UTF-8`
|
word=${2:-$(sselp)}
|
||||||
|
sdcv -n --utf8-output -e "$word" | format_output |
|
||||||
|
lynx -dump -stdin -assume_charset=UTF-8 -display_charset=UTF-8 |
|
||||||
|
popup
|
||||||
|
else
|
||||||
|
word=${1:-$(sselp)}
|
||||||
|
sdcv -n --utf8-output -e "$word" | format_output | popup
|
||||||
|
fi
|
||||||
|
|
32
format_output.c
Normal file
32
format_output.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int second = 0;
|
||||||
|
static char *buf = NULL;
|
||||||
|
static size_t size = 0;
|
||||||
|
ssize_t len = 0;
|
||||||
|
for (long n=1; (len = getline(&buf, &size, stdin)) > 0; n++)
|
||||||
|
{
|
||||||
|
if (n < 5)
|
||||||
|
continue;
|
||||||
|
if (len && buf[len - 1] == '\n')
|
||||||
|
buf[len - 1] = '\0';
|
||||||
|
if (strncmp(buf,"-->", 3) == 0)
|
||||||
|
{
|
||||||
|
if (second)
|
||||||
|
{
|
||||||
|
second = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
putchar('\n');
|
||||||
|
second = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (buf[0] != 0) /* Don't print newline at EOF */
|
||||||
|
puts(buf);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
88
popup.c
88
popup.c
|
@ -1,6 +1,5 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <signal.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
@ -14,18 +13,17 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
/* macros */
|
|
||||||
#define MAX(A, B) ((A) > (B) ? (A) : (B))
|
#define MAX(A, B) ((A) > (B) ? (A) : (B))
|
||||||
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
||||||
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
|
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
|
||||||
* MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
|
* MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
|
||||||
|
|
||||||
//TODO: Make selection possible
|
//TODO: Make selection possible
|
||||||
//TODO: Switch between dictionaries
|
|
||||||
|
|
||||||
#define EXIT_ACTION 0
|
#define EXIT_DISMISS 0
|
||||||
#define EXIT_FAIL 1
|
#define EXIT_FAIL 1
|
||||||
#define EXIT_DISMISS 2
|
#define EXIT_ACTION1 2
|
||||||
|
#define EXIT_ACTION2 3
|
||||||
|
|
||||||
static Display *dpy;
|
static Display *dpy;
|
||||||
static Window window;
|
static Window window;
|
||||||
|
@ -78,45 +76,21 @@ int get_max_len(char *string, XftFont *font, int max_text_width)
|
||||||
if (info.width <= max_text_width)
|
if (info.width <= max_text_width)
|
||||||
return eol;
|
return eol;
|
||||||
|
|
||||||
int temp = eol;
|
|
||||||
|
|
||||||
while (string[eol] != ' ' && eol)
|
/* prefer splitting at spaces */
|
||||||
--eol;
|
/* int temp = eol; */
|
||||||
|
/* while (string[eol] != ' ' && eol) */
|
||||||
|
/* --eol; */
|
||||||
|
|
||||||
if (eol == 0)
|
/* if (eol == 0) */
|
||||||
return temp;
|
/* return temp; */
|
||||||
else
|
/* else */
|
||||||
return ++eol;
|
/* return ++eol; */
|
||||||
}
|
return eol;
|
||||||
|
|
||||||
void expire(int sig)
|
|
||||||
{
|
|
||||||
XEvent event;
|
|
||||||
event.type = ButtonPress;
|
|
||||||
event.xbutton.button = (sig == SIGUSR2) ? (ACTION_BUTTON) : (DISMISS_BUTTON);
|
|
||||||
XSendEvent(dpy, window, 0, 0, &event);
|
|
||||||
XFlush(dpy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
struct sigaction act_expire, act_ignore;
|
|
||||||
|
|
||||||
act_expire.sa_handler = expire;
|
|
||||||
act_expire.sa_flags = SA_RESTART;
|
|
||||||
sigemptyset(&act_expire.sa_mask);
|
|
||||||
|
|
||||||
act_ignore.sa_handler = SIG_IGN;
|
|
||||||
act_ignore.sa_flags = 0;
|
|
||||||
sigemptyset(&act_ignore.sa_mask);
|
|
||||||
|
|
||||||
sigaction(SIGALRM, &act_expire, 0);
|
|
||||||
sigaction(SIGTERM, &act_expire, 0);
|
|
||||||
sigaction(SIGINT, &act_expire, 0);
|
|
||||||
|
|
||||||
sigaction(SIGUSR1, &act_ignore, 0);
|
|
||||||
sigaction(SIGUSR2, &act_ignore, 0);
|
|
||||||
|
|
||||||
if (!(dpy = XOpenDisplay(0)))
|
if (!(dpy = XOpenDisplay(0)))
|
||||||
die("Cannot open display");
|
die("Cannot open display");
|
||||||
|
|
||||||
|
@ -144,16 +118,11 @@ int main()
|
||||||
|
|
||||||
static char *buf = NULL;
|
static char *buf = NULL;
|
||||||
static size_t size = 0;
|
static size_t size = 0;
|
||||||
ssize_t len = 0;
|
char *line = NULL;
|
||||||
char *str;
|
while (getline(&buf, &size, stdin) > 0)
|
||||||
for (;(len = getline(&buf, &size, stdin)) > 0;)
|
|
||||||
{
|
{
|
||||||
/* Remove the trailing newline if one is present. */
|
line=buf;
|
||||||
if (len && buf[len - 1] == '\n')
|
for (unsigned int eol = get_max_len(line, font, max_text_width); eol; line += eol, num_of_lines++, eol = get_max_len(line, font, max_text_width))
|
||||||
buf[len - 1] = '\0';
|
|
||||||
|
|
||||||
str=buf;
|
|
||||||
for (unsigned int eol = get_max_len(str, font, max_text_width); eol; str += eol, num_of_lines++, eol = get_max_len(str, font, max_text_width))
|
|
||||||
{
|
{
|
||||||
if (lines_size <= num_of_lines)
|
if (lines_size <= num_of_lines)
|
||||||
{
|
{
|
||||||
|
@ -166,10 +135,13 @@ int main()
|
||||||
if (!lines[num_of_lines])
|
if (!lines[num_of_lines])
|
||||||
die("malloc failed");
|
die("malloc failed");
|
||||||
|
|
||||||
strncpy(lines[num_of_lines], str, eol);
|
strncpy(lines[num_of_lines], line, eol);
|
||||||
lines[num_of_lines][eol] = '\0';
|
lines[num_of_lines][eol] = '\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
free(buf);
|
||||||
|
if (num_of_lines == 0)
|
||||||
|
die("stdin is empty");
|
||||||
|
|
||||||
unsigned int text_height = font->ascent - font->descent;
|
unsigned int text_height = font->ascent - font->descent;
|
||||||
unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding;
|
unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding;
|
||||||
|
@ -178,7 +150,7 @@ int main()
|
||||||
unsigned int du;
|
unsigned int du;
|
||||||
Window dw;
|
Window dw;
|
||||||
if (!XQueryPointer(dpy, window, &dw, &dw, &x, &y, &di, &di, &du))
|
if (!XQueryPointer(dpy, window, &dw, &dw, &x, &y, &di, &di, &du))
|
||||||
die("Could not query pointer position");
|
die("Could not query pointer position");
|
||||||
#ifdef XINERAMA
|
#ifdef XINERAMA
|
||||||
XineramaScreenInfo *info;
|
XineramaScreenInfo *info;
|
||||||
int n, i=0;
|
int n, i=0;
|
||||||
|
@ -193,7 +165,6 @@ int main()
|
||||||
mh = info[i].height;
|
mh = info[i].height;
|
||||||
mw = info[i].width;
|
mw = info[i].width;
|
||||||
XFree(info);
|
XFree(info);
|
||||||
|
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
|
@ -204,8 +175,8 @@ int main()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Correct on monitor boundary
|
// Correct on monitor boundary
|
||||||
int by = y - y_offset + height + border_size;
|
int by = y - y_offset + height + border_size + MIN_BORDER_DISTANCE;
|
||||||
int rx = x - x_offset + width + border_size;
|
int rx = x - x_offset + width + border_size + MIN_BORDER_DISTANCE;
|
||||||
if (by > mh)
|
if (by > mh)
|
||||||
y = MAX(y-(by-mh), 0);
|
y = MAX(y-(by-mh), 0);
|
||||||
if (rx > mw)
|
if (rx > mw)
|
||||||
|
@ -220,9 +191,6 @@ int main()
|
||||||
XSelectInput(dpy, window, ExposureMask | ButtonPress);
|
XSelectInput(dpy, window, ExposureMask | ButtonPress);
|
||||||
XMapWindow(dpy, window);
|
XMapWindow(dpy, window);
|
||||||
|
|
||||||
sigaction(SIGUSR1, &act_expire, 0);
|
|
||||||
sigaction(SIGUSR2, &act_expire, 0);
|
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
XEvent event;
|
XEvent event;
|
||||||
|
@ -235,14 +203,18 @@ int main()
|
||||||
XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding,
|
XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding,
|
||||||
(FcChar8 *)lines[i], strlen(lines[i]));
|
(FcChar8 *)lines[i], strlen(lines[i]));
|
||||||
}
|
}
|
||||||
/* else if (event.type == ButtonPress) */
|
|
||||||
else if (event.type == ButtonPress)
|
else if (event.type == ButtonPress)
|
||||||
{
|
{
|
||||||
if (event.xbutton.button == DISMISS_BUTTON)
|
if (event.xbutton.button == DISMISS_BUTTON)
|
||||||
break;
|
break;
|
||||||
else if (event.xbutton.button == ACTION_BUTTON)
|
else if (event.xbutton.button == ACTION1_BUTTON)
|
||||||
{
|
{
|
||||||
exit_code = EXIT_ACTION;
|
exit_code = EXIT_ACTION1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (event.xbutton.button == ACTION2_BUTTON)
|
||||||
|
{
|
||||||
|
exit_code = EXIT_ACTION2;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue