From 7bce71c883499802bc52d55b6ca9718abb317468 Mon Sep 17 00:00:00 2001
From: Luis Guilherme Coelho <lgcoelho@disroot.org>
Date: Sun, 24 Dec 2023 14:12:52 -0300
Subject: [PATCH 2/2] home-fish-configuration: Use records for better
 abbreviation support

---
 gnu/home/services/shells.scm | 62 +++++++++++++++++++++++++++++++-----
 1 file changed, 54 insertions(+), 8 deletions(-)

diff --git a/gnu/home/services/shells.scm b/gnu/home/services/shells.scm
index 9dd56f634a..0ae7ac2358 100644
--- a/gnu/home/services/shells.scm
+++ b/gnu/home/services/shells.scm
@@ -2,6 +2,7 @@
 ;;; Copyright © 2021 Andrew Tropin <andrew@trop.in>
 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
 ;;; Copyright © 2023 Efraim Flashner <efraim@flashner.co.il>
+;;; Copyright © 2023 Luis Guilherme Coelho <lgcoelho@disroot.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -29,6 +30,7 @@ (define-module (gnu home services shells)
   #:use-module (guix packages)
   #:use-module (guix records)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-9)
   #:use-module (srfi srfi-26)
   #:use-module (ice-9 match)
 
@@ -47,6 +49,18 @@ (define-module (gnu home services shells)
             home-fish-configuration
             home-fish-extension
 
+            abbreviation
+            abbreviation-expansion
+            abbreviation-marker
+            abbreviation-position
+            abbreviation-pattern
+            abbreviation-name
+            abbreviation?
+
+            fish-function
+            fish-function-name
+            fish-function?
+
             home-inputrc-service-type
             home-inputrc-configuration))
 
@@ -517,12 +531,44 @@ (define (serialize-fish-aliases field-name val)
                (_ ""))
              val)))
 
+(define-record-type <fish-function>
+  (fish-function name)
+  fish-function?
+  (name fish-function-name))
+
+(define-record-type* <abbreviation>
+  abbreviation make-abbreviation
+  abbreviation?
+  (name      abbreviation-name)       ; string | symbol
+  (pattern   abbreviation-pattern     ; string | #f
+             (default #f))
+  (position  abbreviation-position    ; 'command | 'anywhere | #f
+             (default #f))            ;   defaults to 'command -´
+  (marker    abbreviation-marker      ; char
+             (default #\%))
+  (expansion abbreviation-expansion)) ; string | <fish-function>
+
+(define list-of-abbreviations?
+  (list-of abbreviation?))
+
 (define (serialize-fish-abbreviations field-name val)
   #~(string-append
-     #$@(map (match-lambda
-               ((key . value)
-                #~(string-append "abbr --add " #$key " " #$value "\n"))
-               (_ ""))
+     #$@(map (match-record-lambda <abbreviation>
+               (name pattern position marker expansion)
+               #~((@@ (ice-9 format) format) #f
+                   "~%abbr --add '~a' \\~%~
+                   ~@[     --position ~a \\~%~]~
+                   ~@[     --regex \"~a\" \\~%~]~
+                   ~@[     --set-cursor=~a \\~%~]~
+                   ~@[     ~a~]~%"
+                   '#$name
+                   '#$position
+                   #$pattern
+                   #$marker
+                   #$(if (fish-function? expansion)
+                       (format #f "--function ~a"
+                                  (fish-function-name expansion))
+                       (format #f "\"~a\"" expansion))))
              val)))
 
 (define (serialize-fish-env-vars field-name val)
@@ -556,9 +602,9 @@ (define-configuration home-fish-configuration
 shells, see the @code{abbreviations} field."
    (serializer serialize-fish-aliases))
   (abbreviations
-   (alist '())
-   "Association list of abbreviations for Fish.  These are words that,
-when typed in the shell, will automatically expand to the full text."
+   (list-of-abbreviations '())
+   "List of abbreviations for Fish.  These are words that, when
+typed in the shell, will automatically expand to the full text."
    (serializer serialize-fish-abbreviations)))
 
 (define (fish-files-service config)
@@ -597,7 +643,7 @@ (define-configuration/no-serialization home-fish-extension
    (alist '())
    "Association list of Fish aliases.")
   (abbreviations
-   (alist '())
+   (list-of-abbreviations '())
    "Association list of Fish abbreviations."))
 
 (define (home-fish-extensions original-config extension-configs)
-- 
2.41.0

