package miaou-core

  1. Overview
  2. Docs
Miaou core/widgets (no drivers, no SDL)

Install

dune-project
 Dependency

Authors

Maintainers

Sources

v0.5.2.tar.gz
md5=60a3b9f181f24572a06a9492532bfdda
sha512=fcc35a275066be2900e6201782faf47503076fa4640f08cf78067835a6f447b74613009e55b2ac799adb7ca46f1bffa261fc5971753f2cc3c6bef327511c7ef6

doc/src/miaou-core.core/keymap_config.ml.html

Source file keymap_config.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
(*****************************************************************************)
(*                                                                           *)
(* SPDX-License-Identifier: MIT                                              *)
(* Copyright (c) 2026 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(*****************************************************************************)

type rule = {page : string option; key : string; action : string}

type t = rule list

let empty : t = []

let is_empty t = t = []

let rule_count t = List.length t

let rules t =
  List.map
    (fun r ->
      let pp = r.page in
      (pp, r.key, r.action))
    t

(* Normalise a key string from human form (ctrl+r, c-r, Ctrl-X) to the
   canonical Keys.to_string form (C-r, C-x, Shift-Tab, Enter, ...). *)
let normalize_key k =
  let s = String.trim k in
  let lower_prefix p =
    String.length s >= String.length p
    && String.lowercase_ascii (String.sub s 0 (String.length p)) = p
  in
  if String.length s = 0 then s
  else if lower_prefix "ctrl+" then
    let rest = String.sub s 5 (String.length s - 5) in
    "C-" ^ String.lowercase_ascii rest
  else if lower_prefix "ctrl-" then
    let rest = String.sub s 5 (String.length s - 5) in
    "C-" ^ String.lowercase_ascii rest
  else if lower_prefix "c-" then
    let rest = String.sub s 2 (String.length s - 2) in
    "C-" ^ String.lowercase_ascii rest
  else if
    String.lowercase_ascii s = "shift+tab"
    || String.lowercase_ascii s = "shift-tab"
    || String.lowercase_ascii s = "s-tab"
  then "Shift-Tab"
  else
    (* Capitalise common named keys for canonical form. *)
    match String.lowercase_ascii s with
    | "enter" -> "Enter"
    | "escape" | "esc" -> "Escape"
    | "tab" -> "Tab"
    | "backspace" -> "Backspace"
    | "delete" -> "Delete"
    | "up" -> "Up"
    | "down" -> "Down"
    | "left" -> "Left"
    | "right" -> "Right"
    | "home" -> "Home"
    | "end" -> "End"
    | "pageup" -> "PageUp"
    | "pagedown" -> "PageDown"
    | _ -> s

(* Parse "key=value" tokens. Returns (key, value) or None. *)
let parse_kv tok =
  match String.index_opt tok '=' with
  | None -> None
  | Some i ->
      let k = String.sub tok 0 i in
      let v = String.sub tok (i + 1) (String.length tok - i - 1) in
      Some (String.trim k, String.trim v)

(* Split a non-comment line into whitespace-separated tokens. *)
let tokenise line =
  String.split_on_char ' ' line
  |> List.concat_map (String.split_on_char '\t')
  |> List.filter (fun s -> s <> "")

let parse_line ~lineno line =
  let trimmed = String.trim line in
  if trimmed = "" || (String.length trimmed > 0 && trimmed.[0] = '#') then
    Ok None
  else
    let tokens = tokenise trimmed in
    let kvs = List.filter_map parse_kv tokens in
    let lookup k = List.assoc_opt k kvs in
    match (lookup "page", lookup "key", lookup "action") with
    | Some page, Some key, Some action ->
        let page = if page = "*" then None else Some page in
        let key = normalize_key key in
        if action = "" then
          Error (Printf.sprintf "line %d: empty action" lineno)
        else Ok (Some {page; key; action})
    | _ ->
        Error
          (Printf.sprintf
             "line %d: expected `page=<name|*> key=<key> action=<id>`"
             lineno)

let parse text =
  let lines = String.split_on_char '\n' text in
  let rec loop lineno acc = function
    | [] -> Ok (List.rev acc)
    | line :: rest -> (
        match parse_line ~lineno line with
        | Ok None -> loop (lineno + 1) acc rest
        | Ok (Some r) -> loop (lineno + 1) (r :: acc) rest
        | Error msg -> Error msg)
  in
  loop 1 [] lines

let default_path () =
  match Sys.getenv_opt "MIAOU_KEYMAP_FILE" with
  | Some p when p <> "" -> Some p
  | _ -> (
      let xdg = Sys.getenv_opt "XDG_CONFIG_HOME" in
      let home = Sys.getenv_opt "HOME" in
      match (xdg, home) with
      | Some x, _ when x <> "" -> Some (Filename.concat x "miaou/keymap.conf")
      | _, Some h when h <> "" ->
          Some (Filename.concat h ".config/miaou/keymap.conf")
      | _ -> None)

let read_file path =
  try
    let ic = open_in path in
    let len = in_channel_length ic in
    let buf = really_input_string ic len in
    close_in ic ;
    Some buf
  with _ -> None

let load ?path () =
  let p = match path with Some p -> Some p | None -> default_path () in
  match p with
  | None -> Ok empty
  | Some p -> (
      match read_file p with None -> Ok empty | Some text -> parse text)

let find t ~page ~key =
  let key = normalize_key key in
  let rec walk ~want_global = function
    | [] -> None
    | r :: rest ->
        let page_match =
          match (want_global, r.page) with
          | false, Some p -> p = page
          | true, None -> true
          | _ -> false
        in
        if page_match && r.key = key then Some r.action
        else walk ~want_global rest
  in
  match walk ~want_global:false t with
  | Some _ as action -> action
  | None -> walk ~want_global:true t