* gnus-util.el (gnus-frame-or-window-display-name): Exclude invalid display
[gnus] / lisp / gnus-util.el
index 532ac23..dddac92 100644 (file)
@@ -38,6 +38,7 @@
   (defvar nnmail-pathname-coding-system))
 (require 'nnheader)
 (require 'time-date)
+(require 'netrc)
 
 (eval-and-compile
   (autoload 'message-fetch-field "message")
          (setq start (- (length string) tail))))
       string))))
 
+;;; bring in the netrc functions as aliases
+(defalias 'gnus-netrc-get 'netrc-get)
+(defalias 'gnus-netrc-machine 'netrc-machine)
+(defalias 'gnus-parse-netrc 'netrc-parse)
+
 (defun gnus-boundp (variable)
   "Return non-nil if VARIABLE is bound and non-nil."
   (and (boundp variable)
         (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
         (and (setq name (substring from 0 (match-beginning 0)))
              ;; Strip any quotes from the name.
-             (string-match "\".*\"" name)
+             (string-match "^\".*\"$" name)
              (setq name (substring name 1 (1- (match-end 0))))))
     ;; If not, then "address (name)" is used.
     (or name
@@ -900,93 +906,6 @@ ARG is passed to the first function."
        (apply 'run-hooks funcs)
       (set-buffer buf))))
 
-;;;
-;;; .netrc and .authinforc parsing
-;;;
-
-(defun gnus-parse-netrc (file)
-  "Parse FILE and return an list of all entries in the file."
-  (when (file-exists-p file)
-    (with-temp-buffer
-      (let ((tokens '("machine" "default" "login"
-                     "password" "account" "macdef" "force"
-                     "port"))
-           alist elem result pair)
-       (insert-file-contents file)
-       (goto-char (point-min))
-       ;; Go through the file, line by line.
-       (while (not (eobp))
-         (narrow-to-region (point) (gnus-point-at-eol))
-         ;; For each line, get the tokens and values.
-         (while (not (eobp))
-           (skip-chars-forward "\t ")
-           ;; Skip lines that begin with a "#".
-           (if (eq (char-after) ?#)
-               (goto-char (point-max))
-             (unless (eobp)
-               (setq elem
-                     (if (= (following-char) ?\")
-                         (read (current-buffer))
-                       (buffer-substring
-                        (point) (progn (skip-chars-forward "^\t ")
-                                       (point)))))
-               (cond
-                ((equal elem "macdef")
-                 ;; We skip past the macro definition.
-                 (widen)
-                 (while (and (zerop (forward-line 1))
-                             (looking-at "$")))
-                 (narrow-to-region (point) (point)))
-                ((member elem tokens)
-                 ;; Tokens that don't have a following value are ignored,
-                 ;; except "default".
-                 (when (and pair (or (cdr pair)
-                                     (equal (car pair) "default")))
-                   (push pair alist))
-                 (setq pair (list elem)))
-                (t
-                 ;; Values that haven't got a preceding token are ignored.
-                 (when pair
-                   (setcdr pair elem)
-                   (push pair alist)
-                   (setq pair nil)))))))
-         (when alist
-           (push (nreverse alist) result))
-         (setq alist nil
-               pair nil)
-         (widen)
-         (forward-line 1))
-       (nreverse result)))))
-
-(defun gnus-netrc-machine (list machine &optional port defaultport)
-  "Return the netrc values from LIST for MACHINE or for the default entry.
-If PORT specified, only return entries with matching port tokens.
-Entries without port tokens default to DEFAULTPORT."
-  (let ((rest list)
-       result)
-    (while list
-      (when (equal (cdr (assoc "machine" (car list))) machine)
-       (push (car list) result))
-      (pop list))
-    (unless result
-      ;; No machine name matches, so we look for default entries.
-      (while rest
-       (when (assoc "default" (car rest))
-         (push (car rest) result))
-       (pop rest)))
-    (when result
-      (setq result (nreverse result))
-      (while (and result
-                 (not (equal (or port defaultport "nntp")
-                             (or (gnus-netrc-get (car result) "port")
-                                 defaultport "nntp"))))
-       (pop result))
-      (car result))))
-
-(defun gnus-netrc-get (alist type)
-  "Return the value of token TYPE from ALIST."
-  (cdr (assoc type alist)))
-
 ;;; Various
 
 (defvar gnus-group-buffer)             ; Compiler directive
@@ -1301,12 +1220,11 @@ CHOICE is a list of the choice char and help message at IDX."
     (save-window-excursion
       (save-excursion
        (while (not tchar)
-         (setq tchar 
-               (read-char
-                (format "%s (%s?): "
-                        prompt
-                        (mapconcat (lambda (s) (char-to-string (car s)))
-                                   choice ""))))
+         (message "%s (%s?): "
+                  prompt
+                  (mapconcat (lambda (s) (char-to-string (car s)))
+                             choice ""))
+         (setq tchar (read-char))
          (when (not (assq tchar choice))
            (setq tchar nil)
            (setq buf (get-buffer-create "*Gnus Help*"))
@@ -1346,6 +1264,44 @@ CHOICE is a list of the choice char and help message at IDX."
        (kill-buffer buf))
     tchar))
 
+(defun gnus-select-frame-set-input-focus (frame)
+  "Select FRAME, raise it, and set input focus, if possible."
+  (cond ((featurep 'xemacs)
+        (raise-frame frame)
+        (select-frame frame)
+        (focus-frame frame))
+       ;; The function `select-frame-set-input-focus' won't set
+       ;; the input focus under Emacs 21.2 and X window system.
+       ;;((fboundp 'select-frame-set-input-focus)
+       ;; (defalias 'gnus-select-frame-set-input-focus
+       ;;   'select-frame-set-input-focus)
+       ;; (select-frame-set-input-focus frame))
+       (t
+        (raise-frame frame)
+        (select-frame frame)
+        (cond ((and (eq window-system 'x)
+                    (fboundp 'x-focus-frame))
+               (x-focus-frame frame))
+              ((eq window-system 'w32)
+               (w32-focus-frame frame)))
+        (when focus-follows-mouse
+          (set-mouse-position frame (1- (frame-width frame)) 0)))))
+
+(defun gnus-frame-or-window-display-name (object)
+  "Given a frame or window, return the associated display name.
+Return nil otherwise."
+  (if (featurep 'xemacs)
+      (device-connection (dfw-device object))
+    (if (or (framep object)
+           (and (windowp object)
+                (setq object (window-frame object))))
+       (let ((display (frame-parameter object 'display)))
+         (if (and (stringp display)
+                  ;; Exclude invalid display names.
+                  (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'"
+                                display))
+             display)))))
+
 (provide 'gnus-util)
 
 ;;; gnus-util.el ends here