* riece-mini.el (riece-mini-backlog-size): Change default value.
[riece] / lisp / riece-mini.el
index 8c6bbc3..1b23e6a 100644 (file)
 ;; are visiting other buffers.
 
 ;; To use, add the following line to your ~/.riece/init.el:
-;; (add-to-list 'riece-addons 'riece-mini t)
+;; (add-to-list 'riece-addons 'riece-mini)
 ;;
-;; And for using conveniently, bind any global key to
-;; `riece-mini-send-message'.
+;; For using conveniently, bind any global key to
+;; `riece-mini-send-message' and `riece-mini-show-backlog'.
 ;; For example:
 ;; (global-set-key "\C-cm" 'riece-mini-send-message)
+;; (global-set-key "\C-cb" 'riece-mini-show-backlog)
 
 ;;; Code:
 
-(eval-when-compile (require 'riece-message))
+(require 'riece-message)
+
+(defgroup riece-mini nil
+  "riece on minibuffer"
+  :group 'riece)
+
+(defcustom riece-mini-backlog-size 5
+  "*Line numbers for logging back log."
+  :type 'integer
+  :group 'riece-mini)
 
 (defvar riece-mini-last-channel nil)
+(defvar riece-mini-backlog-history nil)
+(defvar riece-mini-backlog-shown nil)
+
+(defvar riece-mini-enabled nil)
+
+(defconst riece-mini-description
+  "Send arrival messages to minibuffer")
 
 (defmacro riece-mini-message-no-log (string &rest args)
   "Like `message', except that message logging is disabled."
 
 (defun riece-mini-display-message-function (message)
   "Show arrival messages to minibuffer."
-  (unless (or (eq (window-buffer (selected-window))
-                 (get-buffer riece-command-buffer))
-             (riece-message-own-p message)
-             (active-minibuffer-window))
-    (let ((open-bracket
-          (funcall riece-message-make-open-bracket-function message))
-         (close-bracket
-          (funcall riece-message-make-close-bracket-function message))
-         (global-name
-          (funcall riece-message-make-global-name-function message)))
-      (setq riece-mini-last-channel (riece-message-target message))
-      (riece-mini-message-no-log
-       "%s" (concat (format-time-string "%H:%M") " "
-                   open-bracket global-name close-bracket
-                   " " (riece-message-text message))))))
+  (let ((string (concat (format-time-string "%H:%M") " "
+                       (riece-format-message message t))))
+    (when (string-match "\\(.*\\)$" string)
+      (setq string (riece-match-string-no-properties 1 string)))
+    (while (>= (length riece-mini-backlog-history)
+              riece-mini-backlog-size)
+      (setq riece-mini-backlog-history
+           (cdr riece-mini-backlog-history)))
+    (setq riece-mini-backlog-history
+         (reverse (cons string (reverse riece-mini-backlog-history))))
+    (when (and riece-mini-enabled
+              (not (or (eq (window-buffer (selected-window))
+                           (get-buffer riece-command-buffer))
+                       (riece-message-own-p message)
+                       (active-minibuffer-window))))
+      (unless (riece-message-type message)
+       (setq riece-mini-last-channel (riece-message-target message)))
+      (riece-mini-message-no-log "%s" string))))
 
 (defun riece-mini-send-message (arg)
   "Send message using minibuffer.
@@ -79,8 +99,8 @@ If twice (C-u C-u), then ask the channel."
         (target
          (cond
           ((equal arg '(16))
-           (completing-read "Channel/User: "
-                            (mapcar #'list riece-current-channels) nil t))
+           (riece-completing-read-identity
+            "Channel/User: " riece-current-channels nil t))
           (arg (or riece-mini-last-channel riece-current-channel))
           (t riece-current-channel)))
         (message (read-string (format "Message to %s: " target))))
@@ -90,11 +110,41 @@ If twice (C-u C-u), then ask the channel."
        (format "PRIVMSG %s :%s\r\n"
               (riece-identity-prefix target)
               message))
-      (riece-own-channel-message message target))))
+      (riece-display-message
+       (riece-make-message (riece-current-nickname) target
+                          message nil t)))))
+
+(defun riece-mini-show-backlog ()
+  "Send back logs to minibuffer."
+  (interactive)
+  (when riece-mini-backlog-history
+    (let ((height (1+ riece-mini-backlog-size)))
+      (mapc #'(lambda (string)
+               (setq height (+ height
+                               (/ (length string) (window-width)))))
+           riece-mini-backlog-history)
+      (let ((max-mini-window-height height)
+           (resize-mini-windows t))
+       (setq riece-mini-backlog-shown t)
+       (riece-mini-message-no-log
+        "%s" (mapconcat #'identity riece-mini-backlog-history "\n"))))))
+
+(defun riece-mini-pre-command ()
+  (when riece-mini-backlog-shown
+    (let ((resize-mini-windows t))
+      (setq riece-mini-backlog-shown nil)
+      (riece-mini-message-no-log ""))))
 
 (defun riece-mini-insinuate ()
   (add-hook 'riece-after-display-message-functions
-           'riece-mini-display-message-function))
+           'riece-mini-display-message-function)
+  (add-hook 'pre-command-hook 'riece-mini-pre-command))
+
+(defun riece-mini-enable ()
+  (setq riece-mini-enabled t))
+
+(defun riece-mini-disable ()
+  (setq riece-mini-enabled nil))
 
 (provide 'riece-mini)