1 ;;; lurk.el --- Little Unibuffer iRc Klient -*- lexical-binding:t -*-
3 ;; Copyright (C) 2021 Tim Vaughan
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 14 June 2021
9 ;; Homepage: http://thelambdalab.xyz/lurk
10 ;; Package-Requires: ((emacs "26"))
12 ;; This file is not part of GNU Emacs.
14 ;; This program is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this file. If not, see <http://www.gnu.org/licenses/>.
38 "Little Unibuffer iRc Klient."
41 (defcustom lurk-nick "plugd"
44 (defcustom lurk-default-quit-msg "Bye"
45 "Default quit message when none supplied.")
47 (defcustom lurk-networks
48 '(("libera" "irc.libera.chat" 6697))
51 (defcustom lurk-allow-ipv6 nil
52 "Set to non-nil to allow use of IPv6.")
54 (defcustom lurk-show-joins nil
55 "Set to non-nil to be notified of joins, parts and quits.")
57 (defcustom lurk-display-header t
58 "If non-nil, use buffer header to display information on current host and channel.")
64 '((t :inherit default))
65 "Face used for Lurk text.")
68 '((t :inherit org-priority))
69 "Face used for the prompt.")
72 '((t :inherit org-tag))
73 "Face used for the context name in the prompt.")
76 '((t :inherit org-agenda-dimmed-todo-face))
77 "Face used for faded Lurk text.")
79 (defface lurk-timestamp
80 '((t :inherit org-agenda-dimmed-todo-face))
81 "Face used for timestamps.")
84 '((t :inherit font-lock-regexp-grouping-construct))
85 "Face used for Lurk error text.")
88 '((t :inherit org-upcoming-deadline))
89 "Face used for Lurk notice text.")
94 (defvar lurk-version "Lurk v0.1"
95 "Value of this string is used in response to CTCP version queries.")
97 (defvar lurk-notice-prefix "-!-")
99 (defvar lurk-error-prefix "!!!")
101 (defvar lurk-prompt-string ">")
103 (defvar lurk-debug nil
104 "If non-nil, enable debug mode.")
107 ;;; Utility procedures
110 (defun lurk--filtered-join (&rest args)
111 (string-join (seq-filter (lambda (el) el) args) " "))
113 (defun lurk--as-string (obj)
115 (with-output-to-string (princ obj))
122 (defvar lurk-response "")
124 (defun lurk-filter (proc string)
125 (dolist (line (split-string (concat lurk-response string) "\n"))
126 (if (string-suffix-p "\r" line)
127 (lurk-eval-msg-string (string-trim line))
128 (setq lurk-response line))))
130 (defun lurk-sentinel (proc string)
131 (unless (equal "open" (string-trim string))
132 (lurk-display-error "Disconnected from server.")
133 (clrhash lurk-contexts)
134 (lurk-set-current-context nil)
136 (cancel-timer lurk-ping-timer)))
138 (defun lurk-start-process (network)
139 (let* ((row (assoc network lurk-networks))
142 (flags (seq-drop row 3)))
143 (make-network-process :name "lurk"
146 :family (if lurk-allow-ipv6 nil 'ipv4)
147 :filter #'lurk-filter
148 :sentinel #'lurk-sentinel
150 :tls-parameters (if (memq :notls flags)
152 (cons 'gnutls-x509pki
153 (gnutls-boot-parameters
154 :type 'gnutls-x509pki
158 (defvar lurk-ping-timer nil)
159 (defvar lurk-ping-period 60)
161 (defun lurk-ping-function ()
162 (lurk-send-msg (lurk-msg nil nil "PING" (car (process-contact (get-process "lurk")))))
163 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))
165 (defun lurk-connect (network)
166 (if (get-process "lurk")
167 (lurk-display-error "Already connected. Disconnect first.")
168 (if (not (assoc network lurk-networks))
169 (lurk-display-error "Network '" network "' is unknown.")
170 (clrhash lurk-contexts)
171 (lurk-set-current-context nil)
172 (lurk-start-process network)
173 (lurk-send-msg (lurk-msg nil nil "USER" lurk-nick 0 "*" lurk-nick))
174 (lurk-send-msg (lurk-msg nil nil "NICK" lurk-nick))
175 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))))
177 (defun lurk-connected-p ()
178 (let ((proc (get-process "lurk")))
179 (and proc (eq (process-status proc) 'open))))
181 (defun lurk-send-msg (msg)
183 (lurk-display-string nil nil (lurk-msg->string msg)))
184 (let ((proc (get-process "lurk")))
185 (if (and proc (eq (process-status proc) 'open))
186 (process-send-string proc (concat (lurk-msg->string msg) "\r\n"))
187 (lurk-display-error "No server connection established.")
188 (error "No server connection established"))))
194 (defun lurk-msg (tags src cmd &rest params)
195 (list (lurk--as-string tags)
196 (lurk--as-string src)
197 (upcase (lurk--as-string cmd))
198 (mapcar #'lurk--as-string
199 (if (and params (listp (elt params 0)))
203 (defun lurk-msg-tags (msg) (elt msg 0))
204 (defun lurk-msg-src (msg) (elt msg 1))
205 (defun lurk-msg-cmd (msg) (elt msg 2))
206 (defun lurk-msg-params (msg) (elt msg 3))
207 (defun lurk-msg-trail (msg)
208 (let ((params (lurk-msg-params msg)))
210 (elt params (- (length params) 1)))))
212 (defvar lurk-msg-regex
214 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
216 (opt (: ":" (: (group (* (not (any space "!" "@"))))
217 (* (not (any space)))))
219 (group (: (* (not whitespace))))
221 (opt (group (+ not-newline))))
222 "Regex used to parse IRC messages.
223 Note that this regex is incomplete. Noteably, we discard the non-nick
224 portion of the source component of the message, as LURK doesn't use this.")
226 (defun lurk-string->msg (string)
227 (if (string-match lurk-msg-regex string)
228 (let* ((tags (match-string 1 string))
229 (src (match-string 2 string))
230 (cmd (upcase (match-string 3 string)))
231 (params-str (match-string 4 string))
234 (let* ((idx (cl-search ":" params-str))
235 (l (split-string (string-trim (substring params-str 0 idx))))
236 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
239 (apply #'lurk-msg (append (list tags src cmd) params)))
240 (error "Failed to parse string " string)))
242 (defun lurk-msg->string (msg)
243 (let ((tags (lurk-msg-tags msg))
244 (src (lurk-msg-src msg))
245 (cmd (lurk-msg-cmd msg))
246 (params (lurk-msg-params msg)))
248 (if tags (concat "@" tags) nil)
249 (if src (concat ":" src) nil)
251 (if (> (length params) 1)
252 (string-join (seq-take params (- (length params) 1)) " ")
254 (if (> (length params) 0)
255 (concat ":" (elt params (- (length params) 1)))
262 (defvar lurk-current-context nil)
263 (defvar lurk-contexts (make-hash-table :test #'equal))
265 (defun lurk-add-context (name)
266 (puthash name nil lurk-contexts))
268 (defun lurk-del-context (name)
269 (remhash name lurk-contexts))
271 (defun lurk-get-context-users (name)
272 (gethash name lurk-contexts))
274 (defun lurk-add-context-users (context users)
277 (gethash context lurk-contexts))
280 (defun lurk-del-context-user (context user)
282 (remove user (gethash context lurk-contexts))
285 (defun lurk-del-user (user)
286 (dolist (context (lurk-get-context-list))
287 (lurk-del-context-user context user)))
289 (defun lurk-rename-user (old-nick new-nick)
290 (dolist (context (lurk-get-context-list))
291 (lurk-del-context-user context old-nick)
292 (lurk-add-context-users context (list new-nick))))
294 (defun lurk-get-context-type (name)
296 ((string-prefix-p "#" name) 'channel)
297 ((string-match-p (rx (or "." "localhost")) name) 'host)
300 (defun lurk-get-context-list ()
302 (maphash (lambda (key val)
303 (cl-pushnew key res))
307 (defun lurk-get-next-context (&optional prev)
308 (if lurk-current-context
309 (let* ((context-list (if prev
310 (reverse (lurk-get-context-list))
311 (lurk-get-context-list)))
312 (context-list* (member lurk-current-context context-list)))
313 (if (> (length context-list*) 1)
318 (defun lurk-set-current-context (context)
319 (setq lurk-current-context context)
320 (lurk-highlight-context context)
322 (lurk-zoom-in lurk-current-context)))
324 (defun lurk-cycle-contexts (&optional rev)
325 (if lurk-current-context
327 (lurk-set-current-context (lurk-get-next-context rev))
328 (lurk-render-prompt))
329 (lurk-display-error "No channels joined.")))
335 (defun lurk-render-prompt ()
336 (with-current-buffer "*lurk*"
337 (let ((update-point (= lurk-input-marker (point)))
338 (update-window-points (mapcar (lambda (w)
339 (list (= (window-point w) lurk-input-marker)
341 (get-buffer-window-list nil nil t))))
343 (set-marker-insertion-type lurk-prompt-marker nil)
344 (set-marker-insertion-type lurk-input-marker t)
345 (let ((inhibit-read-only t))
346 (delete-region lurk-prompt-marker lurk-input-marker)
347 (goto-char lurk-prompt-marker)
349 (propertize (if lurk-current-context
354 (propertize (concat lurk-prompt-string " ")
358 (set-marker-insertion-type lurk-input-marker nil))
360 (goto-char lurk-input-marker))
361 (dolist (v update-window-points)
363 (set-window-point (cadr v) lurk-input-marker))))))
365 (defvar lurk-prompt-marker nil
366 "Marker for prompt position in LURK buffer.")
368 (defvar lurk-input-marker nil
369 "Marker for prompt position in LURK buffer.")
371 (defun lurk-setup-header ()
372 (with-current-buffer "*lurk*"
373 (setq-local header-line-format
375 (let ((proc (get-process "lurk")))
378 "Host: " (car (process-contact proc))
380 (if lurk-current-context
385 (length (lurk-get-context-users lurk-current-context)))
390 (if lurk-zoomed " [ZOOMED]" ""))))))
392 (defun lurk-setup-buffer ()
393 (with-current-buffer (get-buffer-create "*lurk*")
394 (setq-local scroll-conservatively 1)
395 (setq-local buffer-invisibility-spec nil)
396 (if (markerp lurk-prompt-marker)
397 (set-marker lurk-prompt-marker (point-max))
398 (setq lurk-prompt-marker (point-max-marker)))
399 (if (markerp lurk-input-marker)
400 (set-marker lurk-input-marker (point-max))
401 (setq lurk-input-marker (point-max-marker)))
402 (goto-char (point-max))
404 (if lurk-display-header
405 (lurk-setup-header))))
408 ;;; Output formatting and highlighting
411 ;; Idea: the face text property can be a list of faces, applied in
412 ;; order. By assigning each context a unique list and keeping track
413 ;; of these in a hash table, we can easily switch the face
414 ;; corresponding to a particular context by modifying the elements of
417 ;; More subtly, we make only the cdrs of this list shared among
418 ;; all text of a given context, allowing the cars to be different
419 ;; and for different elements of the context-specific text to have
420 ;; different styling.
422 ;; Additionally, we allow selective hiding of contexts via
423 ;; the buffer-invisibility-spec.
425 (defvar lurk-context-facelists (make-hash-table :test 'equal)
426 "List of seen contexts and associated face lists.")
428 (defun lurk-get-context-facelist (context)
429 (let ((facelist (gethash context lurk-context-facelists)))
431 (setq facelist (list 'lurk-text))
432 (puthash context facelist lurk-context-facelists))
435 (defun lurk--fill-strings (col indent &rest strings)
437 (setq buffer-invisibility-spec nil)
438 (let ((fill-column col)
439 (adaptive-fill-regexp (rx-to-string `(= ,indent anychar))))
440 (apply #'insert strings)
441 (fill-region (point-min) (point-max) nil t)
444 (defun lurk-display-string (context prefix &rest strings)
445 (with-current-buffer (get-buffer-create "*lurk*")
447 (goto-char lurk-prompt-marker)
448 (let* ((inhibit-read-only t)
449 (old-pos (marker-position lurk-prompt-marker))
450 (padded-timestamp (concat (format-time-string "%H:%M ")))
451 (padded-prefix (if prefix (concat prefix " ") ""))
452 (context-atom (if context (intern context) nil)))
453 (insert-before-markers
456 (+ (length padded-timestamp)
457 (length padded-prefix))
458 (propertize padded-timestamp
459 'face 'lurk-timestamp
462 'invisible context-atom)
463 (propertize padded-prefix
466 'invisible context-atom)
468 (propertize (concat (apply #'lurk-buttonify-urls strings) "\n")
469 'face (lurk-get-context-facelist context)
472 'invisible context-atom))))))))
474 (defun lurk-display-message (from to text)
475 (let ((context (if (eq 'channel (lurk-get-context-type to))
477 (if (equal to lurk-nick) from to))))
481 (pcase (lurk-get-context-type to)
482 ('channel (concat to " <" from ">"))
483 ('nick (concat "[" from " -> " to "]"))
485 (error "Unsupported context type")))
486 'face (lurk-get-context-facelist context))
489 (defun lurk-display-action (from to action-text)
490 (let ((context (if (eq 'channel (lurk-get-context-type to))
492 (if (equal to lurk-nick) from to))))
496 (concat context " * " from)
497 'face (lurk-get-context-facelist context))
500 (defun lurk-display-notice (context &rest notices)
503 (propertize lurk-notice-prefix 'face 'lurk-notice)
504 (apply #'concat notices)))
506 (defun lurk-display-error (&rest messages)
509 (propertize lurk-error-prefix 'face 'lurk-error)
510 (apply #'concat messages)))
512 (defun lurk-highlight-context (context)
514 (lambda (this-context facelist)
515 (if (equal this-context context)
516 (setcar facelist 'lurk-text)
517 (setcar facelist 'lurk-faded)))
518 lurk-context-facelists)
519 (force-window-update "*lurk*"))
521 (defun lurk-zoom-in (context)
522 (with-current-buffer "*lurk*"
524 (lambda (this-context _)
526 (let ((this-context-atom (intern this-context)))
527 (if (equal this-context context)
528 (remove-from-invisibility-spec this-context-atom)
529 (add-to-invisibility-spec this-context-atom)))))
530 lurk-context-facelists)
531 (force-window-update "*lurk*")))
533 (defun lurk-zoom-out ()
534 (with-current-buffer "*lurk*"
536 (lambda (this-context _)
537 (let ((this-context-atom (if this-context (intern this-context) nil)))
538 (remove-from-invisibility-spec this-context-atom)))
539 lurk-context-facelists)
540 (force-window-update "*lurk*")))
542 (defconst lurk-url-regex
546 (group (or (+ (any alnum "." "-"))
547 (+ (any alnum ":"))))
548 (opt (group (: ":" (+ digit))))
551 (* (any alnum "-/.,#:%=&_?~@+"))
552 (any alnum "-/#:%=&_~@+")))))))
553 "Imperfect regex used to find URLs in plain text.")
555 (defun lurk-click-url (button)
556 (browse-url (button-get button 'url)))
558 (defun lurk-buttonify-urls (&rest strings)
559 "Turn substrings which look like urls in STRING into clickable buttons."
561 (apply #'insert strings)
562 (goto-char (point-min))
563 (while (re-search-forward lurk-url-regex nil t)
564 (let ((url (match-string 0)))
565 (make-text-button (match-beginning 0)
567 'action #'lurk-click-url
571 'help-echo "Open URL in browser.")))
574 (defun lurk-add-formatting (string)
577 (goto-char (point-min))
582 (prev-point (point)))
583 (while (re-search-forward (rx (any "\x02\x1D\x1F\x1E")) nil t)
584 (let ((beg (+ (match-beginning 0) 1)))
586 (add-face-text-property prev-point beg '(:weight bold)))
588 (add-face-text-property prev-point beg '(:slant italic)))
590 (add-face-text-property prev-point beg '(:underline t)))
592 (add-face-text-property prev-point beg '(:strike-through t)))
593 (pcase (match-string 0)
594 ("\x02" (setq bold (not bold)))
595 ("\x1D" (setq italics (not italics)))
596 ("\x1F" (setq underline (not underline)))
597 ("\x1E" (setq strikethrough (not strikethrough))))
598 (delete-region (match-beginning 0) (match-end 0))
599 (setq prev-point (point)))))
603 ;;; Message evaluation
606 (defun lurk-eval-msg-string (string)
608 (lurk-display-string nil nil string))
609 (let* ((msg (lurk-string->msg string)))
610 (lurk-process-autoreplies msg)
611 (pcase (lurk-msg-cmd msg)
614 (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
619 (let* ((params (lurk-msg-params msg))
620 (nick (elt params 0))
621 (text (string-join (seq-drop params 1) " ")))
622 (setq lurk-nick nick)
623 (lurk-display-notice nil text)))
626 (let* ((params (lurk-msg-params msg))
627 (channel (elt params 2))
628 (names (split-string (elt params 3))))
629 (lurk-add-context-users channel names)))
632 (let* ((params (lurk-msg-params msg))
633 (channel (elt params 1)))
636 (lurk--as-string (length (lurk-get-context-users channel)))
637 " users in " channel)))
640 (let* ((params (lurk-msg-params msg))
641 (channel (elt params 1)))
647 (let* ((params (lurk-msg-params msg))
648 (channel (elt params 1))
649 (topic (elt params 2)))
650 (lurk-display-notice channel "Topic: " topic)))
652 ("333") ; Avoid displaying these
654 ((rx (= 3 (any digit)))
655 (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
658 (guard (equal lurk-nick (lurk-msg-src msg))))
659 (let ((channel (car (lurk-msg-params msg))))
660 (lurk-add-context channel)
661 (lurk-set-current-context channel)
662 (lurk-display-notice channel "Joining channel " channel)
663 (lurk-render-prompt)))
666 (let ((channel (car (lurk-msg-params msg)))
667 (nick (lurk-msg-src msg)))
668 (lurk-add-context-users channel (list nick))
670 (lurk-display-notice channel nick " joined channel " channel))))
673 (guard (equal lurk-nick (lurk-msg-src msg))))
674 (let ((channel (car (lurk-msg-params msg))))
675 (lurk-display-notice channel "Left channel " channel)
676 (lurk-del-context channel)
677 (if (equal channel lurk-current-context)
678 (lurk-set-current-context (lurk-get-next-context)))
679 (lurk-render-prompt)))
682 (let ((channel (car (lurk-msg-params msg)))
683 (nick (lurk-msg-src msg)))
684 (lurk-del-context-user channel nick)
686 (lurk-display-notice channel nick " left channel " channel))))
689 (let ((kicker-nick (lurk-msg-src msg))
690 (channel (car (lurk-msg-params msg)))
691 (nick (cadr (lurk-msg-params msg)))
692 (reason (caddr (lurk-msg-params msg))))
693 (if (equal nick lurk-nick)
695 (lurk-display-notice channel kicker-nick " kicked you from " channel ": " reason)
696 (lurk-del-context channel)
697 (if (equal channel lurk-current-context)
698 (lurk-set-current-context (lurk-get-next-context)))
699 (lurk-render-prompt))
700 (lurk-del-context-user channel nick)
701 (lurk-display-notice channel kicker-nick " kicked " nick " from " channel ": " reason))))
704 (let ((nick (lurk-msg-src msg))
705 (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
708 (lurk-display-notice nil nick " quit: " reason))))
711 (guard (equal lurk-nick (lurk-msg-src msg))))
712 (setq lurk-nick (car (lurk-msg-params msg)))
713 (lurk-display-notice nil "Set nick to " lurk-nick))
716 (let ((old-nick (lurk-msg-src msg))
717 (new-nick (car (lurk-msg-params msg))))
718 (lurk-display-notice nil old-nick " is now known as " new-nick)
719 (lurk-rename-user old-nick new-nick)))
722 (let ((nick (lurk-msg-src msg))
723 (channel (car (lurk-msg-params msg)))
724 (text (cadr (lurk-msg-params msg))))
726 ((rx (: "\01VERSION "
727 (let version (* (not "\01")))
729 (lurk-display-notice nil "CTCP version reply from " nick ": " version))
731 (lurk-display-notice nil text)))))
734 (let* ((from (lurk-msg-src msg))
735 (params (lurk-msg-params msg))
737 (text (cadr params)))
740 (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
741 (lurk-send-msg (lurk-msg nil nil "NOTICE"
742 (list from (concat "\01VERSION "
745 (lurk-display-notice nil "CTCP version request received from " from))
747 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
748 (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
749 (lurk-display-notice from "CTCP ping received from " from))
752 (lurk-display-notice from "CTCP userinfo request from " from " (no response sent)"))
754 ((rx (: "\01ACTION " (let action-text (* (not "\01"))) "\01"))
755 (lurk-display-action from to action-text))
758 (lurk-display-message from to text)))))
760 (lurk-display-notice nil (lurk-msg->string msg))))))
763 ;;; User-defined responses
766 (defvar lurk-autoreply-table nil
767 "Table of autoreply messages.
769 Each autoreply is a list of two elements: (matcher reply)
771 Here matcher is a list:
773 (network src cmd params ...)
775 and reply is another list:
779 Each entry in the matcher list is a regular expression tested against the
780 corresponding values in the incomming message. Entries can be nil,
781 in which case they match anything.")
783 (defun lurk--lists-equal (l1 l2)
785 (if (or (not (and (car l1) (car l2)))
786 (string-match (car l1) (car l2)))
787 (lurk--lists-equal (cdr l1) (cdr l2))
791 (defun lurk-process-autoreply (msg autoreply)
792 (let ((matcher (car autoreply))
793 (reply (cadr autoreply)))
794 (let ((network (car matcher)))
795 (when (and (or (not network)
796 (and (get-process "lurk")
797 (equal (car (process-contact (get-process "lurk")))
798 (cadr (assoc network lurk-networks)))))
799 (lurk--lists-equal (cdr matcher)
800 (append (list (lurk-msg-src msg)
802 (lurk-msg-params msg))))
804 (lurk-msg nil nil (car reply) (cdr reply)))))))
806 (defun lurk-process-autoreplies (msg)
809 (lurk-process-autoreply msg autoreply))
810 lurk-autoreply-table))
815 (defun lurk-enter-string (string)
816 (if (string-prefix-p "/" string)
817 (pcase (substring string 1)
818 ((rx (: "DEBUG" (opt (: " " (let setting (or "ON" "OFF"))))))
821 (if (equal (upcase setting) "ON")
825 (lurk-display-notice nil "Debug mode now " (if lurk-debug "on" "off") "."))
828 (if header-line-format
830 (setq-local header-line-format nil)
831 (lurk-display-notice nil "Header disabled."))
833 (lurk-display-notice nil "Header enabled.")))
835 ((rx (: "CONNECT " (let network (* not-newline))))
836 (lurk-display-notice nil "Attempting to connect to " network "...")
837 (lurk-connect network))
839 ((rx (: "TOPIC " (let new-topic (* not-newline))))
840 (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
842 ((rx (: "ME " (let action (* not-newline))))
843 (let ((ctcp-text (concat "\01ACTION " action "\01")))
844 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
845 (list lurk-current-context ctcp-text)))
846 (lurk-display-action lurk-nick lurk-current-context action)))
848 ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
849 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
850 (list nick "\01VERSION\01")))
851 (lurk-display-notice nil "CTCP version request sent to " nick))
853 ((rx "PART" (opt (: " " (let channel (* not-newline)))))
854 (if (or lurk-current-context channel)
855 (lurk-send-msg (lurk-msg nil nil "PART" (if channel
857 lurk-current-context)))
858 (lurk-display-error "No current channel to leave.")))
860 ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
861 (lurk-send-msg (lurk-msg nil nil "QUIT"
862 (or quit-msg lurk-default-quit-msg))))
864 ((rx (: "NICK" (* whitespace) string-end))
865 (lurk-display-notice nil "Current nick: " lurk-nick))
867 ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
868 (if (lurk-connected-p)
869 (lurk-send-msg (lurk-msg nil nil "NICK" nick))
870 (setq lurk-nick nick)
871 (lurk-display-notice nil "Set default nick to '" nick "'")))
873 ((rx (: "LIST" (* whitespace) string-end))
874 (lurk-display-notice nil "This command can generate lots of output. Use `LIST -yes' if you're sure."))
876 ((rx (: "LIST" (+ whitespace) "-YES" (* whitespace) string-end))
877 (lurk-send-msg (lurk-msg nil nil "LIST")))
880 (let to (* (not whitespace)))
882 (let text (* not-newline)))
883 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
884 (lurk-display-message lurk-nick to text))
886 ((rx (: (let cmd-str (+ (not whitespace)))
887 (opt (: " " (let params-str (* not-newline))))))
888 (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
890 (split-string params-str)
893 (unless (string-empty-p string)
894 (if lurk-current-context
896 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
899 (lurk-display-message lurk-nick lurk-current-context string))
900 (lurk-display-error "No current context.")))))
902 (defvar lurk-history nil
903 "Commands and messages sent in current session.")
907 "Enter current contents of line after prompt."
909 (with-current-buffer "*lurk*"
910 (let ((line (buffer-substring lurk-input-marker (point-max))))
911 (push line lurk-history)
912 (setq lurk-history-index nil)
913 (let ((inhibit-read-only t))
914 (delete-region lurk-input-marker (point-max)))
915 (lurk-enter-string line))))
917 (defvar lurk-history-index nil)
919 (defun lurk-history-cycle (delta)
921 (with-current-buffer "*lurk*"
922 (if lurk-history-index
923 (setq lurk-history-index
925 (min (- (length lurk-history) 1)
926 (+ delta lurk-history-index))))
927 (setq lurk-history-index 0))
928 (delete-region lurk-input-marker (point-max))
929 (insert (elt lurk-history lurk-history-index)))))
931 (defun lurk-history-next ()
933 (lurk-history-cycle -1))
935 (defun lurk-history-prev ()
937 (lurk-history-cycle +1))
939 ;;; Interactive functions
942 (defun lurk-cycle-contexts-forward ()
944 (lurk-cycle-contexts))
946 (defun lurk-cycle-contexts-reverse ()
948 (lurk-cycle-contexts t))
950 (defvar lurk-zoomed nil
951 "Keeps track of zoom status.")
953 (defun lurk-toggle-zoom ()
957 (lurk-zoom-in lurk-current-context))
958 (setq lurk-zoomed (not lurk-zoomed)))
960 (defun lurk-complete-nick ()
962 (when (and (>= (point) lurk-input-marker) lurk-current-context)
963 (let* ((end (max lurk-input-marker (point)))
964 (space-idx (save-excursion
965 (re-search-backward " " lurk-input-marker t)))
966 (start (if space-idx (+ 1 space-idx) lurk-input-marker))
967 (completion-ignore-case t))
968 (unless (string-prefix-p "/" (buffer-substring start end))
969 (completion-in-region start end (lurk-get-context-users lurk-current-context))))))
975 (defvar lurk-mode-map
976 (let ((map (make-sparse-keymap)))
977 (define-key map (kbd "RET") 'lurk-enter)
978 (define-key map (kbd "<tab>") 'lurk-complete-nick)
979 (define-key map (kbd "C-c C-z") 'lurk-toggle-zoom)
980 (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
981 (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
982 (define-key map (kbd "<C-up>") 'lurk-history-prev)
983 (define-key map (kbd "<C-down>") 'lurk-history-next)
984 ;; (when (fboundp 'evil-define-key*)
985 ;; (evil-define-key* 'insert map
986 ;; (kbd "<C-Up>") 'lurk-history-prev
987 ;; (kbd "<C-Down>") 'lurk-history-next))
990 (defvar lurk-mode-map)
992 (define-derived-mode lurk-mode text-mode "lurk"
993 "Major mode for LURK.")
995 (when (fboundp 'evil-set-initial-state)
996 (evil-set-initial-state 'lurk-mode 'insert))
998 ;;; Main start procedure
1002 "Switch to *lurk* buffer."
1004 (if (get-buffer "*lurk*")
1005 (switch-to-buffer "*lurk*")
1006 (switch-to-buffer "*lurk*")
1008 (lurk-setup-buffer))
1012 ;;; lurk.el ends here