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