Skip to content

Commit

Permalink
org-backlink now works on my mac
Browse files Browse the repository at this point in the history
  • Loading branch information
florentpastor-wk committed Oct 12, 2020
1 parent b806e92 commit 0d46498
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
2 changes: 2 additions & 0 deletions bugtracker.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Bugs
** TODO Message "Backlink cache is empty" shown repetedly on opening a file
76 changes: 41 additions & 35 deletions org-backlink.el
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(defvar org-backlink-mode-files org-agenda-files
"These files are scanned for backlinks.")
"These files and directories are scanned for backlinks.")


(defface org-backlink-mode-face
Expand All @@ -18,6 +18,7 @@

(setq org-backlink-current-entry-start 0)
(setq org-backlink-current-entry-end 0)
(setq org-backlink-current-entry-file "")

(setq org-backlink-current-entry-backlinks nil)

Expand All @@ -36,8 +37,27 @@
"/"
(org-backlink-mode-get-heading-path))))

(defun org-backlink-mode-expand-files (files)
(seq-reduce (lambda (list elem)
(let ((filelist (pcase elem
((pred file-directory-p)
;; Returns the file of the directory but does not recurse
(directory-files-recursively elem "" nil (lambda (args...) nil) nil)
)
(-
[elem]))))
(seq-concatenate 'list filelist)
)
)
files
(list)))

(defun org-backlink-mode-refresh-cache ()
"Creates a cache for backlink by visiting all links one by one.
Links must be able to be visited for this function to work. When
using org-id accross files, this function should be run after
(org-id-update-id-locations)"
(interactive)
(let ((org-link-search-inhibit-query t)
(linkcache (make-hash-table :test 'equal))
Expand All @@ -46,39 +66,24 @@
(entry (gethash dest linkcache))
(link (list 'file source-file
'path source-path)))
(message "Adding %s as a destination for source %s %s" dest source-file source-path)
(unless (member link entry)
(puthash dest
(cons link entry)
linkcache))))))
(dolist (file org-backlink-mode-files)
(message file)
(with-current-buffer (find-file-noselect file)
(save-excursion
(dolist (file (org-backlink-mode-expand-files org-backlink-mode-files))
(save-excursion
(with-current-buffer (find-file-noselect file)
(goto-char (point-min))
(while (re-search-forward "\\[\\[\\(file:[^:]+::\\)?\\*" nil t)
(ignore-errors
(let* ((filelink (match-string 1))
(source-path (org-backlink-mode-get-heading-path))
(source-file (buffer-file-name)))
(save-excursion
(org-open-at-point)
(if filelink
;; in case of file links the file is opened asynchronously
;; for some reason, so we have to wait for it to open
(run-with-idle-timer
0 nil storefunc linkcache source-file source-path)

;; otherwise we can process it right away
(funcall storefunc linkcache source-file source-path)))))))))

;; restoring original buffer and position after idle timers, which
;; save-excursion cannot do
(run-with-idle-timer
0 nil
(lambda (buffer pos)
(switch-to-buffer buffer buffer)
(goto-char pos))
(current-buffer) (point))
(while (re-search-forward "\\[\\[\\(file\\|id\\):.*?\\]\\[.*?\\]\\]" nil t)
(backward-char) ;; This search query lends us at the end of the link, where org-open-at-point cannot doesn't work
(let* ((source-path (org-backlink-mode-get-heading-path))
(source-file (buffer-file-name)))
(save-excursion
(org-open-at-point)

;; otherwise we can process it right away
(funcall storefunc linkcache source-file source-path)))))))

(setq org-backlink-cache linkcache)
(message "Done.")))
Expand All @@ -92,7 +97,9 @@
(message "Backlink cache is empty. Run org-backlink-mode-refresh-cache.")
nil)))
(when (or (< (point) org-backlink-current-entry-start)
(> (point) org-backlink-current-entry-end))
(> (point) org-backlink-current-entry-end)
(not (string= org-backlink-current-entry-file (buffer-file-name))))
(setq org-backlink-current-entry-file (buffer-file-name))
(setq org-backlink-current-entry-start
(or (ignore-errors (org-entry-beginning-position))
1))
Expand All @@ -106,11 +113,11 @@
org-backlink-cache)))
(save-excursion
(goto-char org-backlink-current-entry-start)
(forward-line 1)
(forward-line 1)
(move-overlay org-backlink-mode-overlay
(line-beginning-position)
(line-beginning-position)
(current-buffer))
(current-buffer))

(overlay-put
org-backlink-mode-overlay
Expand Down Expand Up @@ -176,7 +183,7 @@
(assoc-default
(minibuffer-with-setup-hook
#'minibuffer-completion-help
(completing-read "Select a backlink: " candidates))
(completing-read "Select a backlink: " candidates))
candidates))
(car org-backlink-current-entry-backlinks))))
(find-file (plist-get selected 'file))
Expand All @@ -197,7 +204,6 @@
map)

(if org-backlink-mode
(add-hook 'post-command-hook 'org-backlink-mode-show-backlinks t t)

(add-hook 'post-command-hook 'org-backlink-mode-show-backlinks t t)
(remove-hook 'post-command-hook 'org-backlink-mode-show-backlinks t)
(delete-overlay org-backlink-mode-overlay)))

0 comments on commit 0d46498

Please sign in to comment.