comparison lisp/net/rcirc.el @ 110313:50c8f347bd8e

* lisp/net/rcirc.el (rcirc-server-commands, rcirc-client-commands) (rcirc-completion-start): New variables. (rcirc-nick-completions): Rename to rcirc-completions. (rcirc-nick-completion-start-offset): Delete. (rcirc-completion-at-point): New function for constructing completion data for both nicks and irc commands. Add to completion-at-point-functions in rcirc mode. (rcirc-complete): Rename from rcirc-nick-complete; use rcirc-completion-at-point. (defun-rcirc-command): Update rcirc-client-commands.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Sun, 12 Sep 2010 13:06:19 +0200
parents 0dcf6ddbe02b
children 6e613fbf73d7
comparison
equal deleted inserted replaced
110312:9ac0232d85db 110313:50c8f347bd8e
772 (when (<= rcirc-prompt-end-marker (point)) 772 (when (<= rcirc-prompt-end-marker (point))
773 (delete-region rcirc-prompt-end-marker (point-max)) 773 (delete-region rcirc-prompt-end-marker (point-max))
774 (setq rcirc-input-ring-index (1- rcirc-input-ring-index)) 774 (setq rcirc-input-ring-index (1- rcirc-input-ring-index))
775 (insert (rcirc-prev-input-string -1)))) 775 (insert (rcirc-prev-input-string -1))))
776 776
777 (defvar rcirc-nick-completions nil) 777 (defvar rcirc-server-commands
778 (defvar rcirc-nick-completion-start-offset nil) 778 '("/admin" "/away" "/connect" "/die" "/error" "/info"
779 779 "/invite" "/ison" "/join" "/kick" "/kill" "/links"
780 (defun rcirc-complete-nick () 780 "/list" "/lusers" "/mode" "/motd" "/names" "/nick"
781 "Cycle through nick completions from list of nicks in channel." 781 "/notice" "/oper" "/part" "/pass" "/ping" "/pong"
782 "/privmsg" "/quit" "/rehash" "/restart" "/service" "/servlist"
783 "/server" "/squery" "/squit" "/stats" "/summon" "/time"
784 "/topic" "/trace" "/user" "/userhost" "/users" "/version"
785 "/wallops" "/who" "/whois" "/whowas")
786 "A list of user commands by IRC server.
787 The value defaults to RFCs 1459 and 2812.")
788
789 ;; /me and /ctcp are not defined by `defun-rcirc-command'.
790 (defvar rcirc-client-commands '("/me" "/ctcp")
791 "A list of user commands defined by IRC client rcirc.
792 The list is updated automatically by `defun-rcirc-command'.")
793
794 (defun rcirc-completion-at-point ()
795 "Function used for `completion-at-point-functions' in `rcirc-mode'."
796 (let* ((beg (save-excursion
797 (if (re-search-backward " " rcirc-prompt-end-marker t)
798 (1+ (point))
799 rcirc-prompt-end-marker)))
800 (table (if (and (= beg rcirc-prompt-end-marker)
801 (eq (char-after beg) ?/))
802 (delete-dups
803 (nconc
804 (sort (copy-sequence rcirc-client-commands) 'string-lessp)
805 (sort (copy-sequence rcirc-server-commands) 'string-lessp)))
806 (rcirc-channel-nicks (rcirc-buffer-process) rcirc-target))))
807 (list beg (point) table)))
808
809 (defvar rcirc-completions nil)
810 (defvar rcirc-completion-start nil)
811
812 (defun rcirc-complete ()
813 "Cycle through completions from list of nicks in channel or IRC commands.
814 IRC command completion is performed only if '/' is the first input char."
782 (interactive) 815 (interactive)
783 (if (eq last-command this-command) 816 (if (eq last-command this-command)
784 (setq rcirc-nick-completions 817 (setq rcirc-completions
785 (append (cdr rcirc-nick-completions) 818 (append (cdr rcirc-completions) (list (car rcirc-completions))))
786 (list (car rcirc-nick-completions)))) 819 (let ((completion-ignore-case t)
787 (setq rcirc-nick-completion-start-offset 820 (table (rcirc-completion-at-point)))
788 (- (save-excursion 821 (setq rcirc-completion-start (car table))
789 (if (re-search-backward " " rcirc-prompt-end-marker t) 822 (setq rcirc-completions
790 (1+ (point)) 823 (all-completions (buffer-substring rcirc-completion-start
791 rcirc-prompt-end-marker)) 824 (cadr table))
792 rcirc-prompt-end-marker)) 825 (nth 2 table)))))
793 (setq rcirc-nick-completions 826 (let ((completion (car rcirc-completions)))
794 (let ((completion-ignore-case t))
795 (all-completions
796 (buffer-substring
797 (+ rcirc-prompt-end-marker
798 rcirc-nick-completion-start-offset)
799 (point))
800 (mapcar (lambda (x) (cons x nil))
801 (rcirc-channel-nicks (rcirc-buffer-process)
802 rcirc-target))))))
803 (let ((completion (car rcirc-nick-completions)))
804 (when completion 827 (when completion
805 (delete-region (+ rcirc-prompt-end-marker 828 (delete-region rcirc-completion-start (point))
806 rcirc-nick-completion-start-offset) 829 (insert
807 (point)) 830 (concat completion
808 (insert (concat completion 831 (cond
809 (if (= (+ rcirc-prompt-end-marker 832 ((= (aref completion 0) ?/) " ")
810 rcirc-nick-completion-start-offset) 833 ((= rcirc-completion-start rcirc-prompt-end-marker) ": ")
811 rcirc-prompt-end-marker) 834 (t "")))))))
812 ": "))))))
813 835
814 (defun set-rcirc-decode-coding-system (coding-system) 836 (defun set-rcirc-decode-coding-system (coding-system)
815 "Set the decode coding system used in this channel." 837 "Set the decode coding system used in this channel."
816 (interactive "zCoding system for incoming messages: ") 838 (interactive "zCoding system for incoming messages: ")
817 (setq rcirc-decode-coding-system coding-system)) 839 (setq rcirc-decode-coding-system coding-system))
825 "Keymap for rcirc mode.") 847 "Keymap for rcirc mode.")
826 848
827 (define-key rcirc-mode-map (kbd "RET") 'rcirc-send-input) 849 (define-key rcirc-mode-map (kbd "RET") 'rcirc-send-input)
828 (define-key rcirc-mode-map (kbd "M-p") 'rcirc-insert-prev-input) 850 (define-key rcirc-mode-map (kbd "M-p") 'rcirc-insert-prev-input)
829 (define-key rcirc-mode-map (kbd "M-n") 'rcirc-insert-next-input) 851 (define-key rcirc-mode-map (kbd "M-n") 'rcirc-insert-next-input)
830 (define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete-nick) 852 (define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete)
831 (define-key rcirc-mode-map (kbd "C-c C-b") 'rcirc-browse-url) 853 (define-key rcirc-mode-map (kbd "C-c C-b") 'rcirc-browse-url)
832 (define-key rcirc-mode-map (kbd "C-c C-c") 'rcirc-edit-multiline) 854 (define-key rcirc-mode-map (kbd "C-c C-c") 'rcirc-edit-multiline)
833 (define-key rcirc-mode-map (kbd "C-c C-j") 'rcirc-cmd-join) 855 (define-key rcirc-mode-map (kbd "C-c C-j") 'rcirc-cmd-join)
834 (define-key rcirc-mode-map (kbd "C-c C-k") 'rcirc-cmd-kick) 856 (define-key rcirc-mode-map (kbd "C-c C-k") 'rcirc-cmd-kick)
835 (define-key rcirc-mode-map (kbd "C-c C-l") 'rcirc-toggle-low-priority) 857 (define-key rcirc-mode-map (kbd "C-c C-l") 'rcirc-toggle-low-priority)
945 (let ((buffer (current-buffer))) 967 (let ((buffer (current-buffer)))
946 (with-rcirc-process-buffer process 968 (with-rcirc-process-buffer process
947 (setq rcirc-buffer-alist (cons (cons target buffer) 969 (setq rcirc-buffer-alist (cons (cons target buffer)
948 rcirc-buffer-alist)))) 970 rcirc-buffer-alist))))
949 (rcirc-update-short-buffer-names)) 971 (rcirc-update-short-buffer-names))
972
973 (add-hook 'completion-at-point-functions
974 'rcirc-completion-at-point nil 'local)
950 975
951 (run-hooks 'rcirc-mode-hook)) 976 (run-hooks 'rcirc-mode-hook))
952 977
953 (defun rcirc-update-prompt (&optional all) 978 (defun rcirc-update-prompt (&optional all)
954 "Reset the prompt string in the current buffer. 979 "Reset the prompt string in the current buffer.
2002 ;;; /commands these are called with 3 args: PROCESS, TARGET, which is 2027 ;;; /commands these are called with 3 args: PROCESS, TARGET, which is
2003 ;; the current buffer/channel/user, and ARGS, which is a string 2028 ;; the current buffer/channel/user, and ARGS, which is a string
2004 ;; containing the text following the /cmd. 2029 ;; containing the text following the /cmd.
2005 2030
2006 (defmacro defun-rcirc-command (command argument docstring interactive-form 2031 (defmacro defun-rcirc-command (command argument docstring interactive-form
2007 &rest body) 2032 &rest body)
2008 "Define a command." 2033 "Define a command."
2009 `(defun ,(intern (concat "rcirc-cmd-" (symbol-name command))) 2034 `(progn
2010 (,@argument &optional process target) 2035 (add-to-list 'rcirc-client-commands ,(concat "/" (symbol-name command)))
2011 ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values given" 2036 (defun ,(intern (concat "rcirc-cmd-" (symbol-name command)))
2012 "\nby `rcirc-buffer-process' and `rcirc-target' will be used.") 2037 (,@argument &optional process target)
2013 ,interactive-form 2038 ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values given"
2014 (let ((process (or process (rcirc-buffer-process))) 2039 "\nby `rcirc-buffer-process' and `rcirc-target' will be used.")
2015 (target (or target rcirc-target))) 2040 ,interactive-form
2016 ,@body))) 2041 (let ((process (or process (rcirc-buffer-process)))
2042 (target (or target rcirc-target)))
2043 ,@body))))
2017 2044
2018 (defun-rcirc-command msg (message) 2045 (defun-rcirc-command msg (message)
2019 "Send private MESSAGE to TARGET." 2046 "Send private MESSAGE to TARGET."
2020 (interactive "i") 2047 (interactive "i")
2021 (if (null message) 2048 (if (null message)