From 2a9a52249e67eb1f8887b17be27ae06c3b1603db Mon Sep 17 00:00:00 2001
From: neldoreth <mail@pwmt.org>
Date: Sat, 2 Jan 2010 00:25:18 +0100
Subject: [PATCH] cc_print

List all printers, not the best way
---
 config.def.h |  5 +++-
 zathura.c    | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/config.def.h b/config.def.h
index d058848..db737c8 100644
--- a/config.def.h
+++ b/config.def.h
@@ -32,6 +32,9 @@ static const char notification_w_fgcolor[] = "#000000";
 /* statusbar */
 static const char DEFAULT_TEXT[] = "[No Name]";
 
+/* printing */
+#define LIST_PRINTER_COMMAND "lpstat -v | sed -n '/^.*device for \\(.*\\): .*$/s//\\1/p'"
+
 /* additional settings */
 #define SHOW_SCROLLBARS 0
 
@@ -82,7 +85,7 @@ Command commands[] = {
   /* command,   abbreviation,   function,   completion,   description  */
   {"close",     "c",            cmd_close,  0,            "Close current file" },
   {"open",      "o",            cmd_open,   cc_open,      "Open a file" },
-  {"print",     "p",            cmd_print,  0,            "Print the document" },
+  {"print",     "p",            cmd_print,  cc_print,     "Print the document" },
   {"rotate",    "r",            cmd_rotate, 0,            "Rotate the page" },
   {"set",       "s",            cmd_set,    cc_set,       "Set an option" },
   {"quit",      "q",            cmd_quit,   0,            "Quit zjui" },
diff --git a/zathura.c b/zathura.c
index e9324b0..4bd1896 100644
--- a/zathura.c
+++ b/zathura.c
@@ -250,6 +250,7 @@ gboolean cmd_zoom(int, char**);
 
 /* completion commands */
 Completion* cc_open(char*);
+Completion* cc_print(char*);
 Completion* cc_set(char*);
 
 /* buffer command declarations */
@@ -1528,6 +1529,73 @@ cc_open(char* input)
   return completion;
 }
 
+Completion*
+cc_print(char* input)
+{
+  /* init completion group */
+  Completion *completion = malloc(sizeof(Completion));
+  CompletionGroup* group = malloc(sizeof(CompletionGroup));
+
+  group->value    = NULL;
+  group->next     = NULL;
+  group->elements = NULL;
+
+  completion->groups = group;
+  CompletionElement *last_element = NULL;
+  int element_counter = 0;
+  int input_length = input ? strlen(input) : 0;
+
+  /* read printers */
+  char *current_line = NULL, current_char;
+  int count = 0;
+  FILE *fp;
+
+  fp = popen(LIST_PRINTER_COMMAND, "r");
+
+  if(!fp)
+    return NULL;
+
+  while((current_char = fgetc(fp)) != EOF)
+  {
+    if(!current_line)
+      current_line = malloc(sizeof(char) * 512);
+
+    current_line = realloc(current_line, (count + 1) * sizeof(char));
+
+    if(current_char != '\n')
+      current_line[count++] = current_char;
+    else
+    {
+      current_line[count] = '\0';
+      int line_length = strlen(current_line);
+
+      if( (input_length <= line_length) ||
+          (!strncmp(input, current_line, input_length)) )
+      {
+        CompletionElement* el = malloc(sizeof(CompletionElement));
+        el->value       = g_strdup(current_line);
+        el->description = NULL;
+        el->next        = NULL;
+
+        if(element_counter++ != 0)
+          last_element->next = el;
+        else
+          group->elements = el;
+
+        last_element = el;
+      }
+
+      free(current_line);
+      current_line = NULL;
+      count = 0;
+    }
+  }
+
+  pclose(fp);
+
+  return completion;
+}
+
 Completion*
 cc_set(char* input)
 {