c16b391c6252c5d296874a675185517007a1fd8c
[riece] / lisp / riece-commands.el
1 ;;; riece-commands.el --- commands available in command buffer
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'riece-channel)
28 (require 'riece-complete)
29 (require 'riece-layout)
30 (require 'riece-display)
31 (require 'riece-server)
32 (require 'riece-misc)
33 (require 'riece-identity)
34 (require 'riece-message)
35
36 (autoload 'derived-mode-class "derived")
37
38 ;;; Channel movement:
39 (defun riece-command-switch-to-channel (channel)
40   (interactive (list (riece-completing-read-identity
41                       "Switch to channel/user: "
42                       riece-current-channels nil t)))
43   (unless (equal channel riece-current-channel)
44     (riece-switch-to-channel channel)))
45
46 (defun riece-command-switch-to-channel-by-number (number)
47   (interactive
48    (let ((command-name (symbol-name this-command)))
49      (if (string-match "[0-9]+$" command-name)
50          (list (string-to-number (match-string 0 command-name)))
51        (list (string-to-number (read-string "Switch to number: "))))))
52   (let ((channel (nth (1- number) riece-current-channels)))
53     (if channel
54         (riece-command-switch-to-channel channel)
55       (error "No such number!"))))
56
57 (eval-and-compile
58   (let ((number 1))
59     (while (<= number 20)
60       (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
61                                 (number-to-string number)))
62         'riece-command-switch-to-channel-by-number)
63       (setq number (1+ number)))))
64
65 (defun riece-command-next-channel ()
66   "Select the next channel."
67   (interactive)
68   (when (> (length riece-current-channels) 1)
69     (let ((pointer (cdr (riece-identity-member
70                          riece-current-channel
71                          riece-current-channels))))
72       (while (and pointer
73                   (null (car pointer)))
74         (setq pointer (cdr pointer)))
75       (when (null pointer)
76         (setq pointer riece-current-channels)
77         (while (and pointer
78                     (null (car pointer)))
79           (setq pointer (cdr pointer))))
80       (if (car pointer)
81           (riece-command-switch-to-channel (car pointer))
82         (error "No such channel!")))))
83
84 (defun riece-command-previous-channel ()
85   "Select the previous channel."
86   (interactive)
87   (when (> (length riece-current-channels) 1)
88     (let ((pointer (riece-identity-member
89                     riece-current-channel
90                     riece-current-channels))
91           (start riece-current-channels)
92           channel)
93       (while (and start (not (eq start pointer)))
94         (if (car start)
95             (setq channel (car start)))
96         (setq start (cdr start)))
97       (when (null channel)
98         (setq start (copy-sequence riece-current-channels))
99         (setq start (delq nil start))
100         (and (> (length start) 1)
101              (setq channel (nth (1- (length start)) start))))
102       (if channel
103           (riece-command-switch-to-channel channel)
104         (error "No such channel!")))))
105
106 (defun riece-command-select-command-buffer ()
107   "Select the command buffer."
108   (interactive)
109   (let ((window (get-buffer-window riece-command-buffer)))
110     (if window
111         (select-window window))))
112
113 (defun riece-command-configure-windows ()
114   (interactive)
115   (riece-redisplay-buffers t))
116
117 (defun riece-command-change-layout (name)
118   "Select a layout-name from all current available layouts and change
119 the layout to the selected layout-name."
120   (interactive (list (completing-read "Change layout: " riece-layout-alist)))
121   (setq riece-layout name
122         riece-save-variables-are-dirty t)
123   (riece-command-configure-windows))
124
125 (defun riece-command-toggle-channel-buffer-mode ()
126   (interactive)
127   (setq riece-channel-buffer-mode
128         (not riece-channel-buffer-mode)
129         riece-save-variables-are-dirty t)
130   (riece-command-configure-windows))
131
132 (defun riece-command-toggle-user-list-buffer-mode ()
133   (interactive)
134   (setq riece-user-list-buffer-mode
135         (not riece-user-list-buffer-mode)
136         riece-save-variables-are-dirty t)
137   (riece-command-configure-windows))
138
139 (defun riece-command-toggle-channel-list-buffer-mode ()
140   (interactive)
141   (setq riece-channel-list-buffer-mode
142         (not riece-channel-list-buffer-mode)
143         riece-save-variables-are-dirty t)
144   (riece-command-configure-windows))
145
146 (defun riece-command-finger (user &optional recurse)
147   (interactive
148    (let* ((completion-ignore-case t)
149           (user (riece-completing-read-identity
150                  "Finger user: "
151                  (riece-get-users-on-server (riece-current-server-name))
152                  nil nil nil nil nil t)))
153      (list user current-prefix-arg)))
154   (if recurse
155       (riece-send-string (format "WHOIS %s %s\r\n"
156                                  (riece-identity-prefix user)
157                                  (riece-identity-prefix user)))
158     (riece-send-string (format "WHOIS %s\r\n" (riece-identity-prefix user)))))
159
160 (defun riece-command-topic (topic)
161   (interactive
162    (progn
163      (riece-check-channel-commands-are-usable t)
164      (list (read-from-minibuffer
165             "Set topic: " (cons (or (riece-with-server-buffer
166                                      (riece-identity-server
167                                       riece-current-channel)
168                                      (riece-channel-get-topic
169                                       (riece-identity-prefix
170                                        riece-current-channel)))
171                                     "")
172                                 0)))))
173   (riece-send-string (format "TOPIC %s :%s\r\n"
174                              (riece-identity-prefix riece-current-channel)
175                              topic)))
176
177 (defun riece-command-invite (user)
178   (interactive
179    (let ((completion-ignore-case t))
180      (riece-check-channel-commands-are-usable t)
181      (list (riece-completing-read-identity
182             "Invite user: "
183             (riece-get-users-on-server (riece-current-server-name))
184             nil nil nil nil nil t))))
185   (riece-send-string (format "INVITE %s %s\r\n"
186                              (riece-identity-prefix user)
187                              (riece-identity-prefix riece-current-channel))))
188
189 (defun riece-command-kick (user &optional message)
190   (interactive
191    (let ((completion-ignore-case t))
192      (riece-check-channel-commands-are-usable t)
193      (list (completing-read
194             "Kick user: "
195             (riece-with-server-buffer
196                 (riece-identity-server riece-current-channel)
197               (riece-channel-get-users (riece-identity-prefix
198                                         riece-current-channel))))
199            (if current-prefix-arg
200                (read-string "Message: ")))))
201   (riece-send-string
202    (if message
203        (format "KICK %s %s :%s\r\n"
204                (riece-identity-prefix riece-current-channel)
205                user message)
206      (format "KICK %s %s\r\n"
207              (riece-identity-prefix riece-current-channel)
208              user))))
209
210 (defun riece-command-names (pattern)
211   (interactive
212    (let ((completion-ignore-case t))
213      (list (read-from-minibuffer
214             "NAMES pattern: "
215             (if (and riece-current-channel
216                      (riece-channel-p (riece-identity-prefix
217                                        riece-current-channel)))
218                 (cons (riece-identity-prefix riece-current-channel)
219                       0))))))
220   (if (or (not (equal pattern ""))
221           (yes-or-no-p "Really want to query NAMES without argument? "))
222       (riece-send-string (format "NAMES %s\r\n" pattern))))
223
224 (defun riece-command-who (pattern)
225   (interactive
226    (let ((completion-ignore-case t))
227      (list (read-from-minibuffer
228             "WHO pattern: "
229             (if (and riece-current-channel
230                      (riece-channel-p (riece-identity-prefix
231                                        riece-current-channel)))
232                 (cons (riece-identity-prefix riece-current-channel)
233                       0))))))
234   (if (or (not (equal pattern ""))
235           (yes-or-no-p "Really want to query WHO without argument? "))
236       (riece-send-string (format "WHO %s\r\n" pattern))))
237
238 (defun riece-command-list (pattern)
239   (interactive
240    (let ((completion-ignore-case t))
241      (list (read-from-minibuffer
242             "LIST pattern: "
243             (if (and riece-current-channel
244                      (riece-channel-p (riece-identity-prefix
245                                        riece-current-channel)))
246                 (cons (riece-identity-prefix riece-current-channel)
247                       0))))))
248   (if (or (not (equal pattern ""))
249           (yes-or-no-p "Really want to query LIST without argument? "))
250       (riece-send-string (format "LIST %s\r\n" pattern))))
251
252 (defun riece-command-change-mode (channel change)
253   (interactive
254    (let* ((completion-ignore-case t)
255           (channel
256            (if current-prefix-arg
257                (riece-completing-read-identity
258                 "Change mode for channel/user: "
259                 (riece-get-identities-on-server (riece-current-server-name))
260                 nil nil nil nil nil t)
261              (riece-check-channel-commands-are-usable t)
262              riece-current-channel))
263           (riece-overriding-server-name (riece-identity-server channel))
264           (riece-temp-minibuffer-message
265            (concat "[Available modes: "
266                    (riece-with-server-buffer (riece-identity-server channel)
267                      (if (riece-channel-p (riece-identity-prefix channel))
268                          (if riece-supported-channel-modes
269                              (apply #'string riece-supported-channel-modes))
270                        (if riece-supported-user-modes
271                            (apply #'string riece-supported-user-modes))))
272                    "]")))
273      (list channel
274            (read-from-minibuffer
275             (concat (riece-concat-channel-modes
276                      channel "Mode (? for help)") ": ")
277             nil riece-minibuffer-map))))
278   (if (equal change "")
279       (riece-send-string (format "MODE %s\r\n"
280                                  (riece-identity-prefix channel)))
281     (riece-send-string (format "MODE %s %s\r\n"
282                                (riece-identity-prefix channel)
283                                change))))
284
285 (defun riece-command-set-operators (users &optional arg)
286   (interactive
287    (progn
288      (riece-check-channel-commands-are-usable t)
289      (let ((completion-ignore-case t))
290        (list (riece-completing-read-multiple
291               (if current-prefix-arg
292                   "Unset +o for users"
293                 "Set +o for users")
294               (riece-with-server-buffer
295                   (riece-identity-server riece-current-channel)
296                 (riece-channel-get-users (riece-identity-prefix
297                                          riece-current-channel)))
298               (if current-prefix-arg
299                   (lambda (user)
300                     (memq ?o (cdr user)))
301                 (lambda (user)
302                   (not (memq ?o (cdr user))))))
303              current-prefix-arg))))
304   (let (group)
305     (while users
306       (setq group (cons (car users) group)
307             users (cdr users))
308       (when (or (= (length group) 3)
309                 (null users))
310         (riece-send-string
311          (format "MODE %s %c%s %s\r\n"
312                  (riece-identity-prefix riece-current-channel)
313                  (if current-prefix-arg
314                      ?-
315                    ?+)
316                  (make-string (length group) ?o)
317                  (mapconcat #'identity (nreverse group) " ")))
318         (setq group nil)))))
319
320 (defun riece-command-set-speakers (users &optional arg)
321   (interactive
322    (progn
323      (riece-check-channel-commands-are-usable t)
324      (let ((completion-ignore-case t))
325        (list (riece-completing-read-multiple
326               (if current-prefix-arg
327                   "Unset +v for users"
328                 "Set +v for users")
329               (riece-with-server-buffer
330                   (riece-identity-server riece-current-channel)
331                 (riece-channel-get-users (riece-identity-prefix
332                                           riece-current-channel)))
333               (if current-prefix-arg
334                   (lambda (user)
335                     (memq ?v (cdr user)))
336                 (lambda (user)
337                   (not (memq ?v (cdr user))))))
338              current-prefix-arg))))
339   (let (group)
340     (while users
341       (setq group (cons (car users) group)
342             users (cdr users))
343       (when (or (= (length group) 3)
344                 (null users))
345         (riece-send-string
346          (format "MODE %s %c%s %s\r\n"
347                  (riece-identity-prefix riece-current-channel)
348                  (if current-prefix-arg
349                      ?-
350                    ?+)
351                  (make-string (length group) ?v)
352                  (mapconcat #'identity (nreverse group) " ")))
353         (setq group nil)))))
354
355 (defun riece-command-send-message (message notice)
356   "Send MESSAGE to the current channel."
357   (run-hooks 'riece-command-send-message-hook)
358   (if (equal message "")
359       (error "No text to send"))
360   (riece-check-channel-commands-are-usable)
361   (if notice
362       (progn
363         (riece-send-string
364          (format "NOTICE %s :%s\r\n"
365                  (riece-identity-prefix riece-current-channel)
366                  message))
367         (riece-display-message
368          (riece-make-message (riece-current-nickname) riece-current-channel
369                              message 'notice t)))
370     (riece-send-string
371      (format "PRIVMSG %s :%s\r\n"
372              (riece-identity-prefix riece-current-channel)
373              message))
374     (riece-display-message
375      (riece-make-message (riece-current-nickname) riece-current-channel
376                          message nil t))))
377
378 (defun riece-command-enter-message ()
379   "Send the current line to the current channel."
380   (interactive)
381   (riece-command-send-message (buffer-substring
382                                (riece-line-beginning-position)
383                                (riece-line-end-position))
384                               nil)
385   (let ((next-line-add-newlines t))
386     (next-line 1)))
387
388 (defun riece-command-enter-message-as-notice ()
389   "Send the current line to the current channel as NOTICE."
390   (interactive)
391   (riece-command-send-message (buffer-substring
392                                (riece-line-beginning-position)
393                                (riece-line-end-position))
394                               t)
395   (let ((next-line-add-newlines t))
396     (next-line 1)))
397
398 (defun riece-command-enter-message-to-user (user)
399   "Send the current line to USER."
400   (interactive
401    (if (and (bolp) (eolp))
402        (error "No text to send")
403      (let ((completion-ignore-case t))
404        (list (riece-completing-read-identity
405               "Message to user: "
406               (riece-get-users-on-server (riece-current-server-name))
407               nil nil nil nil nil t)))))
408   (let ((text (buffer-substring
409                (riece-line-beginning-position)
410                (riece-line-end-position))))
411     (riece-send-string
412      (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
413     (riece-display-message
414      (riece-make-message (riece-current-nickname) user text nil t)))
415   (let ((next-line-add-newlines t))
416     (next-line 1)))
417
418 (defun riece-command-join-channel (target key)
419   (let ((process (riece-server-process (riece-identity-server target))))
420     (unless process
421       (error "%s" (substitute-command-keys
422                    "Type \\[riece-command-open-server] to open server.")))
423     (riece-process-send-string process
424                                (if key
425                                    (format "JOIN %s :%s\r\n"
426                                            (riece-identity-prefix target)
427                                            key)
428                                  (format "JOIN %s\r\n"
429                                          (riece-identity-prefix target))))))
430
431 (defun riece-command-join-partner (target)
432   (let ((pointer (riece-identity-member target riece-current-channels)))
433     (if pointer
434         (riece-command-switch-to-channel (car pointer))
435       (riece-join-channel target)
436       (riece-switch-to-channel target))))
437
438 (defun riece-command-join (target &optional key)
439   (interactive
440    (let* ((completion-ignore-case t)
441           (target
442            (if riece-join-channel-candidate
443                (let ((default (riece-format-identity
444                                riece-join-channel-candidate)))
445                  (riece-completing-read-identity
446                   (format "Join channel/user (default %s): " default)
447                   (riece-get-identities-on-server (riece-current-server-name))
448                   nil nil nil nil default))
449              (riece-completing-read-identity
450               "Join channel/user: "
451               (riece-get-identities-on-server (riece-current-server-name)))))
452           key)
453      (if (and current-prefix-arg
454               (riece-channel-p (riece-identity-prefix target)))
455          (setq key
456                (riece-read-passwd (format "Key for %s: "
457                                           (riece-format-identity target)))))
458      (list target key)))
459   (let ((pointer (riece-identity-member target riece-current-channels)))
460     (if pointer
461         (riece-command-switch-to-channel (car pointer))
462       (if (riece-channel-p (riece-identity-prefix target))
463           (riece-command-join-channel target key)
464         (riece-command-join-partner target)))))
465
466 (defun riece-command-part-channel (target message)
467   (let ((process (riece-server-process (riece-identity-server target))))
468     (riece-process-send-string process
469                                (if message
470                                    (format "PART %s :%s\r\n"
471                                            (riece-identity-prefix target)
472                                            message)
473                                  (format "PART %s\r\n"
474                                          (riece-identity-prefix target))))))
475
476 (defun riece-command-part (target &optional message)
477   (interactive
478    (progn
479      (riece-check-channel-commands-are-usable)
480      (let* ((completion-ignore-case t)
481             (target
482              (riece-completing-read-identity
483               (format "Part from channel/user (default %s): "
484                       (riece-format-identity riece-current-channel))
485               riece-current-channels nil nil nil nil
486               (riece-format-identity riece-current-channel)))
487             message)
488        (if (and current-prefix-arg
489                 (riece-channel-p (riece-identity-prefix target)))
490            (setq message (read-string "Message: ")))
491        (list target message))))
492   (if (riece-identity-member target riece-current-channels)
493       (if (riece-channel-p (riece-identity-prefix target))
494           (riece-command-part-channel target message)
495         (riece-part-channel target))
496     (error "You are not talking with %s" target)))
497
498 (defun riece-command-change-nickname (nickname)
499   "Change your nickname to NICK."
500   (interactive "sEnter your nickname: ")
501   (riece-send-string (format "NICK %s\r\n" nickname)))
502
503 (defun riece-command-scroll-down (lines)
504   "Scroll LINES down dialogue buffer from command buffer."
505   (interactive "P")
506   (let ((buffer (if (and riece-channel-buffer-mode
507                          riece-current-channel)
508                     riece-channel-buffer
509                   riece-dialogue-buffer)))
510     (if (get-buffer-window buffer)
511         (condition-case nil
512             (let ((other-window-scroll-buffer buffer))
513               (scroll-other-window-down lines))
514           (beginning-of-buffer
515            (message "Beginning of buffer"))))))
516
517 (defun riece-command-scroll-up (lines)
518   "Scroll LINES up dialogue buffer from command buffer."
519   (interactive "P")
520   (let ((buffer (if (and riece-channel-buffer-mode
521                          riece-current-channel)
522                     riece-channel-buffer
523                   riece-dialogue-buffer)))
524     (if (get-buffer-window buffer)
525         (condition-case nil
526             (let ((other-window-scroll-buffer buffer))
527               (scroll-other-window lines))
528           (end-of-buffer
529            (message "End of buffer"))))))
530
531 (defun riece-command-user-list-scroll-down (lines)
532   "Scroll LINES down user list buffer from command buffer."
533   (interactive "P")
534   (if (get-buffer-window riece-user-list-buffer)
535       (condition-case nil
536           (let ((other-window-scroll-buffer riece-user-list-buffer))
537             (scroll-other-window-down lines))
538         (beginning-of-buffer
539          (message "Beginning of buffer")))))
540
541 (defun riece-command-user-list-scroll-up (lines)
542   "Scroll LINES up user list buffer from command buffer."
543   (interactive "P")
544   (if (get-buffer-window riece-user-list-buffer)
545       (condition-case nil
546           (let ((other-window-scroll-buffer riece-user-list-buffer))
547             (scroll-other-window lines))
548         (end-of-buffer
549          (message "End of buffer")))))
550
551 (defun riece-command-toggle-away (&optional message)
552   "Mark yourself as being away."
553   (interactive
554    (if (and (not (riece-with-server-buffer (riece-identity-server
555                                             (riece-current-nickname))
556                    (riece-user-get-away (riece-identity-prefix
557                                          (riece-current-nickname)))))
558             current-prefix-arg)
559        (list (read-from-minibuffer
560               "Away message: " (cons (or riece-away-message "") 0)))))
561   (if (riece-with-server-buffer (riece-identity-server
562                                  (riece-current-nickname))
563         (riece-user-get-away (riece-identity-prefix
564                               (riece-current-nickname))))
565       (riece-send-string "AWAY\r\n")
566     (riece-send-string (format "AWAY :%s\r\n" (or message
567                                                   riece-away-message)))))
568
569 (defun riece-command-toggle-freeze (&optional arg)
570   "Prevent automatic scrolling of the dialogue window.
571 If prefix argument ARG is non-nil, toggle frozen status."
572   (interactive "P")
573   (with-current-buffer (if (eq (derived-mode-class major-mode)
574                                'riece-dialogue-mode)
575                            (current-buffer)
576                          (if (and riece-channel-buffer-mode
577                                   riece-channel-buffer)
578                              riece-channel-buffer
579                            riece-dialogue-buffer))
580     (setq riece-freeze (if arg
581                            (< 0 (prefix-numeric-value arg))
582                          (not riece-freeze)))
583     (riece-emit-signal 'buffer-freeze-changed
584                        (current-buffer) riece-freeze)))
585
586 (defun riece-command-toggle-own-freeze (&optional arg)
587   "Prevent automatic scrolling of the dialogue window.
588 The difference from `riece-command-freeze' is that your messages are hidden.
589 If prefix argument ARG is non-nil, toggle frozen status."
590   (interactive "P")
591   (with-current-buffer (if (eq (derived-mode-class major-mode)
592                                'riece-dialogue-mode)
593                            (current-buffer)
594                          (if (and riece-channel-buffer-mode
595                                   riece-channel-buffer)
596                              riece-channel-buffer
597                            riece-dialogue-buffer))
598     (if (if arg
599             (< 0 (prefix-numeric-value arg))
600           (not (eq riece-freeze 'own)))
601         (setq riece-freeze 'own)
602       (setq riece-freeze nil))
603     (riece-emit-signal 'buffer-freeze-changed
604                        (current-buffer) riece-freeze)))
605
606 (eval-when-compile
607   (autoload 'riece-exit "riece"))
608 (defun riece-command-quit (&optional arg)
609   "Quit IRC."
610   (interactive "P")
611   (if (null riece-server-process-alist)
612       (progn
613         (message "No server process")
614         (ding))
615     (if (y-or-n-p "Really quit IRC? ")
616         (let ((message
617                (if arg
618                    (read-string "Message: ")
619                riece-quit-message))
620               (alist riece-server-process-alist))
621           (while alist
622             (riece-quit-server-process (cdr (car alist)) message)
623             (setq alist (cdr alist)))))))
624
625 (defun riece-command-raw (command)
626   "Enter raw IRC command, which is sent to the server."
627   (interactive "sIRC command: ")
628   (riece-send-string (concat command "\r\n")))
629
630 (defun riece-command-beginning-of-buffer ()
631   "Scroll channel buffer to the beginning."
632   (interactive)
633   (let (buffer window)
634     (setq buffer (if riece-channel-buffer-mode
635                      riece-channel-buffer
636                    riece-dialogue-buffer))
637     (or (setq window (get-buffer-window buffer))
638         (setq window (get-buffer-window riece-dialogue-buffer)
639               buffer riece-dialogue-buffer))
640     (when window
641       (save-selected-window
642         (select-window window)
643         (goto-char (point-min))))))
644
645 (defun riece-command-end-of-buffer ()
646   "Scroll channel buffer to the end."
647   (interactive)
648   (let (buffer window)
649     (setq buffer (if riece-channel-buffer-mode
650                      riece-channel-buffer
651                    riece-dialogue-buffer))
652     (or (setq window (get-buffer-window buffer))
653         (setq window (get-buffer-window riece-dialogue-buffer)
654               buffer riece-dialogue-buffer))
655     (when window
656       (save-selected-window
657         (select-window window)
658         (goto-char (point-max))))))
659
660 (defun riece-command-copy-region (start end)
661   "Move current region between START and END to `kill-ring'."
662   (interactive "r")
663   (kill-new (buffer-substring-no-properties start end)))
664
665 (defun riece-command-complete-user ()
666   "Complete a user name in the current buffer."
667   (interactive)
668   (let* ((completion-ignore-case t)
669          (table (mapcar (lambda (user)
670                           (list (riece-format-identity user t)))
671                         (riece-get-users-on-server
672                          (riece-current-server-name))))
673          (current (or (current-word) ""))
674          (completion (try-completion current table))
675          (all (all-completions current table)))
676     (if (eq completion t)
677         nil
678       (if (null completion)
679           (message "Can't find completion for \"%s\"" current)
680         (if (equal current completion)
681             (with-output-to-temp-buffer "*Help*"
682               (display-completion-list all))
683           (delete-region (point) (- (point) (length current)))
684           (insert completion))))))
685
686 (defun riece-command-open-server (server-name)
687   (interactive
688    (list (completing-read "Open server: " riece-server-alist)))
689   (if (riece-server-process server-name)
690       (error "%s is already opened" server-name))
691   (riece-open-server
692    (riece-server-name-to-server server-name)
693    server-name))
694
695 (defun riece-command-close-server (server-name &optional message)
696   (interactive
697    (list (completing-read "Close server: " riece-server-process-alist)
698          (if current-prefix-arg
699              (read-string "Message: ")
700            riece-quit-message)))
701   (riece-quit-server-process (riece-server-process server-name) message))
702
703 (defun riece-command-universal-server-name-argument ()
704   (interactive)
705   (let* ((riece-overriding-server-name
706           (completing-read "Server: " riece-server-process-alist))
707          (command
708           (key-binding (read-key-sequence
709                         (format "Command to execute on \"%s\":"
710                                 riece-overriding-server-name)))))
711     (message "")
712     (call-interactively command)))
713
714 (provide 'riece-commands)
715
716 ;;; riece-commands.el ends here