From 37c7d6885280cf339a2b75e52d716343dba56230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Aino=20Violetta=20Al=C3=A9anor?= Date: Thu, 11 May 2023 16:25:10 +0200 Subject: add support for 'favorite' emoji --- README | 7 ++++++- pickme.c | 45 +++++++++++++++++++++++++-------------------- unicode.hs | 28 ++++++++++++++++++++++++---- 3 files changed, 55 insertions(+), 25 deletions(-) diff --git a/README b/README index 3b19aab..2f5fa7e 100644 --- a/README +++ b/README @@ -3,7 +3,12 @@ ** synopsis you type in a string and it lists the emojis whose names contain that -string. +string. when you click an emoji, it is added to your primary +selection, which means you can insert it anywhere by clicking your +middle mouse button. the primary selection is cleared when pickme +exits, so keep it open until you paste the emoji. you can create +~$XDG_DATA_HOME/pickme/favorites~ with a single-line string of emojis +to display before any search is entered. ** dependencies diff --git a/pickme.c b/pickme.c index f120a5a..02899b0 100644 --- a/pickme.c +++ b/pickme.c @@ -1,7 +1,4 @@ #include -#ifdef __GLASGOW_HASKELL__ -//#include "unicode_stub.h" -#endif /* __GLASGOW_HASKELL__ */ #include #include @@ -19,17 +16,12 @@ GtkWidget **buttons; bool inited = false; extern char *searchCharacterByName (const char *); +extern char *favoriteEmojis (void); -void -on_click (GtkWidget *widget, - gpointer data) -{ - gdk_clipboard_set_text (gtk_widget_get_primary_clipboard (widget), (char *)data); -} +// forward declaration +void on_click (GtkWidget *widget, gpointer data); -void -on_input (GtkText *text, - gpointer user_data) +void draw_results (GtkText *text, char *results) { if (!inited) { @@ -59,10 +51,6 @@ on_input (GtkText *text, } gsize buf_size = gtk_entry_buffer_get_bytes (input_buffer); - const char *buffer_contents = gtk_entry_buffer_get_text (input_buffer); - char *results; - - results = searchCharacterByName (buffer_contents); size_t l = strlen (results); if (l == 0) @@ -109,8 +97,21 @@ on_input (GtkText *text, } void -activate (GtkApplication *app, - gpointer user_data) +on_click (GtkWidget *widget, gpointer data) +{ + gdk_clipboard_set_text (gtk_widget_get_primary_clipboard (widget), (char *)data); +} + +void +on_input (GtkText *text, gpointer user_data) +{ + const char *buffer_contents = gtk_entry_buffer_get_text (input_buffer); + char *results = searchCharacterByName (buffer_contents); + draw_results (text, results); +} + +void +activate (GtkApplication *app, gpointer user_data) { window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), "pickme"); @@ -126,11 +127,15 @@ activate (GtkApplication *app, gtk_window_set_child (GTK_WINDOW (window), box); gtk_window_present (GTK_WINDOW (window)); + + if (strcmp (favoriteEmojis (), "")) + { + draw_results (GTK_TEXT (input), favoriteEmojis ()); + } } void -shutdown (GtkApplication *app, - gpointer user_data) +shutdown (GtkApplication *app, gpointer user_data) { if (!inited) { diff --git a/unicode.hs b/unicode.hs index cd58fec..4aa23b3 100644 --- a/unicode.hs +++ b/unicode.hs @@ -8,14 +8,23 @@ import qualified Data.Text as T import qualified Data.Text.IO as IO import Foreign.C.String import Numeric +import System.Directory import System.Environment.Blank +import System.IO import System.IO.Unsafe -unicodeDataPath = (getEnv "XDG_DATA_HOME") >>= \s -> return ( - (case s of +xdgDataHome :: IO [Char] +xdgDataHome = (getEnv "XDG_DATA_HOME") >>= \s-> return + ((case s of Nothing -> "~/.local/share" Just x -> if x == "" then "~/.local/share" else x) - ++ "/pickme/UnicodeData.txt") + ++ "/pickme") + +dataFile :: [Char] -> IO [Char] +dataFile name = xdgDataHome >>= (return . flip (++) ("/" ++ name)) + +unicodeDataPath :: IO [Char] +unicodeDataPath = dataFile "UnicodeData.txt" scsvField :: Int -> T.Text -> T.Text scsvField n t = (T.splitOn (T.pack ";") t)!!n @@ -30,8 +39,19 @@ searchCharacterByName :: CString -> IO CString searchCharacterByName s = peekCString s >>= \z -> characterNames >>= \cns -> unicodeCharacters >>= \ucs -> - newCString $ map + newCString $ map (\x -> toEnum . fst . (!!0) . readHex $ T.unpack (ucs!!(fromJust $ elemIndex x cns))) (filter (\s' -> T.isInfixOf ((T.toUpper . T.pack) z) s') cns) foreign export ccall searchCharacterByName :: CString -> IO CString + +favoritesPath :: IO [Char] +favoritesPath = dataFile "favorites" + +favoriteEmojis :: IO CString +favoriteEmojis = favoritesPath >>= doesFileExist >>= \ bool -> case bool of + True -> favoritesPath >>= flip openFile ReadMode >>= hGetLine >>= newCString + False -> newCString "" + + +foreign export ccall favoriteEmojis :: IO CString -- cgit v1.2.3