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