Basic context highlighting and zooming implemented.
[lurk.git] / lurk.el
1 ;;; lurk.el --- Little Unified iRc Klient -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 2021 Tim Vaughan
4
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 14 June 2021
7 ;; Version: 1.0
8 ;; Keywords: network
9 ;; Homepage: http://thelambdalab.xyz/lurk
10 ;; Package-Requires: ((emacs "26"))
11
12 ;; This file is not part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (provide 'lurk)
32
33
34 ;;; Customizations
35 ;;
36
37 (defgroup lurk nil
38   "Little Unified iRc Klient."
39   :group 'network)
40
41 (defcustom lurk-nick "plugd"
42   "Default nick.")
43
44 (defcustom lurk-default-quit-msg "Bye"
45   "Default quit message when none supplied.")
46
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))
53   "IRC networks.")
54
55 (defcustom lurk-allow-ipv6 nil
56   "Set to non-nil to allow use of IPv6.")
57
58 (defcustom lurk-show-joins nil
59   "Set to non-nil to be notified of joins, parts and quits.")
60
61 ;;; Faces
62 ;;
63
64 (defface lurk-text
65   '((t :inherit font-lock-preprocessor-face))
66   "Face used for Lurk text.")
67
68 (defface lurk-prompt
69   '((t :inherit org-level-2))
70   "Face used for the prompt.")
71
72 (defface lurk-context
73   '((t :inherit org-list-dt))
74   "Face used for the context name in the prompt.")
75
76 (defface lurk-faded
77   '((t :inherit font-lock-preprocessor-face))
78   "Face used for faded Lurk text.")
79
80 (defface lurk-bold
81   '((t :inherit font-lock-function-name-face))
82   "Face used for bold Lurk text.")
83
84 (defface lurk-error
85   '((t :inherit font-lock-regexp-grouping-construct))
86   "Face used for Lurk error text.")
87
88 ;;; Global variables
89 ;;
90
91 (defvar lurk-version "Lurk v0.1")
92
93 (defvar lurk-notice-prefix "-!-")
94
95 (defvar lurk-error-prefix "!!!")
96
97 (defvar lurk-prompt-string
98   (propertize "> " 'face 'lurk-prompt))
99
100
101 ;;; Network process
102 ;;
103
104 (defvar lurk-response "")
105
106 (defun lurk-filter (proc string)
107   (dolist (line (split-string (concat lurk-response string) "\n"))
108     (if (string-suffix-p "\r" line)
109         (lurk-eval-msg-string (string-trim line))
110       (setq lurk-response line))))
111
112 (defun lurk-sentinel (proc string)
113   (unless (equal "open" (string-trim string))
114     (lurk-display-error "Disconnected from server.")
115     (clrhash lurk-contexts)
116     (setq lurk-current-context nil)
117     (lurk-render-prompt)
118     (cancel-timer lurk-ping-timer)))
119
120 (defun lurk-start-process (network)
121   (let* ((row (assoc network lurk-networks))
122          (host (elt row 1))
123          (port (elt row 2))
124          (flags (seq-drop row 3)))
125     (make-network-process :name "lurk"
126                           :host host
127                           :service port
128                           :family (if lurk-allow-ipv6 nil 'ipv4)
129                           :filter #'lurk-filter
130                           :sentinel #'lurk-sentinel
131                           :nowait nil
132                           :tls-parameters (if (memq :notls flags)
133                                               nil
134                                             (cons 'gnutls-x509pki
135                                                   (gnutls-boot-parameters
136                                                    :type 'gnutls-x509pki
137                                                    :hostname host)))
138                           :buffer "*lurk*")))
139
140 (defvar lurk-ping-timer nil)
141 (defvar lurk-ping-period 60)
142
143 (defun lurk-ping-function ()
144   (lurk-send-msg (lurk-msg nil nil "PING" (car (process-contact (get-process "lurk")))))
145   (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))
146
147 (defun lurk-connect (network)
148   (if (get-process "lurk")
149       (lurk-display-error "Already connected.  Disconnect first.")
150     (if (not (assoc network lurk-networks))
151         (lurk-display-error "Network '" network "' is unknown.")
152       (clrhash lurk-contexts)
153       (setq lurk-current-context nil)
154       (lurk-start-process network)
155       (lurk-send-msg (lurk-msg nil nil "USER" lurk-nick 0 "*" lurk-nick))
156       (lurk-send-msg (lurk-msg nil nil "NICK" lurk-nick))
157       (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))))
158
159 (defun lurk-connected-p ()
160   (let ((proc (get-process "lurk")))
161     (and proc (eq (process-status proc) 'open))))
162
163 (defun lurk-send-msg (msg)
164   (let ((proc (get-process "lurk")))
165     (if (and proc (eq (process-status proc) 'open))
166         (process-send-string proc (concat (lurk-msg->string msg) "\r\n"))
167       (lurk-display-error "No server connection established.")
168       (error "No server connection established"))))
169
170
171 ;;; Server messages
172 ;;
173
174 (defun lurk--as-string (obj)
175   (if obj
176       (with-output-to-string (princ obj))
177     nil))
178
179 (defun lurk-msg (tags src cmd &rest params)
180   (list (lurk--as-string tags)
181         (lurk--as-string src)
182         (upcase (lurk--as-string cmd))
183         (mapcar #'lurk--as-string
184                 (if (and params (listp (elt params 0)))
185                     (elt params 0)
186                   params))))
187
188 (defun lurk-msg-tags (msg) (elt msg 0))
189 (defun lurk-msg-src (msg) (elt msg 1))
190 (defun lurk-msg-cmd (msg) (elt msg 2))
191 (defun lurk-msg-params (msg) (elt msg 3))
192 (defun lurk-msg-trail (msg)
193   (let ((params (lurk-msg-params msg)))
194     (if params
195         (elt params (- (length params) 1)))))
196
197 (defvar lurk-msg-regex
198   (rx
199    (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
200         (* whitespace))
201    (opt (: ":" (: (group (* (not (any space "!" "@"))))
202                   (* (not (any space)))))
203         (* whitespace))
204    (group (: (* (not whitespace))))
205    (* whitespace)
206    (opt (group (+ not-newline))))
207   "Regex used to parse IRC messages.
208 Note that this regex is incomplete.  Noteably, we discard the non-nick
209 portion of the source component of the message, as LURK doesn't use this.")
210
211 (defun lurk-string->msg (string)
212   (if (string-match lurk-msg-regex string)
213       (let* ((tags (match-string 1 string))
214              (src (match-string 2 string))
215              (cmd (upcase (match-string 3 string)))
216              (params-str (match-string 4 string))
217              (params
218               (if params-str
219                   (let* ((idx (cl-search ":" params-str))
220                          (l (split-string (string-trim (substring params-str 0 idx))))
221                          (r (if idx (list (substring params-str (+ 1 idx))) nil)))
222                     (append l r))
223                 nil)))
224         (apply #'lurk-msg (append (list tags src cmd) params)))
225     (error "Failed to parse string " string)))
226
227 (defun lurk--filtered-join (&rest args)
228   (string-join (seq-filter (lambda (el) el) args) " "))
229
230 (defun lurk-msg->string (msg)
231   (let ((tags (lurk-msg-tags msg))
232         (src (lurk-msg-src msg))
233         (cmd (lurk-msg-cmd msg))
234         (params (lurk-msg-params msg)))
235     (lurk--filtered-join
236      (if tags (concat "@" tags) nil)
237      (if src (concat ":" src) nil)
238      cmd
239      (if (> (length params) 1)
240          (string-join (seq-take params (- (length params) 1)) " ")
241        nil)
242      (if (> (length params) 0)
243          (concat ":" (elt params (- (length params) 1)))
244        nil))))
245
246
247 ;;; Contexts
248 ;;
249
250 (defvar lurk-current-context nil)
251 (defvar lurk-contexts (make-hash-table :test #'equal))
252
253 (defun lurk-add-context (name)
254   (puthash name nil lurk-contexts))
255
256 (defun lurk-del-context (name)
257   (remhash name lurk-contexts))
258
259 (defun lurk-get-context-users (name)
260   (gethash name lurk-contexts))
261
262 (defun lurk-add-context-users (context users)
263   (puthash context
264            (append users
265                    (gethash context lurk-contexts))
266            lurk-contexts))
267
268 (defun lurk-del-context-user (context user)
269   (puthash context
270            (remove user (gethash context lurk-contexts))
271            lurk-contexts))
272
273 (defun lurk-del-user (user)
274   (dolist (context (lurk-get-context-list))
275     (lurk-del-context-user context user)))
276
277 (defun lurk-rename-user (old-nick new-nick)
278   (dolist (context (lurk-get-context-list))
279     (lurk-del-context-user context old-nick)
280     (lurk-add-context-users context (list new-nick))))
281
282 (defun lurk-get-context-type (name)
283   (cond
284    ((string-prefix-p "#" name) 'channel)
285    ((string-match-p (rx (or "." "localhost")) name) 'host)
286    (t 'nick)))
287
288 (defun lurk-get-context-list ()
289   (let ((res nil))
290     (maphash (lambda (key val)
291                (cl-pushnew key res))
292              lurk-contexts)
293     res))
294
295 (defun lurk-get-next-context (&optional prev)
296   (if lurk-current-context
297       (let* ((context-list (if prev
298                                (reverse (lurk-get-context-list))
299                              (lurk-get-context-list)))
300              (context-list* (member lurk-current-context context-list)))
301         (if (> (length context-list*) 1)
302             (cadr context-list*)
303           (car context-list)))
304     nil))
305
306 (defun lurk-cycle-contexts (&optional rev)
307   (if lurk-current-context
308       (progn
309         (setq lurk-current-context (lurk-get-next-context rev))
310         (lurk-render-prompt))
311     (lurk-display-error "No channels joined.")))
312
313 (defun lurk-cycle-contexts-forward ()
314   (interactive)
315   (lurk-cycle-contexts)
316   (lurk-highlight-context lurk-current-context))
317
318 (defun lurk-cycle-contexts-reverse ()
319   (interactive)
320   (lurk-cycle-contexts t)
321   (lurk-highlight-context lurk-current-context))
322
323
324 ;;; Buffer
325 ;;
326 (defun lurk-render-prompt ()
327   (with-current-buffer "*lurk*"
328     (let ((update-point (= lurk-input-marker (point)))
329           (update-window-points (mapcar (lambda (w)
330                                           (list (= (window-point w) lurk-input-marker)
331                                                 w))
332                                         (get-buffer-window-list nil nil t))))
333       (save-excursion
334         (set-marker-insertion-type lurk-prompt-marker nil)
335         (set-marker-insertion-type lurk-input-marker t)
336         (let ((inhibit-read-only t))
337           (delete-region lurk-prompt-marker lurk-input-marker)
338           (goto-char lurk-prompt-marker)
339           (insert
340            (propertize (if lurk-current-context
341                            lurk-current-context
342                          "")
343                        'face 'lurk-context
344                        'read-only t)
345            (propertize lurk-prompt-string
346                        'face 'lurk-prompt
347                        'read-only t
348                        'rear-nonsticky t)))
349         (set-marker-insertion-type lurk-input-marker nil))
350       (if update-point
351           (goto-char lurk-input-marker))
352       (dolist (v update-window-points)
353         (if (car v)
354             (set-window-point (cadr v) lurk-input-marker))))))
355   
356 (defvar lurk-prompt-marker nil
357   "Marker for prompt position in LURK buffer.")
358
359 (defvar lurk-input-marker nil
360   "Marker for prompt position in LURK buffer.")
361
362 (defun lurk-setup-buffer ()
363   (with-current-buffer (get-buffer-create "*lurk*")
364     (setq-local scroll-conservatively 1)
365     (if (markerp lurk-prompt-marker)
366         (set-marker lurk-prompt-marker (point-max))
367       (setq lurk-prompt-marker (point-max-marker)))
368     (if (markerp lurk-input-marker)
369         (set-marker lurk-input-marker (point-max))
370       (setq lurk-input-marker (point-max-marker)))
371     (goto-char (point-max))
372     (lurk-render-prompt)))
373
374
375 ;;; Output formatting and highlighting
376 ;;
377
378 ;; Partially-implemented idea: the face text property can be
379 ;; a list of faces, applied in order.  By assigning each context
380 ;; a unique list and keeping track of these in a hash table, we can
381 ;; easily switch the face corresponding to a particular context
382 ;; by modifying the elements of this list.
383 ;;
384 ;; More subtly, we make only the cdrs of this list shared among
385 ;; all text of a given context, allowing the cars to be different
386 ;; and for different elements of the context-specific text to have
387 ;; different styling.
388
389 ;; Additionally, we can allow selective hiding of contexts via
390 ;; the buffer-invisibility-spec.
391
392 (defvar lurk-context-facelists (make-hash-table :test 'equal)
393   "List of seen contexts and associated face lists.")
394
395 (defun lurk-get-context-facelist (context)
396   (let ((facelist (gethash context lurk-context-facelists)))
397     (unless facelist
398       (setq facelist (list 'lurk-text))
399       (puthash context facelist lurk-context-facelists))
400     facelist))
401
402 (defun lurk-display-string (context &rest strings)
403   (with-current-buffer (get-buffer-create "*lurk*")
404     (save-excursion
405       (goto-char lurk-prompt-marker)
406       (let ((inhibit-read-only t)
407             (old-pos (marker-position lurk-prompt-marker))
408             (adaptive-fill-regexp (rx (= 6 anychar)))
409             (fill-column 80)
410             (context-atom (if context (intern context) nil)))
411         (insert-before-markers
412          (propertize (concat (format-time-string "%H:%M") " ")
413                      'face (lurk-get-context-facelist context)
414                      'read-only t
415                      'context context
416                      'invisible context-atom
417                      'help-echo (concat "Context: " (or context "none")))
418          (propertize (concat (apply #'concat strings) "\n")
419                      'face (lurk-get-context-facelist context)
420                      'read-only t
421                      'context context
422                      'invisible context-atom
423                      'help-echo (concat "Context: " (or context "none"))))
424         (fill-region old-pos lurk-prompt-marker nil t)))))
425
426 (defun lurk-display-message (from to text)
427   (let ((context (if (eq 'channel (lurk-get-context-type to))
428                      to
429                    (if (equal to lurk-nick) from to))))
430     (lurk-display-string
431      context
432      (pcase (lurk-get-context-type to)
433        ('channel (concat to " <" from "> " text))
434        ('nick (concat "[" from " -> " to "] " text))
435        (_
436         (error "Unsupported context type"))))))
437
438 (defun lurk-display-notice (context &rest notices)
439   (lurk-display-string
440    context
441    lurk-notice-prefix " "
442    (apply #'concat notices)))
443
444 (defun lurk-display-error (&rest messages)
445   (lurk-display-string
446    nil
447    lurk-error-prefix " "
448    (apply #'concat messages)))
449
450 (defun lurk-highlight-context (context)
451   (maphash
452    (lambda (this-context facelist)
453      (if (equal this-context context)
454          (setcar facelist 'lurk-bold)
455        (setcar facelist 'lurk-faded)))
456    lurk-context-facelists)
457   (force-window-update "*lurk*"))
458
459 (defun lurk-zoom-in (context)
460   (with-current-buffer "*lurk*"
461     (maphash
462      (lambda (this-context _)
463        (let ((this-context-atom (if this-context (intern this-context) nil)))
464          (if (equal this-context context)
465              (remove-from-invisibility-spec this-context-atom)
466            (add-to-invisibility-spec this-context-atom))))
467      lurk-context-facelists)
468     (force-window-update "*lurk*")))
469
470 (defun lurk-zoom-out ()
471   (with-current-buffer "*lurk*"
472     (maphash
473      (lambda (this-context _)
474        (let ((this-context-atom (if this-context (intern this-context) nil)))
475          (remove-from-invisibility-spec this-context-atom)))
476      lurk-context-facelists)
477     (force-window-update "*lurk*")))
478
479 ;;; Message evaluation
480 ;;
481
482 (defun lurk-eval-msg-string (string)
483   ;; (lurk-display-string nil string)
484   (let* ((msg (lurk-string->msg string)))
485     ;; (message (pp msg))
486     (pcase (lurk-msg-cmd msg)
487       ("PING"
488        (lurk-send-msg
489         (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
490        ;; (lurk-display-notice nil "ping-pong (server initiated)"))
491
492       ("PONG")
493        ;; (lurk-display-notice nil "ping-pong (client initiated)"))
494
495       ("001"
496        (let* ((params (lurk-msg-params msg))
497               (nick (elt params 0))
498               (text (string-join (seq-drop params 1) " ")))
499          (setq lurk-nick nick)
500          (lurk-display-notice nil text)))
501
502       ("353" ; NAMEREPLY
503        (let* ((params (lurk-msg-params msg))
504               (channel (elt params 2))
505               (names (split-string (elt params 3))))
506          (lurk-add-context-users channel names)))
507
508       ("366" ; ENDOFNAMES
509        (let* ((params (lurk-msg-params msg))
510               (channel (elt params 1)))
511          (lurk-display-notice
512           channel
513           (lurk--as-string (length (lurk-get-context-users channel)))
514           " users in " channel)))
515
516       ((rx (= 3 (any digit)))
517        (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
518
519       ((and "JOIN"
520             (guard (equal lurk-nick (lurk-msg-src msg))))
521        (let ((channel (car (lurk-msg-params msg))))
522          (lurk-add-context channel)
523          (setq lurk-current-context channel)
524          (lurk-display-notice channel "Joining channel " channel)
525          (lurk-render-prompt)))
526
527       ("JOIN"
528        (let ((channel (car (lurk-msg-params msg)))
529              (nick (lurk-msg-src msg)))
530          (lurk-add-context-users channel (list nick))
531          (if lurk-show-joins
532              (lurk-display-notice channel nick " joined channel " channel))))
533
534       ((and "PART"
535             (guard (equal lurk-nick (lurk-msg-src msg))))
536        (let ((channel (car (lurk-msg-params msg))))
537          (lurk-display-notice channel "Left channel " channel)
538          (lurk-del-context channel)
539          (if (equal channel lurk-current-context)
540              (setq lurk-current-context (lurk-get-next-context)))
541          (lurk-render-prompt)))
542
543       ("PART"
544        (let ((channel (car (lurk-msg-params msg)))
545              (nick (lurk-msg-src msg)))
546          (lurk-del-context-user channel nick)
547          (if lurk-show-joins
548              (lurk-display-notice channel nick " left channel " channel))))
549
550       ("QUIT"
551        (let ((nick (lurk-msg-src msg))
552              (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
553          (lurk-del-user nick)
554          (if lurk-show-joins
555              (lurk-display-notice nil nick " quit: " reason))))
556
557       ((and "NICK"
558             (guard (equal lurk-nick (lurk-msg-src msg))))
559        (setq lurk-nick (car (lurk-msg-params msg)))
560        (lurk-display-notice nil "Set nick to " lurk-nick))
561
562       ("NICK"
563        (let ((old-nick (lurk-msg-src msg))
564              (new-nick (car (lurk-msg-params msg))))
565          (lurk-display-notice nil old-nick " is now known as " new-nick)
566          (lurk-rename-user old-nick new-nick)))
567
568       ("NOTICE"
569        (let ((nick (lurk-msg-src msg))
570              (channel (car (lurk-msg-params msg)))
571              (text (cadr (lurk-msg-params msg))))
572          (pcase text
573            ((rx (: "\01VERSION "
574                    (let version (* (not "\01")))
575                    "\01"))
576             (lurk-display-notice nil "CTCP version reply from " nick ": " version))
577            (_
578             (lurk-display-notice nil text)))))
579
580       ("PRIVMSG"
581        (let* ((from (lurk-msg-src msg))
582               (params (lurk-msg-params msg))
583               (to (car params))
584               (text (cadr params)))
585          (pcase text
586            ("\01VERSION\01"
587             (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
588               (lurk-send-msg (lurk-msg nil nil "NOTICE"
589                                        (list from (concat "\01VERSION "
590                                                           version-string
591                                                           "\01")))))
592             (lurk-display-notice nil "CTCP version request received from " from))
593
594            ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
595             (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
596             (lurk-display-notice "CTCP ping received from " from))
597
598            ("\01USERINFO\01"
599             (lurk-display-notice "CTCP userinfo request from " from " (no response sent)"))
600
601            (_
602             (if (and (equal from "BitBot")
603                      (equal to "##moshpit")
604                      (cl-search "\\_o< QUACK!" text))
605               (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to ",bef")))
606             (lurk-display-message from to text)))))
607       (_
608        (lurk-display-notice nil (lurk-msg->string msg))))))
609
610
611 ;;; Command entering
612 ;;
613
614 (defun lurk-enter-string (string)
615   (if (string-prefix-p "/" string)
616       (pcase (substring string 1)
617         ((rx (: "CONNECT " (let network (* not-newline))))
618          (lurk-display-notice nil "Attempting to connect to " network "...")
619          (lurk-connect network))
620
621         ((rx (: "TOPIC " (let new-topic (* not-newline))))
622          (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
623
624         ((rx (: "ME " (let action (* not-newline))))
625          (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
626                                   (list lurk-current-context
627                                         (concat "\01ACTION " action "\01"))))
628          (lurk-display-action lurk-nick action))
629
630         ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
631          (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
632                                   (list nick "\01VERSION\01")))
633          (lurk-display-notice nil "CTCP version request sent to " nick))
634
635         ((rx "PART" (opt (: " " (let channel (* not-newline)))))
636          (if (or lurk-current-context channel)
637              (lurk-send-msg (lurk-msg nil nil "PART" (if channel
638                                                          channel
639                                                        lurk-current-context)))
640            (lurk-display-error "No current channel to leave.")))
641
642         ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
643          (lurk-send-msg (lurk-msg nil nil "QUIT"
644                                   (or quit-msg lurk-default-quit-msg))))
645
646         ((rx (: "NICK" (* whitespace) string-end))
647          (lurk-display-notice nil "Current nick: " lurk-nick))
648
649         ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
650          (if (lurk-connected-p)
651              (lurk-send-msg (lurk-msg nil nil "NICK" nick))
652            (setq lurk-nick nick)
653            (lurk-display-notice nil "Set default nick to '" nick "'")))
654
655         ((rx "MSG "
656              (let to (* (not whitespace)))
657              " "
658              (let text (* not-newline)))
659          (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
660          (lurk-display-message lurk-nick to text))
661
662         ((rx (: (let cmd-str (+ (not whitespace)))
663                 (opt (: " " (let params-str (* not-newline))))))
664          (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
665                                   (if params-str
666                                       (split-string params-str)
667                                     nil)))))
668
669     (unless (string-empty-p string)
670       (if lurk-current-context
671           (progn
672             (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
673                                      lurk-current-context
674                                      string))
675             (lurk-display-message lurk-nick lurk-current-context string))
676         (lurk-display-error "No current context.")))))
677
678 (defun lurk-enter ()
679   "Enter current contents of line after prompt."
680   (interactive)
681   (with-current-buffer "*lurk*"
682     (let ((line (buffer-substring lurk-input-marker (point-max))))
683       (let ((inhibit-read-only t))
684         (delete-region lurk-input-marker (point-max)))
685       (lurk-enter-string line))))
686
687
688 ;;; Mode
689 ;;
690
691 (defvar lurk-mode-map
692   (let ((map (make-sparse-keymap)))
693     (define-key map (kbd "RET") 'lurk-enter)
694     (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
695     (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
696     map))
697
698 (define-derived-mode lurk-mode text-mode "lurk"
699   "Major mode for LURK.")
700
701 (when (fboundp 'evil-set-initial-state)
702   (evil-set-initial-state 'lurk-mode 'insert))
703
704 ;;; Main start procedure
705 ;;
706
707 (defun lurk ()
708   "Switch to *lurk* buffer."
709   (interactive)
710   (if (get-buffer "*lurk*")
711       (switch-to-buffer "*lurk*")
712     (switch-to-buffer "*lurk*"))
713   (lurk-mode)
714   (lurk-setup-buffer)
715   "Started LURK.")
716
717
718
719 ;;; lurk.el ends here