dc8ac186ca83ee4574a31971182284fa1aaf44f6
[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    (let ((completion-ignore-case t))
402      (list (riece-completing-read-identity
403             "Message to user: "
404             (riece-get-users-on-server (riece-current-server-name))
405             nil nil nil nil nil t))))
406   (let ((text (buffer-substring
407                (riece-line-beginning-position)
408                (riece-line-end-position))))
409     (riece-send-string
410      (format "PRIVMSG %s :%s\r\n" (riece-identity-prefix user) text))
411     (riece-display-message
412      (riece-make-message (riece-current-nickname) user text nil t)))
413   (let ((next-line-add-newlines t))
414     (next-line 1)))
415
416 (defun riece-command-join-channel (target key)
417   (let ((process (riece-server-process (riece-identity-server target))))
418     (unless process
419       (error "%s" (substitute-command-keys
420                    "Type \\[riece-command-open-server] to open server.")))
421     (riece-process-send-string process
422                                (if key
423                                    (format "JOIN %s :%s\r\n"
424                                            (riece-identity-prefix target)
425                                            key)
426                                  (format "JOIN %s\r\n"
427                                          (riece-identity-prefix target))))))
428
429 (defun riece-command-join-partner (target)
430   (let ((pointer (riece-identity-member target riece-current-channels)))
431     (if pointer
432         (riece-command-switch-to-channel (car pointer))
433       (riece-join-channel target)
434       (riece-switch-to-channel target))))
435
436 (defun riece-command-join (target &optional key)
437   (interactive
438    (let* ((completion-ignore-case t)
439           (target
440            (if riece-join-channel-candidate
441                (let ((default (riece-format-identity
442                                riece-join-channel-candidate)))
443                  (riece-completing-read-identity
444                   (format "Join channel/user (default %s): " default)
445                   (riece-get-identities-on-server (riece-current-server-name))
446                   nil nil nil nil default))
447              (riece-completing-read-identity
448               "Join channel/user: "
449               (riece-get-identities-on-server (riece-current-server-name)))))
450           key)
451      (if (and current-prefix-arg
452               (riece-channel-p (riece-identity-prefix target)))
453          (setq key
454                (riece-read-passwd (format "Key for %s: "
455                                           (riece-format-identity target)))))
456      (list target key)))
457   (let ((pointer (riece-identity-member target riece-current-channels)))
458     (if pointer
459         (riece-command-switch-to-channel (car pointer))
460       (if (riece-channel-p (riece-identity-prefix target))
461           (riece-command-join-channel target key)
462         (riece-command-join-partner target)))))
463
464 (defun riece-command-part-channel (target message)
465   (let ((process (riece-server-process (riece-identity-server target))))
466     (riece-process-send-string process
467                                (if message
468                                    (format "PART %s :%s\r\n"
469                                            (riece-identity-prefix target)
470                                            message)
471                                  (format "PART %s\r\n"
472                                          (riece-identity-prefix target))))))
473
474 (defun riece-command-part (target &optional message)
475   (interactive
476    (progn
477      (riece-check-channel-commands-are-usable)
478      (let* ((completion-ignore-case t)
479             (target
480              (riece-completing-read-identity
481               (format "Part from channel/user (default %s): "
482                       (riece-format-identity riece-current-channel))
483               riece-current-channels nil nil nil nil
484               (riece-format-identity riece-current-channel)))
485             message)
486        (if (and current-prefix-arg
487                 (riece-channel-p (riece-identity-prefix target)))
488            (setq message (read-string "Message: ")))
489        (list target message))))
490   (if (riece-identity-member target riece-current-channels)
491       (if (riece-channel-p (riece-identity-prefix target))
492           (riece-command-part-channel target message)
493         (riece-part-channel target))
494     (error "You are not talking with %s" target)))
495
496 (defun riece-command-change-nickname (nickname)
497   "Change your nickname to NICK."
498   (interactive "sEnter your nickname: ")
499   (riece-send-string (format "NICK %s\r\n" nickname)))
500
501 (defun riece-command-scroll-down (lines)
502   "Scroll LINES down dialogue buffer from command buffer."
503   (interactive "P")
504   (let ((buffer (if (and riece-channel-buffer-mode
505                          riece-current-channel)
506                     riece-channel-buffer
507                   riece-dialogue-buffer)))
508     (if (get-buffer-window buffer)
509         (condition-case nil
510             (let ((other-window-scroll-buffer buffer))
511               (scroll-other-window-down lines))
512           (beginning-of-buffer
513            (message "Beginning of buffer"))))))
514
515 (defun riece-command-scroll-up (lines)
516   "Scroll LINES up dialogue buffer from command buffer."
517   (interactive "P")
518   (let ((buffer (if (and riece-channel-buffer-mode
519                          riece-current-channel)
520                     riece-channel-buffer
521                   riece-dialogue-buffer)))
522     (if (get-buffer-window buffer)
523         (condition-case nil
524             (let ((other-window-scroll-buffer buffer))
525               (scroll-other-window lines))
526           (end-of-buffer
527            (message "End of buffer"))))))
528
529 (defun riece-command-user-list-scroll-down (lines)
530   "Scroll LINES down user list buffer from command buffer."
531   (interactive "P")
532   (if (get-buffer-window riece-user-list-buffer)
533       (condition-case nil
534           (let ((other-window-scroll-buffer riece-user-list-buffer))
535             (scroll-other-window-down lines))
536         (beginning-of-buffer
537          (message "Beginning of buffer")))))
538
539 (defun riece-command-user-list-scroll-up (lines)
540   "Scroll LINES up user list buffer from command buffer."
541   (interactive "P")
542   (if (get-buffer-window riece-user-list-buffer)
543       (condition-case nil
544           (let ((other-window-scroll-buffer riece-user-list-buffer))
545             (scroll-other-window lines))
546         (end-of-buffer
547          (message "End of buffer")))))
548
549 (defun riece-command-toggle-away (&optional message)
550   "Mark yourself as being away."
551   (interactive
552    (if (and (not (riece-with-server-buffer (riece-identity-server
553                                             (riece-current-nickname))
554                    (riece-user-get-away (riece-identity-prefix
555                                          (riece-current-nickname)))))
556             current-prefix-arg)
557        (list (read-from-minibuffer
558               "Away message: " (cons (or riece-away-message "") 0)))))
559   (if (riece-with-server-buffer (riece-identity-server
560                                  (riece-current-nickname))
561         (riece-user-get-away (riece-identity-prefix
562                               (riece-current-nickname))))
563       (riece-send-string "AWAY\r\n")
564     (riece-send-string (format "AWAY :%s\r\n" (or message
565                                                   riece-away-message)))))
566
567 (defun riece-command-toggle-freeze (&optional arg)
568   "Prevent automatic scrolling of the dialogue window.
569 If prefix argument ARG is non-nil, toggle frozen status."
570   (interactive "P")
571   (with-current-buffer (if (eq (derived-mode-class major-mode)
572                                'riece-dialogue-mode)
573                            (current-buffer)
574                          (if (and riece-channel-buffer-mode
575                                   riece-channel-buffer)
576                              riece-channel-buffer
577                            riece-dialogue-buffer))
578     (setq riece-freeze (if arg
579                            (< 0 (prefix-numeric-value arg))
580                          (not riece-freeze)))
581     (riece-emit-signal 'buffer-freeze-changed
582                        (current-buffer) riece-freeze)))
583
584 (defun riece-command-toggle-own-freeze (&optional arg)
585   "Prevent automatic scrolling of the dialogue window.
586 The difference from `riece-command-freeze' is that your messages are hidden.
587 If prefix argument ARG is non-nil, toggle frozen status."
588   (interactive "P")
589   (with-current-buffer (if (eq (derived-mode-class major-mode)
590                                'riece-dialogue-mode)
591                            (current-buffer)
592                          (if (and riece-channel-buffer-mode
593                                   riece-channel-buffer)
594                              riece-channel-buffer
595                            riece-dialogue-buffer))
596     (if (if arg
597             (< 0 (prefix-numeric-value arg))
598           (not (eq riece-freeze 'own)))
599         (setq riece-freeze 'own)
600       (setq riece-freeze nil))
601     (riece-emit-signal 'buffer-freeze-changed
602                        (current-buffer) riece-freeze)))
603
604 (eval-when-compile
605   (autoload 'riece-exit "riece"))
606 (defun riece-command-quit (&optional arg)
607   "Quit IRC."
608   (interactive "P")
609   (if (null riece-server-process-alist)
610       (progn
611         (message "No server process")
612         (ding))
613     (if (y-or-n-p "Really quit IRC? ")
614         (let ((message
615                (if arg
616                    (read-string "Message: ")
617                riece-quit-message))
618               (alist riece-server-process-alist))
619           (while alist
620             (riece-quit-server-process (cdr (car alist)) message)
621             (setq alist (cdr alist)))))))
622
623 (defun riece-command-raw (command)
624   "Enter raw IRC command, which is sent to the server."
625   (interactive "sIRC command: ")
626   (riece-send-string (concat command "\r\n")))
627
628 (defun riece-command-beginning-of-buffer ()
629   "Scroll channel buffer to the beginning."
630   (interactive)
631   (let (buffer window)
632     (setq buffer (if riece-channel-buffer-mode
633                      riece-channel-buffer
634                    riece-dialogue-buffer))
635     (or (setq window (get-buffer-window buffer))
636         (setq window (get-buffer-window riece-dialogue-buffer)
637               buffer riece-dialogue-buffer))
638     (when window
639       (save-selected-window
640         (select-window window)
641         (goto-char (point-min))))))
642
643 (defun riece-command-end-of-buffer ()
644   "Scroll channel buffer to the end."
645   (interactive)
646   (let (buffer window)
647     (setq buffer (if riece-channel-buffer-mode
648                      riece-channel-buffer
649                    riece-dialogue-buffer))
650     (or (setq window (get-buffer-window buffer))
651         (setq window (get-buffer-window riece-dialogue-buffer)
652               buffer riece-dialogue-buffer))
653     (when window
654       (save-selected-window
655         (select-window window)
656         (goto-char (point-max))))))
657
658 (defun riece-command-copy-region (start end)
659   "Move current region between START and END to `kill-ring'."
660   (interactive "r")
661   (kill-new (buffer-substring-no-properties start end)))
662
663 (defun riece-command-complete-user ()
664   "Complete a user name in the current buffer."
665   (interactive)
666   (let* ((completion-ignore-case t)
667          (table (mapcar (lambda (user)
668                           (list (riece-format-identity user t)))
669                         (riece-get-users-on-server
670                          (riece-current-server-name))))
671          (current (or (current-word) ""))
672          (completion (try-completion current table))
673          (all (all-completions current table)))
674     (if (eq completion t)
675         nil
676       (if (null completion)
677           (message "Can't find completion for \"%s\"" current)
678         (if (equal current completion)
679             (with-output-to-temp-buffer "*Help*"
680               (display-completion-list all))
681           (delete-region (point) (- (point) (length current)))
682           (insert completion))))))
683
684 (defun riece-command-open-server (server-name)
685   (interactive
686    (list (completing-read "Open server: " riece-server-alist)))
687   (if (riece-server-process server-name)
688       (error "%s is already opened" server-name))
689   (riece-open-server
690    (riece-server-name-to-server server-name)
691    server-name))
692
693 (defun riece-command-close-server (server-name &optional message)
694   (interactive
695    (list (completing-read "Close server: " riece-server-process-alist)
696          (if current-prefix-arg
697              (read-string "Message: ")
698            riece-quit-message)))
699   (riece-quit-server-process (riece-server-process server-name) message))
700
701 (defun riece-command-universal-server-name-argument ()
702   (interactive)
703   (let* ((riece-overriding-server-name
704           (completing-read "Server: " riece-server-process-alist))
705          (command
706           (key-binding (read-key-sequence
707                         (format "Command to execute on \"%s\":"
708                                 riece-overriding-server-name)))))
709     (message "")
710     (call-interactively command)))
711
712 (provide 'riece-commands)
713
714 ;;; riece-commands.el ends here