-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoplin-mode.el
More file actions
2088 lines (1783 loc) · 74 KB
/
joplin-mode.el
File metadata and controls
2088 lines (1783 loc) · 74 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; joplin-mode.el --- Joplin interface
;; Copyright (C) 2023 Seong-Kook Shin <cinsky@gmail.com>
;; Author: Seong-Kook Shin <cinsky@gmail.com>
;; Keywords: extensions, tools
;; 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:
(require 'joplin-struct)
(require 'joplin-http)
(require 'joplin-gen)
(require 'joplin-res)
(require 'joplin-tags)
;; Entry points
;;
;; Most of Joplin requests requires token, which stored in the
;; variable `joplin-context'. Also folder structure should be parsed
;; and stored in the variable `joplin-folders'. These are done by
;; `joplin--init', and should be called on all entry points. Usually
;; any user-exposed command for joplin-buffer, joplin-search-buffer,
;; joplin-note-buffer.
;;
;; * Buffer local variables
;;
;; *** Joplin Buffer
;; *** Joplin Search Buffer
;; - joplin-notes
;; - joplin-visible-fields
;; - joplin-search-limit
;; - joplin-search-done
;; - joplin-search-func
;; - joplin-search-args
;; - joplin-search-type: either \\='search or \\='folder
;;
;; *** Joplin Note Buffer
;; A note buffer retrived from JoplinApp alreadys has joplin-note
;; defined, but several functions need to work on vanilla markdown
;; buffer, which does not have any joplin-related buffer local
;; variables.
;;
;; - joplin-note: a JNOTE struct
;; - joplin-resources: list of JRES struct
;; - joplin-resources-files: alist of (title-pathname . JRES)
;;
;; title-pathname is (concat title
;; (char-to-string ?\u0000)
;; absolute-pathname).
;;
;; Even if markdown link target shares the same pathname, if the
;; title of the link is different, then joplin-mode treat them as
;; different resources not as a same resource.
;; - joplin-parent-buffer: parent buffer
;; This indicates where the user came to the note buffer. Currently,
;; either Joplin notebook buffer or Joplin search buffer. Used by
;; `joplin-jump-to-parent'.
;;
(defface joplin-folder-id-face
'((t :inherit shadow))
"Face for Joplin folder id"
:group 'joplin-faces)
(defface joplin-folder-title-face
'((t :inherit font-lock-string-face))
"Face for Joplin Note title"
:group 'joplin-faces)
(defface joplin-note-id-face
'((t :inherit shadow))
"Face for Joplin Note id"
:group 'joplin-faces)
(defface joplin-note-created-time-face
'((t :inherit font-lock-constant-face))
"Face for Joplin Note created_time"
:group 'joplin-faces)
(defface joplin-note-updated-time-face
'((t :inherit flock-lock-type-face))
"Face for Joplin Note updated_time"
:group 'joplin-faces)
(defface joplin-note-title-face
'((t :inherit font-lock-string-face))
"Face for Joplin Note title"
:group 'joplin-faces)
(defface joplin-note-mark-face
'((t :inherit dired-mark))
"Face for Joplin Note mark"
:group 'joplin-faces)
(defface joplin-properties-name-face
'((t :inherit shadow))
"Face for Joplin property name"
:group 'joplin-faces)
(defface joplin-properties-value-face
'((t :inherit font-lock-string-face))
"Face for Joplin property value"
:group 'joplin-faces)
(defvar joplin-external-link-format "joplin://x-callback-url/openNote?id=%s"
"URL for launch JoplinApp note link")
(defvar joplin-no-unicode-symbol nil)
(defvar joplin-note)
(defvar joplin-temp-file)
(defvar joplin-source-file)
(defvar joplin-visible-fields)
(defvar joplin-eob-marker)
(defvar joplin-search-iter)
(defvar joplin-search-limit)
(defvar joplin-search-count)
(defvar joplin-search-func)
(defvar joplin-search-args)
(defvar joplin-search-type)
;;
;; Internal gloval variable. Not expected to be overriden by users.
;;
(defvar joplin-toplev-buffer-name "*Joplin*")
(defvar joplin-search-buffer-name "*Jsearch*")
(defvar joplin-folder-buffer-format "*Jfolder:%s*")
(defvar joplin-folder-name-history nil
"History variable for `joplin--completing-folder-name'.")
(defvar joplin-limit-per-search 100
"The amount of items rendered on *JoplinSearch* buffer.
It should never be larger than 100.
See URL `https://joplinapp.org/api/references/rest_api/#pagination'.")
(defvar joplin-folders nil
"Alist of (FOLDER-ID . FOLDER) where FOLDER-ID is a string, and FOLDER is a JFOLDER struct.")
(defvar joplin-child-folders nil
"Alist of (FOLDER-ID . CHILDREN) where FOLDER-ID is a string, and CHILDREN is a list of JFOLDER struct.
An empty string is used for the root folder id.")
(defconst joplin-folder-id-column 46)
(defconst joplin-folder-char #x01f4c1)
(defconst joplin-folder-symbol (if (and (not joplin-no-unicode-symbol)
(char-displayable-p joplin-folder-char))
(char-to-string joplin-folder-char)
"[ ]"))
;; All note buffers will bound their `buffer-file-name' to
;; `joplin-temp-note-file'. See the comment in `joplin-save-note'.
(defvar joplin-temp-note-file
(let ((tmpfile (make-temp-file "joplin")))
(add-to-list 'kill-emacs-hook 'joplin--cleanup)
tmpfile))
(defun joplin--cleanup ()
"Clean up temporary resources for `joplin-mode'.
Best used for `kill-emacs-hook'."
(delete-file joplin-temp-note-file))
(defun joplin--get-api-token ()
"Get Joplin API token from JoplinApp."
(condition-case e
(let ((auth (alist-get 'auth_token (joplin--http-post "/auth" nil)))
token)
(while (not token)
(read-from-minibuffer
"Switch to JoplinApp, then accept authorization request [RET]: ")
(setq token (alist-get 'token
(joplin--http-get "/auth/check"
`((auth_token . ,auth))))))
token)
(error (joplin--error 'error "%s" e)
;; (signal (car e) (cdr e)) ; to re-rasise
nil)))
(defun joplin--save-token (token)
"Save JoplinApp TOKEN for later use."
(let ((path (concat (file-name-as-directory user-emacs-directory)
joplin-token-file)))
(with-temp-buffer
(insert token)
(with-file-modes #o600
(write-region (point-min) (point-max) path)))))
(defun joplin--load-folders ()
"Read all folders in a vector of alist."
(let ((context (list (cons 'page 0)))
(items [])
(resp '((items . []) (has_more . t))))
(while (not (eq (alist-get 'has_more resp) :json-false))
(setf (alist-get 'page context) (1+ (alist-get 'page context)))
;; (setq resp (plz 'get (joplin-build-url "/folders" context) :as #'json-read))
(setq resp (joplin--http-get "/folders" context))
(setq items (vconcat items (alist-get 'items resp))))
items))
(defun joplin--build-idmap (folders)
"Build an alist of (FOLDER-ID . JFOLDER) from FOLDERS.
FOLDERS is a vector of alist of Joplin folder fields, available
from `joplin--load-folders'."
(let (idmap)
(dotimes (i (length folders))
(let ((item (aref folders i)))
(let ((fdr (make-JFOLDER :id (alist-get 'id item)
:parent_id (alist-get 'parent_id item)
:title (alist-get 'title item))
))
(push (cons (alist-get 'id item) fdr) idmap))))
idmap))
(defun joplin--build-pcmap (folders idmap)
"Build an ALIST of (FOLDER-ID . (CHILD-JFOLDER ...)) from a vector, FOLDERS.
IDMAP is an alist of (FOLDER-ID . JFOLDER)."
(let (pcmap)
(dotimes (i (length folders))
(let* ((item (aref folders i))
(parent (alist-get 'parent_id item))
(record (assoc parent pcmap)))
(if record
(setcdr record
(nconc (cdr record)
(list (alist-get (alist-get 'id item)
idmap nil nil #'equal))))
(setq pcmap
(nconc pcmap
(list (cons (alist-get 'parent_id item)
(list (alist-get (alist-get 'id item)
idmap nil nil #'equal))
)))))))
pcmap))
(defun joplin--init-folders ()
"Init(sync) joplin, build folder structure, etc."
(let ((folders (joplin--load-folders)))
(joplin--parse-folders folders)))
(defun joplin-sync-folders ()
"Reload Joplin folder structures, and render again."
(interactive)
(joplin--init-folders)
(joplin--render-joplin-buffer))
(defun joplin--parse-folders (folders)
(let ((idmap (joplin--build-idmap folders)))
(setq joplin-child-folders (joplin--build-pcmap folders idmap))
(setq joplin-folders idmap)))
(defun joplin--walk-folders (proc &optional parent level)
(setq level (or level 0))
(setq parent (or parent ""))
;; (message (format "DUMP %s [%s] %s" pcmap parent level))
(let ((children (alist-get parent joplin-child-folders nil nil #'equal)))
(dolist (c children)
;; visit
(funcall proc c level)
(joplin--walk-folders proc (JFOLDER-id c) (1+ level)))))
(defun joplin--dump-folders (&optional parent level)
(joplin--walk-folders
(lambda (folder lev)
(joplin--error 'debug "visit: %s%s" (make-string (* lev 2) ?\ )
(JFOLDER-title folder)))))
(defun joplin-get-folder (id)
(joplin--init)
(alist-get id joplin-folders nil nil #'equal))
(defun joplin-get-note (id)
"Return JNOTE struct by fetching note with ID from JoplinApp."
(joplin--init)
(let ((info-ctx (list '(fields . "id,parent_id,title,created_time,updated_time,is_conflict,latitude,longitude,altitude,author,source_url,is_todo,todo_due,todo_completed,source,source_application,application_data,order,user_updated_time,user_created_time,encryption_cipher_text,encryption_applied,markup_language,is_shared,share_id,conflict_original_id,master_key_id,user_data,source")))
src note)
(setq src (joplin--http-get (concat "/notes/" id) info-ctx))
(when src
(let* ((note (build-JNOTE src))
(tags (joplin--note-tags (JNOTE-id note))))
(setf (JNOTE-_tags note) tags)
(setf (JNOTE-_readall note) t)
note))))
(defun joplin--note-tags (id)
(let* ((resp (joplin--http-get (concat "/notes/" id "/tags")))
(items (alist-get 'items resp))
tags)
(mapc (lambda (a)
(let ((tag (build-JTAG a)))
(push tag tags)
(puthash (JTAG-title tag) tag joplin-tags)))
items)
tags))
(defun joplin--note-buffer (id &optional parent buf-name)
"Return Joplin Note buffer.
Argument ID is Joplin Note id. PARENT is the parent buffer for
jump parent action (usually a buffer of `joplin-search' or the
function `joplin-folder', and override the name of the buffer to
BUF-NAME if non-nil."
(or buf-name
(setq buf-name (joplin--note-buffer-name id)))
(let ((buf (get-buffer buf-name)))
(unless (and buf (buffer-live-p buf))
(setq buf (joplin--note-fill-buffer id
(get-buffer-create buf-name)
parent)))
(with-current-buffer buf
(setq-local joplin-parent-buffer parent))
buf))
(defun joplin--note-fill-buffer (id buffer &optional parent-buffer)
"Fill the contents of the Joplin note buffer.
If the corresponding note buffer (with the note ID) is already
exists, this function simply update it's parent buffer to
PARENT-BUFFER, then return.
Otherwise, it retrieves the note contents from JoplinApp, and
update the parent buffer to PARENT-BUFFER, then return."
(let ((note (joplin-get-note id))
(body-ctx (list '(fields . "body")))
src body)
(setq src (joplin--http-get (concat "/notes/" id) body-ctx))
(setq body (alist-get 'body src))
(with-current-buffer buffer
(erase-buffer)
(and (fboundp 'markdown-mode)
(markdown-mode))
(insert body)
(goto-char (point-min))
(joplin-note-mode)
(view-mode-enter nil #'kill-buffer)
(setq-local joplin-note note)
(joplin--buffer-resources)
;;(message "note: %s" note)
(set-buffer-modified-p nil)
(setq-local buffer-file-name joplin-temp-note-file
joplin-parent-buffer parent-buffer
joplin-temp-file t
buffer-stale-function (lambda (&optional noconfirm) nil)
write-file-functions '(joplin-save-note))
buffer)))
(defun joplin--completing-folder-name (prompt)
"Read a Joplin folder name in the minibuffer with PROMPT.
This function returns the folder id in string, or nil."
(let (sel resp)
(cl-dolist (f joplin-folders)
(let ((folder (cdr f)))
(setq sel (cons (cons (JFOLDER-title folder)
(JFOLDER-id folder))
sel))))
(setq resp (completing-read prompt sel nil 'require-match nil 'joplin-folder-name-history))
(and resp
(alist-get resp sel nil nil #'equal))))
(defun joplin--folder-id (&optional id)
;; TODO: remove this unused function if confirmed
(or id
(if (boundp 'joplin-note)
(setq id (JNOTE-parent_id joplin-note)))))
(defun joplin--folder (&optional id)
"Return the folder struct by ID"
;; TODO: remove this unused function if confirmed
(let ((fid (joplin--folder-id id)))
(and fid
(alist-get fid joplin-folders nil nil #'equal))))
(defun joplin-emacs-time (tm)
"Convert Joplin time JOPLIN-TM to Emacs timestamp.
JOPLIN-TM is a milliseconds. See Info node `(elisp)Time of Day'. "
(let ((sec (/ tm 1000))
(micro (* (% tm 1000) 1000)))
(list (lsh sec -16)
(logand sec #xffff)
micro 0)))
(defun joplin-emacs-time (tm)
"Convert Joplin time JOPLIN-TM to Emacs timestamp.
JOPLIN-TM is a milliseconds. See Info node `(elisp)Time of Day'. "
(let ((sec (/ tm 1000))
(micro (* (% tm 1000) 1000)))
(list (lsh sec -16)
(logand sec #xffff)
micro 0)))
(defun joplin--time-string (time)
;; Joplin time is an integer in milliseconds.
(format-time-string "%c" (joplin-emacs-time time)))
(defun joplin--time-string-short (time)
(format-time-string "%m/%d/%yT%H:%M" (joplin-emacs-time time)))
(define-derived-mode joplin-properties-mode tabulated-list-mode "Joplin Properties" "Major mode for listing Joplin properties"
(setq tabulated-list-format [("name" 24 t . (:right-align t))
("value" 55 nil)])
)
(defun joplin--show-properties (val)
(with-current-buffer-window "*JoplinProperties*"
'(display-buffer--maybe-at-bottom (dedicate . t))
#'(lambda (w _v)
(with-selected-window w
(unwind-protect
(progn
(read-string "Press <RET> to continue: "))
(when (window-live-p w)
(quit-restore-window w 'kill)))))
(joplin-properties-mode)
(joplin--properties-refresh val)
(tabulated-list-print)))
(defun joplin--properties-refresh (struct)
(let ((type (type-of struct))
lst)
(setq tabulated-list-entries
(mapcan
(lambda (a)
(let (val)
(condition-case e
(cl-destructuring-bind (slot opts) a
(let ((name (symbol-name slot))
(val (cl-struct-slot-value type slot struct))
(opt ""))
(if (and (string-match "\\(updated\\|created\\)_time\\'"
name)
(integerp val))
(setq opt (concat " ("
(joplin--time-string val) ")")))
(list
(list nil
(vector
(propertize name
'face 'joplin-properties-name-face)
(propertize (format "%S%s" val opt)
'face 'joplin-properties-value-face)
)))))
(wrong-number-of-arguments nil))))
(cdr (cl-struct-slot-info type))))
(tabulated-list-init-header)))
;;;###autoload
(define-derived-mode joplin-mode special-mode "Joplin" "docstring..."
(hl-line-mode)
)
(setq joplin-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [(control ?n)] #'joplin-next-folder)
(define-key map [(control ?p)] #'joplin-previous-folder)
(define-key map [(?n)] #'joplin-next-folder)
(define-key map [(?p)] #'joplin-previous-folder)
(define-key map [(?i)] #'joplin-show-properties)
(define-key map [(?g)] #'joplin-sync-folders)
(define-key map [(?\r)] #'joplin-visit-folder)
(define-key map [(?s)] #'joplin-search)
(define-key map [(?/)] #'joplin-search)
;;(define-key map [(?c)] #'joplin-new-note)
(define-key map [?q] #'(lambda ()
(interactive)
(quit-window 'kill)))
map))
(defun joplin-point-at-folder ()
"Move the point to the beginning of the folder title"
(let ((bol (point))
(eol (line-end-position))
pos)
(setq pos (next-single-property-change bol 'jfolder nil eol))
(if pos
(goto-char pos))))
(defun joplin-move-point-to-folder (fid)
"Move point to the FOLDER line that has id, FID"
(let ((pos (point-min)))
(setq pos
(save-excursion
(cl-loop while (setq pos (next-single-property-change pos 'jfolder))
do
(goto-char pos)
(let ((folder (get-text-property pos 'jfolder)))
(if (and folder
(string-equal (JFOLDER-id folder) fid))
(cl-return pos))))))
(if pos
(goto-char pos))))
(defun joplin-switch-or-pop-to-buffer (buffer &optional switch)
"If SWITCH is non-nil, `switch-to-buffer'. Otherwise `pop-to-buffer'."
(if switch
(switch-to-buffer buffer)
(setq win (get-buffer-window buffer))
(if win
(select-window win)
(pop-to-buffer buffer))))
(defun joplin-show-properties (&optional arg)
(interactive "P")
(let ((v (cond ((eq major-mode 'joplin-mode)
(joplin--folder-at-point))
((eq major-mode 'joplin-search-mode)
(joplin--search-note-at-point))
((eq major-mode 'joplin-resources-mode)
(tabulated-list-get-id))
((and (boundp 'joplin-note-mode) joplin-note-mode)
(and (boundp 'joplin-note)
joplin-note))
(t nil))))
(when v
(joplin--show-properties v))))
(defun joplin-jump-to-parent (&optional arg)
(interactive "P")
(cond ((eq major-mode 'joplin-search-mode)
;; goto toplev buffer
(let ((note (joplin--search-note-at-point))
(buf (joplin-buffer))
fid win)
(and note (setq fid (JNOTE-parent_id note)))
(joplin-switch-or-pop-to-buffer buf arg)
(if fid
(with-current-buffer buf
(joplin-move-point-to-folder fid)))))
((and (boundp 'joplin-note-mode) joplin-note-mode)
;; goto either search or notebook buffer
(let (nid fid)
(if (and (boundp 'joplin-note) joplin-note)
(setq nid (JNOTE-id joplin-note)
fid (JNOTE-parent_id joplin-note)))
(if (and (boundp 'joplin-parent-buffer)
(buffer-live-p joplin-parent-buffer))
(let ((parent joplin-parent-buffer))
(joplin-switch-or-pop-to-buffer parent arg)
(if nid
(with-current-buffer parent
(joplin-search-move-point-to-note nid))))
;; no parent buffer; must be registered recently.
;; try to switch to the folder buffer, if any.
(if fid
(joplin-folder fidarg)))))
))
(defun joplin-next-folder (&optional arg)
(interactive "p")
(or arg (setq arg 1))
(forward-line arg)
(joplin-point-at-folder))
(defun joplin-previous-folder (&optional arg)
(interactive "p")
(or arg (setq arg 1))
(forward-line (- arg))
(joplin-point-at-folder))
(defun joplin-buffer ()
"Return the buffer of top-level joplin buffer. If not, create it."
(unless (get-buffer joplin-toplev-buffer-name)
(joplin--render-joplin-buffer)
(with-current-buffer (get-buffer joplin-toplev-buffer-name)
(goto-char (point-min))))
(get-buffer joplin-toplev-buffer-name))
;; (condition-case e
;; (plz 'post (joplin--build-url "/auth" nil)
;; :as #'json-read
;; :then 'sync)
;; (error e))
(defun joplin--refresh-token ()
"Save Joplin api token in a file retrived from JoplinApp."
(let ((token (joplin--get-api-token)))
(when token
(joplin--save-token token))))
(defun joplin--init()
(unless joplin-context
(joplin--init-context))
(unless joplin-folders
(joplin--init-folders))
(joplin--tags-init)
(unless (file-directory-p joplin-resource-directory)
(make-directory joplin-resource-directory t))
(joplin--resource-cache-remove-expired))
(defun joplin--init-context ()
(let ((path (concat (file-name-as-directory user-emacs-directory)
joplin-token-file))
token)
;; insert-file-contents filename
(if (not (file-readable-p path))
(joplin--refresh-token))
(with-temp-buffer
(insert-file-contents path)
(setq token (string-trim (buffer-string))))
(if (= (length token) 0)
(joplin--error 'error "empty token in %s" path))
(when token
(setq joplin-context (list (cons 'token token))))))
(defmacro joplin--context (&rest args)
;; Return a context(alist) in ARGS with `joplin-context'.
;; ARGS is in the form of [SYM VAL]...
;; Note that joplin-context is not modified
(if (eq (mod (length args) 2) 1)
(signal 'wrong-number-of-arguments (list 'joplin--context (length args))))
(let ((var (make-symbol "context"))
(a (make-symbol "first"))
(b (make-symbol "second"))
(lst (make-symbol "lst")))
`(let (,var ,a ,b (,lst (quote ,args)))
(unless joplin-context
(joplin--init-context))
(while ,lst
(setq ,var (cons (cons (eval (pop ,lst)) (eval (pop ,lst))) ,var)))
,var)))
;;;###autoload
(defun joplin (&optional arg)
"Raise JoplinApp notebook buffer"
(interactive "P")
;; TODO: get token, then set `joplin-context'.
(joplin--init)
(joplin-switch-or-pop-to-buffer (joplin-buffer) arg))
(defun joplin--render-joplin-buffer ()
(or joplin-folders
(joplin--init-folders))
(with-current-buffer (get-buffer-create joplin-toplev-buffer-name)
(let ((inhibit-read-only t))
(erase-buffer)
;; TODO: set the major mode here
(joplin-mode)
(let ((mkr (point-min-marker)))
(set-marker-insertion-type mkr nil)
(joplin--walk-folders
(lambda (folder lev)
(let ((indent (make-string (* lev 4) ?\ ))
fields text)
(push indent fields)
(push joplin-folder-symbol fields)
(push (propertize (JFOLDER-title folder)
'face 'joplin-folder-title-face
'jfield 'title
'jfolder folder)
fields)
(let ((col 0))
(mapc (lambda (s) (cl-incf col (1+ (length s))))
fields)
(push (if (< col joplin-folder-id-column)
(make-string (- joplin-folder-id-column col) ?\s) " ")
fields))
(push (propertize (JFOLDER-id folder)
'face 'joplin-folder-id-face
'jfield 'id)
fields)
(setq text (string-join (nreverse fields) " "))
(insert text)
(insert "\n")
(let ((ol (make-overlay mkr (point-max))))
(overlay-put ol 'joplin-folder folder))
(set-marker mkr (point-max))
)))))
(setq buffer-read-only t)
(current-buffer)))
(defun joplin--update-note ()
(let ((id (JNOTE-id joplin-note))
resp)
(save-restriction
(widen)
;; TODO: how to change the title of the note?
(setq resp (joplin--http-put
(concat "/notes/" id)
(list (cons 'body
(buffer-substring-no-properties (point-min)
(point-max)))))))
(let ((newnote (build-JNOTE resp t)))
(setq joplin-note newnote)
;; TODO: is there any data structure that need to be updated
;; with newnote?
(if (not (eq (JNOTE-is_conflict newnote) 0))
(message "This note has a conflict, check in JoplinApp"))
(set-buffer-modified-p nil)
(message "note updated - %s" (JNOTE-id newnote))
(JNOTE-id newnote))))
;; (let ((title (read-from-minibuffer "Note title: "))
;; (folder (joplin--completing-folder-name "Folder: "))
(defun joplin--register-note (&optional title folder)
;; upload current buffer as a new note
(let (resp data)
;; (and (buffer-modified-p) (save-buffer))
(save-restriction
(widen)
(setq data (list (cons 'title (if (> (length title) 0)
title
(if buffer-file-name
(format "%s" buffer-file-name)
"Untitled")))))
(and folder
(setq data (cons (cons 'parent_id folder) data)))
(setq data (cons (cons 'body
(buffer-substring-no-properties (point-min)
(point-max)))
data))
(setq resp (joplin--http-post "/notes/" data)))
;; not all fields are present on HTTP POST.
;;
;; body, created_time, id, markup_language, order, parent_id,
;; source, source_application, title, updated_time,
;; user_created_time, user_updated_time
(let* ((note (build-JNOTE resp))
(noteid (JNOTE-id note)))
(setq-local joplin-note note)
(message "node posted - %s" noteid)
noteid)))
;; Why bother to bound the note buffer's file name to the temporary
;; file name?
;;
;; I want to override the default behavior of save-buffer for joplin
;; note buffer so that I just use default emacs save action to update
;; the buffer contents in JoplinApp.
;;
;; I was able to do that by installing my own function in the local
;; variable, `write-file-functions', but it only works when the
;; `buffer-file-name' is actually bound to a file. Otherwise, it will
;; ask the user to provide the filename which is not what I want.
;;
;; This create multiple problems at the moment.
;; (1) Emacs still believes that the joplin note buffer is bound to
;; the original file name, which is not what I want. Perhaps I should
;; override `buffer-file-truename' as well?
;; (2) sometimes, Emacs belives that the temp file was modified so
;; that asking to revert the buffer, which is not what I want. I was
;; experiment with `buffer-stale-function' and
;; `set-visited-file-modtime' and it seems that it resolved. But I
;; haven't figured which was actually solving the problem so I left both
;; of them.
;; (3) Ideally, once the buffer registered to JoplinApp, it should not
;; have any relation with any file, but the user can use Emacs default
;; saving action to update the buffer to Joplin. I need a help on this.
(defun joplin-save-note (&optional arg)
(interactive "p")
(joplin--init)
(unless (and (boundp 'joplin-temp-file)
joplin-temp-file)
;; This means the buffer was not originally created by
;; joplin-mode. We do not know whether the user want to keep the
;; file in the file system or not. So, it's better to save it
;; first, then upload it to JoplinApp.
(save-buffer))
(joplin-resource-upload-all)
(if (boundp 'joplin-note)
(joplin--update-note)
(let ((title (read-from-minibuffer "Note title: "))
(folder (joplin--completing-folder-name "Folder: ")))
(joplin--register-note title folder)
(setq-local joplin-source-file buffer-file-name
buffer-file-name joplin-temp-note-file
joplin-temp-file t
buffer-stale-function (lambda (&optional noconfirm) nil)
write-file-functions '(joplin-save-note))
(rename-buffer (joplin--note-buffer-name joplin-note))
(set-buffer-modified-p nil)
(set-visited-file-modtime)
(JNOTE-id joplin-note)))
;; Reminder. This function should return non-nil as it is part of
;; variable `write-file-functions'. Since `joplin--update-note' and
;; `joplin--register-note' both return note id, it should work.
t)
;;;###autoload
(define-minor-mode joplin-note-mode
"a minor mode for Jopline note"
:lighter "JPL"
:keymap
'(([(control ?c) ?j ?i] . joplin-show-properties)
([(control ?c) ?j ?j] . joplin-jump-to-parent)
([(control ?c) ?j ?s] . joplin-save-note)
([(control ?c) ?j ?l] . joplin-resource-upload-at-point)
([(control ?c) ?j ?L] . joplin-resource-upload-all)
([(control ?c) ?j ?r] . joplin-note-list-resources)
([(control ?c) ?j ?t] . joplin-note-do-tags)
([(control ?c) ?j ?v] . joplin-note-in-joplinapp)
))
;;
;; Search
;;
(define-derived-mode joplin-search-mode special-mode "JoplinSearch"
"docstring..."
(make-local-variable 'joplin-notes)
(make-local-variable 'joplin-visible-fields)
(make-local-variable 'joplin-search-limit)
(make-local-variable 'joplin-search-done)
(make-local-variable 'joplin-search-func)
(make-local-variable 'joplin-search-args)
(make-local-variable 'joplin-search-type) ; search or folder
(toggle-truncate-lines 1)
(hl-line-mode)
(setq-local buffer-read-only t
joplin-notes ()
joplin-visible-fields (copy-alist '((id . t)
(created_time . nil)
(updated_time . t)
(title . t)))
joplin-eob-marker (point-min-marker)
joplin-search-iter nil
joplin-search-count 0
joplin-search-limit joplin-limit-per-search)
;; the function preparing the buffer should initialize
;; `joplin-search-func', `joplin-search-args' and `joplin-search-type'.
)
(setq joplin-search-mode-map
(let ((map (make-sparse-keymap)))
;; (define-key map [(control ?n)] #'joplin-next-folder)
;; (define-key map [(control ?p)] #'joplin-previous-folder)
;; (define-key map [(?n)] #'joplin-next-folder)
;; (define-key map [(?p)] #'joplin-previous-folder)
;; (define-key map [(?i)] #'joplin-show-folder-properties)
;; (define-key map [(?g)] #'joplin-sync-folders)
;;(define-key map [(?c)] #'joplin-new-note)
;; id field show/hide
;; created_time show/hide
;; updated_time show/hide
;; mark current line(note)
;; visit the note
;; jump to joplin folder buffer
;; quit
(define-key map [?/] #'joplin-search)
(define-key map [?s] #'joplin-search)
;; (define-key map [?\r] #'joplin-search) ; return key
(define-key map [?g] #'joplin-search-revert)
(define-key map [?n] #'joplin-search-next-line)
(define-key map [?p] #'joplin-search-previous-line)
(define-key map [?d] #'joplin-search-debug)
(define-key map [?t ?i] #'joplin-search-toggle-id)
(define-key map [?t ?c] #'joplin-search-toggle-created-time)
(define-key map [?t ?u] #'joplin-search-toggle-updated-time)
(define-key map [?^] #'joplin-jump-to-parent)
(define-key map [(control ?c) (control ?j)] #'joplin-jump-to-parent)
(define-key map [(control ?c) ?j ?j] #'joplin-jump-to-parent)
(define-key map [?S ?o] #'joplin-search-sort-notes-by-order)
(define-key map [?S ?i] #'joplin-search-sort-notes-by-id)
(define-key map [?S ?t] #'joplin-search-sort-notes-by-title)
(define-key map [?S ?u] #'joplin-search-sort-notes-by-updated_time)
(define-key map [?S ?c] #'joplin-search-sort-notes-by-created_time)
(define-key map [?q] #'quit-window)
;;(define-key map [?o] #'joplin-search-visit-note-other-window)
(define-key map [?\r] #'joplin-search-visit-note)
(define-key map [(meta ?\r)] #'joplin-search-visit-note-in-joplinapp)
(define-key map [?O] #'joplin-search-visit-note-in-joplinapp)
(define-key map [?i] #'joplin-show-properties)
(define-key map [?m] #'joplin-search-mark)
(define-key map [?u] #'joplin-search-unmark)
(define-key map [?U] #'joplin-search-unmark-all)
(define-key map [?d] #'joplin-search-flag-note-for-delete)
(define-key map [?* ?c] #'joplin-search-change-marks)
(define-key map [?* ?d] #'joplin-search-mark-notes-regexp)
(define-key map [?% ?m] #'joplin-search-mark-notes-regexp)
(define-key map [?% ?d] #'joplin-search-flag-notes-regexp)
(define-key map [?t] #'joplin-search-toggle-marks)
(define-key map [?M] #'joplin-search-move-notes)
(define-key map [?x] #'joplin-search-delete-notes)
map))
(defun joplin--note-buffer-name (note)
"Return Joplin Note buffer name.
Argument NOTE is a JNOTE struct or note id in string."
(format "*JoplinNote:%s*" (if (JNOTE-p note) (JNOTE-id note) note)))
(defun joplin-search-visit-note-in-joplinapp ()
"Visit Joplin Note at the point in JoplinApp."
(interactive)
(let* ((note (joplin--search-note-at-point))
(noteid (and note (JNOTE-id note))))
(when noteid
(joplin-note-in-joplinapp noteid))))
(defun joplin-search-visit-note (&optional arg)
"Visit Joplin Note at the point.
With prefix argument ARG non-nil, switch to note buffer with `switch-to-buffer'.
Otherwise, it will try to pop up the buffer in other window.
See `joplin-switch-or-pop-to-buffer' for more details."
(interactive "P")
(let* ((note (joplin--search-note-at-point))
(parent (current-buffer))
note-buf)
(when note
(setq note-buf (joplin--note-buffer (JNOTE-id note) parent))
(joplin-switch-or-pop-to-buffer note-buf arg))))
(defun joplin-search-visit-note-other-window (&optional arg)
(interactive "P")
(joplin-search-visit-note arg))
(defun joplin-clear-search (&optional arg)
(interactive)
(let ((inhibit-read-only t))
(erase-buffer)
(set-marker joplin-eob-marker (point-min))
(setq joplin-notes ()
joplin-search-iter nil
joplin-search-count 0
joplin-search-limit joplin-limit-per-search
joplin-search-func nil
joplin-search-args nil)))
(defun joplin--search-buffer ()
"Return Joplin Search buffer, create one if none exists."
(let ((buf (get-buffer-create joplin-search-buffer-name)))
(with-current-buffer buf
(unless (eq major-mode 'joplin-search-mode)
(joplin-search-mode)
(setq joplin-search-type 'search))
(current-buffer))))
(defun joplin--folder-buffer (fid)
(let ((folder (alist-get fid joplin-folders nil nil #'equal))
bufname)
(setq bufname (format joplin-folder-buffer-format (JFOLDER-title folder)))
(let ((buf (get-buffer-create bufname)))
(with-current-buffer buf
(unless (eq major-mode 'joplin-search-mode)
(joplin-search-mode)
(setq joplin-search-type 'folder))
(current-buffer)))))
(defvar joplin-search-text-history nil
"")