1 ;;; murk.el --- Multiserver Unibuffer iRc Klient -*- lexical-binding:t -*-
3 ;; Copyright (C) 2024 plugd
5 ;; Author: plugd <plugd@thelambdalab.xyz>
6 ;; Created: 11 May 2024
8 ;; Homepage: http://thelambdalab.xyz/murk
10 ;; Package-Requires: ((emacs "26.1"))
12 ;; This file is not part of GNU Emacs.
14 ;; This program is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this file. If not, see <http://www.gnu.org/licenses/>.
29 ;; A very simple IRC server which uses only a single buffer.
41 "Multiserver Unibuffer iRc Klient"
44 (defcustom murk-default-nick "plugd"
48 (defcustom murk-default-quit-msg "Bye"
49 "Default quit message when none supplied."
52 (defcustom murk-networks
53 '(("debug" "localhost" 6667 :notls)
54 ("libera" "irc.libera.chat" 6697)
55 ("tilde" "tilde.chat" 6697)
56 ("freenode" "chat.freenode.net" 6697))
58 :type '(alist :key-type string))
60 (defcustom murk-display-header t
61 "If non-nil, use buffer header to display current host and channel."
69 '((t :inherit default))
70 "Face used for murk text.")
73 '((t :inherit font-lock-keyword-face))
74 "Face used for the prompt.")
77 '((t :inherit murk-context))
78 "Face used for the context name in the prompt.")
81 '((t :inherit shadow))
82 "Face used for faded murk text.")
84 (defface murk-timestamp
85 '((t :inherit shadow))
86 "Face used for timestamps.")
90 "Face used for murk error text.")
93 '((t :inherit warning))
94 "Face used for murk notice text.")
100 (defvar murk-version "Murk v0.0"
101 "Value of this string is used in response to CTCP version queries.")
103 (defvar murk-notice-prefix "-!-")
104 (defvar murk-error-prefix "!!!")
105 (defvar murk-prompt-string ">")
107 (defvar murk-debug nil
108 "If non-nil, enable debug mode.")
111 ;;; Utility procedures
114 (defun murk--filtered-join (&rest args)
115 (string-join (seq-filter (lambda (el) el) args) " "))
117 (defun murk--as-string (obj)
119 (with-output-to-string (princ obj))
123 ;;; Network processes
126 (defvar murk-connection-table nil
127 "An alist associating servers to connection information.
128 This includes the process and the response string.")
130 (defun murk-connection-process (server)
131 (elt (assoc server murk-connection-table) 1))
133 (defun murk-connection-nick (server)
134 (elt (assoc server murk-connection-table) 2))
136 (defun murk-set-connection-nick (server nick)
137 (setf (elt (assoc server murk-connection-table) 2) nick))
139 (defun murk-connection-response (server)
140 (elt (assoc server murk-connection-table) 3))
142 (defun murk-set-connection-response (server string)
143 (setf (elt (assoc server murk-connection-table) 3) string))
145 (defun murk-connection-new (server process nick)
146 (add-to-list 'murk-connection-table
147 (list server process nick "")))
149 (defun murk-connection-remove (server)
150 (setq murk-connection-table
151 (seq-remove (lambda (row) (equal (car row) server))
152 murk-connection-table)))
154 (defun murk-make-server-filter (server)
155 (lambda (_proc string)
156 (dolist (line (split-string (concat (murk-connection-response server) string)
158 (if (string-suffix-p "\r" line)
159 (murk-eval-msg-string server (string-trim line))
160 (murk-set-connection-response server line)))))
162 (defun murk-make-server-sentinel (server)
163 (lambda (_proc string)
164 (unless (equal "open" (string-trim string))
165 (murk-display-error "Disconnected from server.")
166 (murk-connection-remove server)
167 (murk-remove-server-contexts server)
168 (murk-render-prompt))))
170 (defun murk-start-process (server)
171 (let* ((row (assoc server murk-networks))
174 (flags (seq-drop row 3)))
175 (make-network-process :name (concat "murk-" server)
179 :filter (murk-make-server-filter server)
180 :sentinel (murk-make-server-sentinel server)
182 :tls-parameters (if (memq :notls flags)
184 (cons 'gnutls-x509pki
185 (gnutls-boot-parameters
186 :type 'gnutls-x509pki
190 (defvar murk-ping-period 60)
192 ;; IDEA: Have a single ping timer which pings all connected hosts
194 (defun murk-connect (server)
195 (if (assoc server murk-connection-table)
196 (murk-display-error "Already connected to this network")
197 (if (not (assoc server murk-networks))
198 (murk-display-error "Network '" server "' is unknown.")
199 (let ((proc (murk-start-process server)))
200 (murk-connection-new server proc murk-default-nick))
201 (murk-send-msg server (murk-msg nil nil "USER" murk-default-nick 0 "*" murk-default-nick))
202 (murk-send-msg server (murk-msg nil nil "NICK" murk-default-nick))
203 (murk-add-context (list server))
204 (murk-render-prompt))))
206 (defun murk-send-msg (server msg)
208 (murk-display-string nil nil (murk-msg->string msg)))
209 (let ((proc (murk-connection-process server)))
210 (if (and proc (eq (process-status proc) 'open))
211 (process-send-string proc (concat (murk-msg->string msg) "\r\n"))
212 (murk-display-error "No server connection established"))))
218 (defun murk-msg (tags src cmd &rest params)
219 (list (murk--as-string tags)
220 (murk--as-string src)
221 (upcase (murk--as-string cmd))
222 (mapcar #'murk--as-string
223 (if (and params (listp (elt params 0)))
227 (defun murk-msg-tags (msg) (elt msg 0))
228 (defun murk-msg-src (msg) (elt msg 1))
229 (defun murk-msg-cmd (msg) (elt msg 2))
230 (defun murk-msg-params (msg) (elt msg 3))
231 (defun murk-msg-trail (msg)
232 (let ((params (murk-msg-params msg)))
234 (elt params (- (length params) 1)))))
236 (defvar murk-msg-regex
238 (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
240 (opt (: ":" (: (group (* (not (any space "!" "@"))))
241 (* (not (any space)))))
243 (group (: (* (not whitespace))))
245 (opt (group (+ not-newline))))
246 "Regex used to parse IRC messages.
247 Note that this regex is incomplete. Noteably, we discard the non-nick
248 portion of the source component of the message, as mURK doesn't use this.")
250 (defun murk-string->msg (string)
251 (if (string-match murk-msg-regex string)
252 (let* ((tags (match-string 1 string))
253 (src (match-string 2 string))
254 (cmd (upcase (match-string 3 string)))
255 (params-str (match-string 4 string))
258 (let* ((idx (seq-position params-str ?:))
259 (l (split-string (string-trim (substring params-str 0 idx))))
260 (r (if idx (list (substring params-str (+ 1 idx))) nil)))
263 (apply #'murk-msg (append (list tags src cmd) params)))
264 (error "Failed to parse string %s" string)))
266 (defun murk-msg->string (msg)
267 (let ((tags (murk-msg-tags msg))
268 (src (murk-msg-src msg))
269 (cmd (murk-msg-cmd msg))
270 (params (murk-msg-params msg)))
272 (if tags (concat "@" tags) nil)
273 (if src (concat ":" src) nil)
275 (if (> (length params) 1)
276 (string-join (seq-take params (- (length params) 1)) " ")
278 (if (> (length params) 0)
279 (concat ":" (elt params (- (length params) 1)))
286 ;; A context is a list (server channel users) identifying the server
287 ;; and channel. The tail of the list contains the nicks of users
288 ;; present in the channel.
290 ;; Each server has a special context (server) used for messages
291 ;; to/from the server itself.
293 (defvar murk-contexts nil
294 "List of currently-available contexts.
295 The head of this list is always the current context.")
297 (defun murk-current-context ()
298 "Return the current context."
303 (defun murk-contexts-equal (c1 c2)
304 (if (murk-server-context-p c1)
305 (and (murk-server-context-p c2)
306 (equal (murk-context-server c1)
307 (murk-context-server c2)))
308 (and (not (murk-server-context-p c2))
309 (equal (seq-take c1 2)
312 (defun murk-context-server (ctx) (elt ctx 0))
313 (defun murk-context-channel (ctx) (elt ctx 1))
314 (defun murk-context-users (ctx) (elt ctx 2))
315 (defun murk-set-context-users (ctx users)
316 (setcar (cddr ctx) users))
317 (defun murk-server-context-p (ctx) (not (cdr ctx)))
319 (defun murk-add-context (ctx)
320 (add-to-list 'murk-contexts ctx))
322 (defun murk-remove-context (ctx)
326 (murk-contexts-equal this-ctx ctx))
329 (defun murk-remove-server-contexts (server)
331 (seq-remove (lambda (row) (equal (car row) server))
334 (defun murk-context->string (ctx)
335 (if (murk-server-context-p ctx)
336 (concat "[" (murk-context-server ctx) "]")
337 (concat (murk-context-channel ctx) "@"
338 (murk-context-server ctx))))
340 (defun murk-get-context (server &optional name)
342 (let ((test-ctx (list server name)))
343 (seq-find (lambda (ctx)
344 (equal (seq-take ctx 2) test-ctx))
346 (assoc server murk-contexts)))
348 (defun murk-cycle-contexts (&optional reverse)
351 (let ((nminus1 (- (length murk-contexts) 1)))
353 (elt murk-contexts nminus1)
354 (seq-take murk-contexts nminus1)))
355 (append (cdr murk-contexts) (list (car murk-contexts))))))
357 (defun murk-add-context-users (ctx users)
358 (murk-set-context-users
360 (cl-union users (murk-context-users ctx))))
365 (defvar murk-prompt-marker nil
366 "Marker for prompt position in murk buffer.")
368 (defvar murk-input-marker nil
369 "Marker for prompt position in murk buffer.")
371 (defun murk-render-prompt ()
372 (with-current-buffer "*murk*"
373 (let ((update-point (= murk-input-marker (point)))
374 (update-window-points (mapcar (lambda (w)
375 (list (= (window-point w) murk-input-marker)
377 (get-buffer-window-list nil nil t))))
379 (set-marker-insertion-type murk-prompt-marker nil)
380 (set-marker-insertion-type murk-input-marker t)
381 (let ((inhibit-read-only t))
382 (delete-region murk-prompt-marker murk-input-marker)
383 (goto-char murk-prompt-marker)
385 (propertize (let ((ctx (murk-current-context)))
387 (murk-context->string ctx)
391 (propertize murk-prompt-string
394 (propertize " " ; Need this to be separate to mark it as rear-nonsticky
397 (set-marker-insertion-type murk-input-marker nil))
399 (goto-char murk-input-marker))
400 (dolist (v update-window-points)
402 (set-window-point (cadr v) murk-input-marker))))))
404 (defun murk-setup-header ()
405 (with-current-buffer "*murk*"
406 (setq-local header-line-format
408 (let* ((ctx (murk-current-context)))
410 (let ((server (murk-context-server ctx)))
412 "Network: " server ", "
413 (if (murk-server-context-p ctx)
417 (murk-context-channel ctx)
420 (length (murk-context-users ctx)))
422 "No connection")))))))
424 (defun murk-setup-buffer ()
425 (with-current-buffer (get-buffer-create "*murk*")
426 (setq-local scroll-conservatively 1)
427 (setq-local buffer-invisibility-spec nil)
428 (if (markerp murk-prompt-marker)
429 (set-marker murk-prompt-marker (point-max))
430 (setq murk-prompt-marker (point-max-marker)))
431 (if (markerp murk-input-marker)
432 (set-marker murk-input-marker (point-max))
433 (setq murk-input-marker (point-max-marker)))
434 (goto-char (point-max))
436 (if murk-display-header
437 (murk-setup-header))))
439 (defun murk-clear-buffer ()
440 "Completely erase all non-prompt and non-input text from murk buffer."
441 (with-current-buffer "*murk*"
442 (let ((inhibit-read-only t))
443 (delete-region (point-min) murk-prompt-marker))))
446 ;;; Output formatting and highlighting
449 (defun murk--fill-strings (col indent &rest strings)
451 (setq buffer-invisibility-spec nil)
452 (let ((fill-column col)
453 (adaptive-fill-regexp (rx-to-string `(= ,indent anychar))))
454 (apply #'insert strings)
455 (fill-region (point-min) (point-max) nil t)
458 (defun murk-display-string (context prefix &rest strings)
459 (with-current-buffer "*murk*"
461 (goto-char murk-prompt-marker)
462 (let* ((inhibit-read-only t)
463 (old-pos (marker-position murk-prompt-marker))
464 (padded-timestamp (concat (format-time-string "%H:%M ")))
465 (padded-prefix (if prefix (concat prefix " ") ""))
466 (context-atom (if context (intern (murk-context->string context)) nil)))
467 (insert-before-markers
470 (+ (length padded-timestamp)
471 (length padded-prefix))
472 (propertize padded-timestamp
473 'face 'murk-timestamp
476 'invisible context-atom)
477 (propertize padded-prefix
480 'invisible context-atom)
482 (propertize (concat (apply #'murk-buttonify-urls strings) "\n")
485 'invisible context-atom)))))))
486 (murk-scroll-windows-to-last-line))
488 (defun murk-display-message (server from to text)
489 (let ((context (if (string-prefix-p "#" to)
490 (murk-get-context server to)
491 (murk-get-context server))))
494 (if (murk-server-context-p context)
495 (concat "[" from " -> " to "]")
496 (concat (murk-context->string context) " <" from ">"))
499 (defun murk-display-notice (context &rest notices)
502 (propertize murk-notice-prefix 'face 'murk-notice)
503 (apply #'concat notices)))
505 (defun murk-display-error (&rest messages)
508 (propertize murk-error-prefix 'face 'murk-error)
509 (apply #'concat messages)))
511 (defun murk--start-of-final-line ()
512 (with-current-buffer "*murk*"
514 (goto-char (point-max))
515 (line-beginning-position))))
517 (defun murk-scroll-windows-to-last-line ()
518 (with-current-buffer "*murk*"
519 (dolist (window (get-buffer-window-list))
520 (if (>= (window-point window) (murk--start-of-final-line))
521 (with-selected-window window
524 (defconst murk-url-regex
528 (group (or (+ (any alnum "." "-"))
529 (+ (any alnum ":"))))
530 (opt (group (: ":" (+ digit))))
533 (* (any alnum "-/.,#:%=&_?~@+"))
534 (any alnum "-/#:%=&_~@+")))))))
535 "Imperfect regex used to find URLs in plain text.")
537 (defun murk-click-url (button)
538 (browse-url (button-get button 'url)))
540 (defun murk-buttonify-urls (&rest strings)
541 "Turn substrings which look like urls in STRING into clickable buttons."
543 (apply #'insert strings)
544 (goto-char (point-min))
545 (while (re-search-forward murk-url-regex nil t)
546 (let ((url (match-string 0)))
547 (make-text-button (match-beginning 0)
549 'action #'murk-click-url
553 'help-echo "Open URL in browser.")))
556 (defun murk-add-formatting (string)
559 (goto-char (point-min))
564 (prev-point (point)))
565 (while (re-search-forward (rx (or (any "\x02\x1D\x1F\x1E\x0F")
566 (: "\x03" (+ digit) (opt "," (* digit)))))
568 (let ((beg (+ (match-beginning 0) 1)))
570 (add-face-text-property prev-point beg '(:weight bold)))
572 (add-face-text-property prev-point beg '(:slant italic)))
574 (add-face-text-property prev-point beg '(:underline t)))
576 (add-face-text-property prev-point beg '(:strike-through t)))
577 (pcase (match-string 0)
578 ("\x02" (setq bold (not bold)))
579 ("\x1D" (setq italics (not italics)))
580 ("\x1F" (setq underline (not underline)))
581 ("\x1E" (setq strikethrough (not strikethrough)))
586 (setq strikethrough nil))
588 (delete-region (match-beginning 0) (match-end 0))
589 (setq prev-point (point)))))
593 ;;; Message evaluation
596 (defun murk-eval-msg-string (server string)
598 (murk-display-string nil nil string))
599 (let* ((msg (murk-string->msg string)))
600 (pcase (murk-msg-cmd msg)
602 (murk-send-msg server
603 (murk-msg nil nil "PONG" (murk-msg-params msg))))
608 (let* ((params (murk-msg-params msg))
609 (nick (elt params 0))
610 (text (string-join (seq-drop params 1) " ")))
611 (murk-set-connection-nick server nick)
612 (murk-display-notice nil text)))
615 (let* ((params (murk-msg-params msg))
616 (channel (elt params 2))
617 (names (split-string (elt params 3)))
618 (ctx (murk-get-context server channel)))
620 (murk-add-context-users ctx names)
621 (murk-display-notice nil "Users in " channel
622 ": " (string-join names " ")))))
625 (let* ((params (murk-msg-params msg))
626 (channel (elt params 1))
627 (ctx (murk-get-context server channel)))
631 (murk--as-string (length (murk-context-users ctx)))
632 " users in " channel)
633 (murk-display-notice nil "End of " channel " names list."))))
636 ((rx (= 3 (any digit)))
637 (murk-display-notice nil (mapconcat 'identity (cdr (murk-msg-params msg)) " ")))
640 (guard (equal (murk-connection-nick server)
641 (murk-msg-src msg))))
642 (let ((channel (car (murk-msg-params msg))))
643 (murk-add-context (list server channel nil))
644 (murk-display-notice (murk-current-context)
645 "Joining channel " channel " on " server)
646 (murk-render-prompt)))
649 (guard (equal (murk-connection-nick server)
650 (murk-msg-src msg))))
651 (let ((channel (car (murk-msg-params msg))))
652 (murk-display-notice (murk-current-context) "Left channel " channel)
653 (murk-remove-context (list server channel))
654 (murk-render-prompt)))
657 (let ((nick (murk-msg-src msg))
658 (reason (mapconcat 'identity (murk-msg-params msg) " ")))
661 (murk-display-notice nil nick " quit: " reason))))
664 (let* ((from (murk-msg-src msg))
665 (params (murk-msg-params msg))
667 (text (cadr params)))
670 (let ((version-string (concat murk-version " - running on GNU Emacs " emacs-version)))
671 (murk-send-msg server
672 (murk-msg nil nil "NOTICE"
673 (list from (concat "\01VERSION "
676 (murk-display-notice nil "CTCP version request received from "
679 ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
680 (murk-send-msg server (murk-msg nil nil "NOTICE" (list from ping)))
681 (murk-display-notice nil "CTCP ping received from " from " on " server))
684 (murk-display-notice nil "CTCP userinfo request from " from
685 " on " server " (no response sent)"))
688 (murk-display-notice nil "CTCP clientinfo request from " from
689 " on " server " (no response sent)"))
691 ((rx (: "\01ACTION " (let action-text (* (not "\01"))) "\01"))
692 (murk-display-action from to action-text))
695 (murk-display-message server from to text)))))
698 (murk-display-notice nil (murk-msg->string msg))))))
703 (defvar murk-command-table
704 '(("DEBUG" "Toggle debug mode on/off." murk-command-debug murk-boolean-completions)
705 ("HEADER" "Toggle display of header." murk-command-header murk-boolean-completions)
706 ("NETWORKS" "List known IRC networks." murk-command-networks)
707 ("CONNECT" "Connect to an IRC network." murk-command-connect murk-network-completions)
708 ("QUIT" "Disconnect from current network." murk-command-quit)
709 ("JOIN" "Join one or more channels." murk-command-join)
710 ("PART" "Leave channel." murk-command-part murk-context-completions)
711 ("NICK" "Change nick." murk-command-nick)
712 ("MSG" "Send private message to user." murk-command-msg murk-nick-completions)
713 ("CLEAR" "Clear buffer text." murk-command-clear murk-context-completions)
714 ("HELP" "Display help on client commands." murk-command-help murk-help-completions))
715 "Table of commands explicitly supported by murk.")
717 (defun murk-boolean-completions ()
720 (defun murk-network-completions ()
721 (mapcar (lambda (row) (car row)) murk-networks))
723 (defun murk-command-help (params)
725 (let* ((cmd-str (upcase (car params)))
726 (row (assoc cmd-str murk-command-table #'equal)))
729 (murk-display-notice nil "Help for \x02" cmd-str "\x02:")
730 (murk-display-notice nil " " (elt row 1)))
731 (murk-display-notice nil "No such (client-interpreted) command.")))
732 (murk-display-notice nil "Client-interpreted commands:")
733 (dolist (row murk-command-table)
734 (murk-display-notice nil " \x02" (elt row 0) "\x02: " (elt row 1)))
735 (murk-display-notice nil "Use /HELP COMMAND to display information about a specific command.")))
737 (defun murk-command-debug (params)
740 (if (equal (upcase (car params)) "ON")
744 (murk-display-notice nil "Debug mode now " (if murk-debug "on" "off") "."))
746 (defun murk-command-header (params)
749 (equal (upcase (car params)) "ON")
750 (not header-line-format))
753 (murk-display-notice nil "Header enabled."))
754 (setq-local header-line-format nil)
755 (murk-display-notice nil "Header disabled.")))
757 (defun murk-command-clear (params)
760 (dolist (context params)
761 (murk-clear-context context))))
763 (defun murk-command-connect (params)
765 (let ((network (car params)))
766 (murk-display-notice nil "Attempting to connect to " network "...")
767 (murk-connect network))
768 (murk-display-notice nil "Usage: /connect <network>")))
770 (defun murk-command-networks (_params)
771 (murk-display-notice nil "Currently-known networks:")
772 (dolist (row murk-networks)
773 (seq-let (network server port &rest _others) row
774 (murk-display-notice nil "\t" network
776 " " (number-to-string port) "]")))
777 (murk-display-notice nil "(Modify the `murk-networks' variable to add more.)"))
779 (defun murk-command-quit (params)
780 (let ((ctx (murk-current-context)))
782 (murk-display-error "No current context")
783 (let ((quit-msg (if params (string-join params " ") murk-default-quit-msg)))
785 (murk-context-server ctx)
786 (murk-msg nil nil "QUIT" quit-msg))))))
788 (defun murk-command-join (params)
790 (let ((server (murk-context-server (murk-current-context))))
791 (dolist (channel params)
792 (murk-send-msg server (murk-msg nil nil "JOIN" channel))))
793 (murk-display-notice nil "Usage: /join channel [channel2 ...]")))
795 (defun murk-command-part (params)
796 (let* ((server (murk-context-server (murk-current-context)))
799 (murk-context-channel (murk-current-context)))))
801 (murk-send-msg server (murk-msg nil nil "PART" channel))
802 (murk-display-error "No current channel to leave"))))
804 (defun murk-command-msg (params)
805 (let ((server (murk-context-server (murk-current-context))))
806 (if (and params (>= (length params) 2))
807 (let ((to (car params))
808 (text (string-join (cdr params) " ")))
809 (murk-send-msg server (murk-msg nil nil "PRIVMSG" to text))
810 (murk-display-message server
811 (murk-connection-nick server)
813 (murk-display-notice nil "Usage: /msg <nick> <message>"))))
818 (defun murk-enter-string (string)
819 (if (string-prefix-p "/" string)
821 ((rx (: "/" (let cmd-str (+ (not whitespace)))
823 (let params-str (+ anychar))
825 (let ((command-row (assoc (upcase cmd-str) murk-command-table #'equal))
826 (params (if params-str
827 (split-string params-str nil t)
829 (if (and command-row (elt command-row 2))
830 (funcall (elt command-row 2) params)
832 (murk-context-server (murk-current-context))
833 (murk-msg nil nil (upcase cmd-str) params)))))
835 (murk-display-error "Badly formed command")))
836 (unless (string-empty-p string)
837 (let ((ctx (murk-current-context)))
839 (if (not (murk-server-context-p ctx))
840 (let ((server (murk-context-server ctx))
841 (channel (murk-context-channel ctx)))
842 (murk-send-msg server
843 (murk-msg nil nil "PRIVMSG" channel string))
844 (murk-display-message server
845 (murk-connection-nick server)
847 (murk-display-error "No current channel"))
848 (murk-display-error "No current context"))))))
854 (defvar murk-history nil
855 "Commands and messages sent in current session.")
857 (defvar murk-history-index nil)
859 (defun murk-history-cycle (delta)
861 (with-current-buffer "*murk*"
862 (if murk-history-index
863 (setq murk-history-index
865 (min (- (length murk-history) 1)
866 (+ delta murk-history-index))))
867 (setq murk-history-index 0))
868 (delete-region murk-input-marker (point-max))
869 (insert (elt murk-history murk-history-index)))))
872 ;;; Interactive commands
876 "Enter current contents of line after prompt."
878 (with-current-buffer "*murk*"
879 (let ((line (buffer-substring murk-input-marker (point-max))))
880 (push line murk-history)
881 (setq murk-history-index nil)
882 (let ((inhibit-read-only t))
883 (delete-region murk-input-marker (point-max)))
884 (murk-enter-string line))))
886 (defun murk-history-next ()
888 (murk-history-cycle -1))
890 (defun murk-history-prev ()
892 (murk-history-cycle +1))
894 (defun murk-cycle-contexts-forward ()
896 (murk-cycle-contexts)
897 (murk-render-prompt))
899 (defun murk-cycle-contexts-reverse ()
901 (murk-cycle-contexts t)
902 (murk-render-prompt))
904 (defun murk-complete-input ()
906 (let ((completion-ignore-case t))
907 (when (>= (point) murk-input-marker)
908 (pcase (buffer-substring murk-input-marker (point))
909 ((rx (: "/" (let cmd-str (+ (not whitespace))) (+ " ") (* (not whitespace)) string-end))
910 (let ((space-idx (save-excursion
911 (re-search-backward " " murk-input-marker t)))
912 (table-row (assoc (upcase cmd-str) murk-command-table #'equal)))
913 (if (and table-row (elt table-row 3))
914 (let* ((completions-nospace (funcall (elt table-row 3)))
915 (completions (mapcar (lambda (el) (concat el " ")) completions-nospace)))
916 (completion-in-region (+ 1 space-idx) (point) completions)))))
917 ((rx (: "/" (* (not whitespace)) string-end))
918 (message (buffer-substring murk-input-marker (point)))
919 (completion-in-region murk-input-marker (point)
920 (mapcar (lambda (row) (concat "/" (car row) " "))
921 murk-command-table)))
923 (let* ((end (max murk-input-marker (point)))
924 (space-idx (save-excursion
925 (re-search-backward " " murk-input-marker t)))
926 (start (if space-idx (+ 1 space-idx) murk-input-marker)))
927 (unless (string-prefix-p "/" (buffer-substring start end))
928 (let* ((users (murk-context-users (murk-current-context)))
930 (lambda (u) (car (split-string u "@" t)))
932 (completion-in-region start end users-no@)))))))))
937 (defvar murk-mode-map
938 (let ((map (make-sparse-keymap)))
939 (define-key map (kbd "RET") 'murk-enter)
940 (define-key map (kbd "TAB") 'murk-complete-input)
941 (define-key map (kbd "<C-up>") 'murk-history-prev)
942 (define-key map (kbd "<C-down>") 'murk-history-next)
943 (define-key map (kbd "<C-tab>") 'murk-cycle-contexts-forward)
944 (define-key map (kbd "<C-S-iso-lefttab>") 'murk-cycle-contexts-reverse)
945 (define-key map (kbd "<C-S-tab>") 'murk-cycle-contexts-reverse)
946 (when (fboundp 'evil-define-key*)
947 (evil-define-key* 'motion map
948 (kbd "TAB") 'murk-complete-input))
951 (define-derived-mode murk-mode text-mode "murk"
952 "Major mode for murk.")
954 (when (fboundp 'evil-set-initial-state)
955 (evil-set-initial-state 'murk-mode 'insert))
957 ;;; Main start procedure
961 "Start murk or just switch to the murk buffer if one already exists."
963 (if (get-buffer "*murk*")
964 (switch-to-buffer "*murk*")
965 (switch-to-buffer "*murk*")
970 ;;; murk.el ends here