-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg-super-links-peek.el
107 lines (92 loc) · 3.74 KB
/
org-super-links-peek.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
;;; org-super-links-peek.el --- Take a peek at the content of link target -*- lexical-binding: t; -*-
;; Copyright (C) 2020 tosh
;; Author: tosh <[email protected]>
;; Version: 0.4
;; Package-Requires: ((emacs "24.1") (quick-peek "1.0"))
;; URL: https://github.com/toshism/org-super-links-peek
;; Keywords: convenience, hypermedia
;; 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:
;; Use the quick-peek package to take a peek at the target of links.
;; Mostly just a dumber version of org-quick-peek to only handle links.
;;; Code:
(require 'org)
(require 'quick-peek)
(defvar org-drawer-regexp)
(defvar org-super-links-peek-show-lines 10)
(defvar org-super-links-peek-show-drawers nil)
(defun org-super-links-peek--marker-target-link ()
"Return marker for link at point."
(save-window-excursion
(org-open-at-point)
(point-marker)))
(defun org-super-links-peek--get-entry-text (marker keep-drawers num-lines)
"Return Org entry text from node at MARKER.
If KEEP-DRAWERS is non-nil, drawers will be kept, otherwise
removed. If NUM-LINES is non-nil, only return the first that
many lines."
;; Modeled after `org-agenda-get-some-entry-text'
;; Stolen from https://github.com/alphapapa/org-quick-peek
;; thanks alphapapa!
(let (text)
(with-current-buffer (marker-buffer marker)
;; Get raw entry text
(org-with-wide-buffer
(goto-char marker)
;; Skip heading
(end-of-line 1)
;; Get entry text
(setq text (buffer-substring
(point)
(or (save-excursion (outline-next-heading) (point))
(point-max))))))
(unless keep-drawers
(with-temp-buffer
;; Insert entry in temp buffer and remove drawers
(insert text)
(goto-char (point-min))
(while (re-search-forward org-drawer-regexp nil t)
;; Remove drawers
(delete-region (match-beginning 0)
(progn (re-search-forward
"^[ \t]*:END:.*\n?" nil 'move)
(point))))
(setq text (buffer-substring (point-min) (point-max)))))
(when (not (eq num-lines 'none))
;; Remove extra lines
(with-temp-buffer
(insert text)
(goto-char (point-min))
(while (org-super-links-peek-current-line-empty-p)
(kill-whole-line))
(goto-line (1+ num-lines))
(backward-char 1)
(setq text (buffer-substring (point-min) (point)))))
text))
(defun org-super-links-peek-current-line-empty-p ()
(save-excursion
(beginning-of-line)
(looking-at-p "[[:space:]]*$")))
(defun org-super-links-peek-link ()
"Show quick peek of Org heading linked at point."
(interactive)
(let ((m (point-marker)))
(unless (> (quick-peek-hide (marker-position m)) 0)
;; Showing, not hiding
(save-excursion
(let ((target-marker (org-super-links-peek--marker-target-link)))
(quick-peek-show (org-super-links-peek--get-entry-text target-marker
org-super-links-peek-show-drawers
org-super-links-peek-show-lines)
(marker-position m) nil org-super-links-peek-show-lines))))))
(provide 'org-super-links-peek)
;;; org-super-links-peek.el ends here