Fix interactive spec.
[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-version)
32 (require 'riece-server)
33 (require 'riece-misc)
34 (require 'riece-identity)
35 (require 'riece-message)
36
37 ;;; Channel movement:
38 (defun riece-command-switch-to-channel (channel)
39   (interactive (list (riece-completing-read-identity
40                       "Channel/User: " riece-current-channels nil t)))
41   (unless (equal channel riece-current-channels)
42     (riece-switch-to-channel channel)
43     (riece-redisplay-buffers)))
44
45 (defun riece-command-switch-to-channel-by-number (number)
46   (interactive
47    (let ((command-name (symbol-name this-command)))
48      (if (string-match "[0-9]+$" command-name)
49          (list (string-to-number (match-string 0 command-name)))
50        (list (string-to-number (read-string "Number: "))))))
51   (let ((channel (nth (1- number) riece-current-channels)))
52     (if channel
53         (riece-command-switch-to-channel channel)
54       (error "No such number!"))))
55
56 (eval-and-compile
57   (let ((number 1))
58     (while (<= number 20)
59       (defalias (intern (concat "riece-command-switch-to-channel-by-number-"
60                                 (number-to-string number)))
61         'riece-command-switch-to-channel-by-number)
62       (setq number (1+ number)))))
63
64 (defun riece-command-next-channel ()
65   "Select the next channel."
66   (interactive)
67   (when (> (length riece-current-channels) 1)
68     (let ((pointer (cdr (riece-identity-member
69                          riece-current-channel
70                          riece-current-channels))))
71       (while (and pointer
72                   (null (car pointer)))
73         (setq pointer (cdr pointer)))
74       (when (null pointer)
75         (setq pointer riece-current-channels)
76         (while (and pointer
77                     (null (car pointer)))
78           (setq pointer (cdr pointer))))
79       (if (car pointer)
80           (riece-command-switch-to-channel (car pointer))
81         (error "No such channel!")))))
82
83 (defun riece-command-previous-channel ()
84   "Select the previous channel."
85   (interactive)
86   (when (> (length riece-current-channels) 1)
87     (let ((pointer (riece-identity-member
88                     riece-current-channel
89                     riece-current-channels))
90           (start riece-current-channels)
91           channel)
92       (while (and start (not (eq start pointer)))
93         (if (car start)
94             (setq channel (car start)))
95         (setq start (cdr start)))
96       (when (null channel)
97         (setq start (copy-sequence riece-current-channels))
98         (setq start (delq nil start))
99         (and (> (length start) 1)
100              (setq channel (nth (1- (length start)) start))))
101       (if channel
102           (riece-command-switch-to-channel channel)
103         (error "No such channel!")))))
104
105 (defun riece-command-select-command-buffer ()
106   "Select the command buffer."
107   (interactive)
108   (let ((window (get-buffer-window riece-command-buffer)))
109     (if window
110         (select-window window))))
111
112 (defun riece-command-configure-windows ()
113   (interactive)
114   (riece-redisplay-buffers t))
115
116 (defun riece-command-change-layout (name)
117   "Select a layout-name from all current available layouts and change
118 the layout to the selected layout-name."
119   (interactive (list (completing-read "Layout: " riece-layout-alist)))
120   (setq riece-layout name)
121   (riece-command-configure-windows))
122
123 (defun riece-command-toggle-channel-buffer-mode ()
124   (interactive)
125   (setq riece-channel-buffer-mode
126         (not riece-channel-buffer-mode)
127         riece-save-variables-are-dirty t)
128   (riece-command-configure-windows))
129
130 (defun riece-command-toggle-user-list-buffer-mode ()
131   (interactive)
132   (setq riece-user-list-buffer-mode
133         (not riece-user-list-buffer-mode)
134         riece-save-variables-are-dirty t)
135   (riece-command-configure-windows))
136
137 (defun riece-command-toggle-channel-list-buffer-mode ()
138   (interactive)
139   (setq riece-channel-list-buffer-mode
140         (not riece-channel-list-buffer-mode)
141         riece-save-variables-are-dirty t)
142   (riece-command-configure-windows))
143
144 (defun riece-command-finger (user &optional recurse)
145   (interactive
146    (let* ((completion-ignore-case t)
147           (user (completing-read
148                  "User: "
149                  (mapcar #'list (riece-get-users-on-server)))))
150      (list user current-prefix-arg)))
151   (if recurse
152       (riece-send-string (format "WHOIS %s %s\r\n" user user))
153     (riece-send-string (format "WHOIS %s\r\n" user))))
154
155 (defun riece-command-topic (topic)
156   (interactive
157    (progn
158      (riece-check-channel-commands-are-usable t)
159      (list (read-from-minibuffer
160             "Topic: " (cons (or (riece-with-server-buffer
161                                     (riece-identity-server
162                                      riece-current-channel)
163                                   (riece-channel-get-topic
164                                    (riece-identity-prefix
165                                     riece-current-channel)))
166                                 "")
167                             0)))))
168   (riece-send-string (format "TOPIC %s :%s\r\n"
169                              (riece-identity-prefix riece-current-channel)
170                              topic)))
171
172 (defun riece-command-invite (user)
173   (interactive
174    (let ((completion-ignore-case t))
175      (riece-check-channel-commands-are-usable t)
176      (list (completing-read
177             "User: "
178             (mapcar #'list (riece-get-users-on-server))))))
179   (riece-send-string (format "INVITE %s %s\r\n"
180                              user (riece-identity-prefix
181                                    riece-current-channel))))
182
183 (defun riece-command-kick (user &optional message)
184   (interactive
185    (let ((completion-ignore-case t))
186      (riece-check-channel-commands-are-usable t)
187      (list (completing-read
188             "User: "
189             (mapcar #'list
190                     (riece-with-server-buffer
191                         (riece-identity-server riece-current-channel)
192                       (riece-channel-get-users
193                        (riece-identity-prefix riece-current-channel)))))
194            (if current-prefix-arg
195                (read-string "Message: ")))))
196   (riece-send-string
197    (if message
198        (format "KICK %s %s :%s\r\n"
199                (riece-identity-prefix riece-current-channel)
200                user message)
201      (format "KICK %s %s\r\n"
202              (riece-identity-prefix riece-current-channel)
203              user))))
204
205 (defun riece-command-names (pattern)
206   (interactive
207    (let ((completion-ignore-case t))
208      (list (read-from-minibuffer
209             "Pattern: "
210             (if (and riece-current-channel
211                      (riece-channel-p (riece-identity-prefix
212                                        riece-current-channel)))
213                 (cons (riece-identity-prefix riece-current-channel)
214                       0))))))
215   (if (or (not (equal pattern ""))
216           (yes-or-no-p "Really want to query NAMES without argument? "))
217       (riece-send-string (format "NAMES %s\r\n" pattern))))
218
219 (defun riece-command-who (pattern)
220   (interactive
221    (let ((completion-ignore-case t))
222      (list (read-from-minibuffer
223             "Pattern: "
224             (if (and riece-current-channel
225                      (riece-channel-p (riece-identity-prefix
226                                        riece-current-channel)))
227                 (cons (riece-identity-prefix riece-current-channel)
228                       0))))))
229   (if (or (not (equal pattern ""))
230           (yes-or-no-p "Really want to query WHO without argument? "))
231       (riece-send-string (format "WHO %s\r\n" pattern))))
232
233 (defun riece-command-list (pattern)
234   (interactive
235    (let ((completion-ignore-case t))
236      (list (read-from-minibuffer
237             "Pattern: "
238             (if (and riece-current-channel
239                      (riece-channel-p (riece-identity-prefix
240                                        riece-current-channel)))
241                 (cons (riece-identity-prefix riece-current-channel)
242                       0))))))
243   (if (or (not (equal pattern ""))
244           (yes-or-no-p "Really want to query LIST without argument? "))
245       (riece-send-string (format "LIST %s\r\n" pattern))))
246
247 (defun riece-command-change-mode (channel change)
248   (interactive
249    (let* ((completion-ignore-case t)
250           (channel
251            (if current-prefix-arg
252                (riece-completing-read-identity
253                 "Channel/User: " riece-current-channels)
254              (riece-check-channel-commands-are-usable t)
255              riece-current-channel))
256           (riece-overriding-server-name (riece-identity-server channel))
257           (riece-temp-minibuffer-message
258            (concat "[Available modes: "
259                    (riece-with-server-buffer (riece-identity-server channel)
260                      (if (riece-channel-p (riece-identity-prefix channel))
261                          (if riece-supported-channel-modes
262                              (apply #'string riece-supported-channel-modes))
263                        (if riece-supported-user-modes
264                            (apply #'string riece-supported-user-modes))))
265                    "]")))
266      (list channel
267            (read-from-minibuffer
268             (concat (riece-concat-channel-modes
269                      channel "Mode (? for help)") ": ")
270             nil riece-minibuffer-map))))
271   (riece-send-string (format "MODE %s :%s\r\n" (riece-identity-prefix channel)
272                              change)))
273
274 (defun riece-command-set-operators (users &optional arg)
275   (interactive
276    (progn
277      (riece-check-channel-commands-are-usable t)
278      (let ((operators
279             (riece-with-server-buffer
280                 (riece-identity-server riece-current-channel)
281               (riece-channel-get-operators
282                (riece-identity-prefix riece-current-channel))))
283            (completion-ignore-case t)
284            users)
285        (if current-prefix-arg
286            (setq users (riece-completing-read-multiple
287                         "Users"
288                         (mapcar #'list operators)))
289          (setq users (riece-completing-read-multiple
290                       "Users"
291                       (delq nil (mapcar
292                                  (lambda (user)
293                                    (unless (member user operators)
294                                      (list user)))
295                                  (riece-with-server-buffer
296                                      (riece-identity-server
297                                       riece-current-channel)
298                                    (riece-channel-get-users
299                                     (riece-identity-prefix
300                                      riece-current-channel))))))))
301        (list users current-prefix-arg))))
302   (let (group)
303     (while users
304       (setq group (cons (car users) group)
305             users (cdr users))
306       (if (or (= (length group) 3)
307               (null users))
308           (riece-send-string
309            (format "MODE %s %c%s %s\r\n"
310                    (riece-identity-prefix riece-current-channel)
311                    (if current-prefix-arg
312                        ?-
313                      ?+)
314                    (make-string (length group) ?o)
315                    (mapconcat #'identity group " ")))))))
316
317 (defun riece-command-set-speakers (users &optional arg)
318   (interactive
319    (progn
320      (riece-check-channel-commands-are-usable t)
321      (let ((speakers
322             (riece-with-server-buffer
323                 (riece-identity-server riece-current-channel)
324               (riece-channel-get-speakers
325                (riece-identity-prefix riece-current-channel))))
326            (completion-ignore-case t)
327            users)
328        (if current-prefix-arg
329            (setq users (riece-completing-read-multiple
330                         "Users"
331                         (mapcar #'list speakers)))
332          (setq users (riece-completing-read-multiple
333                       "Users"
334                       (delq nil (mapcar
335                                  (lambda (user)
336                                    (unless (member user speakers)
337                                      (list user)))
338                                  (riece-with-server-buffer
339                                      (riece-identity-server
340                                       riece-current-channel)
341                                    (riece-channel-get-users
342                                     (riece-identity-prefix
343                                      riece-current-channel))))))))
344        (list users current-prefix-arg))))
345   (let (group)
346     (while users
347       (setq group (cons (car users) group)
348             users (cdr users))
349       (if (or (= (length group) 3)
350               (null users))
351           (riece-send-string
352            (format "MODE %s %c%s %s\r\n"
353                    (riece-identity-prefix riece-current-channel)
354                    (if current-prefix-arg
355                        ?-
356                      ?+)
357                    (make-string (length group) ?v)
358                    (mapconcat #'identity group " ")))))))
359
360 (defun riece-command-send-message (message notice)
361   "Send MESSAGE to the current channel."
362   (if (equal message "")
363       (error "No text to send"))
364   (riece-check-channel-commands-are-usable)
365   (if notice
366       (progn
367         (riece-send-string
368          (format "NOTICE %s :%s\r\n"
369                  (riece-identity-prefix riece-current-channel)
370                  message))
371         (riece-display-message
372          (riece-make-message (riece-current-nickname) riece-current-channel
373                              message 'notice t)))
374     (riece-send-string
375      (format "PRIVMSG %s :%s\r\n"
376              (riece-identity-prefix riece-current-channel)
377              message))
378     (riece-display-message
379      (riece-make-message (riece-current-nickname) riece-current-channel
380                          message nil t))))
381
382 (defun riece-command-enter-message ()
383   "Send the current line to the current channel."
384   (interactive)
385   (riece-command-send-message (buffer-substring
386                                (riece-line-beginning-position)
387                                (riece-line-end-position))
388                               nil)
389   (let ((next-line-add-newlines t))
390     (next-line 1)))
391
392 (defun riece-command-enter-message-as-notice ()
393   "Send the current line to the current channel as NOTICE."
394   (interactive)
395   (riece-command-send-message (buffer-substring
396                                (riece-line-beginning-position)
397                                (riece-line-end-position))
398                               t)
399   (let ((next-line-add-newlines t))
400     (next-line 1)))
401
402 (defun riece-command-join-channel (target key)
403   (let ((process (riece-server-process (riece-identity-server target))))
404     (unless process
405       (error "%s" (substitute-command-keys
406                    "Type \\[riece-command-open-server] to open server.")))
407     (riece-process-send-string process
408                                (if key
409                                    (format "JOIN %s :%s\r\n"
410                                            (riece-identity-prefix target)
411                                            key)
412                                  (format "JOIN %s\r\n"
413                                          (riece-identity-prefix target))))))
414
415 (defun riece-command-join-partner (target)
416   (let ((pointer (riece-identity-member target riece-current-channels)))
417     (if pointer
418         (riece-command-switch-to-channel (car pointer))
419       (riece-join-channel target)
420       (riece-switch-to-channel target)
421       (riece-redisplay-buffers))))
422
423 (defun riece-command-join (target &optional key)
424   (interactive
425    (let* ((completion-ignore-case t)
426           (target
427            (if riece-join-channel-candidate
428                (let ((default (riece-format-identity
429                                riece-join-channel-candidate)))
430                  (riece-completing-read-identity
431                   (format "Channel/User (default %s): " default)
432                   riece-current-channels nil nil nil nil default))
433              (riece-completing-read-identity
434               "Channel/User: " riece-current-channels)))
435           key)
436      (if (and current-prefix-arg
437               (riece-channel-p (riece-identity-prefix target)))
438          (setq key
439                (riece-read-passwd (format "Key for %s: "
440                                           (riece-format-identity target)))))
441      (list target key)))
442   (let ((pointer (riece-identity-member target riece-current-channels)))
443     (if pointer
444         (riece-command-switch-to-channel (car pointer))
445       (if (riece-channel-p (riece-identity-prefix target))
446           (riece-command-join-channel target key)
447         (riece-command-join-partner target)))))
448
449 (defun riece-command-part-channel (target message)
450   (let ((process (riece-server-process (riece-identity-server target))))
451     (riece-process-send-string process
452                                (if message
453                                    (format "PART %s :%s\r\n"
454                                            (riece-identity-prefix target)
455                                            message)
456                                  (format "PART %s\r\n"
457                                          (riece-identity-prefix target))))))
458
459 (defun riece-command-part (target &optional message)
460   (interactive
461    (progn
462      (riece-check-channel-commands-are-usable)
463      (let* ((completion-ignore-case t)
464             (target
465              (riece-completing-read-identity
466               (format "Channel/User (default %s): "
467                       (riece-format-identity riece-current-channel))
468               riece-current-channels nil nil nil nil
469               (riece-format-identity riece-current-channel)))
470             message)
471        (if (and current-prefix-arg
472                 (riece-channel-p (riece-identity-prefix target)))
473            (setq message (read-string "Message: ")))
474        (list target message))))
475   (if (riece-identity-member target riece-current-channels)
476       (if (riece-channel-p (riece-identity-prefix target))
477           (riece-command-part-channel target message)
478         (riece-part-channel target)
479         (riece-redisplay-buffers))
480     (error "You are not talking with %s" target)))
481
482 (defun riece-command-change-nickname (nickname)
483   "Change your nickname to NICK."
484   (interactive "sEnter your nickname: ")
485   (riece-send-string (format "NICK %s\r\n" nickname)))
486
487 (defun riece-command-scroll-down (lines)
488   "Scroll LINES down dialogue buffer from command buffer."
489   (interactive "P")
490   (let ((other-window-scroll-buffer
491          (if riece-channel-buffer-mode
492              riece-channel-buffer
493            riece-dialogue-buffer)))
494     (when (get-buffer-window other-window-scroll-buffer)
495       (condition-case nil
496           (scroll-other-window-down lines)
497         (beginning-of-buffer
498          (message "Beginning of buffer"))))))
499
500 (defun riece-command-scroll-up (lines)
501   "Scroll LINES up dialogue buffer from command buffer."
502   (interactive "P")
503   (let* ((other-window-scroll-buffer
504           (if riece-channel-buffer-mode
505               riece-channel-buffer
506             riece-dialogue-buffer)))
507     (when (get-buffer-window other-window-scroll-buffer)
508       (condition-case nil
509           (scroll-other-window lines)
510         (end-of-buffer
511          (message "End of buffer"))))))
512
513 (defun riece-command-nick-scroll-down (lines)
514   "Scroll LINES down nick buffer from command buffer."
515   (interactive "P")
516   (let ((other-window-scroll-buffer riece-user-list-buffer))
517     (when (get-buffer-window other-window-scroll-buffer)
518       (condition-case nil
519           (scroll-other-window-down lines)
520         (beginning-of-buffer
521          (message "Beginning of buffer"))))))
522
523 (defun riece-command-nick-scroll-up (lines)
524   "Scroll LINES up nick buffer from command buffer."
525   (interactive "P")
526   (let* ((other-window-scroll-buffer riece-user-list-buffer))
527     (when (get-buffer-window other-window-scroll-buffer)
528       (condition-case nil
529           (scroll-other-window lines)
530         (end-of-buffer
531          (message "End of buffer"))))))
532
533 (defun riece-command-toggle-away (&optional message)
534   "Mark yourself as being away."
535   (interactive
536    (if (and (not (riece-with-server-buffer (riece-identity-server
537                                             (riece-current-nickname))
538                    (riece-user-get-away (riece-identity-prefix
539                                          (riece-current-nickname)))))
540             (or (null riece-away-message)
541                 current-prefix-arg))
542        (let ((message (read-string "Away message: ")))
543          (list message))))
544   (if message
545       (riece-send-string (format "AWAY :%s\r\n" message))
546     (riece-send-string "AWAY\r\n")))
547
548 (defun riece-command-toggle-freeze (&optional arg)
549   "Prevent automatic scrolling of the dialogue window.
550 If prefix argument ARG is non-nil, toggle frozen status."
551   (interactive "P")
552   (with-current-buffer (if (and riece-channel-buffer-mode
553                                 riece-channel-buffer)
554                            riece-channel-buffer
555                          riece-dialogue-buffer)
556     (setq riece-freeze (if arg
557                            (< 0 (prefix-numeric-value arg))
558                          (not riece-freeze))))
559   (riece-update-status-indicators)
560   (force-mode-line-update t))
561
562 (defun riece-command-toggle-own-freeze (&optional arg)
563   "Prevent automatic scrolling of the dialogue window.
564 The difference from `riece-command-freeze' is that your messages are hidden.
565 If prefix argument ARG is non-nil, toggle frozen status."
566   (interactive "P")
567   (with-current-buffer (if (and riece-channel-buffer-mode
568                                 riece-channel-buffer)
569                            riece-channel-buffer
570                          riece-dialogue-buffer)
571     (if (if arg
572             (< 0 (prefix-numeric-value arg))
573           (not (eq riece-freeze 'own)))
574         (setq riece-freeze 'own)
575       (setq riece-freeze nil)))
576   (riece-update-status-indicators)
577   (force-mode-line-update t))
578
579 (defun riece-command-quit (&optional arg)
580   "Quit IRC."
581   (interactive "P")
582   (if (y-or-n-p "Really quit IRC? ")
583       (let ((message
584              (if arg
585                  (read-string "Message: ")
586                (or riece-quit-message
587                    (riece-extended-version))))
588             (alist riece-server-process-alist))
589         (while alist
590           (riece-quit-server-process (cdr (car alist)) message)
591           (setq alist (cdr alist))))))
592
593 (defun riece-command-raw (command)
594   "Enter raw IRC command, which is sent to the server."
595   (interactive "sIRC command: ")
596   (riece-send-string (concat command "\r\n")))
597
598 (defun riece-command-end-of-buffer ()
599   "Get end of the dialogue buffer."
600   (interactive)
601   (let (buffer window)
602     (setq buffer (if riece-channel-buffer-mode
603                      riece-channel-buffer
604                    riece-dialogue-buffer))
605     (or (setq window (get-buffer-window buffer))
606         (setq window (get-buffer-window riece-dialogue-buffer)
607               buffer riece-dialogue-buffer))
608     (when window
609       (save-selected-window
610         (select-window window)
611         (goto-char (point-max))))))
612
613 (defun riece-command-copy-region (start end)
614   "Move current region between START and END to `kill-ring'."
615   (interactive "r")
616   (kill-new (buffer-substring-no-properties start end)))
617
618 (defun riece-command-open-server (server-name)
619   (interactive
620    (list (completing-read "Server: " riece-server-alist)))
621   (if (riece-server-process server-name)
622       (error "%s is already opened" server-name))
623   (riece-open-server
624    (riece-server-name-to-server server-name)
625    server-name))
626
627 (defun riece-command-close-server (server-name &optional message)
628   (interactive
629    (list (completing-read "Server: " riece-server-process-alist)
630          (if current-prefix-arg
631              (read-string "Message: ")
632            (or riece-quit-message
633                (riece-extended-version)))))
634   (riece-quit-server-process (riece-server-process server-name) message))
635
636 (defun riece-command-universal-server-name-argument ()
637   (interactive)
638   (let* ((riece-overriding-server-name
639           (completing-read "Server: " riece-server-process-alist))
640          (command
641           (key-binding (read-key-sequence
642                         (format "Command to execute on \"%s\":"
643                                 riece-overriding-server-name)))))
644     (message "")
645     (call-interactively command)))
646
647 (provide 'riece-commands)
648
649 ;;; riece-commands.el ends here