-
Notifications
You must be signed in to change notification settings - Fork 5
/
tracwiki-mode.el
2134 lines (1970 loc) · 75.5 KB
/
tracwiki-mode.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
;;; tracwiki-mode.el --- Emacs Major mode for working with Trac
;; Copyright (C) 2013 Matthew Erickson <peawee@peawee.net>
;; Author: Matthew Erickson <peawee@peawee.net>
;; Maintainer: Matthew Erickson <peawee@peawee.net>
;; Created: March 13, 2013
;; Version: 0.2
;; Package-Requires: ((xml-rpc "1.6.8"))
;; Keywords: Trac, wiki, tickets
;; 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 2, 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, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; tracwiki-mode is a major mode for editing Trac-formatted text files
;; in GNU Emacs. tracwiki-mode is free software, licensed under the
;; GNU GPL.
;;
;; Starting in version 0.2, I've pretty much just copypasta'ed most of
;; the original trac-wiki from Shun-ichi GOTO:
;; http://trac-hacks.org/wiki/EmacsWikiEditScript Future versions will
;; include better English, yet more controls, and better
;; customization. Seriously though, without Shun-ichi Goto's work,
;; none of this magic would exist.
;;
;;
;;; Overview:
;; Overview:
;; Features:
;; * Multiple project access.
;; * Retrieve page from remote site and edit it with highlighting.
;; * Commit page with version check and detects remote update.
;; * Diff / Ediff between editing text and original.
;; * Revert local edit.
;; * Merge with most recent version if it is modified by other user.
;; * Show history of page (but not so informative)
;; * Preview page on Emacs with w3m (textual).
;; * Preview page with external browser with CSS.
;; * Search words on remote trac site and view result with highlighting.
;; * Jump to the page from search result.
;; * Completion for macro name and wiki page name in buffer.
;;
;; Requirement
;; (local side)
;; * Works on Emacs 21.4 or later.
;; * need xml-rpc.el with small patch for I18N (non-ascii)
;; * mule-ucs is required to use CJK text on Emacs 21.4.
;; * Optionally w3m and emacs-w3m is required for previewing.
;; (server side)
;; * Trac 0.10 or later.
;; * XmlRpcPlugin is installed and enabled.
;;
;; Restriction:
;; * It is not well on error handling (auth fail, spam-filtered, etc.)
;; * Cannot delete page version.
;; * Cannot operates tickets.
;; * Cannot treat tags provided by TagsPlugin.
;;
;;;;; Configuration:
;; Step 1. Get and enable XmlRpcPlugin on your trac site.
;; http://trac-hacks.org/wiki/XmlRpcPlugin
;;
;; Install and setup it referencing the page above for
;; documentation. Don't forget enabling the plugin in
;; trac.ini and adding permissions to allow user access via
;; XML-RPC. For example, it is recommended to add the
;; 'XML_RPC' permission to 'authenticated' subjects to allow
;; XML-RPC access only for reliable users.
;;
;; Step 2. Set the project information variable `trac-projects'
;; in your .emacs.
;;
;; If you have a trac site you frequently visit, you can
;; register the url of that site with a project name. To do this
;; use `tracwiki-define-project':
;;
;; (tracwiki-define-project "trac-hacks"
;; "http://trac-hacks.org/" t)
;;
;; 1st argument is the project name which is used on selection.
;; 2nd arugment is actual url.
;; 3rd optional argument indicates if login is required to access
;; the site.
;;
;; If you have multiple projects on one site, you can use
;; `tracwiki-define-multiple-projects'.
;; Ex.
;; (tracwiki-define-multiple-projects
;; '("proj1" "proj2" "test")
;; "http://www.foo.bar.org/" t)
;;
;; An example above is equivalent to three
;; `tracwiki-define-project' definition.
;;
;;
;; Step 3. Set proxy information.
;;
;; The url library gets proxy information via the variable
;; `url-proxy-services'. Ensure it is set in your .emacs if
;; you require proxying.
;;
;; See the info page of the url package for more detail.
;; Jump to the info node by evaluating this:
;; (info-goto-node "(url)Proxies")
;;
;; Step 4. Set autoload and more.
;;
;; Set the autoload definition in your .emacs for convenience:
;;
;; (autoload 'tracwiki "tracwiki"
;; "Trac wiki editing entry-point." t)
;;
;; Also load mule-ucs if you're an Emacs 21.x user.
;;; NOTICE:
;; There is an issue concerning authentication. If your target trac
;; site provides multiple authentication schemes (ex. both NTLM and
;; BASIC) and first one is not supported by the url package, the
;; authentication step is ignored. It's bug of url-http.el. On this
;; case, you may encounter endless user/pass query loops. For
;; example, this case will be encountered when the trac site uses
;; mod_auth_sspi for domain/ActiveDirectory authentication and
;; allowing fallback to basic authentication. This setting generates
;; two WWW-Authenticate: line and first one is NTLM auth and url
;; package cannot recognize it. Thus fail.
;;
;; To avoid this:
;; - Apply following patch
;; http://www.meadowy.org/~gotoh/trac-wiki/url-http.el-multi-auth.patch
;;
;; or
;;
;; - Preset auth information by your hand into url-basic-auth-storage
;; (or url-digets-auth-storage) variable like this:
;;
;; (let ((auth (base64-encode-string (format "%s:%s" user pass))))
;; (set (symbolvalue 'url-basic-real-auth-storage)
;; '(("www.some.org:80" (realm . auth))
;; ("www.other.net:80" ....
;;
;;; Usage:
;; You can start editing by `M-x tracwiki`.
;; Flow of editing is:
;; 1. M-x tracwiki
;; 2. Specify project name.
;; 3. Specify page name.
;; 4. Edit page content.
;; 5. Check differences.
;; 6. Preview page output.
;; 6. Commit it.
;; The `tracwiki' command asks you for the project name defined by
;; `tracwiki-define-project' or `tracwiki-define-multiple-projects'
;; with completion. If you want to specify the URL directly, hit
;; ENTER wihout any characters when you are asked for the project
;; name, then enter site the URL at the next prompt.
;;
;; Then it asks for the page name to edit with completion. Available
;; page names are retreived from the remote site by XML-RPC requests
;; dynamicaly. If you specified a non existing name, it means
;; creating a new page and start editing it from empty.
;;
;; After you edit the page content, you should commit by
;; `tracwiki-commit' (C-c C-c) to finish editing.
;; If you want to cancel editing, you can kill the buffer.
;; Or use `tracwiki-revert' (C-c C-u) to cancel changes in buffer.
;; While editing the page, the buffer is in `tracwiki-mode' which is
;; based on `text-mode'. You can specify some mode specific commands:
;;
;; C-c C-c ... `tracwiki-commit'
;; Commit edits in the current buffer.
;; The same project that created the buffer is used by default, or
;; specify the project with C-u.
;; C-c C-o ... `tracwiki-edit'
;; Edit another page in new buffer.
;; C-c C-p ... `tracwiki-preview'
;; Preview current content with w3m (text based).
;; With C-u, preview with an external browser (graphical).
;; C-c = ... `tracwiki-diff'
;; C-c C-d ... `tracwiki-diff'
;; Make a diff between the current content and the original content
;; With C-u, execute ediff instead of diff.
;; C-c C-m ... `tracwiki-merge'
;; Merge with most recent page content using `ediff-merge'.
;; If it is not modified, turn the current buffer to the newest version.
;; C-c C-u ... `tracwiki-revert'
;; Revert to the original content discarding current modifications.
;; It shows the diff and asks you to confirm before proceeding.
;; M-C-i ... `tracwiki-complete-at-point'
;; Complete macro name or page name on current point.
;; The macro names are collected from "WikiMacros" page on the
;; site and cached.
;; C-c C-h ... `tracwiki-history'
;; Show page history in other buffer.
;; History is information returned from xmlrpc plugin.
;; On each revision entry, you can show diff on its revision
;; with the '=' key.
;; C-c C-s ... `tracwiki-search'
;; Search on the project site for specified keywords.
;; You can specify keywords and filters. The result is shown
;; in another buffer with highlighting.
;;
;; Since outline minor mode is also enabled by tracwiki mode,
;; You can fold the sections.
;;; References:
;; - JSPWiki: Wiki RPC Interface 2
;; http://www.jspwiki.org/Wiki.jsp?page=WikiRPCInterface2
;;
;; - XmlRpcPlugin - Trac Hacks
;; http://trac-hacks.org/wiki/XmlRpcPlugin
;;
(require 'url)
(require 'url-http)
(require 'xml-rpc)
(eval-when-compile
(require 'cl)
(require 'w3m nil t)
(require 'ediff)
(require 'hi-lock))
;;; Constants
(defconst tracwiki-mode-version "0.2"
"Tracwiki mode version number")
;;; Customizable Variables
(defgroup tracwiki nil
"Major mode for editing text files and tickets from a Trac wiki."
:prefix "tracwiki-"
:group 'wp
:link '(url-link "https://github.com/merickson/tracwiki-mode"))
(defcustom trac-projects nil
"List of projects. More documentation coming soon, promise!"
:group 'tracwiki
:type 'sexp)
(defcustom tracwiki-uri-types
'("acap" "cid" "data" "dav" "fax" "file" "ftp" "gopher" "http" "https"
"imap" "ldap" "mailto" "mid" "modem" "news" "nfs" "nntp" "pop" "prospero"
"rtsp" "service" "sip" "tel" "telnet" "tip" "urn" "vemmi" "wais")
"Link types for syntax highlighting of URIs."
:group 'tracwiki
:type 'list)
(defcustom tracwiki-hide-system-pages t
"If non-nil, do not list system pages on completion.
System pages are defined by `tracwiki-system-pages' as a regexp.
Although these pages are not listed, you can visit them by
specifying a page name explicitly."
:group 'tracwiki
:type '(choice (const :tag "Off" nil)
(other :tag "On" t)))
(defcustom tracwiki-hidden-pages nil
"*List of regexps to be hidden on completion of a page name.
System pages are always hidden if `tracwiki-hide-system-pages' is
non-nil."
:group 'tracwiki
:type 'sexp)
(defcustom tracwiki-search-default-filters '("wiki")
"*List of search filter name(s) to use as the default.
Available filter names are:
wiki : Search in all wiki pages.
ticket : Search the description and comments of all tickets.
changeset : Search the commit log of all changesets."
:group 'tracwiki
:type 'sexp)
(defcustom tracwiki-max-history 100
"*Maximum number of wiki page revisions to fetch.
See `tracwiki-history'."
:group 'tracwiki
:type 'integer)
(defcustom tracwiki-history-count 10
"*Normal number of wiki page revisions to fetch.
See `tracwiki-history'."
:group 'tracwiki
:type 'integer)
(defcustom tracwiki-use-keepalive
(and (boundp 'url-http-real-basic-auth-storage)
url-http-attempt-keepalives)
"*If non-nil, use keep-alive option for http connection.
This value should be nil for old url library such as the one
on debian sarge stable ."
:group 'tracwiki
:type '(choice (const :tag "Off" nil)
(other :tag "On" t)))
(defcustom tracwiki-update-page-name-cache-on-visit t
"*If non-nil, update the page name cache on ever `tracwiki-edit' call.
Collecting page names might be expensive for some environments and uses.
By setting this variable to nil, the page name cache is updated only when
invoking the `tracwiki' command or doing completion with the prefix in the
page edit buffer.")
;;;
;;; Internal variables
;;;
(defconst tracwiki-diff-buffer-name
(if (<= 22 emacs-major-version)
"*Diff*"
"*diff*")
"Buffer name of `diff' output.")
(defvar tracwiki-macro-name-cache nil
"Alist of endpoint and macro name list.")
(defvar tracwiki-page-name-cache nil
"Alist of endpoint and page name list.")
(defvar trac-rpc-endpoint nil)
(make-variable-buffer-local 'trac-rpc-endpoint)
(defvar tracwiki-project-info nil)
(make-variable-buffer-local 'tracwiki-project-info)
(defvar tracwiki-page-info nil)
(make-variable-buffer-local 'tracwiki-page-info)
(defvar tracwiki-search-keyword-hist nil)
(defvar tracwiki-search-filter-hist nil)
(defvar tracwiki-search-filter-cache nil
"Alist of end-point and list of filter names supported in site.
This value is made automaticaly on first search access.")
(defconst tracwiki-system-pages
'("CamelCase" "InterMapTxt" "InterTrac" "InterWiki"
"RecentChanges" "TitleIndex"
"TracAccessibility" "TracAdmin" "TracBackup" "TracBrowser"
"TracCgi" "TracChangeset" "TracEnvironment" "TracFastCgi"
"TracGuide" "TracHacks" "TracImport" "TracIni"
"TracInstall" "TracInstallPlatforms" "TracInterfaceCustomization"
"TracLinks" "TracLogging" "TracModPython" "TracMultipleProjects"
"TracNotification" "TracPermissions" "TracPlugins" "TracQuery"
"TracReports" "TracRevisionLog" "TracRoadmap" "TracRss"
"TracSearch" "TracStandalone" "TracSupport" "TracSyntaxColoring"
"TracTickets" "TracTicketsCustomFields" "TracTimeline"
"TracUnicode" "TracUpgrade" "TracWiki" "WikiDeletePage"
"WikiFormatting" "WikiHtml" "WikiMacros" "WikiNewPage"
"WikiPageNames" "WikiProcessors" "WikiRestructuredText"
"WikiRestructuredTextLinks"
;; appeared in 0.11
"TracWorkflow" "PageTemplates")
"List of page names provided by trac as default.
These files are hidden on completion since not edited usualy.
These can be listed setting by variable
`tracwiki-hide-system-pages' as nil.
Two pages WikiStart and SandBox is not in this list because
user may need or want to edit them.")
(defconst tracwiki-link-type-keywords
'("ticket" "comment" "report" "changeset" "log" "diff" "wiki"
"milestone" "attachment" "source")
"Trac link type keywords to be used in font-lock.")
(defun tracwiki-link-face (face)
"Return FACE if not escaped by '!', or return 'normal."
(if (eq (char-before (match-beginning 0)) ?!)
'shadow
face))
(defconst tracwiki-camel-case-regexp
"\\<\\([A-Z][a-z]+\\(?:[A-Z][a-z]*[a-z/]\\)+\\)"
"Regular expression for camel case.")
;; for tracwiki mode, simple
(defvar tracwiki-font-lock-keywords
`(("^\\(\\(=+\\) \\(.*\\) \\(=+\\)\\)\\(.*\\)" ; section heading
(1 (if (string= (match-string 2) (match-string 4))
'bold
;; Warn if starting/ending '=' count is not ballanced.
'font-lock-warning-face))
(5 'shadow))
("^=.*" . font-lock-warning-face) ; invalid section heading
("`[^`\n]*`" . 'shadow) ; inline quote
("\\(''+\\)[^'\n]*\\(''+\\)" ; bold and italic
(0 (let ((b (match-string 1))
(e (match-string 2)))
(if (not (string= b e))
font-lock-warning-face
(cond
((string= b "''") 'italic)
((string= b "'''") 'bold)
((string= b "''''") 'bold-italic))))))
("\\[\\[\\([^]()]+\\)[^]\n]*\\]\\]" ; macro
;; font-lock-preprocessor-face is not defined in emacs 21
(0 (tracwiki-link-face
(if (tracwiki-macro-exist-p (match-string 1))
font-lock-type-face
font-lock-warning-face))))
("{[0-9]+}" ; {1}
(0 (tracwiki-link-face font-lock-type-face)))
("\\[\\(\\w+:\\)?\\([^] #\n]*\\)[^]\n]*\\]" ; bracket trac link
(0 (let ((whole (match-string 0))
(scheme (match-string 1))
(name (match-string 2)))
(tracwiki-link-face
(cond
((save-match-data ; [1], [1/trunk], [../file], [/trunk]
(string-match "^\\[[1-9./]" whole))
font-lock-function-name-face)
((or (string= scheme "wiki:")
(null scheme))
(if (tracwiki-page-exist-p name)
font-lock-function-name-face
font-lock-warning-face))
(t
font-lock-function-name-face))))))
(,(format "\\(?:%s\\):\\(?:\"[^\"\n]*\"\\|[^ \t\n]\\)+" ; types
(regexp-opt tracwiki-link-type-keywords))
(0 (tracwiki-link-face font-lock-function-name-face)))
("\\w+://[^ \t\n]+" . font-lock-function-name-face) ; raw url
("\\(?:#\\|\\br\\)[0-9]+\\(?::[0-9a-z]+\\)?\\b" ; r123 or #123
(0 (tracwiki-link-face font-lock-function-name-face)))
(,(concat tracwiki-camel-case-regexp ; camel case
"\\(?:#\\(?:\\w\\|[-:.]\\)+\\)?\\>") ; fragments
(0 (tracwiki-link-face
(if (tracwiki-page-exist-p (match-string 1))
font-lock-function-name-face
font-lock-warning-face))))
("||" . 'shadow) ; table delimiter
)
"For `tracwiki-mode'.")
;; for history buffer
(defvar tracwiki-history-font-lock-keywords
'(("^\\([^:]+:\\) +\\(.*\\)"
(1 'bold)
(2 'shadow)))
"For history buffer.")
;; for search result
(defvar tracwiki-search-result-font-lock-keywords
'(("^\\([^ \n]+\\):.*"
(1 'bold))
("^ \\(http://.*\\)"
(1 font-lock-function-name-face))
("^\\(By \\w+ -- [-: 0-9]+\\)\n\n"
(1 font-lock-type-face)))
"For search result buffer.")
;; history holder
(defvar tracwiki-project-history nil)
(defvar tracwiki-url-history nil)
(defvar tracwiki-page-history nil)
(defvar tracwiki-comment-history nil)
;; key map
(defvar tracwiki-mode-map (make-sparse-keymap))
(define-key tracwiki-mode-map "\C-c\C-c" 'tracwiki-commit)
(define-key tracwiki-mode-map "\C-c=" 'tracwiki-diff)
(define-key tracwiki-mode-map "\C-c\C-d" 'tracwiki-diff)
(define-key tracwiki-mode-map "\C-c\C-u" 'tracwiki-revert)
(define-key tracwiki-mode-map "\C-c\C-m" 'tracwiki-merge)
(define-key tracwiki-mode-map "\C-c\C-p" 'tracwiki-preview)
(define-key tracwiki-mode-map "\C-c\C-l" 'tracwiki-history)
(define-key tracwiki-mode-map "\C-c\C-o" 'tracwiki-edit)
(define-key tracwiki-mode-map "\C-c\C-s" 'tracwiki-search)
(define-key tracwiki-mode-map "\C-c\C-v" 'tracwiki-view-page)
(define-key tracwiki-mode-map "\C-c\C-i" 'tracwiki-show-info)
(define-key tracwiki-mode-map "\C-\M-i" 'tracwiki-complete-at-point)
(define-key tracwiki-mode-map "\C-x\C-s" 'tracwiki-save)
(defvar tracwiki-search-result-mode-map nil)
(let ((map (make-sparse-keymap)))
(define-key map "n" 'tracwiki-search-result-next)
(define-key map "p" 'tracwiki-search-result-prev)
(define-key map "e" 'tracwiki-search-result-edit)
(define-key map "o" 'tracwiki-search-result-edit)
(define-key map "\C-c\C-s" 'tracwiki-search)
(define-key map "s" 'tracwiki-search)
(define-key map "q" 'tracwiki-delete-window-or-bury-buffer)
(setq tracwiki-search-result-mode-map map))
;; accessor
(defsubst tracwiki-page-version ()
"Get page version of current page in buffer.
If editing page is newly created, returns 0."
(if (null tracwiki-page-info)
(error "Page information is not exist!"))
(cdr (assoc "version" tracwiki-page-info)))
(defsubst tracwiki-page-name ()
"Get page name of current page."
(if (null tracwiki-page-info)
(error "Page information is not exist!"))
(cdr (assoc "name" tracwiki-page-info)))
(defsubst tracwiki-page-author ()
"Get last modified author of current page.
If editing page is newly created, return nil."
(if (null tracwiki-page-info)
(error "Page information is not exist!"))
(cdr (assoc "author" tracwiki-page-info)))
(defsubst tracwiki-page-hash ()
"Get hash value of original page content.
If editing page is newly created, return hash of empty page."
(if (null tracwiki-page-info)
(error "Page information is not exist!"))
(cdr (assoc "hash" tracwiki-page-info)))
(defsubst tracwiki-page-modified-time ()
"Get last modified time as readable format.
If editing page is newly created, return buffer creation time."
(let ((modified (cdr (assoc "lastModified" tracwiki-page-info))))
(tracwiki-convert-to-readable-time-string modified)))
;; workaround for old emacs/url
(when (eq emacs-major-version 21)
;; mail-header-extract<f> is required by url-extract-mime-headers<f>
;; but it does not do require<f>. So I do it.
(require 'mailheader)
(defmacro with-local-quit (&rest body)
(declare (debug t) (indent 0))
`(condition-case nil
(let ((inhibit-quit nil))
,@body)
(quit (setq quit-flag t)
(eval '(ignore nil)))))
;; adapt to new multi-byte handling.
(defadvice encode-coding-string (around tracwiki (str enc))
"Adapt to allow emacs 22 style multi-byte operation."
(setq ad-return-value
(with-temp-buffer
(insert str)
(encode-coding-region
(point-min) (point-max) enc)
(buffer-string))))
;; Making callback argument.
(defadvice url-retrieve (before tracwiki
(url &optional callback args) activate)
"Bug workaround advice for Emacs 21 and w3-url-e21(2005.10.23-5).
Url package (debian testing, stable) for Emacs 21.4 has a bug.
It seems the function `url-retrieve-synchronously' does not pass
callback argument but `url-http-handle-authentication' expects url is
in 1st element of callback argument. This advice fakes for this."
(if (null args)
(setq args (list url))))
;;; these 2 advices are for asking on every 401 auth reply.
(defadvice url-http-handle-authentication (before tracwiki (proxy) activate)
"Clear auth info if last request with authentication is failed.
This behaviour is required for old url libraries as workaround because
it re-use bad auth data on retrying although authentication is failed."
(when (< 0 tracwiki-auth-retry-count) ; internal global variable
(setq url-http-real-basic-auth-storage nil
url-http-real-digest-auth-storage nil))
(setq tracwiki-auth-retry-count (1+ tracwiki-auth-retry-count)))
)
(when (and (< emacs-major-version 22)
(boundp 'url-basic-auth-storage)
(not (boundp 'url-http-real-basic-auth-storage)))
;; This is for Emacs 21 and old url library (on debian sarge stable)
;; Old url library does not remember authentication data
;; due to local binding in url-http-handle-authentication<f>.
;; So it asked you user/pass every time.
;; This advice grab the authentication info very after
;; prompted and hold to use on next time.
(defvar trac-rpc-basic-auth-storage nil
"Grabbed basic authentication data")
(defvar trac-rpc-digest-auth-storage nil
"Grabbed digest authentication data")
(defadvice url-get-authentication (around tracwiki activate)
"Trap to grab authentication data."
;; remember auth info into our own storage
(let ((url-basic-auth-storage trac-rpc-basic-auth-storage)
(url-digest-auth-storage trac-rpc-digest-auth-storage))
ad-do-it
(setq trac-rpc-basic-auth-storage url-basic-auth-storage
trac-rpc-digest-auth-storage url-digest-auth-storage))))
(if (not (fboundp 'buffer-local-value))
(defun buffer-local-value (sym buf)
(if (not (buffer-live-p buf))
nil
(with-current-buffer buf
(if (boundp sym)
(symbol-value sym)
nil)))))
(defun tracwiki-url-retrieve-synchronously (url)
"Fetch the content from URL.
This is alternative function of `url-retrieve-synchronously' to
avoid buf of url library. Url library comes with Emacs 22 and
older veresion has a problem on synchronizing when authentication
is occured. The code is copied from `url.el' of Emacs 22 and
modified not to quit by closing (or exiting) of first process.
Without this, `url-retrieve-synchronously' returns wrong buffer
on getting response \"403: auth required\"."
(url-do-setup)
(lexical-let ((retrieval-done nil)
(asynch-buffer nil))
(setq asynch-buffer
(url-retrieve url (lambda (&rest ignored)
(url-debug 'retrieval "Synchronous fetching done (%S)" (current-buffer))
(setq retrieval-done t
asynch-buffer (current-buffer)))))
(if (null asynch-buffer)
;; We do not need to do anything, it was a mailto or something
;; similar that takes processing completely outside of the URL
;; package.
nil
(let ((proc (get-buffer-process asynch-buffer)))
;; If the access method was synchronous, `retrieval-done' should
;; hopefully already be set to t. If it is nil, and `proc' is also
;; nil, it implies that the async process is not running in
;; asynch-buffer. This happens e.g. for FTP files. In such a case
;; url-file.el should probably set something like a `url-process'
;; buffer-local variable so we can find the exact process that we
;; should be waiting for. In the mean time, we'll just wait for any
;; process output.
(while (not retrieval-done)
(url-debug 'retrieval
"Spinning in url-retrieve-synchronously: %S (%S)"
retrieval-done asynch-buffer)
(if (buffer-local-value 'url-redirect-buffer asynch-buffer)
(setq proc (get-buffer-process
(setq asynch-buffer
(buffer-local-value 'url-redirect-buffer
asynch-buffer))))
(if (and proc (memq (process-status proc)
;;'(closed exit signal failed))
;; MODIFIED! do not accept 'closed nor 'exit
'(signal failed))
;; Make sure another process hasn't been started.
(eq proc (or (get-buffer-process asynch-buffer) proc)))
;; FIXME: It's not clear whether url-retrieve's callback is
;; guaranteed to be called or not. It seems that url-http
;; decides sometimes consciously not to call it, so it's not
;; clear that it's a bug, but even then we need to decide how
;; url-http can then warn us that the download has completed.
;; In the mean time, we use this here workaround.
;; XXX: The callback must always be called. Any
;; exception is a bug that should be fixed, not worked
;; around.
(setq retrieval-done t))
;; We used to use `sit-for' here, but in some cases it wouldn't
;; work because apparently pending keyboard input would always
;; interrupt it before it got a chance to handle process input.
;; `sleep-for' was tried but it lead to other forms of
;; hanging. --Stef
(unless (or (with-local-quit
(accept-process-output proc))
(null proc))
;; accept-process-output returned nil, maybe because the process
;; exited (and may have been replaced with another). If we got
;; a quit, just stop.
(sit-for 0.01) ; give a chance to callback
(when quit-flag
(delete-process proc))
(setq proc (and (not quit-flag)
(get-buffer-process asynch-buffer)))))))
;; wait a little to avoid bug in e21 not to re-use closing session.
(if (< emacs-major-version 22)
(sleep-for 0.05)) ; not to re-use closing session
;; fix-up lacked tail. On old emacs (emacs 21), sometime some bytes
;; of response are lacked. like "</methodRespons"
;; So fix it.
(with-current-buffer asynch-buffer
(save-excursion
(goto-char (point-max))
(beginning-of-line)
(when (looking-at "</method")
(delete-region (point) (save-excursion
(end-of-line) (point)))
(insert "</methodResponse>\n")
(message "*** Fixed http response ***")
(sit-for 0.5))))
asynch-buffer)))
(defvar tracwiki-auth-retry-count 0
"Internal global variable to count auth retry.")
(defadvice url-retrieve-synchronously (around tracwiki (url) activate)
"Advice to use our alternative function.
And aslo providing workaround of buf for storing auth info in old bad
url library behaviour."
;; to avoid endless auth retry wihtout asking in old url library.
(let ((orig-basic-auth-storage (copy-sequence
url-http-real-basic-auth-storage)))
(unwind-protect
(setq tracwiki-auth-retry-count 0
ad-return-value (tracwiki-url-retrieve-synchronously url))
;; restore original if fail
;; merge with original if success
(if (and ad-return-value
(with-current-buffer ad-return-value
(save-excursion
(goto-char 1)
(looking-at "HTTP/[0-9.]+ 2[0-9]+"))))
;; success, merge backuped storage into real storage
(tracwiki-merge-storage url-http-real-basic-auth-storage
orig-basic-auth-storage)
;; failed, restore original
(setq url-http-real-basic-auth-storage
orig-basic-auth-storage))
;; clean up on ether successed or fail.
(tracwiki-cleanup-auth-storage url-http-real-basic-auth-storage))))
;; predicate
(defun tracwiki-page-exist-p (page)
"Return non-nil if PAGE exists in page name cache or no cache.
Note that if buffer does not has end-point information, return
also non-nil because we cannot get cache data. In other word,
\"I don't know\" is non-nil."
(or (null trac-rpc-endpoint)
(tracwiki-cache-item-exist-p page tracwiki-page-name-cache)))
(defun tracwiki-macro-exist-p (macro)
"Return non-nil if MACRO exists in macro name cache or no cache.
Note that if buffer does not has end-point information, return
also non-nil because we cannot get cache data. In other word,
\"I don't know\" is non-nil."
(or (null trac-rpc-endpoint)
(tracwiki-cache-item-exist-p macro tracwiki-macro-name-cache)))
(defun tracwiki-cache-item-exist-p (item cache)
"Return non-nil if ITEM exists in CACHE or CACHE is nil.
Note that if buffer does not has end-point information, return
also non-nil because we cannot get cache data. In other word,
\"I don't know\" is non-nil."
(let ((items (and trac-rpc-endpoint
(cdr (assoc trac-rpc-endpoint cache)))))
(or (null items)
(member item items))))
;; cache macro
(defmacro tracwiki-with-cache (cache-name ep no-cache &rest body)
"Update CACHE-NAME for EP regarding NO-CACHE with result of BODY.
CACHE-NAME is symbol of variable which is cache data storage formated
as alist of end-point and cache data.
EP is end-point string and works as key of cache data to select.
If NO-CACHE is nil, return data in cache if exist without executing
BODY. If NO-CACHE is non-nil, always run BODY and update cache with its
result data."
`(let* ((entry (assoc ,ep (symbol-value ,cache-name)))
(data (if (and (not ,no-cache) entry)
(cdr entry)
,@body)))
(if (null entry)
(set ,cache-name (cons (cons ,ep data)
(symbol-value ,cache-name)))
(setcdr entry data))
data))
(put 'tracwiki-with-cache 'lisp-indent-function 3)
(defun tracwiki-update-page-name-cache ()
"Update cache of wiki page names."
(interactive)
(prog1
(tracwiki-with-cache
'tracwiki-page-name-cache
trac-rpc-endpoint 'update
(trac-rpc-get-all-pages))
(if (interactive-p)
(message "Page name cache is updated."))))
(defun tracwiki-update-macro-name-cache ()
"Update cache of wiki macro names."
(interactive)
(prog1
(tracwiki-with-cache
'tracwiki-macro-name-cache
trac-rpc-endpoint 'update
(tracwiki-collect-macro-names))
(if (interactive-p)
(message "Macro name cache is updated."))))
;; mode
(define-derived-mode tracwiki-mode text-mode "TracWiki"
"Trac Wiki authorizing mode with XML-RPC access."
(set (make-local-variable 'font-lock-defaults)
'(tracwiki-font-lock-keywords t))
(require 'font-lock)
(if font-lock-mode
(font-lock-fontify-buffer))
(set (make-local-variable 'outline-regexp) "^=+ ")
(outline-minor-mode 1))
;; XML-RPC functions
(defun trac-rpc-call (method &rest args)
"Call METHOD with ARGS via XML-RPC and return response data.
WARNING: This functionis not use because synchronous
`xml-rpc-method-call' has strange behavour on authentication
retrying. Use `trac-rpc-call-async' instead."
(when (< emacs-major-version 22)
(ad-activate 'encode-coding-string))
(unwind-protect
(let* ((url-http-attempt-keepalives tracwiki-use-keepalive)
(ep trac-rpc-endpoint)
(xml-rpc-base64-encode-unicode nil)
(xml-rpc-base64-decode-unicode nil)
(result (with-temp-buffer
(apply 'xml-rpc-method-call
ep method args))))
(if (and (numberp result) (= result 0))
nil
(if (stringp result)
(apply `concat (split-string result "\r"))
result)))
(when (< emacs-major-version 22)
(ad-deactivate 'encode-coding-string))))
(defun trac-rpc-get-page (page &optional version)
"Get content of PAGE in VERSION invoking XML-RPC call.
If VERSION is omitted, most recent version is selected."
(if version
(trac-rpc-call 'wiki.getPageVersion page version)
(trac-rpc-call 'wiki.getPage page)))
(defun trac-rpc-get-page-info (page &optional version)
"Get information of PAGE in VERSION invoking XML-RPC call.
If VERSION is omitted, most recent version is selected."
(if version
(trac-rpc-call 'wiki.getPageInfoVersion page version)
(trac-rpc-call 'wiki.getPageInfo page)))
(defun trac-rpc-get-page-html (page &optional version)
"Get rendered content of PAGE in VERSION invoking XML-RPC call.
If VERSION is omitted, most recent version is selected."
(if version
(trac-rpc-call 'wiki.getPageHTMLVersion page version)
(trac-rpc-call 'wiki.getPageHTML page)))
(defun trac-rpc-get-all-pages (&optional endpoint)
"Get list of page names available in remote site of ENDPOINT.
If optional argument EP is nil, use `trac-rpc-endpoint' is used."
(let ((trac-rpc-endpoint (or endpoint trac-rpc-endpoint)))
(trac-rpc-call 'wiki.getAllPages)))
(defun trac-rpc-put-page (page content comment)
"Update PAGE as CONTENT with COMMENT.
COMMENT can be nil."
(let ((attributes `(("comment" . ,(or comment "")))))
(trac-rpc-call 'wiki.putPage page content attributes)))
(defun trac-rpc-wiki-to-html (content)
"Covnert wiki CONTENT into html via XML-RPC method call."
(trac-rpc-call 'wiki.wikiToHtml content))
(defun trac-rpc-get-page-version (&optional page)
"Get latest version of PAGE in remote."
(if (or (null tracwiki-page-info)
(null trac-rpc-endpoint))
(error "Page information is not exist!"))
(let ((info (trac-rpc-get-page-info
(or page (tracwiki-page-name)))))
(if (null info)
0 ; no page, return version 0
(cdr-safe (assoc "version" info)))))
;; mode functions and utilities
(defun tracwiki-read-page-name (&optional default)
"Enter page name with competion.
If DEFAULT is specified, use it as initial input on completion."
(let ((cached (and (not tracwiki-update-page-name-cache-on-visit)
(cdr (assoc trac-rpc-endpoint
tracwiki-page-name-cache))))
(all (tracwiki-with-cache
'tracwiki-page-name-cache
trac-rpc-endpoint tracwiki-update-page-name-cache-on-visit
(trac-rpc-get-all-pages)))
(re (concat "^\\(?:"
(if tracwiki-hide-system-pages
(concat (regexp-opt tracwiki-system-pages) "\\|"))
(mapconcat 'identity tracwiki-hidden-pages "\\|")
"\\)\\(?:\\.[a-z]\\{2\\}\\)?$")) ; lang suffix
pages page)
(dolist (page all)
(unless (string-match re page)
(add-to-list 'pages page)))
(while (null page)
(setq page (completing-read (if cached "Page name (cached): "
"Page name: ")
(mapcar 'list pages)
nil nil default 'tracwiki-page-history)))
page))
(defun tracwiki-save ()
"Alternative function to avoid usual file save function."
(interactive)
(cond
((buffer-file-name)
(save-buffer)) ; usual save when editing local file
((y-or-n-p "You are editing remote page. Save to file? ")
;; add header information and write to file
(let ((content (buffer-string))
(filename))
(with-temp-buffer
(insert content)
(setq filename (read-file-name "Enter filename to save: "))
;; save to file with confirming overwriting.
(write-region (point-min) (point-max) filename nil nil nil t))
;; ask to visit onto saved file.
(if (and filename (y-or-n-p "Visit to saved file? "))
(find-file-other-window filename)
(message "saved."))))
(t
(message "Canceled. (You may want to commit current page by %s)"
(substitute-command-keys "\\[tracwiki-commit]")))))
(defun tracwiki-ask-project ()
"Prompts to enter project name and return its information data.
Returns project info which is property list of some data. If hit
enter without project name, ask enter project informations
interectively and remember temporary project information data
named as \"dir@host\". It will be kept until re-start Emacs."
(let* ((project (and (or trac-projects
tracwiki-project-history)
(completing-read "Select project (or empty to define): "
trac-projects
nil t nil
'tracwiki-project-history)))
(pinfo (and project
(cdr (assoc project trac-projects)))))
(or pinfo
;; make project data interactively.
(let* ((rawurl (read-string "Site URL: "
trac-rpc-endpoint
'tracwiki-url-history))
(url (url-generic-parse-url
(tracwiki-strip-url-trailer
rawurl '("xmlrpc" "login" "wiki"))))
(login (or (elt url 1)
(string-match "/login\\(?:/\\|$\\)" rawurl)
(y-or-n-p "Login? ")))
(host (elt url 3))
(name (file-name-nondirectory
(directory-file-name (elt url 5))))
(project-name (format "%s@%s" name host))
info)
;; build project info property list
(prog1
;; make return value
(setq info (list :name project-name
:endpoint (format "%s://%s:%s%s%s/xmlrpc"
(elt url 0)
(elt url 3)
(elt url 4)
(directory-file-name (elt url 5))
(if (string= login "")
""