Added switchcontext command
[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   (maphash
625    (lambda (this-context facelist)
626      (if (equal (take 2 this-context) (take 2 (murk-current-context)))
627          (setcar facelist 'murk-text)
628        (setcar facelist 'murk-faded)))
629    murk-context-facelists)
630   (force-window-update "*murk*"))
631
632 (defun murk-zoom-in (context)
633   (with-current-buffer "*murk*"
634     (maphash
635      (lambda (this-context _)
636        (when this-context
637          (let ((this-context-atom
638                 (intern (murk-context->string this-context))))
639            (if (equal this-context (take 2 context))
640                (remove-from-invisibility-spec this-context-atom)
641              (add-to-invisibility-spec this-context-atom)))))
642      murk-context-facelists)
643     (force-window-update "*murk*"))
644   (murk-scroll-windows-to-last-line))
645
646 (defun murk-zoom-out ()
647   (with-current-buffer "*murk*"
648     (maphash
649      (lambda (this-context _)
650        (let ((this-context-atom
651               (if this-context
652                   (intern (murk-context->string this-context))
653                 nil)))
654          (remove-from-invisibility-spec this-context-atom)))
655      murk-context-facelists)
656     (force-window-update "*murk*"))
657   (murk-scroll-windows-to-last-line))
658
659 (defun murk--start-of-final-line ()
660   (with-current-buffer "*murk*"
661     (save-excursion
662       (goto-char (point-max))
663       (line-beginning-position))))
664
665 (defun murk-scroll-windows-to-last-line ()
666   (with-current-buffer "*murk*"
667     (dolist (window (get-buffer-window-list))
668       (if (>= (window-point window) (murk--start-of-final-line))
669           (with-selected-window window
670             (recenter -1))))))
671
672 (defconst murk-url-regex
673   (rx (:
674        (group (+ alpha))
675        "://"
676        (group (or (+ (any alnum "." "-"))
677                   (+ (any alnum ":"))))
678        (opt (group (: ":" (+ digit))))
679        (opt (group (: "/"
680                       (opt
681                        (* (any alnum "-/.,#:%=&_?~@+"))
682                        (any alnum "-/#:%=&_~@+")))))))
683   "Imperfect regex used to find URLs in plain text.")
684
685 (defun murk-click-url (button)
686   (browse-url (button-get button 'url)))
687
688 (defun murk-buttonify-urls (&rest strings)
689   "Turn substrings which look like urls in STRING into clickable buttons."
690   (with-temp-buffer
691     (apply #'insert strings)
692     (goto-char (point-min))
693     (while (re-search-forward murk-url-regex nil t)
694       (let ((url (match-string 0)))
695         (make-text-button (match-beginning 0)
696                           (match-end 0)
697                           'action #'murk-click-url
698                           'url url
699                           'follow-link t
700                           'face 'button
701                           'help-echo "Open URL in browser.")))
702     (buffer-string)))
703
704 (defun murk-add-formatting (string)
705   (with-temp-buffer
706     (insert string)
707     (goto-char (point-min))
708     (let ((bold nil)
709           (italics nil)
710           (underline nil)
711           (strikethrough nil)
712           (prev-point (point)))
713       (while (re-search-forward (rx (or (any "\x02\x1D\x1F\x1E\x0F")
714                                         (: "\x03" (* digit) (opt "," (* digit)))))
715                                 nil t)
716         (let ((beg (+ (match-beginning 0) 1)))
717           (if bold
718               (add-face-text-property prev-point beg '(:weight bold)))
719           (if italics
720               (add-face-text-property prev-point beg '(:slant italic)))
721           (if underline
722               (add-face-text-property prev-point beg '(:underline t)))
723           (if strikethrough
724               (add-face-text-property prev-point beg '(:strike-through t)))
725           (pcase (match-string 0)
726             ("\x02" (setq bold (not bold)))
727             ("\x1D" (setq italics (not italics)))
728             ("\x1F" (setq underline (not underline)))
729             ("\x1E" (setq strikethrough (not strikethrough)))
730             ("\x0F" ; Reset
731              (setq bold nil)
732              (setq italics nil)
733              (setq underline nil)
734              (setq strikethrough nil))
735             (_))
736           (delete-region (match-beginning 0) (match-end 0))
737           (setq prev-point (point)))))
738     (buffer-string)))
739
740
741 ;;; Message evaluation
742 ;;
743
744 (defun murk-eval-msg-string (network string)
745   (if murk-debug
746       (murk-display-string nil nil string))
747   (let* ((msg (murk-string->msg string)))
748     (murk-process-autoreplies network msg)
749     (pcase (murk-msg-cmd msg)
750       ("PING"
751        (murk-send-msg network
752         (murk-msg nil nil "PONG" (murk-msg-params msg))))
753
754       ("PONG")
755
756       ("001" ; RPL_WELCOME
757        (let* ((params (murk-msg-params msg))
758               (nick (elt params 0))
759               (text (string-join (seq-drop params 1) " ")))
760          (murk-set-connection-nick network nick)
761          (murk-display-notice (murk-get-context network) text)))
762
763       ("353" ; NAMEREPLY
764        (let* ((params (murk-msg-params msg))
765               (channel (elt params 2))
766               (names (split-string (elt params 3)))
767               (ctx (murk-get-context network channel)))
768          (if ctx
769              (murk-add-context-users ctx names)
770            (murk-display-notice nil "Users in " channel
771                                 ": " (string-join names " ")))))
772
773       ("366" ; ENDOFNAMES
774        (let* ((params (murk-msg-params msg))
775               (channel (elt params 1))
776               (ctx (murk-get-context network channel)))
777          (if ctx
778              (murk-display-notice
779               ctx
780               (murk--as-string (length (murk-context-users ctx)))
781               " users in " channel)
782            (murk-display-notice nil "End of " channel " names list."))))
783
784       ("331" ; RPL_NOTOPIC
785        (let* ((params (murk-msg-params msg))
786               (channel (elt params 1))
787               (ctx (murk-get-context network channel)))
788          (murk-display-notice ctx "No topic set.")))
789
790       ("332" ; RPL_TOPIC
791        (let* ((params (murk-msg-params msg))
792               (channel (elt params 1))
793               (topic (elt params 2))
794               (ctx (murk-get-context network channel)))
795          (murk-display-notice ctx "Topic: " topic)))
796
797       ((rx (= 3 (any digit)))
798        (murk-display-notice (murk-get-context network)
799                             (mapconcat 'identity (cdr (murk-msg-params msg)) " ")))
800
801       ((and "JOIN"
802             (guard (equal (murk-connection-nick network)
803                           (murk-msg-src msg))))
804        (let ((channel (car (murk-msg-params msg))))
805          (murk-add-context (list network channel nil))
806          (murk-display-notice (murk-current-context)
807                               "Joining channel " channel " on " network)
808          (murk-highlight-current-context)
809          (murk-render-prompt)))
810
811       ("JOIN"
812        (let* ((channel (car (murk-msg-params msg)))
813               (nick (murk-msg-src msg))
814               (ctx (murk-get-context network channel)))
815          (murk-add-context-users ctx (list nick))
816          (if murk-show-joins
817              (murk-display-notice ctx nick " joined channel " channel
818                                   " on " network))))
819
820       ((and "PART"
821             (guard (equal (murk-connection-nick network)
822                           (murk-msg-src msg))))
823        (let ((channel (car (murk-msg-params msg))))
824          (murk-display-notice (murk-current-context) "Left channel " channel)
825          (murk-remove-context (list network channel))
826          (murk-highlight-current-context)
827          (murk-render-prompt)))
828
829       ("PART"
830        (let* ((channel (car (murk-msg-params msg)))
831               (nick (murk-msg-src msg))
832               (ctx (murk-get-context network channel)))
833          (murk-del-context-user ctx nick)
834          (if murk-show-joins
835              (murk-display-notice ctx nick " left channel " channel
836                                   " on " network))))
837
838       ((and "NICK"
839             (guard (equal (murk-connection-nick network)
840                           (murk-msg-src msg))))
841        (let ((new-nick (car (murk-msg-params msg)))
842              (old-nick (murk-connection-nick network)))
843          (murk-set-connection-nick network new-nick)
844          (murk-rename-network-user network old-nick new-nick)
845          (murk-display-notice (murk-get-context network)
846                               "Nick set to " new-nick " on " network)))
847
848       ("NICK"
849        (let ((old-nick (murk-msg-src msg))
850              (new-nick (car (murk-msg-params msg))))
851          (murk-display-notice nil old-nick " is now known as " new-nick
852                               " on " network)
853          (murk-rename-network-user network old-nick new-nick)))
854
855       ("TOPIC"
856        (let ((channel (car (murk-msg-params msg)))
857              (nick (murk-msg-src msg))
858              (topic (cadr (murk-msg-params msg))))
859          (murk-display-notice (murk-get-context network channel)
860                               nick " set the topic: " topic)))
861
862       ("QUIT"
863        (let ((nick (murk-msg-src msg))
864              (reason (mapconcat 'identity (murk-msg-params msg) " ")))
865          (murk-del-network-user network nick)
866          (if murk-show-joins
867              (murk-display-notice (murk-get-context network)
868                                   nick " on " network " has quit: " reason))))
869
870       ("NOTICE"
871        (let ((nick (murk-msg-src msg))
872              (channel (car (murk-msg-params msg)))
873              (text (cadr (murk-msg-params msg))))
874          (pcase text
875            ((rx (: "\01VERSION "
876                    (let version (* (not "\01")))
877                    "\01"))
878             (murk-display-notice (murk-get-context network)
879                                  "CTCP version reply from " nick ": " version))
880            (_
881             (murk-display-notice (murk-get-context network channel) text)))))
882
883       ("PRIVMSG"
884        (let* ((from (murk-msg-src msg))
885               (params (murk-msg-params msg))
886               (to (car params))
887               (text (cadr params)))
888          (pcase text
889            ("\01VERSION\01"
890             (let ((version-string (concat murk-version " - running on GNU Emacs " emacs-version)))
891               (murk-send-msg network
892                              (murk-msg nil nil "NOTICE"
893                                        (list from (concat "\01VERSION "
894                                                           version-string
895                                                           "\01")))))
896             (murk-display-notice (murk-get-context network)
897                                  "CTCP version request received from "
898                                  from " on " network))
899
900            ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
901             (murk-send-msg network (murk-msg nil nil "NOTICE" (list from ping)))
902             (murk-display-notice (murk-get-context network)
903                                  "CTCP ping received from " from " on " network))
904
905            ("\01USERINFO\01"
906             (murk-display-notice (murk-get-context network)
907                                  "CTCP userinfo request from " from
908                                  " on " network " (no response sent)"))
909
910            ("\01CLIENTINFO\01"
911             (murk-display-notice (murk-get-context network)
912                                  "CTCP clientinfo request from " from
913                                  " on " network " (no response sent)"))
914
915            ((rx (: "\01ACTION " (let action-text (* (not "\01"))) "\01"))
916             (murk-display-action network from to action-text))
917
918            (_
919             (murk-display-message network from to text)))))
920
921       (_
922        (murk-display-notice (murk-get-context network)
923                             (murk-msg->string msg))))))
924
925
926 ;;; User-defined responses
927 ;;
928
929 (defun murk--lists-equal (l1 l2)
930     (if (and l1 l2)
931         (if (or (not (and (car l1) (car l2)))
932                 (string-match (car l1) (car l2)))
933             (murk--lists-equal (cdr l1) (cdr l2))
934           nil)
935       t))
936
937 (defun murk-process-autoreply (network msg autoreply)
938   (let ((matcher (car autoreply))
939         (reply (cadr autoreply)))
940     (let ((target-network (car matcher)))
941       (when (and (or (not target-network)
942                      (and (equal network target-network)))
943                  (murk--lists-equal (cdr matcher)
944                                     (append (list (murk-msg-src msg)
945                                                   (murk-msg-cmd msg))
946                                             (murk-msg-params msg))))
947         (murk-send-msg network
948          (murk-msg nil nil (car reply) (cdr reply)))))))
949
950 (defun murk-process-autoreplies (network msg)
951   (mapc
952    (lambda (autoreply)
953      (murk-process-autoreply network msg autoreply))
954    murk-autoreply-table))
955
956
957 ;;; Commands
958 ;;
959
960 (defvar murk-command-table
961   '(("DEBUG" "Toggle debug mode on/off." murk-command-debug murk-boolean-completions)
962     ("HEADER" "Toggle display of header." murk-command-header murk-boolean-completions)
963     ("SHOWJOINS" "Toggles display of joins/parts." murk-command-showjoins murk-boolean-completions)
964     ("NETWORKS" "List known IRC networks." murk-command-networks)
965     ("CONNECT" "Connect to an IRC network." murk-command-connect murk-network-completions)
966     ("QUIT" "Disconnect from current network." murk-command-quit)
967     ("JOIN" "Join one or more channels." murk-command-join)
968     ("PART" "Leave channel." murk-command-part murk-channel-completions)
969     ("SWITCHCONTEXT" "Switch current context" murk-command-switch-context murk-context-completions)
970     ("NICK" "Change nick." murk-command-nick)
971     ("LIST" "Display details of one or more channels." murk-command-list)
972     ("TOPIC" "Set/query topic for current channel." murk-command-topic)
973     ("USERS" "List nicks of users in current channel." murk-command-users)
974     ("MSG" "Send private message to user." murk-command-msg murk-nick-completions)
975     ("ME" "Display action." murk-command-me)
976     ("VERSION" "Request version of another user's client via CTCP." murk-command-version murk-nick-completions)
977     ("CLEAR" "Clear buffer text." murk-command-clear murk-context-completions)
978     ("HELP" "Display help on client commands." murk-command-help murk-help-completions))
979   "Table of commands explicitly supported by murk.")
980
981 (defun murk-boolean-completions ()
982   '("on" "off"))
983
984 (defun murk-network-completions ()
985   (mapcar (lambda (row) (car row)) murk-networks))
986
987 (defun murk-help-completions ()
988   (mapcar (lambda (row) (car row)) murk-command-table))
989
990 (defun murk-channel-completions ()
991   (mapcar (lambda (ctx)
992             (murk-context->string ctx))
993           (seq-filter (lambda (ctx)
994                         (not (murk-network-context-p ctx)))
995                       murk-contexts)))
996
997 (defun murk-context-completions ()
998   (mapcar (lambda (ctx) (murk-context->string ctx)) murk-contexts))
999
1000 (defun murk-command-help (params)
1001   (if params
1002       (let* ((cmd-str (upcase (car params)))
1003              (row (assoc cmd-str murk-command-table #'equal)))
1004         (if row
1005             (progn
1006               (murk-display-notice nil "Help for \x02" cmd-str "\x02:")
1007               (murk-display-notice nil "  " (elt row 1)))
1008           (murk-display-notice nil "No such (client-interpreted) command.")))
1009     (murk-display-notice nil "Client-interpreted commands:")
1010     (dolist (row murk-command-table)
1011       (murk-display-notice nil "  \x02" (elt row 0) "\x02: " (elt row 1)))
1012     (murk-display-notice nil "Use /HELP COMMAND to display information about a specific command.")))
1013
1014 (defun murk-command-debug (params)
1015   (setq murk-debug 
1016         (if params
1017             (if (equal (upcase (car params)) "ON")
1018                 t
1019               nil)
1020           (not murk-debug)))
1021   (murk-display-notice nil "Debug mode now " (if murk-debug "on" "off") "."))
1022
1023 (defun murk-command-header (params)
1024   (if
1025       (if params
1026           (equal (upcase (car params)) "ON")
1027         (not header-line-format))
1028       (progn
1029         (murk-setup-header)
1030         (murk-display-notice nil "Header enabled."))
1031     (setq-local header-line-format nil)
1032     (murk-display-notice nil "Header disabled.")))
1033
1034 (defun murk-command-showjoins (params)
1035   (setq murk-show-joins 
1036         (if params
1037             (if (equal (upcase (car params)) "ON")
1038                 t
1039               nil)
1040           (not murk-show-joins)))
1041   (murk-display-notice nil "Joins/parts will now be "
1042                        (if murk-show-joins "shown" "hidden") "."))
1043
1044 (defun murk-command-connect (params)
1045   (if params
1046       (let ((network (car params)))
1047         (murk-display-notice nil "Attempting to connect to " network "...")
1048         (murk-connect network))
1049     (murk-display-notice nil "Usage: /connect <network>")))
1050
1051 (defun murk-command-networks (_params)
1052   (murk-display-notice nil "Currently-known networks:")
1053   (dolist (row murk-networks)
1054     (seq-let (network network port &rest _others) row
1055       (murk-display-notice nil "\t" network
1056                            " [" network
1057                            " " (number-to-string port) "]")))
1058   (murk-display-notice nil "(Modify the `murk-networks' variable to add more.)"))
1059
1060 (defun murk-command-quit (params)
1061   (let ((ctx (murk-current-context)))
1062     (if (not ctx)
1063         (murk-display-error "No current network")
1064       (let ((quit-msg (if params (string-join params " ") murk-default-quit-msg)))
1065         (murk-send-msg
1066          (murk-context-network ctx)
1067          (murk-msg nil nil "QUIT" quit-msg))))))
1068
1069 (defun murk-command-join (params)
1070   (if params
1071       (let ((network (murk-context-network (murk-current-context))))
1072         (dolist (channel params)
1073           (murk-send-msg network (murk-msg nil nil "JOIN" channel))))
1074     (murk-display-notice nil "Usage: /join channel [channel2 ...]")))
1075
1076 (defun murk-command-part (params)
1077   (let ((ctx (cond
1078               ((not params) (murk-current-context))
1079               ((seq-contains (car params) "@") (murk-string->context (car params)))
1080               (t (list (murk-context-network (murk-current-context))  (car params))))))
1081     (let ((network (murk-context-network ctx))
1082           (channel (murk-context-channel ctx)))
1083       (if channel
1084           (murk-send-msg network (murk-msg nil nil "PART" channel))
1085         (murk-display-error "Specify which channel to leave")))))
1086
1087 (defun murk-command-switch-context (params)
1088   (if (not params)
1089       (murk-display-notice nil "Usage: /switchcontext #channel@network")
1090     (let ((ctx (murk-string->context (car params))))
1091       (murk-switch-to-context ctx)
1092       (murk-highlight-current-context)
1093       (murk-render-prompt)
1094       (if murk-zoomed
1095           (murk-zoom-in (murk-current-context))))))
1096
1097 (defun murk-command-nick (params)
1098   (if params
1099       (let ((new-nick (string-join params " "))
1100             (ctx (murk-current-context)))
1101         (if ctx
1102             (murk-send-msg (murk-context-network ctx)
1103                            (murk-msg nil nil "NICK" new-nick))
1104           (murk-display-error "No current connection")))
1105     (murk-display-notice nil "Usage: /nick <new-nick>")))
1106
1107 (defun murk-command-list (params)
1108   (let ((ctx (murk-current-context)))
1109     (if ctx
1110         (if (not params)
1111             (murk-display-notice nil "This command can generate lots of output. Use `/LIST -yes' if you really want this, or `/LIST <channel_regexp>' to reduce the output.")
1112           (let ((network (murk-context-network ctx)))
1113             (if (equal (upcase (car params)) "-YES")
1114                 (murk-send-msg network (murk-msg nil nil "LIST"))
1115               (murk-send-msg network (murk-msg nil nil "LIST"
1116                                               (car params))))))
1117       (murk-display-error "No current connection"))))
1118
1119 (defun murk-command-topic (params)
1120   (let ((ctx (murk-current-context)))
1121     (if (and ctx (not (murk-network-context-p ctx)))
1122         (let ((network (murk-context-network ctx))
1123               (channel (murk-context-channel ctx)))
1124           (if params
1125               (murk-send-msg network
1126                              (murk-msg nil nil "TOPIC" channel
1127                                        (string-join params " ")))
1128             (murk-send-msg network
1129                            (murk-msg nil nil "TOPIC" channel))))
1130       (murk-display-notice nil "No current channel."))))
1131
1132 (defun murk-command-msg (params)
1133   (let ((network (murk-context-network (murk-current-context))))
1134     (if (and params (>= (length params) 2))
1135         (let ((to (car params))
1136               (text (string-join (cdr params) " ")))
1137           (murk-send-msg network (murk-msg nil nil "PRIVMSG" to text))
1138           (murk-display-message network
1139                                 (murk-connection-nick network)
1140                                 to text))
1141       (murk-display-notice nil "Usage: /msg <nick> <message>"))))
1142
1143 (defun murk-command-me (params)
1144   (let* ((ctx (murk-current-context))
1145          (network (murk-context-network ctx)))
1146     (if (and ctx (not (murk-network-context-p ctx)))
1147         (if params
1148             (let* ((channel (murk-context-channel ctx))
1149                    (my-nick (murk-connection-nick network))
1150                    (action (string-join params " "))
1151                    (ctcp-text (concat "\01ACTION " action "\01")))
1152               (murk-send-msg network
1153                              (murk-msg nil nil "PRIVMSG"
1154                                        (list channel ctcp-text)))
1155               (murk-display-action network my-nick channel action))
1156           (murk-display-notice nil "Usage: /me <action>"))
1157       (murk-display-notice nil "No current channel."))))
1158
1159 (defun murk-command-version (params)
1160   (let ((ctx (murk-current-context)))
1161     (if ctx
1162         (if params
1163             (let ((network (murk-context-network ctx))
1164                   (nick (car params)))
1165               (murk-send-msg network
1166                              (murk-msg nil nil "PRIVMSG"
1167                                        (list nick "\01VERSION\01")))
1168               (murk-display-notice ctx "CTCP version request sent to "
1169                                    nick " on " network))
1170           (murk-display-notice ctx "Usage: /version <nick>"))
1171       (murk-display-notice nil "No current channel."))))
1172
1173 (defun murk-command-users (_params)
1174   (let ((ctx (murk-current-context)))
1175     (if (and ctx (not (murk-network-context-p ctx)))
1176         (let ((channel (murk-context-channel ctx))
1177               (network (murk-context-network ctx))
1178               (users (murk-context-users ctx)))
1179           (murk-display-notice ctx "Users in " channel " on " network ":")
1180           (murk-display-notice ctx (string-join users " ")))
1181       (murk-display-notice nil "No current channel."))))
1182
1183
1184 ;;; Command entering
1185 ;;
1186
1187 (defun murk-enter-string (string)
1188   (if (string-prefix-p "/" string)
1189       (pcase string
1190         ((rx (: "/" (let cmd-str (+ (not whitespace)))
1191                 (opt (+ whitespace)
1192                      (let params-str (+ anychar))
1193                      string-end)))
1194          (let ((command-row (assoc (upcase  cmd-str) murk-command-table #'equal))
1195                (params (if params-str
1196                            (split-string params-str nil t)
1197                          nil)))
1198            (if (and command-row (elt command-row 2))
1199                (funcall (elt command-row 2) params)
1200              (murk-send-msg
1201               (murk-context-network (murk-current-context))
1202               (murk-msg nil nil (upcase cmd-str) params)))))
1203         (_
1204          (murk-display-error "Badly formed command")))
1205     (unless (string-empty-p string)
1206       (let ((ctx (murk-current-context)))
1207         (if ctx
1208             (if (not (murk-network-context-p ctx))
1209                 (let ((network (murk-context-network ctx))
1210                       (channel (murk-context-channel ctx)))
1211                   (murk-send-msg network
1212                                  (murk-msg nil nil "PRIVMSG" channel string))
1213                   (murk-display-message network
1214                                         (murk-connection-nick network)
1215                                         channel string))
1216               (murk-display-error "No current channel"))
1217           (murk-display-error "No current context"))))))
1218
1219
1220 ;;; Command history
1221 ;;
1222
1223 (defvar murk-history nil
1224   "Commands and messages sent in current session.")
1225
1226 (defvar murk-history-index nil)
1227
1228 (defun murk-history-cycle (delta)
1229   (when murk-history
1230     (with-current-buffer "*murk*"
1231       (if murk-history-index
1232           (setq murk-history-index
1233                 (max 0
1234                      (min (- (length murk-history) 1)
1235                           (+ delta murk-history-index))))
1236         (setq murk-history-index 0))
1237       (delete-region murk-input-marker (point-max))
1238       (insert (elt murk-history murk-history-index)))))
1239
1240
1241 ;;; Interactive commands
1242 ;;
1243
1244 (defun murk-enter ()
1245   "Enter current contents of line after prompt."
1246   (interactive)
1247   (with-current-buffer "*murk*"
1248     (let ((line (buffer-substring murk-input-marker (point-max))))
1249       (push line murk-history)
1250       (setq murk-history-index nil)
1251       (let ((inhibit-read-only t))
1252         (delete-region murk-input-marker (point-max)))
1253       (murk-enter-string line))))
1254
1255 (defun murk-history-next ()
1256   (interactive)
1257   (murk-history-cycle -1))
1258
1259 (defun murk-history-prev ()
1260   (interactive)
1261   (murk-history-cycle +1))
1262
1263 (defun murk-cycle-contexts-forward ()
1264   (interactive)
1265   (murk-cycle-contexts)
1266   (murk-highlight-current-context)
1267   (murk-render-prompt)
1268   (if murk-zoomed
1269       (murk-zoom-in (murk-current-context))))
1270
1271 (defun murk-cycle-contexts-reverse ()
1272   (interactive)
1273   (murk-cycle-contexts t)
1274   (murk-highlight-current-context)
1275   (murk-render-prompt)
1276   (if murk-zoomed
1277       (murk-zoom-in (murk-current-context))))
1278
1279 (defvar murk-zoomed nil
1280   "Keeps track of zoom status.")
1281
1282 (defun murk-toggle-zoom ()
1283   (interactive)
1284   (if murk-zoomed
1285       (murk-zoom-out)
1286     (murk-zoom-in (murk-current-context)))
1287   (setq murk-zoomed (not murk-zoomed)))
1288
1289
1290 (defun murk-complete-input ()
1291   (interactive)
1292   (let ((completion-ignore-case t))
1293     (when (>= (point) murk-input-marker)
1294       (pcase (buffer-substring murk-input-marker (point))
1295         ((rx (: "/" (let cmd-str (+ (not whitespace))) (+ " ") (* (not whitespace)) string-end))
1296          (let ((space-idx (save-excursion
1297                             (re-search-backward " " murk-input-marker t)))
1298                (table-row (assoc (upcase cmd-str) murk-command-table #'equal)))
1299            (if (and table-row (elt table-row 3))
1300                (let* ((completions-nospace (funcall (elt table-row 3)))
1301                       (completions (mapcar (lambda (el) (concat el " ")) completions-nospace)))
1302                  (completion-in-region (+ 1 space-idx) (point) completions)))))
1303         ((rx (: "/" (* (not whitespace)) string-end))
1304          (message (buffer-substring murk-input-marker (point)))
1305          (completion-in-region murk-input-marker (point)
1306                                (mapcar (lambda (row) (concat "/" (car row) " "))
1307                                        murk-command-table)))
1308         (_
1309          (let* ((end (max murk-input-marker (point)))
1310                 (space-idx (save-excursion
1311                              (re-search-backward " " murk-input-marker t)))
1312                 (start (if space-idx (+ 1 space-idx) murk-input-marker)))
1313            (unless (string-prefix-p "/" (buffer-substring start end))
1314              (let* ((users (murk-context-users (murk-current-context)))
1315                     (users-no@ (mapcar
1316                                 (lambda (u) (car (split-string u "@" t)))
1317                                 users)))
1318                (completion-in-region start end users-no@)))))))))
1319
1320 ;;; Mode
1321 ;;
1322
1323 (defvar murk-mode-map
1324   (let ((map (make-sparse-keymap)))
1325     (define-key map (kbd "RET") 'murk-enter)
1326     (define-key map (kbd "TAB") 'murk-complete-input)
1327     (define-key map (kbd "C-c C-z") 'murk-toggle-zoom)
1328     (define-key map (kbd "<C-up>") 'murk-history-prev)
1329     (define-key map (kbd "<C-down>") 'murk-history-next)
1330     (define-key map (kbd "<C-tab>") 'murk-cycle-contexts-forward)
1331     (define-key map (kbd "<C-S-iso-lefttab>") 'murk-cycle-contexts-reverse)
1332     (define-key map (kbd "<C-S-tab>") 'murk-cycle-contexts-reverse)
1333     (when (fboundp 'evil-define-key*)
1334       (evil-define-key* 'motion map
1335         (kbd "TAB") 'murk-complete-input))
1336     map))
1337
1338 (define-derived-mode murk-mode text-mode "murk"
1339   "Major mode for murk.")
1340
1341 (when (fboundp 'evil-set-initial-state)
1342   (evil-set-initial-state 'murk-mode 'insert))
1343
1344 ;;; Main start procedure
1345 ;;
1346
1347 ;;;###autoload
1348 (defun murk ()
1349   "Start murk or just switch to the murk buffer if one already exists."
1350   (interactive)
1351   (if (get-buffer "*murk*")
1352       (switch-to-buffer "*murk*")
1353     (switch-to-buffer "*murk*")
1354     (murk-mode)
1355     (murk-setup-buffer))
1356   "Started murk.")
1357
1358 ;;; murk.el ends here