ordered switch branches in kpress alphabetically, applied Sanders patch for PgUp...
[dmenu.git] / main.c
diff --git a/main.c b/main.c
index 7b4365e..0263948 100644 (file)
--- a/main.c
+++ b/main.c
@@ -2,17 +2,16 @@
  * (C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>
  * See LICENSE file for license details.
  */
-
 #include "dmenu.h"
 
 #include <ctype.h>
+#include <locale.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/select.h>
 #include <sys/time.h>
-#include <X11/cursorfont.h>
 #include <X11/Xutil.h>
 #include <X11/keysym.h>
 
@@ -146,7 +145,7 @@ kpress(XKeyEvent * e) {
 
        len = strlen(text);
        buf[0] = 0;
-       num = XLookupString(e, buf, sizeof(buf), &ksym, 0);
+       num = XLookupString(e, buf, sizeof buf, &ksym, 0);
        if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
                        || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
                        || IsPrivateKeypadKey(ksym))
@@ -171,6 +170,42 @@ kpress(XKeyEvent * e) {
                }
        }
        switch(ksym) {
+       default:
+               if(num && !iscntrl((int) buf[0])) {
+                       buf[num] = 0;
+                       if(len > 0)
+                               strncat(text, buf, sizeof text);
+                       else
+                               strncpy(text, buf, sizeof text);
+                       match(text);
+               }
+               break;
+       case XK_BackSpace:
+               if((i = len)) {
+                       prev_nitem = nitem;
+                       do {
+                               text[--i] = 0;
+                               match(text);
+                       } while(i && nitem && prev_nitem == nitem);
+                       match(text);
+               }
+               break;
+       case XK_End:
+               while(next) {
+                       sel = curr = next;
+                       calcoffsets();
+               }
+               while(sel->right)
+                       sel = sel->right;
+               break;
+       case XK_Escape:
+               ret = 1;
+               running = False;
+               break;
+       case XK_Home:
+               sel = curr = item;
+               calcoffsets();
+               break;
        case XK_Left:
                if(!(sel && sel->left))
                        return;
@@ -180,18 +215,15 @@ kpress(XKeyEvent * e) {
                        calcoffsets();
                }
                break;
-       case XK_Tab:
-               if(!sel)
-                       return;
-               strncpy(text, sel->text, sizeof(text));
-               match(text);
+       case XK_Next:
+               if(next) {
+                       sel = curr = next;
+                       calcoffsets();
+               }
                break;
-       case XK_Right:
-               if(!(sel && sel->right))
-                       return;
-               sel=sel->right;
-               if(sel == next) {
-                       curr = next;
+       case XK_Prior:
+               if(prev) {
+                       sel = curr = prev;
                        calcoffsets();
                }
                break;
@@ -205,29 +237,21 @@ kpress(XKeyEvent * e) {
                fflush(stdout);
                running = False;
                break;
-       case XK_Escape:
-               ret = 1;
-               running = False;
-               break;
-       case XK_BackSpace:
-               if((i = len)) {
-                       prev_nitem = nitem;
-                       do {
-                               text[--i] = 0;
-                               match(text);
-                       } while(i && nitem && prev_nitem == nitem);
-                       match(text);
+       case XK_Right:
+               if(!(sel && sel->right))
+                       return;
+               sel=sel->right;
+               if(sel == next) {
+                       curr = next;
+                       calcoffsets();
                }
                break;
-       default:
-               if(num && !iscntrl((int) buf[0])) {
-                       buf[num] = 0;
-                       if(len > 0)
-                               strncat(text, buf, sizeof(text));
-                       else
-                               strncpy(text, buf, sizeof(text));
-                       match(text);
-               }
+       case XK_Tab:
+               if(!sel)
+                       return;
+               strncpy(text, sel->text, sizeof text);
+               match(text);
+               break;
        }
        drawmenu();
 }
@@ -240,7 +264,7 @@ readstdin(void) {
        Item *i, *new;
 
        i = 0;
-       while(fgets(buf, sizeof(buf), stdin)) {
+       while(fgets(buf, sizeof buf, stdin)) {
                len = strlen(buf);
                if (buf[len - 1] == '\n')
                        buf[len - 1] = 0;
@@ -287,24 +311,31 @@ main(int argc, char *argv[]) {
        timeout.tv_sec = 3;
        /* command line args */
        for(i = 1; i < argc; i++)
-               if(!strncmp(argv[i], "-font", 6))
-                       font = argv[++i];
-               else if(!strncmp(argv[i], "-normbg", 8))
-                       normbg = argv[++i];
-               else if(!strncmp(argv[i], "-normfg", 8))
-                       normfg = argv[++i];
-               else if(!strncmp(argv[i], "-selbg", 7))
-                       selbg = argv[++i];
-               else if(!strncmp(argv[i], "-selfg", 7))
-                       selfg = argv[++i];
-               else if(!strncmp(argv[i], "-t", 3))
-                       timeout.tv_sec = atoi(argv[++i]);
+               if(!strncmp(argv[i], "-font", 6)) {
+                       if(++i < argc) font = argv[i];
+               }
+               else if(!strncmp(argv[i], "-normbg", 8)) {
+                       if(++i < argc) normbg = argv[i];
+               }
+               else if(!strncmp(argv[i], "-normfg", 8)) {
+                       if(++i < argc) normfg = argv[i];
+               }
+               else if(!strncmp(argv[i], "-selbg", 7)) {
+                       if(++i < argc) selbg = argv[i];
+               }
+               else if(!strncmp(argv[i], "-selfg", 7)) {
+                       if(++i < argc) selfg = argv[i];
+               }
+               else if(!strncmp(argv[i], "-t", 3)) {
+                       if(++i < argc) timeout.tv_sec = atoi(argv[i]);
+               }
                else if(!strncmp(argv[i], "-v", 3)) {
                        fputs("dmenu-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
                        exit(EXIT_SUCCESS);
                }
                else
                        eprint("usage: dmenu [-font <name>] [-{norm,sel}{bg,fg} <color>] [-t <seconds>] [-v]\n", stdout);
+       setlocale(LC_CTYPE, "");
        dpy = XOpenDisplay(0);
        if(!dpy)
                eprint("dmenu: cannot open display\n");
@@ -341,7 +372,6 @@ main(int argc, char *argv[]) {
                        DefaultDepth(dpy, screen), CopyFromParent,
                        DefaultVisual(dpy, screen),
                        CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
-       XDefineCursor(dpy, win, XCreateFontCursor(dpy, XC_xterm));
        /* pixmap */
        dc.drawable = XCreatePixmap(dpy, root, mw, mh, DefaultDepth(dpy, screen));
        dc.gc = XCreateGC(dpy, root, 0, 0);
@@ -357,7 +387,7 @@ main(int argc, char *argv[]) {
        XSync(dpy, False);
 
        /* main event loop */
-       while(running && !XNextEvent(dpy, &ev)) {
+       while(running && !XNextEvent(dpy, &ev))
                switch (ev.type) {
                default:        /* ignore all crap */
                        break;
@@ -369,7 +399,6 @@ main(int argc, char *argv[]) {
                                drawmenu();
                        break;
                }
-       }
 
        /* cleanup */
        while(allitems) {