Added channel autojoin.
[lurk.git] / murk.el
1 ;;; murk.el --- Multinetwork Unibuffer iRc Klient -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 2024 plugd
4
5 ;; Author: plugd <plugd@thelambdalab.xyz>
6 ;; Created: 11 May 2024
7 ;; Version: 0.0
8 ;; Homepage: http://thelambdalab.xyz/murk
9 ;; Keywords: comm
10 ;; Package-Requires: ((emacs "26.1"))
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 ;; A very simple IRC client which uses only a single buffer.
30
31 ;;; Code:
32
33 (provide 'murk)
34
35 (require 'cl-lib)
36
37
38 ;;; Customizations
39
40 (defgroup murk nil
41   "Multinetwork Unibuffer iRc Klient"
42   :group 'network)
43
44 (defcustom murk-default-nick "plugd"
45   "Default nick."
46   :type '(string))
47
48 (defcustom murk-default-quit-msg "Bye"
49   "Default quit message when none supplied."
50   :type '(string))
51
52 (defcustom murk-networks
53   '(("debug" "localhost" 6697)
54     ("libera" "irc.libera.chat" 6697)
55     ("tilde" "tilde.chat" 6697)
56     ("sdf" "irc.sdf.org" 6697)
57     ("freenode" "chat.freenode.net" 6697)
58     ("mbr" "mbrserver.com" 6667 :notls))
59   "IRC networks."
60   :type '(alist :key-type string))
61
62 (defcustom murk-show-joins nil
63   "Set to non-nil to be notified of joins, parts and quits.")
64
65 (defcustom murk-display-header t
66   "If non-nil, use buffer header to display current host and channel."
67   :type '(boolean))
68
69 (defcustom murk-autoreply-table nil
70   "Table of autoreply messages.
71
72 Each autoreply is a list of two elements: (matcher reply)
73
74 Here matcher is a list:
75
76 (network src cmd params ...)
77
78 and reply is another list:
79
80  (cmd params ...)
81
82 Each entry in the matcher list is a regular expression tested against the
83 corresponding values in the incomming message.  Entries can be nil,
84 in which case they match anything."
85   :type '(list (list) (list)))
86
87
88 ;;; Faces
89 ;;
90
91 (defface murk-text
92   '((t :inherit default))
93   "Face used for murk text.")
94
95 (defface murk-prompt
96   '((t :inherit font-lock-keyword-face))
97   "Face used for the prompt.")
98
99 (defface murk-context
100   '((t :inherit murk-context))
101   "Face used for the context name in the prompt.")
102
103 (defface murk-faded
104   '((t :inherit shadow))
105   "Face used for faded murk text.")
106
107 (defface murk-timestamp
108   '((t :inherit shadow))
109   "Face used for timestamps.")
110
111 (defface murk-error
112   '((t :inherit error))
113   "Face used for murk error text.")
114
115 (defface murk-notice
116   '((t :inherit warning))
117   "Face used for murk notice text.")
118
119
120 ;;; Global variables
121 ;;
122
123 (defvar murk-version "Murk v0.0"
124   "Value of this string is used in response to CTCP version queries.")
125
126 (defvar murk-notice-prefix "-!-")
127 (defvar murk-error-prefix "!!!")
128 (defvar murk-prompt-string ">")
129
130 (defvar murk-debug nil
131   "If non-nil, enable debug mode.")
132
133
134 ;;; Utility procedures
135 ;;
136
137 (defun murk--filtered-join (&rest args)
138   (string-join (seq-filter (lambda (el) el) args) " "))
139
140 (defun murk--as-string (obj)
141   (if obj
142       (with-output-to-string (princ obj))
143     nil))
144
145
146 ;;; Network processes
147 ;;
148
149 (defvar murk-connection-table nil
150   "An alist associating networks to connection information.
151 This includes the process and the response string.")
152
153 (defun murk-connection-process (network)
154   (elt (assoc network murk-connection-table) 1))
155
156 (defun murk-connection-nick (network)
157   (elt (assoc network murk-connection-table) 2))
158
159 (defun murk-set-connection-nick (network nick)
160   (setf (elt (assoc network murk-connection-table) 2) nick))
161
162 (defun murk-connection-response (network)
163   (elt (assoc network murk-connection-table) 3))
164
165 (defun murk-set-connection-response (network string)
166   (setf (elt (assoc network murk-connection-table) 3) string))
167
168 (defun murk-connection-new (network process nick)
169   (add-to-list 'murk-connection-table
170                (list network process nick "")))
171
172 (defun murk-connection-remove (network)
173   (setq murk-connection-table
174         (seq-remove (lambda (row) (equal (car row) network))
175                     murk-connection-table)))
176
177 (defun murk-make-network-filter (network)
178   (lambda (_proc string)
179     (dolist (line (split-string (concat (murk-connection-response network) string)
180                                 "\n"))
181       (if (string-suffix-p "\r" line)
182           (murk-eval-msg-string network (string-trim line))
183         (murk-set-connection-response network line)))))
184
185 (defun murk-make-network-sentinel (network)
186   (lambda (_proc string)
187     (unless (equal "open" (string-trim string))
188       (murk-display-error "Disconnected from network.")
189       (murk-connection-remove network)
190       (murk-remove-network-contexts network)
191       (murk-highlight-current-context)
192       (murk-render-prompt))))
193
194 (defun murk-start-process (network)
195   (let* ((row (assoc network murk-networks))
196          (host (elt row 1))
197          (port (elt row 2))
198          (flags (seq-drop row 3)))
199     (make-network-process :name (concat "murk-" network)
200                           :host host
201                           :service port
202                           :family nil
203                           :filter (murk-make-network-filter network)
204                           :sentinel (murk-make-network-sentinel network)
205                           :nowait nil
206                           :tls-parameters (if (memq :notls flags)
207                                               nil
208                                             (cons 'gnutls-x509pki
209                                                   (gnutls-boot-parameters
210                                                    :type 'gnutls-x509pki
211                                                    :hostname host)))
212                           :buffer "*murk*")))
213
214 (defvar murk-ping-period 60)
215
216 ;; IDEA: Have a single ping timer which pings all connected hosts
217
218 (defun murk-connect (network)
219   (if (assoc network murk-connection-table)
220       (murk-display-error "Already connected to this network")
221     (if (not (assoc network murk-networks))
222         (murk-display-error "Network '" network "' is unknown.")
223       (let ((proc (murk-start-process network)))
224         (murk-connection-new network proc murk-default-nick))
225       (murk-send-msg network (murk-msg nil nil "USER" murk-default-nick 0 "*" murk-default-nick))
226       (murk-send-msg network (murk-msg nil nil "NICK" murk-default-nick))
227       (murk-add-context (list network))
228       (murk-highlight-current-context)
229       (murk-render-prompt))))
230
231 (defun murk-send-msg (network msg)
232   (if murk-debug
233       (murk-display-string nil nil (murk-msg->string msg)))
234   (let ((proc (murk-connection-process network)))
235     (if (and proc (eq (process-status proc) 'open))
236         (process-send-string proc (concat (murk-msg->string msg) "\r\n"))
237       (murk-display-error "No network connection established"))))
238
239
240 ;;; network messages
241 ;;
242
243 (defun murk-msg (tags src cmd &rest params)
244   (list (murk--as-string tags)
245         (murk--as-string src)
246         (upcase (murk--as-string cmd))
247         (mapcar #'murk--as-string
248                 (if (and params (listp (elt params 0)))
249                     (elt params 0)
250                   params))))
251
252 (defun murk-msg-tags (msg) (elt msg 0))
253 (defun murk-msg-src (msg) (elt msg 1))
254 (defun murk-msg-cmd (msg) (elt msg 2))
255 (defun murk-msg-params (msg) (elt msg 3))
256 (defun murk-msg-trail (msg)
257   (let ((params (murk-msg-params msg)))
258     (if params
259         (elt params (- (length params) 1)))))
260
261 (defvar murk-msg-regex
262   (rx
263    (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
264         (* whitespace))
265    (opt (: ":" (: (group (* (not (any space "!" "@"))))
266                   (* (not (any space)))))
267         (* whitespace))
268    (group (: (* (not whitespace))))
269    (* whitespace)
270    (opt (group (+ not-newline))))
271   "Regex used to parse IRC messages.
272 Note that this regex is incomplete.  Noteably, we discard the non-nick
273 portion of the source component of the message, as mURK doesn't use this.")
274
275 (defun murk-string->msg (string)
276   (if (string-match murk-msg-regex string)
277       (let* ((tags (match-string 1 string))
278              (src (match-string 2 string))
279              (cmd (upcase (match-string 3 string)))
280              (params-str (match-string 4 string))
281              (params
282               (if params-str
283                   (let* ((idx (seq-position params-str ?:))
284                          (l (split-string (string-trim (substring params-str 0 idx))))
285                          (r (if idx (list (substring params-str (+ 1 idx))) nil)))
286                     (append l r))
287                 nil)))
288         (apply #'murk-msg (append (list tags src cmd) params)))
289     (error "Failed to parse string %s" string)))
290
291 (defun murk-msg->string (msg)
292   (let ((tags (murk-msg-tags msg))
293         (src (murk-msg-src msg))
294         (cmd (murk-msg-cmd msg))
295         (params (murk-msg-params msg)))
296     (murk--filtered-join
297      (if tags (concat "@" tags) nil)
298      (if src (concat ":" src) nil)
299      cmd
300      (if (> (length params) 1)
301          (string-join (seq-take params (- (length params) 1)) " ")
302        nil)
303      (if (> (length params) 0)
304          (concat ":" (elt params (- (length params) 1)))
305        nil))))
306
307
308 ;;; Contexts
309 ;;
310
311 ;; A context is a list (network channel users) identifying the network
312 ;; and channel.  The tail of the list contains the nicks of users
313 ;; present in the channel.
314 ;;
315 ;; Each network has a special context (network) used for messages
316 ;; to/from the network itself.
317
318 (defvar murk-contexts nil
319   "List of currently-available contexts.
320 The head of this list is always the current context.")
321
322 (defun murk-current-context ()
323   "Return the current context."
324   (if murk-contexts
325       (car murk-contexts)
326     nil))
327
328 (defun murk-contexts-equal (c1 c2)
329   (if (murk-network-context-p c1)
330       (and (murk-network-context-p c2)
331            (equal (murk-context-network c1)
332                   (murk-context-network c2)))
333     (and (not (murk-network-context-p c2))
334          (equal (seq-take c1 2)
335                 (seq-take c2 2)))))
336
337 (defun murk-context-network (ctx)
338   (elt ctx 0))
339
340 (defun murk-context-channel (ctx)
341   (elt ctx 1))
342
343 (defun murk-context-users (ctx)
344   (elt ctx 2))
345
346 (defun murk-set-context-users (ctx users)
347   (setcar (cddr ctx) users))
348
349 (defun murk-network-context-p (ctx)
350   (not (cdr ctx)))
351
352 (defun murk-add-context (ctx)
353   (add-to-list 'murk-contexts ctx))
354
355 (defun murk-remove-context (ctx)
356   (setq murk-contexts
357         (seq-remove
358          (lambda (this-ctx)
359            (murk-contexts-equal this-ctx ctx))
360          murk-contexts)))
361
362 (defun murk-remove-network-contexts (network)
363   (setq murk-contexts
364         (seq-remove (lambda (row) (equal (car row) network))
365                     murk-contexts)))
366
367 (defun murk-context->string (ctx)
368   (concat
369    (if (murk-network-context-p ctx)
370        ""
371      (concat (murk-context-channel ctx) "@"))
372    (murk-context-network ctx)))
373
374 (defun murk-string->context (string)
375   (if (not (string-prefix-p "#" string))
376       (murk-get-context string)
377     (let* ((parts (string-split string "@"))
378            (channel (elt parts 0))
379            (network (elt parts 1)))
380       (murk-get-context network channel))))
381
382 (defun murk-get-context (network &optional channel)
383   (if (and channel (string-prefix-p "#" channel))
384       (let ((test-ctx (list network channel)))
385         (seq-find (lambda (ctx)
386                     (equal (seq-take ctx 2) test-ctx))
387                   murk-contexts))
388     (car (member (list network) murk-contexts))))
389
390 (defun murk-cycle-contexts (&optional reverse)
391   (setq murk-contexts
392         (if reverse
393             (let ((nminus1 (- (length murk-contexts) 1)))
394               (cons
395                (elt murk-contexts nminus1)
396                (seq-take murk-contexts nminus1)))
397           (append (cdr murk-contexts) (list (car murk-contexts))))))
398
399 (defun murk-switch-to-context (ctx)
400   (setq murk-contexts
401         (let* ((new-head (memq ctx murk-contexts))
402                (new-tail (take (- (length murk-contexts)
403                                   (length new-head))
404                                murk-contexts)))
405           (append new-head new-tail))))
406
407 (defun murk-add-context-users (ctx users)
408   (murk-set-context-users
409    ctx
410    (cl-union users (murk-context-users ctx))))
411
412 (defun murk-del-context-user (ctx user)
413   (murk-set-context-users
414    ctx
415    (delete user (murk-context-users ctx))))
416
417 (defun murk-del-network-user (network user)
418   (dolist (ctx murk-contexts)
419     (if (and (equal (murk-context-network ctx) network)
420              (not (murk-network-context-p ctx)))
421         (murk-del-context-user ctx user))))
422
423 (defun murk-rename-network-user (network old-nick new-nick)
424   (dolist (ctx murk-contexts)
425     (when (and (equal (murk-context-network ctx) network)
426                (member old-nick (murk-context-users ctx)))
427       (murk-del-context-user ctx old-nick)
428       (murk-add-context-users ctx (list new-nick)))))
429
430 ;;; Buffer
431 ;;
432
433 (defvar murk-prompt-marker nil
434   "Marker for prompt position in murk buffer.")
435
436 (defvar murk-input-marker nil
437   "Marker for prompt position in murk buffer.")
438
439 (defun murk-render-prompt ()
440   (with-current-buffer "*murk*"
441     (let ((update-point (= murk-input-marker (point)))
442           (update-window-points (mapcar (lambda (w)
443                                           (list (= (window-point w) murk-input-marker)
444                                                 w))
445                                         (get-buffer-window-list nil nil t))))
446       (save-excursion
447         (set-marker-insertion-type murk-prompt-marker nil)
448         (set-marker-insertion-type murk-input-marker t)
449         (let ((inhibit-read-only t))
450           (delete-region murk-prompt-marker murk-input-marker)
451           (goto-char murk-prompt-marker)
452           (insert
453            (propertize (let ((ctx (murk-current-context)))
454                          (if ctx
455                              (murk-context->string ctx)
456                            ""))
457                        'face 'murk-context
458                        'read-only t)
459            (propertize murk-prompt-string
460                        'face 'murk-prompt
461                        'read-only t)
462            (propertize " " ; Need this to be separate to mark it as rear-nonsticky
463                        'read-only t
464                        'rear-nonsticky t)))
465         (set-marker-insertion-type murk-input-marker nil))
466       (if update-point
467           (goto-char murk-input-marker))
468       (dolist (v update-window-points)
469         (if (car v)
470             (set-window-point (cadr v) murk-input-marker))))))
471   
472 (defun murk-setup-header ()
473   (with-current-buffer "*murk*"
474     (setq-local header-line-format
475                 '((:eval
476                    (let* ((ctx (murk-current-context)))
477                      (if ctx
478                          (let ((network (murk-context-network ctx)))
479                            (concat
480                             "Network: " network ", "
481                             (if (murk-network-context-p ctx)
482                                 "network"
483                               (concat
484                                "Channel: "
485                                (murk-context-channel ctx)
486                                " ("
487                                (number-to-string
488                                 (length (murk-context-users ctx)))
489                                ")"))))
490                        "No connection")))))))
491
492 (defun murk-setup-buffer ()
493   (with-current-buffer (get-buffer-create "*murk*")
494     (setq-local scroll-conservatively 1)
495     (setq-local buffer-invisibility-spec nil)
496     (if (markerp murk-prompt-marker)
497         (set-marker murk-prompt-marker (point-max))
498       (setq murk-prompt-marker (point-max-marker)))
499     (if (markerp murk-input-marker)
500         (set-marker murk-input-marker (point-max))
501       (setq murk-input-marker (point-max-marker)))
502     (goto-char (point-max))
503     (murk-highlight-current-context)
504     (murk-render-prompt)
505     (if murk-display-header
506         (murk-setup-header))))
507
508 (defun murk-clear-buffer ()
509   "Completely erase all non-prompt and non-input text from murk buffer."
510   (with-current-buffer "*murk*"
511     (let ((inhibit-read-only t))
512       (delete-region (point-min) murk-prompt-marker))))
513
514
515 ;;; Output formatting and highlighting
516 ;;
517
518 ;; Idea: the face text property can be a list of faces, applied in
519 ;; order.  By assigning each context a unique list and keeping track
520 ;; of these in a hash table, we can easily switch the face
521 ;; corresponding to a particular context by modifying the elements of
522 ;; this list.
523 ;;
524 ;; More subtly, we make only the cdrs of this list shared among
525 ;; all text of a given context, allowing the cars to be different
526 ;; and for different elements of the context-specific text to have
527 ;; different styling.
528
529 ;; Additionally, we allow selective hiding of contexts via
530 ;; the buffer-invisibility-spec.
531
532 (defvar murk-context-facelists (make-hash-table :test 'equal)
533   "List of seen contexts and associated face lists.")
534
535 (defun murk-get-context-facelist (context)
536   (let* ((short-ctx (take 2 context))
537          (facelist (gethash short-ctx murk-context-facelists)))
538     (unless facelist
539       (setq facelist (list 'murk-text))
540       (puthash short-ctx facelist murk-context-facelists))
541     facelist))
542
543 (defun murk--fill-strings (col indent &rest strings)
544   (with-temp-buffer
545     (setq buffer-invisibility-spec nil)
546     (let ((fill-column col)
547           (adaptive-fill-regexp (rx-to-string `(= ,indent anychar))))
548       (apply #'insert strings)
549       (fill-region (point-min) (point-max) nil t)
550       (buffer-string))))
551
552 (defun murk-display-string (context prefix &rest strings)
553   (with-current-buffer "*murk*"
554     (save-excursion
555       (goto-char murk-prompt-marker)
556       (let* ((inhibit-read-only t)
557              (old-pos (marker-position murk-prompt-marker))
558              (padded-timestamp (concat (format-time-string "%H:%M ")))
559              (padded-prefix (if prefix (concat prefix " ") ""))
560              (short-ctx (take 2 context))
561              (context-atom (if short-ctx
562                                (intern (murk-context->string short-ctx))
563                              nil))
564              (context-face (murk-get-context-facelist short-ctx)))
565         (insert-before-markers
566          (murk--fill-strings
567           80
568           (+ (length padded-timestamp)
569              (length padded-prefix))
570           (propertize padded-timestamp
571                       'face 'murk-timestamp
572                       'read-only t
573                       'context short-ctx
574                       'invisible context-atom)
575           (propertize padded-prefix
576                       'read-only t
577                       'context short-ctx
578                       'invisible context-atom)
579           (murk-add-formatting
580            (propertize (concat (apply #'murk-buttonify-urls strings) "\n")
581                        'face context-face
582                        'read-only t
583                        'context short-ctx
584                        'invisible context-atom)))))))
585   (murk-scroll-windows-to-last-line))
586
587 (defun murk-display-message (network from to text)
588   (let ((context (if (string-prefix-p "#" to)
589                      (murk-get-context network to)
590                    (murk-get-context network))))
591     (murk-display-string
592      context
593      (propertize
594       (if (murk-network-context-p context)
595           (concat "[" from "->" to "]")
596         (concat (murk-context->string context) " <" from ">"))
597       'face (murk-get-context-facelist context))
598      text)))
599
600 (defun murk-display-action (network from to action-text)
601   (let ((context (if (string-prefix-p "#" to)
602                      (murk-get-context network to)
603                    (murk-get-context network))))
604     (murk-display-string
605      context
606      (propertize
607       (concat (murk-context->string context) " *")
608       'face (murk-get-context-facelist context))
609      from " " action-text)))
610
611 (defun murk-display-notice (context &rest notices)
612   (murk-display-string
613    context
614    (propertize murk-notice-prefix 'face 'murk-notice)
615    (apply #'concat notices)))
616
617 (defun murk-display-error (&rest messages)
618   (murk-display-string
619    nil
620    (propertize murk-error-prefix 'face 'murk-error)
621    (apply #'concat messages)))
622
623 (defun murk-highlight-current-context ()
624   (with-current-buffer "*murk*"
625     (maphash
626      (lambda (this-context facelist)
627        (if (equal (take 2 this-context) (take 2 (murk-current-context)))
628            (setcar facelist 'murk-text)
629          (setcar facelist 'murk-faded)))
630      murk-context-facelists))
631   (force-window-update "*murk*"))
632
633 (defun murk-zoom-in (context)
634   (with-current-buffer "*murk*"
635     (maphash
636      (lambda (this-context _)
637        (when this-context
638          (let ((this-context-atom
639                 (intern (murk-context->string this-context))))
640            (if (equal this-context (take 2 context))
641                (remove-from-invisibility-spec this-context-atom)
642              (add-to-invisibility-spec this-context-atom)))))
643      murk-context-facelists)
644     (force-window-update "*murk*"))
645   (murk-scroll-windows-to-last-line))
646
647 (defun murk-zoom-out ()
648   (with-current-buffer "*murk*"
649     (maphash
650      (lambda (this-context _)
651        (let ((this-context-atom
652               (if this-context
653                   (intern (murk-context->string this-context))
654                 nil)))
655          (remove-from-invisibility-spec this-context-atom)))
656      murk-context-facelists)
657     (force-window-update "*murk*"))
658   (murk-scroll-windows-to-last-line))
659
660 (defun murk--start-of-final-line ()
661   (with-current-buffer "*murk*"
662     (save-excursion
663       (goto-char (point-max))
664       (line-beginning-position))))
665
666 (defun murk-scroll-windows-to-last-line ()
667   (with-current-buffer "*murk*"
668     (dolist (window (get-buffer-window-list))
669       (if (>= (window-point window) (murk--start-of-final-line))
670           (with-selected-window window
671             (recenter -1))))))
672
673 (defconst murk-url-regex
674   (rx (:
675        (group (+ alpha))
676        "://"
677        (group (or (+ (any alnum "." "-"))
678                   (+ (any alnum ":"))))
679        (opt (group (: ":" (+ digit))))
680        (opt (group (: "/"
681                       (opt
682                        (* (any alnum "-/.,#:%=&_?~@+"))
683                        (any alnum "-/#:%=&_~@+")))))))
684   "Imperfect regex used to find URLs in plain text.")
685
686 (defun murk-click-url (button)
687   (browse-url (button-get button 'url)))
688
689 (defun murk-buttonify-urls (&rest strings)
690   "Turn substrings which look like urls in STRING into clickable buttons."
691   (with-temp-buffer
692     (apply #'insert strings)
693     (goto-char (point-min))
694     (while (re-search-forward murk-url-regex nil t)
695       (let ((url (match-string 0)))
696         (make-text-button (match-beginning 0)
697                           (match-end 0)
698                           'action #'murk-click-url
699                           'url url
700                           'follow-link t
701                           'face 'button
702                           'help-echo "Open URL in browser.")))
703     (buffer-string)))
704
705 (defun murk-add-formatting (string)
706   (with-temp-buffer
707     (insert string)
708     (goto-char (point-min))
709     (let ((bold nil)
710           (italics nil)
711           (underline nil)
712           (strikethrough nil)
713           (prev-point (point)))
714       (while (re-search-forward (rx (or (any "\x02\x1D\x1F\x1E\x0F")
715                                         (: "\x03" (* digit) (opt "," (* digit)))))
716                                 nil t)
717         (let ((beg (+ (match-beginning 0) 1)))
718           (if bold
719               (add-face-text-property prev-point beg '(:weight bold)))
720           (if italics
721               (add-face-text-property prev-point beg '(:slant italic)))
722           (if underline
723               (add-face-text-property prev-point beg '(:underline t)))
724           (if strikethrough
725               (add-face-text-property prev-point beg '(:strike-through t)))
726           (pcase (match-string 0)
727             ("\x02" (setq bold (not bold)))
728             ("\x1D" (setq italics (not italics)))
729             ("\x1F" (setq underline (not underline)))
730             ("\x1E" (setq strikethrough (not strikethrough)))
731             ("\x0F" ; Reset
732              (setq bold nil)
733              (setq italics nil)
734              (setq underline nil)
735              (setq strikethrough nil))
736             (_))
737           (delete-region (match-beginning 0) (match-end 0))
738           (setq prev-point (point)))))
739     (buffer-string)))
740
741
742 ;;; Message evaluation
743 ;;
744
745 (defun murk-eval-msg-string (network string)
746   (if murk-debug
747       (murk-display-string nil nil string))
748   (let* ((msg (murk-string->msg string)))
749     (murk-process-autoreplies network msg)
750     (pcase (murk-msg-cmd msg)
751       ("PING"
752        (murk-send-msg network
753         (murk-msg nil nil "PONG" (murk-msg-params msg))))
754
755       ("PONG")
756
757       ("001" ; RPL_WELCOME
758        (let* ((params (murk-msg-params msg))
759               (nick (elt params 0))
760               (text (string-join (seq-drop params 1) " ")))
761          (murk-set-connection-nick network nick)
762          (murk-display-notice (murk-get-context network) text))
763        (let* ((row (assoc network murk-networks))
764               (channels (if (memq :channels row)
765                             (cdr (memq :channels row))
766                           nil)))
767          (dolist (channel channels)
768            (murk-command-join (list channel)))))
769
770       ("353" ; NAMEREPLY
771        (let* ((params (murk-msg-params msg))
772               (channel (elt params 2))
773               (names (split-string (elt params 3)))
774               (ctx (murk-get-context network channel)))
775          (if ctx
776              (murk-add-context-users ctx names)
777            (murk-display-notice ctx "Users in " channel
778                                 ": " (string-join names " ")))))
779
780       ("366" ; ENDOFNAMES
781        (let* ((params (murk-msg-params msg))
782               (channel (elt params 1))
783               (ctx (murk-get-context network channel)))
784          (if ctx
785              (murk-display-notice
786               ctx
787               (murk--as-string (length (murk-context-users ctx)))
788               " users in " channel)
789            (murk-display-notice (murk-get-context network)
790                                 "End of " channel " names list."))))
791
792       ("331" ; RPL_NOTOPIC
793        (let* ((params (murk-msg-params msg))
794               (channel (elt params 1))
795               (ctx (murk-get-context network channel)))
796          (murk-display-notice ctx "No topic set.")))
797
798       ("332" ; RPL_TOPIC
799        (let* ((params (murk-msg-params msg))
800               (channel (elt params 1))
801               (topic (elt params 2))
802               (ctx (murk-get-context network channel)))
803          (murk-display-notice ctx "Topic: " topic)))
804
805       ((rx (= 3 (any digit)))
806        (murk-display-notice (murk-get-context network)
807                             (mapconcat 'identity (cdr (murk-msg-params msg)) " ")))
808
809       ((and "JOIN"
810             (guard (equal (murk-connection-nick network)
811                           (murk-msg-src msg))))
812        (let ((channel (car (murk-msg-params msg))))
813          (murk-add-context (list network channel nil))
814          (murk-display-notice (murk-current-context)
815                               "Joining channel " channel " on " network)
816          (murk-highlight-current-context)
817          (murk-render-prompt)))
818
819       ("JOIN"
820        (let* ((channel (car (murk-msg-params msg)))
821               (nick (murk-msg-src msg))
822               (ctx (murk-get-context network channel)))
823          (murk-add-context-users ctx (list nick))
824          (if murk-show-joins
825              (murk-display-notice ctx nick " joined channel " channel
826                                   " on " network))))
827
828       ((and "PART"
829             (guard (equal (murk-connection-nick network)
830                           (murk-msg-src msg))))
831        (let ((channel (car (murk-msg-params msg))))
832          (murk-display-notice (murk-current-context) "Left channel " channel)
833          (murk-remove-context (list network channel))
834          (murk-highlight-current-context)
835          (murk-render-prompt)))
836
837       ("PART"
838        (let* ((channel (car (murk-msg-params msg)))
839               (nick (murk-msg-src msg))
840               (ctx (murk-get-context network channel)))
841          (murk-del-context-user ctx nick)
842          (if murk-show-joins
843              (murk-display-notice ctx nick " left channel " channel
844                                   " on " network))))
845
846       ((and "NICK"
847             (guard (equal (murk-connection-nick network)
848                           (murk-msg-src msg))))
849        (let ((new-nick (car (murk-msg-params msg)))
850              (old-nick (murk-connection-nick network)))
851          (murk-set-connection-nick network new-nick)
852          (murk-rename-network-user network old-nick new-nick)
853          (murk-display-notice (murk-get-context network)
854                               "Nick set to " new-nick " on " network)))
855
856       ("NICK"
857        (let ((old-nick (murk-msg-src msg))
858              (new-nick (car (murk-msg-params msg))))
859          (murk-display-notice (murk-get-context network)
860                               old-nick " is now known as " new-nick
861                               " on " network)
862          (murk-rename-network-user network old-nick new-nick)))
863
864       ("TOPIC"
865        (let ((channel (car (murk-msg-params msg)))
866              (nick (murk-msg-src msg))
867              (topic (cadr (murk-msg-params msg))))
868          (murk-display-notice (murk-get-context network channel)
869                               nick " set the topic: " topic)))
870
871       ("QUIT"
872        (let ((nick (murk-msg-src msg))
873              (reason (mapconcat 'identity (murk-msg-params msg) " ")))
874          (murk-del-network-user network nick)
875          (if murk-show-joins
876              (murk-display-notice (murk-get-context network)
877                                   nick " on " network " has quit: " reason))))
878
879       ("NOTICE"
880        (let ((nick (murk-msg-src msg))
881              (channel (car (murk-msg-params msg)))
882              (text (cadr (murk-msg-params msg))))
883          (pcase text
884            ((rx (: "\01VERSION "
885                    (let version (* (not "\01")))
886                    "\01"))
887             (murk-display-notice (murk-get-context network)
888                                  "CTCP version reply from " nick ": " version))
889            (_
890             (murk-display-notice (murk-get-context network channel) text)))))
891
892       ("PRIVMSG"
893        (let* ((from (murk-msg-src msg))
894               (params (murk-msg-params msg))
895               (to (car params))
896               (text (cadr params)))
897          (pcase text
898            ("\01VERSION\01"
899             (let ((version-string (concat murk-version " - running on GNU Emacs " emacs-version)))
900               (murk-send-msg network
901                              (murk-msg nil nil "NOTICE"
902                                        (list from (concat "\01VERSION "
903                                                           version-string
904                                                           "\01")))))
905             (murk-display-notice (murk-get-context network)
906                                  "CTCP version request received from "
907                                  from " on " network))
908
909            ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
910             (murk-send-msg network (murk-msg nil nil "NOTICE" (list from ping)))
911             (murk-display-notice (murk-get-context network)
912                                  "CTCP ping received from " from " on " network))
913
914            ("\01USERINFO\01"
915             (murk-display-notice (murk-get-context network)
916                                  "CTCP userinfo request from " from
917                                  " on " network " (no response sent)"))
918
919            ("\01CLIENTINFO\01"
920             (murk-display-notice (murk-get-context network)
921                                  "CTCP clientinfo request from " from
922                                  " on " network " (no response sent)"))
923
924            ((rx (: "\01ACTION " (let action-text (* (not "\01"))) "\01"))
925             (murk-display-action network from to action-text))
926
927            (_
928             (murk-display-message network from to text)))))
929
930       (_
931        (murk-display-notice (murk-get-context network)
932                             (murk-msg->string msg))))))
933
934
935 ;;; User-defined responses
936 ;;
937
938 (defun murk--lists-equal (l1 l2)
939     (if (and l1 l2)
940         (if (or (not (and (car l1) (car l2)))
941                 (string-match (car l1) (car l2)))
942             (murk--lists-equal (cdr l1) (cdr l2))
943           nil)
944       t))
945
946 (defun murk-process-autoreply (network msg autoreply)
947   (let ((matcher (car autoreply))
948         (reply (cadr autoreply)))
949     (let ((target-network (car matcher)))
950       (when (and (or (not target-network)
951                      (and (equal network target-network)))
952                  (murk--lists-equal (cdr matcher)
953                                     (append (list (murk-msg-src msg)
954                                                   (murk-msg-cmd msg))
955                                             (murk-msg-params msg))))
956         (murk-send-msg network
957          (murk-msg nil nil (car reply) (cdr reply)))))))
958
959 (defun murk-process-autoreplies (network msg)
960   (mapc
961    (lambda (autoreply)
962      (murk-process-autoreply network msg autoreply))
963    murk-autoreply-table))
964
965
966 ;;; Commands
967 ;;
968
969 (defvar murk-command-table
970   '(("DEBUG" "Toggle debug mode on/off." murk-command-debug murk-boolean-completions)
971     ("HEADER" "Toggle display of header." murk-command-header murk-boolean-completions)
972     ("SHOWJOINS" "Toggles display of joins/parts." murk-command-showjoins murk-boolean-completions)
973     ("NETWORKS" "List known IRC networks." murk-command-networks)
974     ("CONNECT" "Connect to an IRC network." murk-command-connect murk-network-completions)
975     ("QUIT" "Disconnect from current network." murk-command-quit)
976     ("JOIN" "Join one or more channels." murk-command-join)
977     ("PART" "Leave channel." murk-command-part murk-channel-completions)
978     ("SWITCHCONTEXT" "Switch current context" murk-command-switch-context murk-context-completions)
979     ("NICK" "Change nick." murk-command-nick)
980     ("LIST" "Display details of one or more channels." murk-command-list)
981     ("TOPIC" "Set/query topic for current channel." murk-command-topic)
982     ("USERS" "List nicks of users in current channel." murk-command-users)
983     ("MSG" "Send private message to user." murk-command-msg murk-nick-completions)
984     ("ME" "Display action." murk-command-me)
985     ("VERSION" "Request version of another user's client via CTCP." murk-command-version murk-nick-completions)
986     ("CLEAR" "Clear buffer text." murk-command-clear murk-context-completions)
987     ("HELP" "Display help on client commands." murk-command-help murk-help-completions))
988   "Table of commands explicitly supported by murk.")
989
990 (defun murk-boolean-completions ()
991   '("on" "off"))
992
993 (defun murk-network-completions ()
994   (mapcar (lambda (row) (car row)) murk-networks))
995
996 (defun murk-help-completions ()
997   (mapcar (lambda (row) (car row)) murk-command-table))
998
999 (defun murk-channel-completions ()
1000   (mapcar (lambda (ctx)
1001             (murk-context->string ctx))
1002           (seq-filter (lambda (ctx)
1003                         (not (murk-network-context-p ctx)))
1004                       murk-contexts)))
1005
1006 (defun murk-context-completions ()
1007   (mapcar (lambda (ctx) (murk-context->string ctx)) murk-contexts))
1008
1009 (defun murk-command-help (params)
1010   (if params
1011       (let* ((cmd-str (upcase (car params)))
1012              (row (assoc cmd-str murk-command-table #'equal)))
1013         (if row
1014             (progn
1015               (murk-display-notice nil "Help for \x02" cmd-str "\x02:")
1016               (murk-display-notice nil "  " (elt row 1)))
1017           (murk-display-notice nil "No such (client-interpreted) command.")))
1018     (murk-display-notice nil "Client-interpreted commands:")
1019     (dolist (row murk-command-table)
1020       (murk-display-notice nil "  \x02" (elt row 0) "\x02: " (elt row 1)))
1021     (murk-display-notice nil "Use /HELP COMMAND to display information about a specific command.")))
1022
1023 (defun murk-command-debug (params)
1024   (setq murk-debug 
1025         (if params
1026             (if (equal (upcase (car params)) "ON")
1027                 t
1028               nil)
1029           (not murk-debug)))
1030   (murk-display-notice nil "Debug mode now " (if murk-debug "on" "off") "."))
1031
1032 (defun murk-command-header (params)
1033   (if
1034       (if params
1035           (equal (upcase (car params)) "ON")
1036         (not header-line-format))
1037       (progn
1038         (murk-setup-header)
1039         (murk-display-notice nil "Header enabled."))
1040     (setq-local header-line-format nil)
1041     (murk-display-notice nil "Header disabled.")))
1042
1043 (defun murk-command-showjoins (params)
1044   (setq murk-show-joins 
1045         (if params
1046             (if (equal (upcase (car params)) "ON")
1047                 t
1048               nil)
1049           (not murk-show-joins)))
1050   (murk-display-notice nil "Joins/parts will now be "
1051                        (if murk-show-joins "shown" "hidden") "."))
1052
1053 (defun murk-command-connect (params)
1054   (if params
1055       (let ((network (car params)))
1056         (murk-display-notice nil "Attempting to connect to " network "...")
1057         (murk-connect network))
1058     (murk-display-notice nil "Usage: /connect <network>")))
1059
1060 (defun murk-command-networks (_params)
1061   (murk-display-notice nil "Currently-known networks:")
1062   (dolist (row murk-networks)
1063     (seq-let (network network port &rest _others) row
1064       (murk-display-notice nil "\t" network
1065                            " [" network
1066                            " " (number-to-string port) "]")))
1067   (murk-display-notice nil "(Modify the `murk-networks' variable to add more.)"))
1068
1069 (defun murk-command-quit (params)
1070   (let ((ctx (murk-current-context)))
1071     (if (not ctx)
1072         (murk-display-error "No current network")
1073       (let ((quit-msg (if params (string-join params " ") murk-default-quit-msg)))
1074         (murk-send-msg
1075          (murk-context-network ctx)
1076          (murk-msg nil nil "QUIT" quit-msg))))))
1077
1078 (defun murk-command-join (params)
1079   (if params
1080       (let ((network (murk-context-network (murk-current-context))))
1081         (dolist (channel params)
1082           (murk-send-msg network (murk-msg nil nil "JOIN" channel))))
1083     (murk-display-notice nil "Usage: /join channel [channel2 ...]")))
1084
1085 (defun murk-command-part (params)
1086   (let ((ctx (cond
1087               ((not params) (murk-current-context))
1088               ((seq-contains (car params) "@") (murk-string->context (car params)))
1089               (t (list (murk-context-network (murk-current-context))  (car params))))))
1090     (let ((network (murk-context-network ctx))
1091           (channel (murk-context-channel ctx)))
1092       (if channel
1093           (murk-send-msg network (murk-msg nil nil "PART" channel))
1094         (murk-display-error "Specify which channel to leave")))))
1095
1096 (defun murk-command-switch-context (params)
1097   (if (not params)
1098       (murk-display-notice nil "Usage: /switchcontext #channel@network")
1099     (let ((ctx (murk-string->context (car params))))
1100       (murk-switch-to-context ctx)
1101       (murk-highlight-current-context)
1102       (murk-render-prompt)
1103       (if murk-zoomed
1104           (murk-zoom-in (murk-current-context))))))
1105
1106 (defun murk-command-nick (params)
1107   (if params
1108       (let ((new-nick (string-join params " "))
1109             (ctx (murk-current-context)))
1110         (if ctx
1111             (murk-send-msg (murk-context-network ctx)
1112                            (murk-msg nil nil "NICK" new-nick))
1113           (murk-display-error "No current connection")))
1114     (murk-display-notice nil "Usage: /nick <new-nick>")))
1115
1116 (defun murk-command-list (params)
1117   (let ((ctx (murk-current-context)))
1118     (if ctx
1119         (if (not params)
1120             (murk-display-notice nil "This command can generate lots of output."
1121                                  " Use `/LIST -yes' if you really want this,"
1122                                  " or `/LIST <channel_regexp>' to reduce the output.")
1123           (let ((network (murk-context-network ctx)))
1124             (if (equal (upcase (car params)) "-YES")
1125                 (murk-send-msg network (murk-msg nil nil "LIST"))
1126               (murk-send-msg network (murk-msg nil nil "LIST"
1127                                               (car params))))))
1128       (murk-display-error "No current connection"))))
1129
1130 (defun murk-command-topic (params)
1131   (let ((ctx (murk-current-context)))
1132     (if (and ctx (not (murk-network-context-p ctx)))
1133         (let ((network (murk-context-network ctx))
1134               (channel (murk-context-channel ctx)))
1135           (if params
1136               (murk-send-msg network
1137                              (murk-msg nil nil "TOPIC" channel
1138                                        (string-join params " ")))
1139             (murk-send-msg network
1140                            (murk-msg nil nil "TOPIC" channel))))
1141       (murk-display-notice nil "No current channel."))))
1142
1143 (defun murk-command-msg (params)
1144   (let ((network (murk-context-network (murk-current-context))))
1145     (if (and params (>= (length params) 2))
1146         (let ((to (car params))
1147               (text (string-join (cdr params) " ")))
1148           (murk-send-msg network (murk-msg nil nil "PRIVMSG" to text))
1149           (murk-display-message network
1150                                 (murk-connection-nick network)
1151                                 to text))
1152       (murk-display-notice nil "Usage: /msg <nick> <message>"))))
1153
1154 (defun murk-command-me (params)
1155   (let* ((ctx (murk-current-context))
1156          (network (murk-context-network ctx)))
1157     (if (and ctx (not (murk-network-context-p ctx)))
1158         (if params
1159             (let* ((channel (murk-context-channel ctx))
1160                    (my-nick (murk-connection-nick network))
1161                    (action (string-join params " "))
1162                    (ctcp-text (concat "\01ACTION " action "\01")))
1163               (murk-send-msg network
1164                              (murk-msg nil nil "PRIVMSG"
1165                                        (list channel ctcp-text)))
1166               (murk-display-action network my-nick channel action))
1167           (murk-display-notice nil "Usage: /me <action>"))
1168       (murk-display-notice nil "No current channel."))))
1169
1170 (defun murk-command-version (params)
1171   (let ((ctx (murk-current-context)))
1172     (if ctx
1173         (if params
1174             (let ((network (murk-context-network ctx))
1175                   (nick (car params)))
1176               (murk-send-msg network
1177                              (murk-msg nil nil "PRIVMSG"
1178                                        (list nick "\01VERSION\01")))
1179               (murk-display-notice ctx "CTCP version request sent to "
1180                                    nick " on " network))
1181           (murk-display-notice ctx "Usage: /version <nick>"))
1182       (murk-display-notice nil "No current channel."))))
1183
1184 (defun murk-command-users (_params)
1185   (let ((ctx (murk-current-context)))
1186     (if (and ctx (not (murk-network-context-p ctx)))
1187         (let ((channel (murk-context-channel ctx))
1188               (network (murk-context-network ctx))
1189               (users (murk-context-users ctx)))
1190           (murk-display-notice ctx "Users in " channel " on " network ":")
1191           (murk-display-notice ctx (string-join users " ")))
1192       (murk-display-notice nil "No current channel."))))
1193
1194
1195 ;;; Command entering
1196 ;;
1197
1198 (defun murk-enter-string (string)
1199   (if (string-prefix-p "/" string)
1200       (pcase string
1201         ((rx (: "/" (let cmd-str (+ (not whitespace)))
1202                 (opt (+ whitespace)
1203                      (let params-str (+ anychar))
1204                      string-end)))
1205          (let ((command-row (assoc (upcase  cmd-str) murk-command-table #'equal))
1206                (params (if params-str
1207                            (split-string params-str nil t)
1208                          nil)))
1209            (if (and command-row (elt command-row 2))
1210                (funcall (elt command-row 2) params)
1211              (murk-send-msg
1212               (murk-context-network (murk-current-context))
1213               (murk-msg nil nil (upcase cmd-str) params)))))
1214         (_
1215          (murk-display-error "Badly formed command")))
1216     (unless (string-empty-p string)
1217       (let ((ctx (murk-current-context)))
1218         (if ctx
1219             (if (not (murk-network-context-p ctx))
1220                 (let ((network (murk-context-network ctx))
1221                       (channel (murk-context-channel ctx)))
1222                   (murk-send-msg network
1223                                  (murk-msg nil nil "PRIVMSG" channel string))
1224                   (murk-display-message network
1225                                         (murk-connection-nick network)
1226                                         channel string))
1227               (murk-display-error "No current channel"))
1228           (murk-display-error "No current context"))))))
1229
1230
1231 ;;; Command history
1232 ;;
1233
1234 (defvar murk-history nil
1235   "Commands and messages sent in current session.")
1236
1237 (defvar murk-history-index nil)
1238
1239 (defun murk-history-cycle (delta)
1240   (when murk-history
1241     (with-current-buffer "*murk*"
1242       (if murk-history-index
1243           (setq murk-history-index
1244                 (max 0
1245                      (min (- (length murk-history) 1)
1246                           (+ delta murk-history-index))))
1247         (setq murk-history-index 0))
1248       (delete-region murk-input-marker (point-max))
1249       (insert (elt murk-history murk-history-index)))))
1250
1251
1252 ;;; Interactive commands
1253 ;;
1254
1255 (defun murk-enter ()
1256   "Enter current contents of line after prompt."
1257   (interactive)
1258   (with-current-buffer "*murk*"
1259     (let ((line (buffer-substring murk-input-marker (point-max))))
1260       (push line murk-history)
1261       (setq murk-history-index nil)
1262       (let ((inhibit-read-only t))
1263         (delete-region murk-input-marker (point-max)))
1264       (murk-enter-string line))))
1265
1266 (defun murk-history-next ()
1267   (interactive)
1268   (murk-history-cycle -1))
1269
1270 (defun murk-history-prev ()
1271   (interactive)
1272   (murk-history-cycle +1))
1273
1274 (defun murk-cycle-contexts-forward ()
1275   (interactive)
1276   (murk-cycle-contexts)
1277   (murk-highlight-current-context)
1278   (murk-render-prompt)
1279   (if murk-zoomed
1280       (murk-zoom-in (murk-current-context))))
1281
1282 (defun murk-cycle-contexts-reverse ()
1283   (interactive)
1284   (murk-cycle-contexts t)
1285   (murk-highlight-current-context)
1286   (murk-render-prompt)
1287   (if murk-zoomed
1288       (murk-zoom-in (murk-current-context))))
1289
1290 (defvar murk-zoomed nil
1291   "Keeps track of zoom status.")
1292
1293 (defun murk-toggle-zoom ()
1294   (interactive)
1295   (if murk-zoomed
1296       (murk-zoom-out)
1297     (murk-zoom-in (murk-current-context)))
1298   (setq murk-zoomed (not murk-zoomed)))
1299
1300
1301 (defun murk-complete-input ()
1302   (interactive)
1303   (let ((completion-ignore-case t))
1304     (when (>= (point) murk-input-marker)
1305       (pcase (buffer-substring murk-input-marker (point))
1306         ((rx (: "/" (let cmd-str (+ (not whitespace))) (+ " ") (* (not whitespace)) string-end))
1307          (let ((space-idx (save-excursion
1308                             (re-search-backward " " murk-input-marker t)))
1309                (table-row (assoc (upcase cmd-str) murk-command-table #'equal)))
1310            (if (and table-row (elt table-row 3))
1311                (let* ((completions-nospace (funcall (elt table-row 3)))
1312                       (completions (mapcar (lambda (el) (concat el " ")) completions-nospace)))
1313                  (completion-in-region (+ 1 space-idx) (point) completions)))))
1314         ((rx (: "/" (* (not whitespace)) string-end))
1315          (message (buffer-substring murk-input-marker (point)))
1316          (completion-in-region murk-input-marker (point)
1317                                (mapcar (lambda (row) (concat "/" (car row) " "))
1318                                        murk-command-table)))
1319         (_
1320          (let* ((end (max murk-input-marker (point)))
1321                 (space-idx (save-excursion
1322                              (re-search-backward " " murk-input-marker t)))
1323                 (start (if space-idx (+ 1 space-idx) murk-input-marker)))
1324            (unless (string-prefix-p "/" (buffer-substring start end))
1325              (let* ((users (murk-context-users (murk-current-context)))
1326                     (users-no@ (mapcar
1327                                 (lambda (u) (car (split-string u "@" t)))
1328                                 users)))
1329                (completion-in-region start end users-no@)))))))))
1330
1331 ;;; Mode
1332 ;;
1333
1334 (defvar murk-mode-map
1335   (let ((map (make-sparse-keymap)))
1336     (define-key map (kbd "RET") 'murk-enter)
1337     (define-key map (kbd "TAB") 'murk-complete-input)
1338     (define-key map (kbd "C-c C-z") 'murk-toggle-zoom)
1339     (define-key map (kbd "<C-up>") 'murk-history-prev)
1340     (define-key map (kbd "<C-down>") 'murk-history-next)
1341     (define-key map (kbd "<C-tab>") 'murk-cycle-contexts-forward)
1342     (define-key map (kbd "<C-S-iso-lefttab>") 'murk-cycle-contexts-reverse)
1343     (define-key map (kbd "<C-S-tab>") 'murk-cycle-contexts-reverse)
1344     (when (fboundp 'evil-define-key*)
1345       (evil-define-key* 'motion map
1346         (kbd "TAB") 'murk-complete-input))
1347     map))
1348
1349 (define-derived-mode murk-mode text-mode "murk"
1350   "Major mode for murk.")
1351
1352 (when (fboundp 'evil-set-initial-state)
1353   (evil-set-initial-state 'murk-mode 'insert))
1354
1355 ;;; Main start procedure
1356 ;;
1357
1358 ;;;###autoload
1359 (defun murk ()
1360   "Start murk or just switch to the murk buffer if one already exists."
1361   (interactive)
1362   (if (get-buffer "*murk*")
1363       (switch-to-buffer "*murk*")
1364     (switch-to-buffer "*murk*")
1365     (murk-mode)
1366     (murk-setup-buffer))
1367   "Started murk.")
1368
1369 ;;; murk.el ends here