-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvulpea-note.el
146 lines (123 loc) · 5.11 KB
/
vulpea-note.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
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
;;; vulpea-note.el --- Vulpea note definition -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2020-2021 Boris Buliga
;;
;; Author: Boris Buliga <[email protected]>
;; Maintainer: Boris Buliga <[email protected]>
;;
;; Created: 28 Feb 2021
;;
;; URL: https://github.com/d12frosted/vulpea
;;
;; License: GPLv3
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see
;; <http://www.gnu.org/licenses/>.
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Vulpea is a fox.
;;
;;; Code:
(require 'ol)
(require 'dash)
(cl-defstruct vulpea-note
id
path
level
title
primary-title
aliases
tags
links
properties
meta
attach-dir
outline-path)
(autoload 'vulpea-db-query-by-ids "vulpea-db")
(defun vulpea-note-meta-get-list (note prop &optional type)
"Get all values of PROP from NOTE meta.
Each element value depends on TYPE:
- string (default) - an interpreted object (without trailing
newline)
- number - an interpreted number
- link - path of the link (either ID of the linked note or raw link)
- note - linked `vulpea-note'
- symbol - an interned symbol."
(setq type (or type 'string))
(let ((items (cdr (assoc prop (vulpea-note-meta note)))))
(if (eq type 'note)
(let* ((kvps (cl-loop for it in items
collect (if (string-match org-link-bracket-re it)
(let ((link (match-string 1 it))
(desc (match-string 2 it)))
(if (string-prefix-p "id:" link)
(cons (string-remove-prefix "id:" link) desc)
(user-error "Expected id link, but got '%s'" it)))
(user-error "Expected link, but got '%s'" it))))
(ids (mapcar #'car kvps))
(notes (cl-loop for note in (vulpea-db-query-by-ids ids)
unless (vulpea-note-primary-title note)
collect note)))
(cl-loop for it in kvps
collect (let* ((id (car it))
(desc (cdr it))
(note (--find (string-equal id (vulpea-note-id it)) notes)))
(when (seq-contains-p (vulpea-note-aliases note) desc)
(setf (vulpea-note-primary-title note) (vulpea-note-title note))
(setf (vulpea-note-title note) desc))
note)))
(cl-loop for it in items
collect (pcase type
(`string it)
(`symbol (intern it))
(`number (string-to-number it))
(`link (if (string-match org-link-bracket-re it)
(let ((link (match-string 1 it)))
(if (string-prefix-p "id:" link)
(string-remove-prefix "id:" link)
link)))))))))
(defun vulpea-note-meta-get (note prop &optional type)
"Get value of PROP from NOTE meta.
Result depends on TYPE:
- string (default) - an interpreted object (without trailing
newline)
- number - an interpreted number
- link - path of the link (either ID of the linked note or raw link)
- note - linked `vulpea-note'
- symbol - an interned symbol.
If the note contains multiple values for a given PROP, the first
one is returned. In case all values are required, use
`vulpea-note-meta-get-list'."
(car (vulpea-note-meta-get-list note prop type)))
(cl-defmethod vulpea-note-tagged-all-p ((note vulpea-note) &rest tags)
"Return non-nil if a NOTE is tagged by all of the TAGS."
(let ((note-tags (vulpea-note-tags note)))
(--all-p (-contains-p note-tags it) tags)))
(cl-defmethod vulpea-note-tagged-any-p ((note vulpea-note) &rest tags)
"Return non-nil if a NOTE is tagged by any of the TAGS."
(let ((note-tags (vulpea-note-tags note)))
(--any-p (-contains-p note-tags it) tags)))
(cl-defmethod vulpea-note-links-to-all-p ((note vulpea-note) &rest links)
"Return non-nil if a NOTE links to all LINKS."
(let ((note-links (vulpea-note-links note)))
(--all-p (-contains-p note-links it) links)))
(cl-defmethod vulpea-note-links-to-any-p ((note vulpea-note) &rest links)
"Return non-nil if a NOTE links to at least one of LINKS."
(let ((note-links (vulpea-note-links note)))
(--any-p (-contains-p note-links it) links)))
(provide 'vulpea-note)
;;; vulpea-note.el ends here