forked from kametoku/orgmine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorgmine.el
3408 lines (3100 loc) · 124 KB
/
orgmine.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
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
;;; orgmine.el --- minor mode for org-mode with redmine integration
;; Copyright (C) 2015-2017 Tokuya Kameshima
;; Author: Tokuya Kameshima <kametoku at gmail dot com>
;; Keywords: outlines, hypermedia, calendar, wp
;; Homepage: https://github.com/kametoku/orgmine
;; This file is not part of GNU Emacs.
;;
;; 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; orgmine is a minor mode for org-mode with Redmine integration.
;; In a orgmine-mode buffer, you can retrieve the issues from Redmine,
;; edit the entries locally, and submit the changes to Redmine.
;; - [ ] implement orgmine-copy-issue to push an issue tree to clipboard
;; so that it will be inserted into the buffer as a new issue.
;; - [ ] suppress adding TODO keyword to headlines without :issue: tag. If
;; a headline already has a TODO keyword, changing todo status is
;; permitted. Alternatively, if the current position is under an
;; issue subtree, changing todo keyword will be applied to the issue
;; headline. This is the case for setting properties as well.
;; - [ ] improve syncing process with cache effectively.
;; - [ ] changing issue status will updates TODO keyword as well.
;; - [ ] more supports for custom fields.
;; - [ ] orgmine-y-or-n permits scroll the plist buffer.
;; - [ ] a command to show properties of the current entry in other window.
;;; Code:
(require 'elmine)
(require 'request)
(require 'json)
(require 's)
(require 'org)
(require 'org-archive)
(require 'timezone)
(defgroup orgmine nil
"Options concerning orgmie minor mode."
:tag "Org Mine"
:group 'org)
(defcustom orgmine-issue-title-format
"[[redmine:issues/%{id}][#%{id}]] %{subject}"
"Title format for issue entry."
:group 'orgmine)
(defcustom orgmine-journal-title-format
"[[redmine:issues/%{id}#note-%{count}][V#%{id}-%{count}]] %{created_on} %{author}"
"Title format for journal entry."
:group 'orgmine)
(defcustom orgmine-version-title-format
"[[redmine:versions/%{id}][V#%{id}]] %{name}"
"Title format for version entry."
:group 'orgmine)
(defcustom orgmine-tracker-title-format "%{name}"
"Title format for tracker entry."
:group 'orgmine)
(defcustom orgmine-project-title-format
"[[redmine:projects/%{identifier}][%{identifier}]] %{name}"
"Title format for project entry."
:group 'orgmine)
(defcustom orgmine-wiki-page-title-format
"[[redmine:projects/%{project}/wiki/%{title}][%{title}]]"
"Title format for wiki page entry."
:group 'orgmine)
(defcustom orgmine-title-format-regexp
(let ((brackert-link-regexp
"\\[\\[\\(?:[^][]+\\)\\]\\(?:\\[\\(?:[^][]+\\)\\]\\)?\\]"))
(concat "^[ \t]*" brackert-link-regexp "[ \t]*\\(.*?\\)"
"[ \t]*\\(?:(" brackert-link-regexp ")\\)?$"))
"Regular express to extract subject part from headline title."
:group 'orgmine)
(defcustom orgmine-user-name-format "%{firstname} %{lastname}"
"User name format."
:group 'orgmine)
(defcustom orgmine-attachment-format
(concat "[[%{content_url}][%{filename}]] (%{filesize} bytes)"
" %{author.name} %{created_on}")
"attachment item format."
:group 'orgmine)
(defcustom orgmine-journal-details-drawer "DETAILS"
"Drawer name to hold journal details."
:group 'orgmine)
(defcustom orgmine-note-block-begin "#+begin_src gfm"
""
:group 'orgmine)
(defcustom orgmine-note-block-end "#+end_src"
""
:group 'orgmine)
(defcustom orgmine-tags
'((update-me . "UPDATE_ME")
(create-me . "CREATE_ME")
(refile-me . "REFILE_ME")
(project . "project")
(tracker . "tracker")
(versions . "versions")
(version . "version")
(issue . "issue")
(description . "description")
(journals . "journals")
(journal . "journal")
(attachments . "attachments")
(wiki . "wiki"))
"Alist of tags which are used in orgmine mode."
:group 'orgmine)
(defvar orgmine-tag-update-me)
(defvar orgmine-tag-create-me)
(defvar orgmine-tag-refile-me)
(defvar orgmine-tag-project)
(defvar orgmine-tag-tracker)
(defvar orgmine-tag-versions)
(defvar orgmine-tag-version)
(defvar orgmine-tag-issue)
(defvar orgmine-tag-description)
(defvar orgmine-tag-journals)
(defvar orgmine-tag-journal)
(defvar orgmine-tag-attachments)
(defvar orgmine-tag-wiki)
(defcustom orgmine-servers
'(("redmine"
(host . "http://www.example.com")
(api-key . "blabblabblab")
(issue-title-format . "[[redmine:issues/%{id}][#%{id}]] %{subject}")
(journal-title-format
. "[[redmine:issues/%{id}#note-%{count}][V#%{id}-%{count}]] %{created_on} %{author}")
(version-title-format . "[[redmine:versions/%{id}][V#%{id}]] %{name}")
(tracker-title-format . "%{name}")
(project-title-format
. "[[redmine:projects/%{identifier}][%{identifier}]] %{name}")
(user-name-format . "%{firstname} %{lastname}")
(default-todo-keyword . "New"))
("localhost"
(host . "http://localhost:8080/redmine")
(api-key . "XXX")
(issue-title-format . "[\[localhost:issues/%{id}][#%{id}]] %{subject}")
(journal-title-format
. "[\[localhost:issues/%{id}#note-%{count}][V#%{id}-%{count}]] %{created_on} %{author}")
(version-title-format . "[\[localhost:versions/%{id}][V#%{id}]] %{name}")
(project-title-format . "[\[localhost:projects/%{identifier}][%{name}]]")
(user-name-format . "%{firstname} %{lastname}")
(default-todo-keyword . "New")))
"An alist of redmine servers.
Each element has the form (NAME CONFIGURATION)."
:group 'orgmine)
(defcustom orgmine-setup-hook nil
"Hook called in `orgmine-setup'."
:group 'orgmine
:type 'hook)
(defcustom orgmine-issue-buffer-hook nil
"Hook called in `orgmine-issue-buffer'."
:group 'orgmine
:type 'hook)
;;
;; for Redmine REST API (replacement of elmine.el, in the future)
;; Use request.el for the backend http protocol.
;; You can use curl command line program if it is installed.
;;
(defun orgmine/api-json-read ()
"Parse and return the JSON object following point.
Advance point just past JSON object."
(json-skip-whitespace)
(unless (eq (json-peek) :json-eof)
(let ((json-object-type 'plist)
(json-array-type 'list))
(json-read))))
(defun orgmine/api-decode (json-string)
"Parse a JSON string JSON-STRING and return an object.
Per default JSON objects are going to be hashtables and JSON
arrays are going to be lists."
(unless (or (null json-string)
(string= json-string ""))
(let ((json-object-type 'plist)
(json-array-type 'list))
(json-read-from-string json-string))))
(defun orgmine/api-encode (object)
"Return a JSON representation from the given object OBJECT."
(let ((json-object-type 'plist)
(json-array-type 'list))
(encode-coding-string (json-encode object) 'utf-8)))
(defun orgmine/api-build-url (path)
(concat orgmine-host path))
(defun orgmine/api-plist-to-alist (plist)
;; (:k1 v1 :k2 v2 ...) -> (("k1" . v1) ("k2" . v2) ...)
(let (alist)
(while plist
(let ((key (elmine/ensure-string (car plist))) ; XXX
(value (car (cdr plist))))
(push (cons key value) alist))
(setq plist (cdr (cdr plist))))
alist))
(defun orgmine/api-raw (method path data params &optional content-type)
"Perform a raw HTTP request with given METHOD, a relative PATH and a
plist of PARAMS for the query.
This is a request.el version of `elmine/api-raw'."
(if (and (not (null data))
(listp data))
(setq data (orgmine/api-encode data)))
(let* ((orgmine-host (cond ((boundp 'orgmine-host) orgmine-host)
((boundp 'redmine-host) redmine-host)
(t elmine/host)))
(orgmine-api-key (cond ((boundp 'orgmine-api-key) orgmine-api-key)
((boundp 'redmine-api-key) redmine-api-key)
(t elmine/api-key)))
(url (orgmine/api-build-url path))
(headers `(("Content-Type" . ,(or content-type "application/json"))
("X-Redmine-API-Key" . ,orgmine-api-key)))
(params (orgmine/api-plist-to-alist params))
(response (request url :type method :params params :headers headers
:data data :parser 'orgmine/api-json-read :sync t))
(err (request-response-error-thrown response))
(status-code (request-response-status-code response)) ; eg, 200
(status-text (request-response-header response "status")) ;eg, "200 OK"
(body (request-response-data response)))
(cond ((eq status-code 404)
(signal 'no-such-resource (list status-text url))) ;should be error?
(err (signal (car err) (cdr err))))
response))
(defun orgmine/api-get (element path &rest params)
"Perform an HTTP GET request and return a PLIST with the request information."
(let* ((params (if (listp (car params)) (car params) params))
(response (orgmine/api-raw "GET" path nil params))
(object (request-response-data response)))
(if element
(plist-get object element)
object)))
(defalias 'elmine/api-get 'orgmine/api-get)
(defun orgmine/api-post (element object path &rest params)
"Perform an http POST request."
(let* ((params (if (listp (car params)) (car params) params))
(data (list element object))
(response (orgmine/api-raw "POST" path data params))
(object (request-response-data response)))
object))
(defalias 'elmine/api-post 'orgmine/api-post)
(defun orgmine/api-put (element object path &rest params)
"Perform an http PUT request."
(let* ((params (if (listp (car params)) (car params) params))
(data (list element object))
(response (orgmine/api-raw "PUT" path data params))
(object (request-response-data response)))
object))
(defalias 'elmine/api-put 'orgmine/api-put)
(defun orgmine/api-delete (path &rest params)
"Perform an http DELETE request."
(let* ((params (if (listp (car params)) (car params) params))
(response (orgmine/api-raw "DELETE" path nil params))
(object (request-response-data response)))
object))
(defalias 'elmine/api-delete 'orgmine/api-delete)
;; ;; workaround for decode the returned string as utf-8
;; (defadvice json-read-string (around json-read-string-decode activate)
;; "Decode string processed in `json-read-string' as utf-8."
;; (let ((string ad-do-it))
;; (decode-coding-string string 'utf-8)))
;; redefine the function for workaround
(defun orgmine/json-read-string ()
"Read the JSON string at point."
(unless (char-equal (json-peek) ?\")
(signal 'json-string-format (list "doesn't start with '\"'!")))
;; Skip over the '"'
(json-advance)
(let ((characters '())
(char (json-peek)))
(while (not (char-equal char ?\"))
(push (if (char-equal char ?\\)
(json-read-escaped-char)
(json-pop))
characters)
(setq char (json-peek)))
;; Skip over the '"'
(json-advance)
(if characters
;; kame<<<
;; (apply 'string (nreverse characters))
;; =======
(decode-coding-string (apply 'string (nreverse characters))
'utf-8)
;; >>>kame
"")))
(defalias 'json-read-string 'orgmine/json-read-string)
;;; XXX
;; http://www.redmine.org/projects/redmine/wiki/Rest_IssueJournals
;; '(:journals ((:details ((:new_value "3" :name "fixed_version_id" :property "attr"))
;; :created_on "2015-08-02T14:19:02Z"
;; :notes "" :user (:name "Tokuya Kameshima" :id 3) :id 3)
;; ...))
(defun orgmine/get-issue-with-journals (id)
"Get a specific issue including journals, relations and attachments via id."
;; (elmine/api-get :issue (format "/issues/%s.json?include=journals" id)))
;; (elmine/api-get :issue (format "/issues/%s.json" id) :include "journals"))
(elmine/api-get :issue (format "/issues/%s.json" id)
:include "journals,relations,attachments"))
(defalias 'elmine/get-issue-with-journals 'orgmine/get-issue-with-journals)
(defun orgmine/get-project-trackers (project)
"Get trackers of a specific project."
;; (elmine/api-get :issue (format "/issues/%s.json?include=journals" id)))
(let ((plist (elmine/api-get :project (format "/projects/%s.json" project)
:include "trackers")))
(plist-get plist :trackers)))
(defalias 'elmine/get-project-trackers 'orgmine/get-project-trackers)
(defun orgmine/get-users ()
"Get a list with users."
(elmine/api-get-all :users "/users.json"))
(defalias 'elmine/get-users 'orgmine/get-users)
(defun orgmine/get-custom-fields (filters)
"Get a list with custom fields."
(apply #'elmine/api-get-all :custom_fields "/custom_fields.json" filters))
(defalias 'elmine/get-custom-fields 'orgmine/get-custom-fields)
(defun orgmine/create-relation (&rest params)
"Create a new relation"
(let* ((object (if (listp (car params)) (car params) params))
(issue-id (plist-get object :issue_id))
(issue-to-id (plist-get object :issue_to_id))
(relation-type (plist-get object :relation_type))
(delay (plist-get object :delay))
;; plist should not have the :issue_id element.
;; If not, redmine returns 500 error.
(plist (list :issue_to_id issue-to-id
:relation_type relation-type)))
(and delay (setq plist (plist-put plist :delay delay)))
(elmine/api-post :relation plist
(format "/issues/%s/relations.json" issue-id))))
(defalias 'elmine/create-relation 'orgmine/create-relation)
(defun orgmine/delete-relation (id)
"Delete an relation with a specific id."
(elmine/api-delete (format "/relations/%s.json" id)))
(defalias 'elmine/delete-relation 'orgmine/delete-relation)
(defun orgmine/api-post-octet-stream (data path &rest params)
"Does an http POST request and returns response status as symbol."
(let* ((params (if (listp (car params)) (car params) params))
(response (elmine/api-raw "POST" path data params
"application/octet-stream"))
(object (request-response-data response)))
object))
(defun orgmine/upload-file (file)
"upload a specific file to Redmine for the attachment."
(let ((data (with-temp-buffer
(insert-file-contents-literally file)
(buffer-string))))
(orgmine/api-post-octet-stream data "/uploads.json")))
(defalias 'elmine/upload-file 'orgmine/upload-file)
(defun orgmine/get-project-wiki-pages (project &rest filters)
"Get a list of wiki pages for a specific project."
(apply #'elmine/api-get-all :wiki_pages
(format "/projects/%s/wiki/index.json" project) filters))
(defalias 'elmine/get-project-wiki-pages 'orgmine/get-project-wiki-pages)
(defun orgmine/get-wiki-page (project title)
"Get a specific wiki page via project and title."
(elmine/api-get :wiki_page
(format "/projects/%s/wiki/%s.json" project title)
:include "attachments"))
(defalias 'elmine/get-wiki-page 'orgmine/get-wiki-page)
(defun orgmine/update-wiki-page (project title &rest params)
"Create or update a specific wiki page via project and title."
(let ((object (if (listp (car params)) (car params) params)))
(elmine/api-put :wiki_page object
(format "/projects/%s/wiki/%s.json" project title))))
(defalias 'elmine/update-wiki-page 'orgmine/update-wiki-page)
(defun elmine/delete-wiki-page (project title)
"Delete a specific wiki page entry."
(elmine/api-delete (format "/projects/%s/wiki/%s.json" project title)))
(defalias 'elmine/delete-wiki-page 'orgmine/delete-wiki-page)
(defun orgmine-server (base-url)
"Return the server entry of the Redmine server in `orgmine-servers'
whose host is BASE-URL."
(catch 'found
(mapc (lambda (elem)
(let ((host (cdr (assoc 'host (cdr elem)))))
(if (string= host base-url)
(throw 'found elem))))
orgmine-servers)
nil))
(defun orgmine-parse-issue-url (url)
"Parse URL and return a cons (SERVER . ISSUE-ID)."
(save-match-data
(if (string-match "^\\(http.*\\)/issues/\\([0-9]+\\)" url)
;; redmine url -> orgmine
(let* ((base-url (match-string 1 link))
(issue-id (match-string 2 link))
(server (orgmine-server base-url)))
(if server
(cons (car server) issue-id))))))
(defun orgmine-issue-buffer (server issue-id &optional title)
"Create an orgmine issue buffer."
(let* ((bufname (format "*OrgMine-%s:issues/%s*" server issue-id))
(buf (get-buffer-create bufname)))
(switch-to-buffer buf)
(erase-buffer)
(if title (insert (format "#+TITLE: %s\n" title)))
(insert (format "#+PROPERTY: om_server %s\n\n" server))
(set-buffer-file-coding-system 'utf-8)
(org-mode)
(orgmine-mode t)
(save-excursion
(orgmine-insert-issue issue-id))
(hide-subtree)
(show-branches)
(org-align-all-tags)
(set-buffer-modified-p nil)
(run-hooks 'orgmine-issue-buffer-hook)
(message "Editing issue #%s on %s" issue-id server)))
(defun orgmine-tag (key)
"Return tag."
(cdr (assoc key orgmine-tags)))
(defun orgmine-setup-custom-fields (config)
(set (make-local-variable 'orgmine-custom-fields) nil)
(mapc (lambda (plist)
(let ((name (orgmine-custom-field-property-name plist)))
(add-to-list 'orgmine-custom-fields (cons name plist))))
config))
(defun orgmine-setup-tags ()
(mapc (lambda (elem)
(let* ((key (car elem))
(value (cdr elem))
(symbol (intern (format "orgmine-tag-%s" key))))
(set (make-local-variable symbol) value)))
orgmine-tags))
(defvar orgmine-valid-variables
'(host api-key issue-title-format journal-title-format version-title-format
tracker-title-format project-title-format wiki-page-title-format
user-name-format custom-fields default-todo-keyword))
(defun orgmine-setup ()
"Setup buffer local variables from ORGMINE-SERVERS per om_server property."
(let* ((server (cdr (assoc-string "om_server" org-file-properties t)))
(config (cdr (assoc-string server orgmine-servers t))))
(if config
(set (make-local-variable 'orgmine-server) server))
(mapc (lambda (elem)
(let* ((key (car elem))
(symbol (intern (format "orgmine-%s" key)))
(value (cdr elem)))
(if (memq key orgmine-valid-variables)
(progn
(set (make-local-variable symbol) value)
(if (eq key 'custom-fields)
(orgmine-setup-custom-fields value)))
(message "orgmine-setup: %s: skipped - invalid name" key))))
config))
(orgmine-setup-tags)
(run-hooks 'orgmine-setup-hook))
(defvar orgmine-mode-map (make-sparse-keymap)
"Keymap for `orgmine-mode', a minor mode.")
(define-minor-mode orgmine-mode
"minor mode for org-mode with Redmine integration"
:lighter "Mine" :keymap orgmine-mode-map
(org-load-modules-maybe)
(orgmine-setup)
(set (make-local-variable 'orgmine-statuses) nil)
(make-local-variable 'org-tags-exclude-from-inheritance)
(if (and orgmine-journal-details-drawer
(boundp 'org-drawers))
(add-to-list 'org-drawers orgmine-journal-details-drawer))
(mapc (lambda (tag)
(add-to-list 'org-tags-exclude-from-inheritance tag))
(list orgmine-tag-update-me orgmine-tag-create-me orgmine-tag-refile-me
orgmine-tag-project orgmine-tag-tracker
orgmine-tag-versions orgmine-tag-version
orgmine-tag-issue
orgmine-tag-description orgmine-tag-journals orgmine-tag-journal
orgmine-tag-wiki orgmine-tag-attachments))
(define-key orgmine-mode-map "\C-cma" 'orgmine-add-attachment)
(define-key orgmine-mode-map "\C-cmA" 'orgmine-insert-all-versions)
(define-key orgmine-mode-map "\C-cmc" 'orgmine-submit)
(define-key orgmine-mode-map "\C-cmd" 'orgmine-add-description)
(define-key orgmine-mode-map "\C-cme" 'orgmine-ediff)
(define-key orgmine-mode-map "\C-cmf" 'orgmine-fetch)
(define-key orgmine-mode-map "\C-cmg" 'orgmine-goto-issue)
(define-key orgmine-mode-map "\C-cmG" 'orgmine-goto-version)
(define-key orgmine-mode-map "\C-cmi" 'orgmine-add-issue)
(define-key orgmine-mode-map "\C-cmI" 'orgmine-insert-issue)
(define-key orgmine-mode-map "\C-cmj" 'orgmine-add-journal)
(define-key orgmine-mode-map "\C-cmk" 'orgmine-skeletonize-subtree)
(define-key orgmine-mode-map "\C-cmp" 'orgmine-add-project)
(define-key orgmine-mode-map "\C-cmP" 'orgmine-insert-project)
(define-key orgmine-mode-map "\C-cmr" 'orgmine-refile-me)
(define-key orgmine-mode-map "\C-cms" 'orgmine-sync-subtree-recursively)
(define-key orgmine-mode-map "\C-cmS" 'orgmine-sync-buffer)
(define-key orgmine-mode-map "\C-cmT" 'orgmine-insert-tracker)
(define-key orgmine-mode-map "\C-cmu" 'orgmine-goto-parent-issue)
(define-key orgmine-mode-map "\C-cmv" 'orgmine-add-version)
(define-key orgmine-mode-map "\C-cmV" 'orgmine-insert-version)
(define-key orgmine-mode-map "\C-cmw" 'orgmine-add-wiki-page)
(define-key orgmine-mode-map "\C-cmW" 'orgmine-insert-wiki-page)
(define-key orgmine-mode-map "\C-cm\C-w" 'orgmine-refile)
(define-key orgmine-mode-map "\C-cm#" 'orgmine-insert-template)
(define-key orgmine-mode-map "\C-cm;;" 'orgmine-set-entry-property)
(define-key orgmine-mode-map "\C-cm;a" 'orgmine-set-assigned-to)
(define-key orgmine-mode-map "\C-cm;c" 'orgmine-set-custom-field)
(define-key orgmine-mode-map "\C-cm;d" 'orgmine-set-done-ratio)
(define-key orgmine-mode-map "\C-cm;t" 'orgmine-set-tracker)
(define-key orgmine-mode-map "\C-cm;v" 'orgmine-set-version)
(define-key orgmine-mode-map "\C-cm/a" 'orgmine-show-assigned-to)
(define-key orgmine-mode-map "\C-cm/c" 'orgmine-show-child-issues)
(define-key orgmine-mode-map "\C-cm/d" 'orgmine-show-descriptions)
(define-key orgmine-mode-map "\C-cm/i" 'orgmine-show-issues)
(define-key orgmine-mode-map "\C-cm/j" 'orgmine-show-journals)
(define-key orgmine-mode-map "\C-cm/m" 'orgmine-show-assigned-to-me)
(define-key orgmine-mode-map "\C-cm/n" 'orgmine-show-notes)
(define-key orgmine-mode-map "\C-cm/p" 'orgmine-show-projects)
(define-key orgmine-mode-map "\C-cm/r" 'orgmine-show-all)
(define-key orgmine-mode-map "\C-cm/t" 'orgmine-show-trackers)
(define-key orgmine-mode-map "\C-cm/u" 'orgmine-show-create-or-update)
(define-key orgmine-mode-map "\C-cm/v" 'orgmine-show-versions)
(define-key orgmine-mode-map "\C-cm/w" 'orgmine-show-refile)
(define-key orgmine-mode-map "\C-cm?" 'orgmine-ediff)
(add-hook 'org-after-todo-state-change-hook 'orgmine-after-todo-state-change)
)
(defun orgmine-insert-demoted-heading (&optional title tags-list)
"Insert a demoted headling at the beginning of the current line."
(move-beginning-of-line nil)
(if (save-match-data
(or (looking-at "^\\*+ ") (eobp)))
(open-line 1))
(outline-insert-heading)
(org-do-demote)
(insert (or title ""))
(mapc (lambda (tag)
(org-toggle-tag tag 'on))
tags-list))
(defun orgmine-idname-to-id (idname &optional for-filter)
;; "ID:NAME" -> "ID"
(save-match-data
(cond ((string-match "^[0-9]+" idname)
(match-string 0 idname))
((and for-filter
(string-match "^!?\\*" idname)) ; "*" and "!*" for filter.
(match-string 0 idname)))))
(defun orgmine-redmine-date (date)
;; "[2011-03-02 Wed]" -> "2011-03-02"
(save-match-data
(if (string-match "[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}" date)
(match-string 0 date)
"")))
(defun orgmine-org-date (date)
;; "2011-03-02" -> "[2011-03-02 Wed]"
(condition-case nil
(let ((time (apply 'encode-time (org-parse-time-string date))))
(format-time-string (org-time-stamp-format nil t) time))
(error "")))
(defun orgmine-tz-org-date (time-string)
;; "2015-08-07T02:55:08Z" -> "[2015-08-07 Fri 11:55]"
(save-match-data
(let* ((time-vector (timezone-fix-time time-string nil nil))
(time (apply 'encode-time
(cdr (nreverse (append time-vector nil))))))
(format-time-string (org-time-stamp-format t t) time))))
(defun orgmine-format-value (plist key)
;; author.name extract value of (:author (:name NAME))
(save-match-data
(let ((key-list (org-split-string key "\\."))
(value plist))
(mapc (lambda (k)
(setq value (plist-get value (intern (format ":%s" k)))))
key-list)
value)))
(defun orgmine-format (string plist)
(with-temp-buffer
(insert string)
(goto-char (point-min))
(while (re-search-forward "%{\\([^}]+\\)}" nil t)
;; (let* ((key (intern (format ":%s" (match-string 1))))
;; (value (plist-get plist key)))
(let* ((key-str (match-string 1))
(key (intern (format ":%s" key-str)))
(value (orgmine-format-value plist key-str)))
(cond ((member key '(:created_on :updated_on :closed_on))
(setq value (orgmine-tz-org-date value))))
(replace-match (elmine/ensure-string value) t t)))
(buffer-string)))
(defun orgmine-extract-subject (title)
(save-match-data
(if (string-match orgmine-title-format-regexp title)
(match-string 1 title)
title)))
(defun orgmine-map-region (func beg end &optional only-same-level)
"Call FUNC for every heading between BEG and END."
(let ((next-heading-func
(if only-same-level 'outline-get-next-sibling 'outline-next-heading))
level)
(save-excursion
(setq end (copy-marker end))
(goto-char beg)
(if (outline-on-heading-p t)
(funcall func))
(while (and (progn
(funcall next-heading-func)
(< (point) end))
(not (eobp)))
(funcall func))
(set-marker end nil))))
(defun orgmine-tags-in-tag-p (tags1 tags2)
(catch 'found
(mapc (lambda (tag)
(if (member tag tags2)
(throw 'found tag)))
tags1)
nil))
;;;
(defun orgmine-find-headline (tag &optional end only-same-level)
"Search forward from point for headline with TAG
within the region between the current position and END.
If found, returns the beginning position of the headline."
(let* ((pred (cond ((stringp tag) 'member)
((listp tag) 'orgmine-tags-in-tag-p)))
(pos (catch 'found
(orgmine-map-region (lambda ()
(if (funcall pred tag (org-get-tags))
(throw 'found (point))))
(point) (or end (point-max))
only-same-level)
nil)))
(if pos (goto-char pos))))
(defun orgmine-find-headline-prop (tag key value &optional end)
"Search forward from point for headline with TAG and property of KEY is VALUE.
within the region between the current position and END.
If found, returns the beginning position of the headline."
(let* ((value-regexp (if (orgmine-id-property-p key)
(format "%s\\(:?:.*\\)?" (regexp-quote value))
(regexp-quote value)))
(name (orgmine-property-name key))
(property-regexp (format "^[ \t]*:%s:[ \t]+%s[ \t]*$"
(regexp-quote name) value-regexp)))
(catch 'found
(while (re-search-forward property-regexp end t)
(let ((pos (point))
(tags (progn
(outline-previous-heading)
(org-get-tags))))
(if (and (member tag tags)
(equal (nth 1 (orgmine-get-property nil key)) value))
(throw 'found (point)))
(goto-char pos)))
nil)))
(defun orgmine-find-headline-ancestor (tag &optional no-error)
"Find a headline with TAG going back to ancestor headlines.
Return org-element data of the headline found.
If not found and NO-ERROR, return nil. Otherwise, raise an error."
;; +Set point to the beginning of the headline found and return non-nil.+
(org-with-wide-buffer
(unless (eq (org-element-type (org-element-at-point)) 'headline)
(outline-previous-heading))
(catch 'found
(let (no-more-ancestor)
(while (not no-more-ancestor)
(let ((element (org-element-at-point)))
(cond ((member tag (org-element-property :tags element))
(throw 'found element))
((<= (funcall outline-level) 1)
(setq no-more-ancestor t)) ; not found
(t (outline-up-heading 1))))))
(unless no-error
(error "No redmine %s headline found" tag)))))
(defun orgmine-delete-headline (tag &optional end only-same-level)
"Search forward from point for headline with TAG
within the region between the current position and END.
If the headline is found, delete the subtree of the headline."
(save-excursion
(while (orgmine-find-headline tag end only-same-level)
(let ((region (orgmine-subtree-region)))
(delete-region (car region) (cdr region)))
(outline-next-heading))))
(defun orgmine-note (headline)
"return note in src-block element."
(save-excursion
(save-restriction
(let ((start (org-element-property :begin headline)))
(goto-char start)
(outline-next-heading)
(narrow-to-region start (point))
(let* ((tree (org-element-parse-buffer))
(src-block (org-element-map tree 'src-block 'identity nil t)))
(org-element-property :value src-block))))))
(defun orgmine-um-headline (beg end tag)
"return headlines with :UPDATE_ME: tag."
(save-excursion
(goto-char beg)
(let ((headline))
(while (orgmine-find-headline orgmine-tag-update-me end t)
(let ((tags (org-get-tags)))
(cond ((member tag tags)
(if headline
(error "More than one %s headlines for an entry." tag)
(setq headline (org-element-at-point))))
((or (member orgmine-tag-description tags)
(member orgmine-tag-attachments tags)
(member orgmine-tag-journal tags)
(member orgmine-tag-issue tags)
(member orgmine-tag-version tags)
(member orgmine-tag-tracker tags)
(member orgmine-tag-project tags)
(member orgmine-tag-wiki tags))) ; just ignore
(t (error "invalid headline %s for :UPDATE_ME: tag." tag))))
;; (outline-next-heading))
(outline-get-next-sibling))
headline)))
(defun orgmine-parse-attachments-plain-list (element)
"Parse plain list of attachments to upload.
Return list of plist (:path PATH :filename FILENAME :description DESCRIPTION)."
(if (not element)
nil
(save-excursion
(goto-char (org-element-property :begin element))
(let* ((end (cdr (orgmine-subtree-region)))
(plain-link-regexp "file:\\([^ \t\n]+\\)\\(?:::[^ \t\n]+\\)")
(bracket-link-regexp
(concat "\\[\\[\\(?:file:\\([^][]+\\)"
"\\(?:::\\(?:[^][]+\\)\\)?\\)\\]"
"\\(?:\\[\\([^][]+\\)\\]\\)?\\]"))
attachments)
(while (re-search-forward "^[ \t]*[+*-] +" end t)
(let ((plist
(cond ((looking-at plain-link-regexp)
(let* ((path (match-string-no-properties 1))
(filename (file-name-nondirectory path)))
(list :path path :filename filename)))
((looking-at bracket-link-regexp)
(let* ((path (match-string-no-properties 1))
(filename (file-name-nondirectory
(or (match-string-no-properties 2)
path))))
(list :path path :filename filename))))))
(when plist
(goto-char (match-end 0))
(if (looking-at "[ \t]*\\(.+\\)[ \t]*$")
(let ((description (match-string-no-properties 1)))
(setq plist (plist-put plist :description description))))
(add-to-list 'attachments plist t))))
attachments))))
(defun orgmine-um-headlines (beg end)
"return headlines with :UPDATE_ME: tag.
Return value: (DESCRIPTION JOURNAL ATTACHMENTS)"
(save-excursion
(let* ((description (orgmine-um-headline beg end orgmine-tag-description))
(attachments (orgmine-parse-attachments-plain-list
(orgmine-um-headline beg end orgmine-tag-attachments)))
(journals (progn
(goto-char beg)
(orgmine-find-headline orgmine-tag-journals end t)))
(journal (and journals
(goto-char journals)
(org-goto-first-child)
(orgmine-um-headline (point) end
orgmine-tag-journal))))
(list description journal attachments))))
(defun orgmine-current-issue-heading (&optional no-error)
"Move to the beginning of the current issue headline."
(let ((issue (orgmine-find-headline-ancestor orgmine-tag-issue no-error)))
(when issue
(goto-char (org-element-property :begin issue))
issue)))
(defun orgmine-current-entry-heading (&optional no-error)
"Move to the beginning of current entry headline
or move to current issue headline."
(condition-case err
(org-back-to-heading)
(error (unless no-error (error (nth 1 err)))))
(let ((tags (org-get-tags)))
(cond ((or (member orgmine-tag-project tags)
(member orgmine-tag-tracker tags)
(member orgmine-tag-version tags)
(member orgmine-tag-wiki tags))
(org-element-at-point))
(t (orgmine-current-issue-heading no-error)))))
(defun orgmine-property-name (key)
"Convert Redmine REST API property name to org-mode property name."
(format "om_%s" key))
(defun orgmine-prop (property)
;; "trcker" -> :tracker_id
(intern (format (if (orgmine-id-property-p property)
":%s_id" ":%s")
property)))
(defun orgmine-name (plist &optional format escape)
(let ((name (if format
(orgmine-format format plist)
(plist-get plist :name))))
(if (and name escape)
(replace-regexp-in-string " " "\\\\ " name)
name)))
(defun orgmine-idname (plist &optional format escape)
;; plist -> "ID:NAME"
(let ((id (plist-get plist :id))
(name (orgmine-name plist format escape)))
(cond ((and id name)
(format "%s:%s" id name))
(id
(elmine/ensure-string id)))))
(defun orgmine-delete-properties (pom regexp)
"Delete entry properties at POM which match REGEXP."
(let ((properties (orgmine-entry-properties pom 'all)))
(save-match-data
(mapc (lambda (prop)
(let ((property (car prop)))
(if (string-match regexp property)
;; (org-delete-property property))))
(org-entry-delete nil property))))
properties))))
(defun orgmine-custom-field-property-name (plist)
;; (:value "3" :name "Owner" :id 1) -> "om_cf_1_Owner"
(let ((org-url-hexify-p nil))
(format "om_cf_%s_%s"
(plist-get plist :id) (org-link-escape (plist-get plist :name)
'(? ?% ?:)))))
(defun orgmine-custom-field-plist (property-name)
;; "om_cf_1_Owner" -> (:name "Owner" :id 1)
(save-match-data
(if (string-match "^om_cf_\\([0-9]+\\)_\\(.*\\)" property-name)
(list :name (org-link-unescape (match-string 2 property-name))
:id (match-string 1 property-name)))))
(defun orgmine-set-properties-custom-fields (custom-fields)
;; ((:value \"3\" :name \"Owner\" :id 1))
;; erase "oc_cf_*" properties.
(orgmine-delete-properties nil "^om_cf_")
(mapc (lambda (cf-plist)
(let* ((name (orgmine-custom-field-property-name cf-plist))
(value (plist-get cf-plist :value))
(str-value (if (listp value)
(mapconcat 'org-entry-protect-space value " ")
(org-entry-protect-space
(elmine/ensure-string value)))))
(if (and value (> (length str-value) 0))
(org-set-property name str-value))))
custom-fields))
(defvar orgmine-relations
'(("duplicates" . "duplicated")
("blocks" . "blocked")
("precedes" . "follows")
("copied_to" . "copied_from")))
(defun orgmine-relation-property-type (plist &optional my-id)
(let ((type (plist-get plist :relation_type))
(issue-to-id (plist-get plist :issue_to_id)))
(if (equal my-id issue-to-id)
(or (cdr (assoc type orgmine-relations))
(car (rassoc type orgmine-relations))
type)
type)))
(defun orgmine-relation-property-name (plist &optional my-id)
;; (:relation_type "precedes" :issue_to_id 10 :delay 0 :id 1234)
;; -> "om_relation_1234_precedesr"
(let ((type (orgmine-relation-property-type plist my-id))
(id (plist-get plist :id)))
(format "om_relation_%s_%s" id type)))
(defun orgmine-relation-property-value (plist &optional my-id)
(let* ((type (orgmine-relation-property-type plist my-id))
(issue-to-id (plist-get plist :issue_to_id))
(other-id (elmine/ensure-string (if (equal my-id issue-to-id)
(plist-get plist :issue_id)
issue-to-id)))
(delay (plist-get relation :delay)))
(if (and (member type '("precedes" "follows")) delay)
(format "%s/d%s" other-id delay)
other-id)))
(defun orgmine-relation-plist (property &optional my-id)
;; "om_relation_1234_precedes" -> (:relation_type "precedes" :id 1234)
(save-match-data
(let ((name (car property))
(value (cdr property))
plist)
(if (string-match "^om_relation_\\(?:\\([0-9]+\\)_\\)?\\(.*\\)" name)
(let ((id (match-string 1 name))
(type (match-string 2 name)))
(if type
(progn (setq plist (list :relation_type type))
(if id (setq plist (plist-put plist :id id)))))))
(if (and plist
(string-match "^\\([0-9]+\\)\\(?:/d\\([0-9]+\\)\\)?" value))
(let* ((other-id (match-string 1 value))
(delay (match-string 2 value)))
(setq plist (plist-put plist :issue_to_id other-id))
(if my-id (setq plist (plist-put plist :issue_id my-id)))
(if delay (setq plist (plist-put plist :delay delay)))))
plist)))
(defun orgmine-set-properties-relations (relations redmine-issue)
(orgmine-delete-properties nil "^om_relation_")
(let ((id (plist-get redmine-issue :id)))
(mapc (lambda (relation)
(let* ((name (orgmine-relation-property-name relation id))
(value (orgmine-relation-property-value relation id)))