aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2019-12-11 17:20:02 -0500
committerStefan Monnier2019-12-11 17:20:02 -0500
commit394c91e4bf0e9244f6b0f41b4ba74c1dbf3097a2 (patch)
tree91f82910db045ddd2321972a003e80ce1236d890
parentbad2532f664e11e5b32c1194f2274ba2d1f0116b (diff)
downloademacs-394c91e4bf0e9244f6b0f41b4ba74c1dbf3097a2.tar.gz
emacs-394c91e4bf0e9244f6b0f41b4ba74c1dbf3097a2.zip
* lisp/net/eudc.el (eudc-query-with-words): New function
Extracted from eudc-expand-inline. (eudc-expand-inline): Use it.
-rw-r--r--lisp/net/eudc.el164
1 files changed, 85 insertions, 79 deletions
diff --git a/lisp/net/eudc.el b/lisp/net/eudc.el
index 586dd210ed5..9533a562d88 100644
--- a/lisp/net/eudc.el
+++ b/lisp/net/eudc.el
@@ -777,6 +777,45 @@ If REPLACE is non-nil, then this expansion replaces the name in the buffer.
777Multiple servers can be tried with the same query until one finds a match, 777Multiple servers can be tried with the same query until one finds a match,
778see `eudc-inline-expansion-servers'." 778see `eudc-inline-expansion-servers'."
779 (interactive) 779 (interactive)
780 (let* ((end (point))
781 (beg (save-excursion
782 (if (re-search-backward "\\([:,]\\|^\\)[ \t]*"
783 (point-at-bol) 'move)
784 (goto-char (match-end 0)))
785 (point)))
786 (query-words (split-string (buffer-substring-no-properties beg end)
787 "[ \t]+"))
788 (response-strings (eudc-query-with-words query-words)))
789 (if (null response-strings)
790 (error "No match")
791
792 (if (or
793 (and replace (not eudc-expansion-overwrites-query))
794 (and (not replace) eudc-expansion-overwrites-query))
795 (kill-ring-save beg end))
796 (cond
797 ((or (= (length response-strings) 1)
798 (null eudc-multiple-match-handling-method)
799 (eq eudc-multiple-match-handling-method 'first))
800 (delete-region beg end)
801 (insert (car response-strings)))
802 ((eq eudc-multiple-match-handling-method 'select)
803 (eudc-select response-strings beg end))
804 ((eq eudc-multiple-match-handling-method 'all)
805 (delete-region beg end)
806 (insert (mapconcat #'identity response-strings ", ")))
807 ((eq eudc-multiple-match-handling-method 'abort)
808 (error "There is more than one match for the query"))))))
809
810;;;###autoload
811(defun eudc-query-with-words (query-words)
812 "Query the directory server, and return the matching responses.
813The variable `eudc-inline-query-format' controls how to associate the
814individual QUERY-WORDS with directory attribute names.
815After querying the server for the given string, the expansion specified by
816`eudc-inline-expansion-format' is applied to the matches before returning them.inserted in the buffer at point.
817Multiple servers can be tried with the same query until one finds a match,
818see `eudc-inline-expansion-servers'."
780 (cond 819 (cond
781 ((eq eudc-inline-expansion-servers 'current-server) 820 ((eq eudc-inline-expansion-servers 'current-server)
782 (or eudc-server 821 (or eudc-server
@@ -792,103 +831,70 @@ see `eudc-inline-expansion-servers'."
792 (t 831 (t
793 (error "Wrong value for `eudc-inline-expansion-servers': %S" 832 (error "Wrong value for `eudc-inline-expansion-servers': %S"
794 eudc-inline-expansion-servers))) 833 eudc-inline-expansion-servers)))
795 (let* ((end (point)) 834 (let* (query-formats
796 (beg (save-excursion
797 (if (re-search-backward "\\([:,]\\|^\\)[ \t]*"
798 (point-at-bol) 'move)
799 (goto-char (match-end 0)))
800 (point)))
801 (query-words (split-string (buffer-substring-no-properties beg end)
802 "[ \t]+"))
803 query-formats
804 response
805 response-strings
806 (eudc-former-server eudc-server) 835 (eudc-former-server eudc-server)
807 (eudc-former-protocol eudc-protocol) 836 (eudc-former-protocol eudc-protocol)
808 servers) 837 ;; Prepare the list of servers to query
809 838 (servers
810 ;; Prepare the list of servers to query
811 (setq servers (copy-sequence eudc-server-hotlist))
812 (setq servers
813 (cond 839 (cond
814 ((eq eudc-inline-expansion-servers 'hotlist) 840 ((eq eudc-inline-expansion-servers 'hotlist)
815 eudc-server-hotlist) 841 eudc-server-hotlist)
816 ((eq eudc-inline-expansion-servers 'server-then-hotlist) 842 ((eq eudc-inline-expansion-servers 'server-then-hotlist)
817 (if eudc-server 843 (if eudc-server
818 (cons (cons eudc-server eudc-protocol) 844 (cons (cons eudc-server eudc-protocol)
819 (delete (cons eudc-server eudc-protocol) servers)) 845 (delete (cons eudc-server eudc-protocol)
846 (copy-sequence eudc-server-hotlist)))
820 eudc-server-hotlist)) 847 eudc-server-hotlist))
821 ((eq eudc-inline-expansion-servers 'current-server) 848 ((eq eudc-inline-expansion-servers 'current-server)
822 (list (cons eudc-server eudc-protocol))))) 849 (list (cons eudc-server eudc-protocol))))))
850
823 (if (and eudc-max-servers-to-query 851 (if (and eudc-max-servers-to-query
824 (> (length servers) eudc-max-servers-to-query)) 852 (> (length servers) eudc-max-servers-to-query))
825 (setcdr (nthcdr (1- eudc-max-servers-to-query) servers) nil)) 853 (setcdr (nthcdr (1- eudc-max-servers-to-query) servers) nil))
826 854
827 (unwind-protect 855 (unwind-protect
828 (progn 856 (let ((response
829 (setq response 857 (catch 'found
830 (catch 'found 858 ;; Loop on the servers
831 ;; Loop on the servers 859 (dolist (server servers)
832 (while servers 860 (eudc-set-server (car server) (cdr server) t)
833 (eudc-set-server (caar servers) (cdar servers) t) 861
834 862 ;; Determine which formats apply in the query-format list
835 ;; Determine which formats apply in the query-format list 863 (setq query-formats
836 (setq query-formats 864 (or
837 (or 865 (eudc-extract-n-word-formats eudc-inline-query-format
838 (eudc-extract-n-word-formats eudc-inline-query-format 866 (length query-words))
839 (length query-words)) 867 (if (null eudc-protocol-has-default-query-attributes)
840 (if (null eudc-protocol-has-default-query-attributes) 868 '(name))))
841 '(name)))) 869
842 870 ;; Loop on query-formats
843 ;; Loop on query-formats 871 (while query-formats
844 (while query-formats 872 (let ((response
845 (setq response
846 (eudc-query 873 (eudc-query
847 (eudc-format-query query-words (car query-formats)) 874 (eudc-format-query query-words (car query-formats))
848 (eudc-translate-attribute-list 875 (eudc-translate-attribute-list
849 (cdr eudc-inline-expansion-format)))) 876 (cdr eudc-inline-expansion-format)))))
850 (if response 877 (if response
851 (throw 'found response)) 878 (throw 'found response)))
852 (setq query-formats (cdr query-formats))) 879 (setq query-formats (cdr query-formats))))
853 (setq servers (cdr servers))) 880 ;; No more servers to try... no match found
854 ;; No more servers to try... no match found 881 nil))
855 nil)) 882 (response-strings '()))
856 883
857 884 ;; Process response through eudc-inline-expansion-format
858 (if (null response) 885 (dolist (r response)
859 (error "No match") 886 (let ((response-string
860 887 (apply #'format
861 ;; Process response through eudc-inline-expansion-format 888 (car eudc-inline-expansion-format)
862 (dolist (r response) 889 (mapcar (function
863 (let ((response-string 890 (lambda (field)
864 (apply #'format 891 (or (cdr (assq field r))
865 (car eudc-inline-expansion-format) 892 "")))
866 (mapcar (function 893 (eudc-translate-attribute-list
867 (lambda (field) 894 (cdr eudc-inline-expansion-format))))))
868 (or (cdr (assq field r)) 895 (if (> (length response-string) 0)
869 ""))) 896 (push response-string response-strings))))
870 (eudc-translate-attribute-list 897 response-strings)
871 (cdr eudc-inline-expansion-format))))))
872 (if (> (length response-string) 0)
873 (push response-string response-strings))))
874
875 (if (or
876 (and replace (not eudc-expansion-overwrites-query))
877 (and (not replace) eudc-expansion-overwrites-query))
878 (kill-ring-save beg end))
879 (cond
880 ((or (= (length response-strings) 1)
881 (null eudc-multiple-match-handling-method)
882 (eq eudc-multiple-match-handling-method 'first))
883 (delete-region beg end)
884 (insert (car response-strings)))
885 ((eq eudc-multiple-match-handling-method 'select)
886 (eudc-select response-strings beg end))
887 ((eq eudc-multiple-match-handling-method 'all)
888 (delete-region beg end)
889 (insert (mapconcat #'identity response-strings ", ")))
890 ((eq eudc-multiple-match-handling-method 'abort)
891 (error "There is more than one match for the query")))))
892 (or (and (equal eudc-server eudc-former-server) 898 (or (and (equal eudc-server eudc-former-server)
893 (equal eudc-protocol eudc-former-protocol)) 899 (equal eudc-protocol eudc-former-protocol))
894 (eudc-set-server eudc-former-server eudc-former-protocol t))))) 900 (eudc-set-server eudc-former-server eudc-former-protocol t)))))