-
Notifications
You must be signed in to change notification settings - Fork 2
/
org-zettel-ref-ui.el
121 lines (104 loc) · 4.67 KB
/
org-zettel-ref-ui.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
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
;;; org-zettel-ref-ui.el --- User interface for org-zettel-ref -*- lexical-binding: t; -*-
;;; Commentary:
;; This file contains user interface related functions for org-zettel-ref.
;;; Code:
(require 'org-zettel-ref-core)
(defun org-zettel-ref-add-quick-note ()
"Use highlight system to add a quick note."
(interactive)
(let* ((note-text (read-string "Insert note: "))
(highlight-id (org-zettel-ref-highlight-generate-id)))
(insert (format "<<hl-%s>> §n{%s}"
highlight-id
note-text))
(org-zettel-ref-highlight-refresh)
(org-zettel-ref-sync-highlights)))
(defun org-zettel-ref-quick-markup ()
"Use highlight system to quickly mark text."
(interactive)
(if (use-region-p)
(let* ((beg (region-beginning))
(end (region-end))
(text (buffer-substring-no-properties beg end))
(type (completing-read "Select mark type: "
(mapcar #'car org-zettel-ref-highlight-types)
nil t))
(type-char (org-zettel-ref-highlight-type-to-char type))
(highlight-id (org-zettel-ref-highlight-generate-id)))
(delete-region beg end)
(insert (format "<<hl-%s>> §%s{%s}"
highlight-id
type-char
text))
(org-zettel-ref-highlight-refresh)
(org-zettel-ref-sync-highlights))
(message "Please select the text to mark first")))
(defcustom org-zettel-ref-quick-markup-key "C-c m"
"Key binding for quick markup function in org-zettel-ref-mode.
This should be a string that can be passed to `kbd'."
:type 'string
:group 'org-zettel-ref)
(defun org-zettel-ref-setup-quick-markup ()
"Set up the key binding for quick markup."
(local-set-key (kbd org-zettel-ref-quick-markup-key) 'org-zettel-ref-quick-markup))
(defun org-zettel-ref-clean-multiple-targets ()
"Remove various markup and unnecessary elements from the current buffer.
Cleans:
1. <<target>> markers completely (including empty ones)
2. #+begin_html...#+end_html blocks
3. :PROPERTIES: blocks under headers
4. *Bold* and _Underline_ markers (preserving heading stars)
5. Trailing backslashes at end of lines"
(interactive)
(let* ((case-fold-search nil)
(pre (car org-emphasis-regexp-components))
(post (nth 1 org-emphasis-regexp-components))
(border (nth 2 org-emphasis-regexp-components))
(emphasis-regexp (concat "\\([" pre "]\\|^\\)"
"\\(\\([*_]\\)"
"\\([^*\n]\\|"
"[^*\n]*"
"[^*\n]\\)"
"\\3\\)"
"\\([" post "]\\|$\\)")))
;; Remove <<target>> markers completely (including empty ones)
(goto-char (point-min))
(while (re-search-forward "<<[^>]*>>" nil t)
(replace-match ""))
;; Remove #+begin_html...#+end_html blocks
(goto-char (point-min))
(let ((case-fold-search t))
(while (re-search-forward "^[ \t]*#\\+begin_html[ \t]*\n\\(\\(?:[^\n]*\n\\)*?\\)[ \t]*#\\+end_html[ \t]*\n" nil t)
(replace-match ""))
(goto-char (point-min))
(while (re-search-forward "^[ \t]*#\\+begin_html[ \t]*\n" nil t)
(replace-match ""))
(goto-char (point-min))
(while (re-search-forward "^[ \t]*#\\+end_html[ \t]*\n" nil t)
(replace-match "")))
;; Remove :PROPERTIES: blocks
(goto-char (point-min))
(while (re-search-forward "^[ \t]*:PROPERTIES:\n\\(?:^[ \t]*:\\(?:\\w\\|-\\)+:.*\n\\)*^[ \t]*:END:\n" nil t)
(replace-match ""))
;; Remove *Bold* and _Underline_ markers, but preserve heading stars
(goto-char (point-min))
(while (re-search-forward emphasis-regexp nil t)
(unless (save-match-data
(string-match-p "^\\*+[ \t]"
(buffer-substring (line-beginning-position)
(match-beginning 0))))
(replace-match "\\1\\4\\5")))
;; Remove trailing backslashes at end of lines (both single and double)
(goto-char (point-min))
(while (re-search-forward "\\\\+[ \t]*$" nil t)
(replace-match ""))
(message "Cleaned up markers, HTML blocks, properties blocks, emphasis markers, and trailing backslashes.")))
(defun org-zettel-ref-clean-targets-and-sync ()
"Clean all markup from the current buffer and then sync the overview."
(interactive)
(org-zettel-ref-clean-multiple-targets)
(save-buffer)
(when (fboundp 'org-zettel-ref-sync-overview)
(org-zettel-ref-sync-overview)))
(provide 'org-zettel-ref-ui)
;;; org-zettel-ref-ui.el ends here