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