* riece-misc.el (riece-insert): Use (recenter -1) to set window
[riece] / lisp / riece-misc.el
index d3a21d4..543549f 100644 (file)
 (require 'riece-channel)
 (require 'riece-server)
 (require 'riece-user)
-
-(defun riece-get-buffer-create (name)
-  (let ((buffer (get-buffer-create name)))
+(require 'riece-mode)
+
+(defun riece-get-buffer-create (name &optional init-major-mode)
+  (let ((buffer (get-buffer name)))
+    (unless (and buffer
+                (or (null init-major-mode)
+                    (eq (with-current-buffer buffer
+                          major-mode)
+                        init-major-mode)))
+      (setq buffer (generate-new-buffer name)))
     (unless (memq buffer riece-buffer-list)
       (setq riece-buffer-list (cons buffer riece-buffer-list)))
     buffer))
   (while buffers
     (run-hooks 'riece-before-insert-functions)
     (save-excursion
-      (set-buffer (riece-get-buffer-create (car buffers)))
+      (set-buffer (car buffers))
       (let ((inhibit-read-only t)
            buffer-read-only
            (start (goto-char (point-max))))
        (insert (format-time-string "%H:%M") " " string)
        (if (and (not (riece-frozen (current-buffer)))
                 (get-buffer-window (current-buffer)))
-           (set-window-point (get-buffer-window (current-buffer))
-                             (point)))
+           (recenter -1 (get-buffer-window (current-buffer))))
        (run-hook-with-args 'riece-after-insert-functions start (point))))
     (setq buffers (cdr buffers))))
 
 \(i.e. it matches `riece-channel-regexp')"
   (string-match (concat "^" riece-channel-regexp) string))
 
+(defun riece-user-p (string)
+  "Return t if STRING is a user.
+\(i.e. it matches `riece-user-regexp')"
+  (string-match (concat "^" riece-user-regexp) string))
+
 (defun riece-current-nickname ()
   "Return the current nickname."
-  (riece-with-server-buffer (riece-identity-server riece-current-channel)
+  (riece-with-server-buffer (riece-current-server-name)
     (if riece-real-nickname
        (riece-make-identity riece-real-nickname riece-server-name))))
 
        (while (string-match "^\\([^ ]+\\) +" string)
          (setq parameters (nconc parameters (list (match-string 1 string)))
                string (substring string (match-end 0)))
-         (and (not (equal "" string)) (eq ?: (aref string 0))
-              (setq string (substring string 1))
-              (throw 'done nil))))
-      (or (equal "" string)
-         (setq parameters (nconc parameters (list string))))
+         (when (and (not (equal "" string)) (eq ?: (aref string 0)))
+           (setq string (substring string 1)
+                 parameters (nconc parameters (list string)))
+           (throw 'done nil)))
+       (or (equal "" string)
+           (setq parameters (nconc parameters (list string)))))
       parameters)))
 
 (defun riece-concat-channel-topic (target string)
   (riece-with-server-buffer (riece-identity-server target)
     (let ((modes (riece-channel-get-modes (riece-identity-prefix target))))
       (if modes
-         (concat string " [" (apply #'string modes) "]")
+         (concat string " ["
+                 (mapconcat
+                  (lambda (mode)
+                    (if (riece-mode-parameter mode)
+                        (format "%c(%s)"
+                                (riece-mode-flag mode)
+                                (riece-mode-parameter mode))
+                      (char-to-string (riece-mode-flag mode))))
+                  modes "")
+                 "]")
        string))))
 
 (defun riece-concat-message (string message)
 (defun riece-concat-server-name (string)
   (if (equal riece-server-name "")
       string
-    (concat string " (from " riece-server-name ")")))
+    (let ((server-name (concat " (from " riece-server-name ")")))
+      (put-text-property 0 (length server-name)
+                        'riece-server-name riece-server-name
+                        server-name)
+      (concat string server-name))))
+
+(defun riece-concat-user-status (status string)
+  (if status
+      (concat string " [" (mapconcat #'identity status ", ") "]")
+    string))
 
 (defun riece-prefix-user-at-host (prefix)
   (if (string-match "!" prefix)
       (substring user-at-host 1)
     user-at-host))
 
-(defun riece-get-users-on-server ()
-  (riece-with-server-buffer (riece-identity-server riece-current-channel)
-    (let (users)
+(defun riece-get-users-on-server (server-name)
+  (delq nil (mapcar (lambda (identity)
+                     (if (riece-user-p (riece-identity-prefix identity))
+                         identity))
+                   (riece-get-identities-on-server server-name))))
+
+(defun riece-get-identities-on-server (server-name)
+  (riece-with-server-buffer server-name
+    (let (identities)
       (mapatoms
        (lambda (atom)
-        (unless (riece-channel-p (symbol-name atom))
-          (setq users (cons (symbol-name atom) users))))
+        (setq identities
+              (cons (riece-make-identity (symbol-name atom) server-name)
+                    identities)))
        riece-obarray)
-      (if (member riece-real-nickname users)
-         users
-       (cons riece-real-nickname users)))))
+      identities)))
+
+(defun riece-check-channel-commands-are-usable (&optional channel)
+   (unless riece-current-channel
+     (error (substitute-command-keys
+            "Type \\[riece-command-join] to join a channel")))
+   (if (and channel
+           (not (riece-channel-p (riece-identity-prefix
+                                  riece-current-channel))))
+       (error "Not on a channel")))
 
 (provide 'riece-misc)