-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpk-bl.el
89 lines (73 loc) · 2.76 KB
/
pk-bl.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
;;; pk-bl.el --- Elisp code for the pk-bl package -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Peter Klenner
;; Author: Peter Klenner <[email protected]>
;; Version: 0.0.1
;; Keywords: Manual Backlinking for org-files
;; Homepage: https://github.com/RetepRennelk/pk-backlinking
;; Package-Requires: ((emacs "25") (org "9") (ox-json "0.2"))
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(defun pk-bl--list-to-indented-list (lst &optional level)
(if (= (length lst) 0)
""
(let ((level (or level 0))
(L (length lst)))
(concat
(pk-bl--list-prefix level)
(format "%s\n" (car lst))
(pk-bl--list-to-indented-list (cdr lst) (1+ level))))))
(defun pk-bl--list-prefix (indent_level &optional prefix)
"
(list-prefix 0) yields '- '
(list-prefix 1) yields ' - '
(list-prefix 2) yields ' - '
(list-prefix 0 \"+\") yields '+ '
(list-prefix 1 \"+\") yields ' + '
(list-prefix 2 \"+\") yields ' + '
"
(let ((prefix (or prefix "-")))
(concat
(make-string (* 2 indent_level) (string-to-char " "))
(concat prefix " "))))
(defun pk-bl--find-all-org-files ()
(let ((dir (projectile-project-root)))
(directory-files-recursively dir "\\.org$")))
(defun pk-bl--process-link (source_dir)
(let* ((id (eos/org-custom-id-get nil 'create))
(header (org-get-heading t t))
(filename (buffer-file-name))
(relative_filename (pk-bl--relative-name filename source_dir))
(parents (org-get-outline-path))
(child (format "[[%s::#%s][%s]]" relative_filename id header))
(parents (append parents (list child))))
(pk-bl--list-to-indented-list parents)))
(defun pk-bl--relative-name (target source_dir)
(concat "./"
(file-relative-name target
source_dir)))
(defun pk-bl ()
"Insert backlinks to the current headline under a new subheadline."
(interactive)
(let ((id (org-entry-get (point) "custom_id"))
(current_dir (file-name-directory (buffer-file-name))))
(when id
(org-insert-subheading 1)
(insert "Backlinks\n")
(insert
(string-join
(org-ql-select
'pk-bl--find-all-org-files
`(link ,id)
:action `(pk-bl--process-link ,current_dir)))))))
(provide 'pk-bl)