]> err.no Git - eweouz/commitdiff
Add various search functions which make this program useful
authorTollef Fog Heen <tfheen@err.no>
Mon, 14 Jul 2008 18:13:14 +0000 (20:13 +0200)
committerTollef Fog Heen <tfheen@err.no>
Mon, 14 Jul 2008 18:13:14 +0000 (20:13 +0200)
lisp/eweouz.el

index f375815cd83bb452df0c662bdbb8345dc4535078..7b14282ff2977662a0655a53cecdadbc45f4bed1 100644 (file)
 (defvar eweouz-path "/home/tfheen/src/eds-address/build/src/dump-addressbook")
 
 (defvar eweouz-buffer nil)
+(defvar eweouz-pop-up-target-lines 5)
 
-;;;###autoload
-(defun eweouz (string)
-  (interactive "MSearch for: ")
-  "Search all entries for the simple string STRING in all fields"
-  (set-buffer (get-buffer-create eweouz-buffer-name))
-  (widen)
-  (delete-region (point-min) (point-max))
-
+(defun eweouz-search-do (func string)
+  "Search for the simple string STRING in all fields"
   (with-temp-buffer
     (call-process eweouz-path nil (current-buffer) t string)
     (set-text-properties (point-min) (point-max) nil nil)
       (let ((m-start (point)))
        (message (format "%s" (point)))
        (search-forward-regexp "^END:VCARD")
-       (eweouz-show (vcard-parse-string (buffer-substring m-start (point))))))))
+       (funcall func (vcard-parse-string (buffer-substring m-start (point))))))))
+
+;;;###autoload
+(defun eweouz-complete (&optional start-pos)
+  (interactive)
+  (eweouz-erase-buffer)
+  (let* ((end (point))
+        (beg (or start-pos
+                 (save-excursion
+                   (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
+                   (goto-char (match-end 0))
+                   (point))))
+        (orig (buffer-substring beg end))
+        (typed (downcase orig))
+        (match-recs '()))
+    (eweouz-search-do '(lambda (x) (add-to-list 'match-recs x)) typed)
+    (message (format "%s" match-recs))
+    (message (format "%d" (length match-recs)))
+    (cond ((= 1 (length match-recs))
+         ; Just one match; insert it
+          (progn
+            (delete-region beg end)
+            (insert (vcard-format-sample-get-name (car match-recs)))))
+         ((< 1 (length match-recs))
+          ;; For now, just display the records and leave the user to
+          ;; complete
+          (save-excursion
+            (set-buffer (get-buffer-create eweouz-buffer-name))
+            (mapcar '(lambda (x) (insert (format "%s\n" 
+                                                 (vcard-format-sample-get-name x)))) match-recs)
+            (eweouz-pop-up-eweouz-buffer))))))
+
+;
+; Mostly stolen from bbdb-pop-up-bbdb-buffer
+
+(defun eweouz-pop-up-eweouz-buffer (&optional horiz-predicate)
+ (let ((b (current-buffer)))
+    (if (get-buffer-window eweouz-buffer-name)
+       nil
+      (let* ((first-window (selected-window))
+            (tallest-window first-window)
+            (window first-window))
+       ;; find the tallest window...
+       (while (not (eq (setq window (previous-window window)) first-window))
+         (if (> (window-height window) (window-height tallest-window))
+             (setq tallest-window window)))
+       ;; select it and split it...
+       (select-window tallest-window)
+       (let ((size (min
+                    (- (window-height tallest-window)
+                       window-min-height 1)
+                    (- (window-height tallest-window)
+                       (max window-min-height
+                            (1+ eweouz-pop-up-target-lines))))))
+         (split-window tallest-window
+                       (if (> size 0) size window-min-height)))
+       (if (memq major-mode
+                 '(gnus-Group-mode gnus-Subject-mode gnus-Article-mode))
+           (goto-char (point-min)))  ; make gnus happy...
+       ;; goto the bottom of the two...
+       (select-window (next-window))
+       ;; make it display *EWEOUZ*...
+       (let ((pop-up-windows nil))
+         (switch-to-buffer (get-buffer-create eweouz-buffer-name)))
+       ;; select the original window we were in...
+       (select-window first-window)))
+    ;; and make sure the current buffer is correct as well.
+    (set-buffer b)
+    nil))
+
+(defun eweouz-erase-buffer ()
+  (save-excursion
+    (set-buffer (get-buffer-create eweouz-buffer-name))
+    (erase-buffer)))
+
+;;;###autoload
+(defun eweouz (string)
+  (interactive "MSearch for: ")
+  "Search all entries for the simple string STRING in all fields"
+  (eweouz-erase-buffer)
+  (eweouz-search-do 'eweouz-show string))
 
 (defun eweouz-show (record)
   "Display an entry in the current buffer"