-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnote-1.0.el
147 lines (119 loc) · 4.46 KB
/
dnote-1.0.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
147
;;; dnote.el --- Wrapper for the dnote CLI -*- lexical-binding: t -*-
;; Copyright 2020 by Dave Pearson <davep@davep.org>
;; Author: Dave Pearson <davep@davep.org>
;; Version: 1.0
;; Keywords: docs
;; URL: https://github.com/davep/dnote.el
;; Package-Requires: ((emacs "24.3") (markdown-mode "2.0"))
;; 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/>.
;;; Commentary:
;;
;; dnote.el is a wrapper for the dnote CLI. See
;; https://github.com/dnote/dnote for more details about dnote itself.
;;; Code:
(require 'browse-url)
(require 'markdown-mode)
(defgroup dnote nil
"Wrapper for the dnote CLI."
:group 'docs
:link '(url-link :tag "dnote.el on GitHub"
"https://github.com/davep/dnote.el")
:link '(url-link :tag "dnote on GitHub"
"https://github.com/dnote/dnote"))
(defcustom dnote-app-url "https://app.getdnote.com/"
"The URL of the dnote web application."
:type 'string
:group 'dnote)
(defvar dnote--path nil
"The location of the dnote CLI.")
(defvar-local dnote--book nil
"The book the current buffer relates to.")
(defun dnote--installed-p ()
"Is dnote installed?"
(unless (get 'dnote--path 'checked)
(setq dnote--path (executable-find "dnote"))
(put 'dnote--path 'checked t))
(not (null dnote--path)))
(defun dnote--cmd (cmd)
"Run the dnote command CMD."
(if (dnote--installed-p)
(shell-command-to-string (format "%s %s" dnote--path cmd))
(error "The dnote CLI does not appear to be installed")))
(defun dnote--books ()
"Get the list of dnote books."
(split-string (dnote--cmd "view --name-only")))
(defun dnote--add (book content)
"Add CONTENT to BOOK in dnote."
(dnote--cmd
(format "add %s --content %s" (shell-quote-argument book) (shell-quote-argument content))))
(defun dnote--read-book ()
"Prompt the user for a dnote book name.
If the buffer-local `dnote--book' is non-nil no read is done and
its value is returned."
(or dnote--book (completing-read "Book: " (dnote--books))))
;;;###autoload
(defun dnote-quick-add (book content)
"Quickly add CONTENT to BOOK.
This command is designed to very quickly add a one-liner to dnote.
Note that if `use-region-p' returns t, CONTENT will default to
the content of that region."
(interactive
(let* ((book (dnote--read-book))
(text (read-string
(format "Text (for %s): " book)
(when (use-region-p) (buffer-substring-no-properties (region-beginning) (region-end))))))
(list book text)))
(dnote--add book content)
(message "Note added to book \"%s\"." book))
;;;###autoload
(defun dnote-add-current-buffer (book)
"Add the content of the current buffer to BOOK."
(interactive (list (dnote--read-book)))
(dnote--add book (buffer-substring-no-properties (point-min) (point-max)))
;; If this is a dnote-mode buffer make the (currently, given my design
;; requirement) safe assumption that we can kill the buffer now that we've
;; saved it.
(when (derived-mode-p 'dnote-mode)
(kill-buffer))
(message "Saved as a note in \"%s\"." book))
(defvar dnote-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'dnote-add-current-buffer)
map)
"Mode map for `dnote-mode'.")
;;;###autoload
(define-derived-mode dnote-mode markdown-mode "dnote"
"Major mode for editing a dnote note.
The key bindings for `dnote-mode' are:
\\{dnote-mode-map}")
;;;###autoload
(defun dnote-add (book)
"Write a note and add it to BOOK."
(interactive (list (dnote--read-book)))
(with-current-buffer (pop-to-buffer (format "*dnote add: %s*" book))
(dnote-mode)
(setq dnote--book book)))
;;;###autoload
(defun dnote-app ()
"Visit the dnote application on the web."
(interactive)
(browse-url dnote-app-url))
;;;###autoload
(defun dnote-sync ()
"Perform a sync of dnote notes."
(interactive)
(with-help-window "*dnote sync*"
(princ (dnote--cmd "sync"))))
(provide 'dnote)
;;; dnote.el ends here