From 831037b23ec05fe41702572a23c35fbce3864716 Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Mon, 14 Jul 2008 20:13:14 +0200 Subject: [PATCH] Add various search functions which make this program useful --- lisp/eweouz.el | 93 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 9 deletions(-) diff --git a/lisp/eweouz.el b/lisp/eweouz.el index f375815..7b14282 100644 --- a/lisp/eweouz.el +++ b/lisp/eweouz.el @@ -26,15 +26,10 @@ (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) @@ -44,7 +39,87 @@ (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" -- 2.39.5