1 ;;; lurk.el --- Little Uni-buffer 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 Uni-buffer 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)
49 ("freenode" "chat.freenode.net" 6697)
50 ("tilde" "tilde.chat" 6697)
51 ("mbr" "mbrserver.com" 6667 :notls)
52 ("local" "localhost" 6697))
55 (defcustom lurk-allow-ipv6 nil
56 "Set to non-nil to allow use of IPv6.")
58 (defcustom lurk-show-joins nil
59 "Set to non-nil to be notified of joins, parts and quits.")
61 (defcustom lurk-display-header t
62 "If non-nil, use buffer header to display information on current host and channel.")
68 '((t :inherit default))
69 "Face used for Lurk text.")
72 '((t :inherit org-priority))
73 "Face used for the prompt.")
76 '((t :inherit org-tag))
77 "Face used for the context name in the prompt.")
80 '((t :inherit org-agenda-dimmed-todo-face))
81 "Face used for faded Lurk text.")
83 (defface lurk-timestamp
84 '((t :inherit org-agenda-dimmed-todo-face))
85 "Face used for timestamps.")
88 '((t :inherit font-lock-regexp-grouping-construct))
89 "Face used for Lurk error text.")
92 '((t :inherit org-upcoming-deadline))
93 "Face used for Lurk notice text.")
98 (defvar lurk-version "Lurk v0.1"
99 "Value of this string is used in response to CTCP version queries.")
101 (defvar lurk-notice-prefix "-!-")
103 (defvar lurk-error-prefix "!!!")
105 (defvar lurk-prompt-string ">")
107 (defvar lurk-debug nil
108 "If non-nil, enable debug mode.")
113 (defvar lurk-response "")
115 (defun lurk-filter (proc string)
116 (dolist (line (split-string (concat lurk-response string) "\n"))
117 (if (string-suffix-p "\r" line)
118 (lurk-eval-msg-string (string-trim line))
119 (setq lurk-response line))))
121 (defun lurk-sentinel (proc string)
122 (unless (equal "open" (string-trim string))
123 (lurk-display-error "Disconnected from server.")
124 (clrhash lurk-contexts)
125 (lurk-set-current-context nil)
127 (cancel-timer lurk-ping-timer)))
129 (defun lurk-start-process (network)
130 (let* ((row (assoc network lurk-networks))
133 (flags (seq-drop row 3)))
134 (make-network-process :name "lurk"
137 :family (if lurk-allow-ipv6 nil 'ipv4)
138 :filter #'lurk-filter
139 :sentinel #'lurk-sentinel
141 :tls-parameters (if (memq :notls flags)
143 (cons 'gnutls-x509pki
144 (gnutls-boot-parameters
145 :type 'gnutls-x509pki
149 (defvar lurk-ping-timer nil)
150 (defvar lurk-ping-period 60)
152 (defun lurk-ping-function ()
153 (lurk-send-msg (lurk-msg nil nil "PING" (car (process-contact (get-process "lurk")))))
154 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))
156 (defun lurk-connect (network)
157 (if (get-process "lurk")
158 (lurk-display-error "Already connected. Disconnect first.")
159 (if (not (assoc network lurk-networks))
160 (lurk-display-error "Network '" network "' is unknown.")
161 (clrhash lurk-contexts)
162 (lurk-set-current-context nil)
163 (lurk-start-process network)
164 (lurk-send-msg (lurk-msg nil nil "USER" lurk-nick 0 "*" lurk-nick))
165 (lurk-send-msg (lurk-msg nil nil "NICK" lurk-nick))
166 (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))))
168 (defun lurk-connected-p ()
169 (let ((proc (get-process "lurk")))
170 (and proc (eq (process-status proc) 'open))))
172 (defun lurk-send-msg (msg)
174 (lurk-display-string nil nil (lurk-msg->string msg)))
175 (let ((proc (get-process "lurk")))
176 (if (and proc (eq (process-status proc) 'open))
177 (process-send-string proc (concat (lurk-msg->string msg) "\r\n"))
178 (lurk-display-error "No server connection established.")
179 (error "No server connection established"))))
185 (defun lurk--as-string (obj)
187 (with-output-to-string (princ obj))
190 (defun lurk-msg (tags src cmd &rest params)
191 (list (lurk--as-string tags)
192 (lurk--as-string src)
193 (upcase (lurk--as-string cmd))
194 (mapcar #'lurk--as-string
195 (if (and params (listp (elt params 0)))
199 (defun lurk-msg-tags (msg) (elt msg 0))
200 (defun lurk-msg-src (msg) (elt msg 1))
201 (defun lurk-msg-cmd (msg) (elt msg 2))
202 (defun lurk-msg-params (msg) (elt msg 3))
203 (defun lurk-msg-trail (msg)
204 (let ((params (lurk-msg-params msg)))
206 (elt params (- (length params) 1)))))
208 (defvar lurk-msg-regex
210 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
212 (opt (: ":" (: (group (* (not (any space "!" "@"))))
213 (* (not (any space)))))
215 (group (: (* (not whitespace))))
217 (opt (group (+ not-newline))))
218 "Regex used to parse IRC messages.
219 Note that this regex is incomplete. Noteably, we discard the non-nick
220 portion of the source component of the message, as LURK doesn't use this.")
222 (defun lurk-string->msg (string)
223 (if (string-match lurk-msg-regex string)
224 (let* ((tags (match-string 1 string))
225 (src (match-string 2 string))
226 (cmd (upcase (match-string 3 string)))
227 (params-str (match-string 4 string))
230 (let* ((idx (cl-search ":" params-str))
231 (l (split-string (string-trim (substring params-str 0 idx))))
232 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
235 (apply #'lurk-msg (append (list tags src cmd) params)))
236 (error "Failed to parse string " string)))
238 (defun lurk--filtered-join (&rest args)
239 (string-join (seq-filter (lambda (el) el) args) " "))
241 (defun lurk-msg->string (msg)
242 (let ((tags (lurk-msg-tags msg))
243 (src (lurk-msg-src msg))
244 (cmd (lurk-msg-cmd msg))
245 (params (lurk-msg-params msg)))
247 (if tags (concat "@" tags) nil)
248 (if src (concat ":" src) nil)
250 (if (> (length params) 1)
251 (string-join (seq-take params (- (length params) 1)) " ")
253 (if (> (length params) 0)
254 (concat ":" (elt params (- (length params) 1)))
261 (defvar lurk-current-context nil)
262 (defvar lurk-contexts (make-hash-table :test #'equal))
264 (defun lurk-add-context (name)
265 (puthash name nil lurk-contexts))
267 (defun lurk-del-context (name)
268 (remhash name lurk-contexts))
270 (defun lurk-get-context-users (name)
271 (gethash name lurk-contexts))
273 (defun lurk-add-context-users (context users)
276 (gethash context lurk-contexts))
279 (defun lurk-del-context-user (context user)
281 (remove user (gethash context lurk-contexts))
284 (defun lurk-del-user (user)
285 (dolist (context (lurk-get-context-list))
286 (lurk-del-context-user context user)))
288 (defun lurk-rename-user (old-nick new-nick)
289 (dolist (context (lurk-get-context-list))
290 (lurk-del-context-user context old-nick)
291 (lurk-add-context-users context (list new-nick))))
293 (defun lurk-get-context-type (name)
295 ((string-prefix-p "#" name) 'channel)
296 ((string-match-p (rx (or "." "localhost")) name) 'host)
299 (defun lurk-get-context-list ()
301 (maphash (lambda (key val)
302 (cl-pushnew key res))
306 (defun lurk-get-next-context (&optional prev)
307 (if lurk-current-context
308 (let* ((context-list (if prev
309 (reverse (lurk-get-context-list))
310 (lurk-get-context-list)))
311 (context-list* (member lurk-current-context context-list)))
312 (if (> (length context-list*) 1)
317 (defun lurk-set-current-context (context)
318 (setq lurk-current-context context)
319 (lurk-highlight-context context)
321 (lurk-zoom-in lurk-current-context)))
323 (defun lurk-cycle-contexts (&optional rev)
324 (if lurk-current-context
326 (lurk-set-current-context (lurk-get-next-context rev))
327 (lurk-render-prompt))
328 (lurk-display-error "No channels joined.")))
334 (defun lurk-render-prompt ()
335 (with-current-buffer "*lurk*"
336 (let ((update-point (= lurk-input-marker (point)))
337 (update-window-points (mapcar (lambda (w)
338 (list (= (window-point w) lurk-input-marker)
340 (get-buffer-window-list nil nil t))))
342 (set-marker-insertion-type lurk-prompt-marker nil)
343 (set-marker-insertion-type lurk-input-marker t)
344 (let ((inhibit-read-only t))
345 (delete-region lurk-prompt-marker lurk-input-marker)
346 (goto-char lurk-prompt-marker)
348 (propertize (if lurk-current-context
353 (propertize (concat lurk-prompt-string " ")
357 (set-marker-insertion-type lurk-input-marker nil))
359 (goto-char lurk-input-marker))
360 (dolist (v update-window-points)
362 (set-window-point (cadr v) lurk-input-marker))))))
364 (defvar lurk-prompt-marker nil
365 "Marker for prompt position in LURK buffer.")
367 (defvar lurk-input-marker nil
368 "Marker for prompt position in LURK buffer.")
370 (defun lurk-setup-header ()
371 (setq-local header-line-format
373 (let ((proc (get-process "lurk")))
376 "Host: " (car (process-contact proc))
378 (if lurk-current-context
383 (length (lurk-get-context-users lurk-current-context)))
388 (defun lurk-setup-buffer ()
389 (with-current-buffer (get-buffer-create "*lurk*")
390 (setq-local scroll-conservatively 1)
391 (setq-local buffer-invisibility-spec nil)
392 (if (markerp lurk-prompt-marker)
393 (set-marker lurk-prompt-marker (point-max))
394 (setq lurk-prompt-marker (point-max-marker)))
395 (if (markerp lurk-input-marker)
396 (set-marker lurk-input-marker (point-max))
397 (setq lurk-input-marker (point-max-marker)))
398 (goto-char (point-max))
400 (if (lurk-display-header)
401 (lurk-setup-header))))
404 ;;; Output formatting and highlighting
407 ;; Partially-implemented idea: the face text property can be
408 ;; a list of faces, applied in order. By assigning each context
409 ;; a unique list and keeping track of these in a hash table, we can
410 ;; easily switch the face corresponding to a particular context
411 ;; by modifying the elements of this list.
413 ;; More subtly, we make only the cdrs of this list shared among
414 ;; all text of a given context, allowing the cars to be different
415 ;; and for different elements of the context-specific text to have
416 ;; different styling.
418 ;; Additionally, we allow selective hiding of contexts via
419 ;; the buffer-invisibility-spec.
421 (defvar lurk-context-facelists (make-hash-table :test 'equal)
422 "List of seen contexts and associated face lists.")
424 (defun lurk-get-context-facelist (context)
425 (let ((facelist (gethash context lurk-context-facelists)))
427 (setq facelist (list 'lurk-text))
428 (puthash context facelist lurk-context-facelists))
431 (defun lurk--fill-strings (col indent &rest strings)
433 (setq buffer-invisibility-spec nil)
434 (let ((fill-column col)
435 (adaptive-fill-regexp (rx-to-string `(= ,indent anychar))))
436 (apply #'insert strings)
437 (fill-region (point-min) (point-max) nil t)
440 (defun lurk-display-string (context prefix &rest strings)
441 (with-current-buffer (get-buffer-create "*lurk*")
443 (goto-char lurk-prompt-marker)
444 (let* ((inhibit-read-only t)
445 (old-pos (marker-position lurk-prompt-marker))
446 (padded-timestamp (concat (format-time-string "%H:%M ")))
447 (padded-prefix (if prefix (concat prefix " ") ""))
448 (context-atom (if context (intern context) nil)))
449 (insert-before-markers
452 (+ (length padded-timestamp)
453 (length padded-prefix))
454 (propertize padded-timestamp
455 'face 'lurk-timestamp
458 'invisible context-atom)
459 (propertize padded-prefix
462 'invisible context-atom)
463 (propertize (concat (lurk-buttonify-urls (apply #'concat strings)) "\n")
464 'face (lurk-get-context-facelist context)
467 'invisible context-atom)))))))
469 (defun lurk-display-message (from to text)
470 (let ((context (if (eq 'channel (lurk-get-context-type to))
472 (if (equal to lurk-nick) from to))))
476 (pcase (lurk-get-context-type to)
477 ('channel (concat to " <" from ">"))
478 ('nick (concat "[" from " -> " to "]"))
480 (error "Unsupported context type")))
481 'face (lurk-get-context-facelist context))
484 (defun lurk-display-action (from to action-text)
485 (let ((context (if (eq 'channel (lurk-get-context-type to))
487 (if (equal to lurk-nick) from to))))
493 (defun lurk-display-notice (context &rest notices)
496 (propertize lurk-notice-prefix 'face 'lurk-notice)
497 (apply #'concat notices)))
499 (defun lurk-display-error (&rest messages)
502 (propertize lurk-error-prefix 'face 'lurk-error)
503 (apply #'concat messages)))
505 (defun lurk-highlight-context (context)
507 (lambda (this-context facelist)
508 (if (equal this-context context)
509 (setcar facelist 'lurk-text)
510 (setcar facelist 'lurk-faded)))
511 lurk-context-facelists)
512 (force-window-update "*lurk*"))
514 (defun lurk-zoom-in (context)
515 (with-current-buffer "*lurk*"
517 (lambda (this-context _)
519 (let ((this-context-atom (intern this-context)))
520 (if (equal this-context context)
521 (remove-from-invisibility-spec this-context-atom)
522 (add-to-invisibility-spec this-context-atom)))))
523 lurk-context-facelists)
524 (force-window-update "*lurk*")))
526 (defun lurk-zoom-out ()
527 (with-current-buffer "*lurk*"
529 (lambda (this-context _)
530 (let ((this-context-atom (if this-context (intern this-context) nil)))
531 (remove-from-invisibility-spec this-context-atom)))
532 lurk-context-facelists)
533 (force-window-update "*lurk*")))
535 (defconst lurk-url-regex
539 (group (or (+ (any alnum "." "-"))
540 (+ (any alnum ":"))))
541 (opt (group (: ":" (+ digit))))
544 (* (any alnum "-/.,#:%=&_"))
545 (any alnum "-/#:%=&_")))))))
546 "Imperfect regex used to find URLs in plain text.")
548 (defun lurk-click-url (button)
549 (browse-url (button-get button 'url)))
551 (defun lurk-buttonify-urls (string)
552 "Turn substrings which look like urls in STRING into clickable buttons."
555 (goto-char (point-min))
556 (while (re-search-forward lurk-url-regex nil t)
557 (let ((url (match-string 0)))
558 (make-text-button (match-beginning 0)
560 'action #'lurk-click-url
564 'help-echo "Open URL in browser.")))
568 ;;; Message evaluation
571 (defun lurk-eval-msg-string (string)
573 (lurk-display-string nil nil string))
574 (let* ((msg (lurk-string->msg string)))
575 (pcase (lurk-msg-cmd msg)
578 (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
583 (let* ((params (lurk-msg-params msg))
584 (nick (elt params 0))
585 (text (string-join (seq-drop params 1) " ")))
586 (setq lurk-nick nick)
587 (lurk-display-notice nil text)))
590 (let* ((params (lurk-msg-params msg))
591 (channel (elt params 2))
592 (names (split-string (elt params 3))))
593 (lurk-add-context-users channel names)))
596 (let* ((params (lurk-msg-params msg))
597 (channel (elt params 1)))
600 (lurk--as-string (length (lurk-get-context-users channel)))
601 " users in " channel)))
604 (let* ((params (lurk-msg-params msg))
605 (channel (elt params 1)))
611 (let* ((params (lurk-msg-params msg))
612 (channel (elt params 1))
613 (topic (elt params 2)))
614 (lurk-display-notice channel "Topic: " topic)))
616 ("333") ; Avoid displaying these
618 ((rx (= 3 (any digit)))
619 (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
622 (guard (equal lurk-nick (lurk-msg-src msg))))
623 (let ((channel (car (lurk-msg-params msg))))
624 (lurk-add-context channel)
625 (lurk-set-current-context channel)
626 (lurk-display-notice channel "Joining channel " channel)
627 (lurk-render-prompt)))
630 (let ((channel (car (lurk-msg-params msg)))
631 (nick (lurk-msg-src msg)))
632 (lurk-add-context-users channel (list nick))
634 (lurk-display-notice channel nick " joined channel " channel))))
637 (guard (equal lurk-nick (lurk-msg-src msg))))
638 (let ((channel (car (lurk-msg-params msg))))
639 (lurk-display-notice channel "Left channel " channel)
640 (lurk-del-context channel)
641 (if (equal channel lurk-current-context)
642 (lurk-set-current-context (lurk-get-next-context)))
643 (lurk-render-prompt)))
646 (let ((channel (car (lurk-msg-params msg)))
647 (nick (lurk-msg-src msg)))
648 (lurk-del-context-user channel nick)
650 (lurk-display-notice channel nick " left channel " channel))))
653 (let ((kicker-nick (lurk-msg-src msg))
654 (channel (car (lurk-msg-params msg)))
655 (nick (cadr (lurk-msg-params msg)))
656 (reason (caddr (lurk-msg-params msg))))
657 (if (equal nick lurk-nick)
659 (lurk-display-notice channel kicker-nick " kicked you from " channel ": " reason)
660 (lurk-del-context channel)
661 (if (equal channel lurk-current-context)
662 (lurk-set-current-context (lurk-get-next-context)))
663 (lurk-render-prompt))
664 (lurk-del-context-user channel nick)
665 (lurk-display-notice channel kicker-nick " kicked " nick " from " channel ": " reason))))
668 (let ((nick (lurk-msg-src msg))
669 (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
672 (lurk-display-notice nil nick " quit: " reason))))
675 (guard (equal lurk-nick (lurk-msg-src msg))))
676 (setq lurk-nick (car (lurk-msg-params msg)))
677 (lurk-display-notice nil "Set nick to " lurk-nick))
680 (let ((old-nick (lurk-msg-src msg))
681 (new-nick (car (lurk-msg-params msg))))
682 (lurk-display-notice nil old-nick " is now known as " new-nick)
683 (lurk-rename-user old-nick new-nick)))
686 (let ((nick (lurk-msg-src msg))
687 (channel (car (lurk-msg-params msg)))
688 (text (cadr (lurk-msg-params msg))))
690 ((rx (: "\01VERSION "
691 (let version (* (not "\01")))
693 (lurk-display-notice nil "CTCP version reply from " nick ": " version))
695 (lurk-display-notice nil text)))))
698 (let* ((from (lurk-msg-src msg))
699 (params (lurk-msg-params msg))
701 (text (cadr params)))
704 (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
705 (lurk-send-msg (lurk-msg nil nil "NOTICE"
706 (list from (concat "\01VERSION "
709 (lurk-display-notice nil "CTCP version request received from " from))
711 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
712 (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
713 (lurk-display-notice from "CTCP ping received from " from))
716 (lurk-display-notice from "CTCP userinfo request from " from " (no response sent)"))
718 ((rx (: "\01ACTION " (let action-text (* (not "\01"))) "\01"))
719 (lurk-display-action from to action-text))
722 (lurk-display-message from to text)))))
724 (lurk-display-notice nil (lurk-msg->string msg))))))
730 (defun lurk-enter-string (string)
731 (if (string-prefix-p "/" string)
732 (pcase (substring string 1)
734 (setq lurk-debug (not lurk-debug))
735 (lurk-display-notice nil "Debug mode now " (if lurk-debug "on" "off") "."))
738 (if header-line-format
740 (setq-local header-line-format nil)
741 (lurk-display-notice nil "Header disabled."))
743 (lurk-display-notice nil "Header enabled.")))
745 ((rx (: "CONNECT " (let network (* not-newline))))
746 (lurk-display-notice nil "Attempting to connect to " network "...")
747 (lurk-connect network))
749 ((rx (: "TOPIC " (let new-topic (* not-newline))))
750 (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
752 ((rx (: "ME " (let action (* not-newline))))
753 (let ((ctcp-text (concat "\01ACTION " action "\01")))
754 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
755 (list lurk-current-context ctcp-text)))
756 (lurk-display-action lurk-nick lurk-current-context action)))
758 ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
759 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
760 (list nick "\01VERSION\01")))
761 (lurk-display-notice nil "CTCP version request sent to " nick))
763 ((rx "PART" (opt (: " " (let channel (* not-newline)))))
764 (if (or lurk-current-context channel)
765 (lurk-send-msg (lurk-msg nil nil "PART" (if channel
767 lurk-current-context)))
768 (lurk-display-error "No current channel to leave.")))
770 ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
771 (lurk-send-msg (lurk-msg nil nil "QUIT"
772 (or quit-msg lurk-default-quit-msg))))
774 ((rx (: "NICK" (* whitespace) string-end))
775 (lurk-display-notice nil "Current nick: " lurk-nick))
777 ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
778 (if (lurk-connected-p)
779 (lurk-send-msg (lurk-msg nil nil "NICK" nick))
780 (setq lurk-nick nick)
781 (lurk-display-notice nil "Set default nick to '" nick "'")))
784 (lurk-display-notice nil "This command can generate lots of output. Use `LIST -yes' if you're sure."))
786 ((rx (: "LIST" (+ whitespace) "-YES"))
787 (lurk-send-msg (lurk-msg nil nil "LIST")))
790 (let to (* (not whitespace)))
792 (let text (* not-newline)))
793 (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
794 (lurk-display-message lurk-nick to text))
796 ((rx (: (let cmd-str (+ (not whitespace)))
797 (opt (: " " (let params-str (* not-newline))))))
798 (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
800 (split-string params-str)
803 (unless (string-empty-p string)
804 (if lurk-current-context
806 (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
809 (lurk-display-message lurk-nick lurk-current-context string))
810 (lurk-display-error "No current context.")))))
812 (defvar lurk-history nil
813 "Commands and messages sent in current session.")
817 "Enter current contents of line after prompt."
819 (with-current-buffer "*lurk*"
820 (let ((line (buffer-substring lurk-input-marker (point-max))))
821 (push line lurk-history)
822 (setq lurk-history-index nil)
823 (let ((inhibit-read-only t))
824 (delete-region lurk-input-marker (point-max)))
825 (lurk-enter-string line))))
827 (defvar lurk-history-index nil)
829 (defun lurk-history-cycle (delta)
831 (with-current-buffer "*lurk*"
832 (if lurk-history-index
833 (setq lurk-history-index
835 (min (- (length lurk-history) 1)
836 (+ delta lurk-history-index))))
837 (setq lurk-history-index 0))
838 (delete-region lurk-input-marker (point-max))
839 (insert (elt lurk-history lurk-history-index)))))
841 (defun lurk-history-next ()
843 (lurk-history-cycle -1))
845 (defun lurk-history-prev ()
847 (lurk-history-cycle +1))
849 ;;; Interactive functions
852 (defun lurk-cycle-contexts-forward ()
854 (lurk-cycle-contexts))
856 (defun lurk-cycle-contexts-reverse ()
858 (lurk-cycle-contexts t))
860 (defvar lurk-zoomed nil
861 "Keeps track of zoom status.")
863 (defun lurk-toggle-zoom ()
867 (lurk-zoom-in lurk-current-context))
868 (setq lurk-zoomed (not lurk-zoomed)))
870 (defun lurk-complete-nick ()
872 (when (and (>= (point) lurk-input-marker) lurk-current-context)
873 (let* ((end (max lurk-input-marker (point)))
874 (space-idx (save-excursion
875 (re-search-backward " " lurk-input-marker t)))
876 (start (if space-idx (+ 1 space-idx) lurk-input-marker))
877 (completion-ignore-case t))
878 (unless (string-prefix-p "/" (buffer-substring start end))
879 (completion-in-region start end (lurk-get-context-users lurk-current-context))))))
885 (defvar lurk-mode-map
886 (let ((map (make-sparse-keymap)))
887 (define-key map (kbd "RET") 'lurk-enter)
888 (define-key map (kbd "<tab>") 'lurk-complete-nick)
889 (define-key map (kbd "C-c C-z") 'lurk-toggle-zoom)
890 (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
891 (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
892 (define-key map (kbd "<C-up>") 'lurk-history-prev)
893 (define-key map (kbd "<C-down>") 'lurk-history-next)
894 ;; (when (fboundp 'evil-define-key*)
895 ;; (evil-define-key* 'insert map
896 ;; (kbd "<C-Up>") 'lurk-history-prev
897 ;; (kbd "<C-Down>") 'lurk-history-next))
900 (defvar lurk-mode-map)
902 (define-derived-mode lurk-mode text-mode "lurk"
903 "Major mode for LURK.")
905 (when (fboundp 'evil-set-initial-state)
906 (evil-set-initial-state 'lurk-mode 'insert))
908 ;;; Main start procedure
912 "Switch to *lurk* buffer."
914 (if (get-buffer "*lurk*")
915 (switch-to-buffer "*lurk*")
916 (switch-to-buffer "*lurk*")
922 ;;; lurk.el ends here