-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnx-util.el
79 lines (69 loc) · 2.62 KB
/
nx-util.el
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
;;; nx-util.el --- Description -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2024 Benjamin Andresen
;;
;; Author: Benjamin Andresen <[email protected]>
;; Maintainer: Benjamin Andresen <[email protected]>
;; Created: December 14, 2024
;; Modified: December 14, 2024
;; Version: 0.0.1
;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
;; Homepage: https://github.com/bennyandresen/nx-util
;; Package-Requires: ((emacs "24.3"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Description
;;
;;; Code:
(require 'ht)
(require 'dash)
(require 'nx)
(require 'dash-x)
(defun nx-util-id-buffer-map (&optional buffer)
"Build hash table mapping nx/id values to their buffer positions in BUFFER."
(with-current-buffer (or buffer (current-buffer))
(let ((id-map (make-hash-table :test 'equal))
(pos (point-min)))
(while (< pos (point-max))
(when-let* ((id (get-text-property pos 'nx/id))
(end (next-property-change pos)))
(puthash id (vector pos end) id-map)
(setq pos end))
(setq pos (1+ pos)))
id-map)))
(defun nx-util-validate-id-coverage (&optional buffer)
"Return list of positions where nx/id property is missing."
(with-current-buffer (or buffer (current-buffer))
(let ((missing-regions nil)
(pos (point-min)))
(while (< pos (point-max))
(unless (get-text-property pos 'nx/id)
(push (vector pos
(or (next-property-change pos)
(point-max)))
missing-regions))
(setq pos (or (next-property-change pos)
(point-max))))
(nreverse missing-regions))))
(defun nx-util-replace-id-contents (buffer nx-id contents)
(when (nx? contents)
(with-current-buffer buffer
(-let* [(buffer-map (nx-util-id-buffer-map buffer))
([beg end] (ht-get buffer-map nx-id))]
(jujutsu-dev--display-in-buffer buffer-map)
(when (and beg end)
(let ((inhibit-read-only t))
(save-excursion
(delete-region beg end)
(goto-char beg)
(insert (jujutsu-status--render-node contents nil)))))))))
(-comment
(jujutsu-status--render-node (nx :text (ht (:text "Foo") (:face 'default))) nil)
(nx-util-replace-id-contents (current-buffer) 'nx-1263 (nx :text (ht (:text "Foo:") (:face 'default))))
(nx :div (ht) nil)
)
(provide 'nx-util)
;;; nx-util.el ends here