-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuse-init.el.bak
372 lines (351 loc) · 15.6 KB
/
muse-init.el.bak
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
(add-to-list 'load-path "~/.emacs.d/myBigEmacs/muse/lisp")
(load-file "~/.emacs.d/myEmacs/load-lisp/muse-header-footer.el")
(require 'muse-mode) ; load authoring mode
(require 'muse-html) ; load publishing styles I use
(require 'muse-latex)
(require 'muse-wiki)
(require 'muse-texinfo)
(require 'muse-docbook)
(require 'muse-project) ; publish files in projects
(require 'muse-colors)
;;{{{ don't force inline-css
(require 'htmlize)
(defun htmlize-region-for-paste (beg end)
"Htmlize the region and return just the HTML as a string.
This forces the `inline-css' style and only returns the HTML body,
but without the BODY tag. This should make it useful for inserting
the text to another HTML buffer."
(let ((htmlbuf (htmlize-region beg end)))
(unwind-protect
(with-current-buffer htmlbuf
(buffer-substring (plist-get htmlize-buffer-places 'content-start)
(plist-get htmlize-buffer-places 'content-end)))
(kill-buffer htmlbuf))))
;;}}}
(add-to-list 'magic-mode-alist
'("#title " . muse-mode))
(defvar muse-tag-alist
'(("example")
("literal")
("lisp" n)
("src" ("lang" ("emacs-lisp") ("perl") ("sql") ("c++") ("sh")) n))
"Tag list for `sgml-tag'.")
(defun my-muse-mode-hook ()
;; auto insert
(when (= (buffer-size) 0)
(let ((page (muse-page-name)))
(cond ((string= page "WikiIndex")
(insert "#title 目录\n"
"<lisp>(ywb-muse-index-as-string t t t)</lisp>\n"))
((string= page "RecentChanges")
(insert "#title 最近更新\n"
"<lisp>(ywb-muse-generate-recentchanges)</lisp>\n"))
(t (insert "#title ")))))
(auto-fill-mode 1)
(define-key muse-mode-map (kbd "C-c C-c") 'ywb-muse-preview-source)
(define-key muse-mode-map (kbd "C-c C-j") 'ywb-muse-preview-html)
(define-key muse-mode-map (kbd "C-c C-m") 'ywb-muse-preview-with-w3m)
;; borrow some command from sgml mode
(set (make-local-variable 'sgml-tag-alist) muse-tag-alist)
(modify-syntax-entry ?> ")" muse-mode-syntax-table)
(modify-syntax-entry ?< "(" muse-mode-syntax-table)
(define-key muse-mode-map (kbd "C-c /") 'sgml-close-tag)
(define-key muse-mode-map (kbd "C-c t") 'sgml-tag))
(add-hook 'muse-mode-hook 'my-muse-mode-hook)
(setq muse-wiki-ignore-bare-project-names t)
(defvar ywb-muse-recentchanges-page "RecentChanges"
"Recent change page name. See `ywb-muse-generate-recentchanges'")
;;{{{ project setting
(defvar my-emacs-muse-home "~/temp/tiddlyWiki/muse")
(setq muse-project-alist
`(
("Programming"
("~/temp/tiddlyWiki/muse/source/wiki/programming" :default "index" :force-publish ,(list ywb-muse-recentchanges-page))
(:base "html" :path "~/temp/tiddlyWiki/muse/publish/wiki/programming")
)
("WiKi" (,@(muse-project-alist-dirs "~/temp/tiddlyWiki/muse/source/wiki")
:default "index"
:force-publish ("WikiIndex")
)
,@(muse-project-alist-styles "~/temp/tiddlyWiki/muse/source/wiki"
"~/temp/tiddlyWiki/muse/publish/wiki"
"wiki-xhtml"))
("Default" (,@(muse-project-alist-dirs "~/temp/tiddlyWiki/muse/source/default")
:default "index"
:force-publish ("WikiIndex"))
,@(muse-project-alist-styles "~/temp/tiddlyWiki/muse/source/default"
"~/temp/tiddlyWiki/muse/publish"
"default-xhtml"))
;; ("Website"
;; ("~/Muse/Website" :default "index" :force-publish ,(list ywb-muse-recentchanges-page))
;; (:base "html" :path "~/Muse/publish_html/website"))
;; ("ElispIntro"
;; ("~/Muse/ElispIntro" :default "index" :force-publish ,(list ywb-muse-recentchanges-page))
;; (:base "html" :path "~/Muse/publish_html/elispintro"))
;; ("Programming"
;; ("~/Muse/Programming" :default "index" :force-publish ,(list ywb-muse-recentchanges-page))
;; (:base "html" :path "~/Muse/publish_html/programming"))
;; ("Other"
;; ("~/Muse/Other" :default "index" :force-publish ,(list ywb-muse-recentchanges-page))
;; (:base "html" :path "~/Muse/publish_html/other"))
;; ("Emacs"
;; ("~/Muse/Emacs" :default "index" :force-publish ,(list ywb-muse-recentchanges-page))
;; (:base "html" :path "~/Muse/publish_html/emacs"))
;; ("Perl"
;; ("~/Muse/Perl" :default "index" :force-publish ,(list ywb-muse-recentchanges-page))
;; (:base "html" :path "~/Muse/publish_html/perl"))
;; ("WikiPlanner"
;; ("~/Muse/Plan" :default "index"
;; :major-mode planner-mode
;; :visit-link planner-visit-link)
;; (:base "html" :path "~/Muse/publish_html/planner"))
;; ("Report"
;; ("~/Muse/Report" :default "index")
;; (:base "html" :path "~/work/Report/"))
;; ("StdLib"
;; ("~/Muse/StdLib" :default "index" :force-publish ,(list ywb-muse-recentchanges-page))
;; (:base "html" :path "~/Muse/publish_html/stdlib/"))
;; ("Test"
;; ("~/Muse/Test" :default "index")
;; (:base "html" :path "~/Muse/publish_html/test/"))
))
;;}}}
;;{{{ Recent change
(require 'file-stat nil t)
(when (not (fboundp 'file-stat-mtime))
(defsubst file-stat-mtime (file &optional id-format)
(nth 5 (file-attributes file id-format))))
(defvar ywb-muse-recentchanges-format "%Y年%m月%d日")
(defun ywb-muse-generate-recentchanges (&optional show-proj &rest project-list)
"Generate recent changes of project files.
If SHOW-PROJ is non-nil, the index will add project name.
If PROJECT-LIST is given, all changes in these projects will display"
(or project-list (setq project-list (list (car (muse-project)))))
(let ((curr-file (muse-current-file))
(curr-buf (current-buffer))
(content "")
last files header current changed beg pos)
;; if this file is not save yet, just return an empty string
(if (not (file-exists-p curr-file))
""
(with-temp-buffer
(insert-file-contents curr-file)
;; search for last change time stamp.
;; if there is one, get it and update the time stamp
;; if didn't have one, insert current time stamp after directives
(goto-char (point-min))
(if (re-search-forward "^; last time stamp: \\([0-9]+\\(\\.[0-9]+\\)?\\)"
nil t)
(progn
(setq last (seconds-to-time (string-to-number (match-string 1))))
;; (re-search-forward "^; last time stamp: \\([0-9.]+\\)" nil t)
(replace-match (number-to-string (float-time)) nil nil nil 1))
(re-search-forward "^[^#]" nil t)
(backward-char 1)
(insert (format "; last time stamp: %d\n" (float-time))))
;; get all file in the project-list newer than last
(setq files (ywb-muse-get-rc-page project-list (or last '(0 0))))
(when files
(re-search-forward "</lisp>")
(forward-line 1)
(setq beg (point))
;; insert href for the pages. Pages are collected under the
;; same header generated by `ywb-muse-recentchanges-format'
(setq header (format-time-string
ywb-muse-recentchanges-format
(nth 2 (car files))))
(insert "* " header "\n")
(dolist (file files)
(setq current (format-time-string
ywb-muse-recentchanges-format
(nth 2 file)))
(unless (string= current header)
(insert "\n* " current "\n")
(setq header current))
(insert " - [[" (car file) "#" (cadr file) "]["
(if show-proj (concat (car file) "-") "")
(cadr file) "]]"
;; if the page is not register in the this
;; recentchange, a new tag will add
(save-excursion
(if (re-search-forward
(regexp-quote (concat "[" (car file) "#" (cadr file) "]"))
nil t) "" " *(new)*"))
"\n"))
(setq pos (point))
(if (re-search-forward header nil t)
;; if we update this file in the same peroid, the duplicate
;; line should removed.
(progn
(re-search-forward "^[*]" nil t)
(setq content (mapconcat 'identity
(delete-dups (split-string (delete-and-extract-region beg (point)) "\n")) "\n"))
(insert content)
;; make change in publishing buffer
(save-excursion
(set-buffer curr-buf)
(save-restriction
(widen)
(goto-char (point-min))
(re-search-forward "^[*]" nil t)
(delete-region (point)
(progn
(re-search-forward "^[*]" nil t)
(point))))))
(setq content (buffer-substring beg (point))))
(write-region (point-min) (point-max) curr-file)
(message "Use M-x revert-buffer to update current buffer")))
content)))
(defun ywb-muse-get-rc-page (project-list newer)
(let (files mtime)
(dolist (proj project-list)
(dolist (file (muse-project-file-alist (muse-project proj)))
(when (file-exists-p (cdr file))
(setq mtime (file-stat-mtime (cdr file)))
(if (and (time-less-p newer mtime)
;; autosave file
(not (string-match "^\.#" (car file)))
;; the page itself
(not (string= ywb-muse-recentchanges-page (car file))))
(setq files (cons (list proj (car file) mtime) files))))))
(sort files (lambda (f1 f2) (time-less-p (nth 2 f2) (nth 2 f1))))))
;;}}}
;;{{{ Generate Index with title
(defun ywb-muse-index-as-string (&optional as-list exclude-private exclude-current &rest project-list)
"Generate the index of all wiki file using title.
See also `muse-index-as-string'.
PROJECT-LIST is the index of projects to insert.
"
(unless project-list
(setq project-list (list (car (muse-project)))))
(let ((current (muse-page-name))
(max-size 200) ; the max size to search for title
files title)
(with-temp-buffer
(dolist (project project-list)
(setq files
(sort (copy-alist (muse-project-file-alist project))
(function
(lambda (l r)
(string-lessp (car l) (car r))))))
(when (and exclude-current current)
(setq files (delete (assoc current files) files)))
(unless (= (length project-list) 1)
(insert "* " project "\n"))
(dolist (file files)
(when (and (file-exists-p (cdr file))
(not (and exclude-private
(muse-project-private-p (cdr file)))))
(insert " - [[" project "#" (car file) "]["
(with-temp-buffer
(insert-file-contents (cdr file) nil 0 max-size)
(goto-char (point-min))
(if (re-search-forward "^#title\\s-*" nil t)
(buffer-substring (point) (line-end-position))
(car file)))
"]]\n"))))
(insert "\n")
(buffer-string))))
;;}}}
;;{{{ color src tag
(defun muse-colors-src-tag (beg end)
"Strip properties and mark as literal."
(let (face)
(muse-unhighlight-region beg end)
(save-excursion
(goto-char beg)
(let ((fs 1) content face-list fe mode
(font-lock-verbose nil))
(when (re-search-forward "<src\\s-*lang=\"\\(.*\\)\"\\s-*>" nil t)
(setq mode (intern-soft (concat (match-string 1) "-mode"))
beg (match-end 0))
(when (and mode (fboundp mode))
(goto-char end)
(setq end
(if (re-search-backward "</src>" nil t)
(match-beginning 0)
(point-max))
content (buffer-substring-no-properties beg end))
(with-temp-buffer
(funcall mode)
(insert content)
(font-lock-fontify-buffer)
(or (get-text-property fs 'face)
(setq fs (next-single-property-change fs 'face)))
(while (and fs (< fs (point-max)))
(setq fe (or (next-single-property-change fs 'face)
(point-max))
face (get-text-property fs 'face))
(and face fe (setq face-list (cons (list (1- fs) (1- fe) face) face-list)))
(setq fs fe)))
(when face-list
(dolist (f (nreverse face-list))
(put-text-property (+ beg (car f)) (+ beg (cadr f))
'face (nth 2 f))))))))))
(add-to-list 'muse-colors-tags '("src" t nil nil muse-colors-src-tag))
;;}}}
;;{{{ math tag
(defun ywb-muse-publish-math-tag (beg end attrs)
(require 'org)
(let ((tag (or (cdr (assoc "tag" attrs)) "span")))
(insert (concat "<" tag " class=\"math\">"
(org-export-html-convert-sub-super
(delete-and-extract-region beg end))
"</" tag ">"))
(muse-publish-mark-read-only beg (point))))
(add-to-list 'muse-html-markup-tags
'("math" t t t ywb-muse-publish-math-tag))
;;}}}
;;{{{ Preview commands
(defun ywb-muse-output-file ()
(let ((style (muse-style
(muse-project-get-applicable-style buffer-file-name
(cddr muse-current-project)))))
(muse-publish-output-file buffer-file-name
(muse-style-element :path style) style)))
(defun ywb-muse-preview-with-w3m ()
"Preview the html file"
(interactive)
(muse-project-publish-this-file)
(let ((file (ywb-muse-output-file)))
(w3m-goto-url (if (string-match "^[a-zA-Z]:" file)
(ywb-convert-to-cygwin-path file)
(concat "file://" file)))))
(defun ywb-muse-preview-html ()
"Preview the html file"
(interactive)
(muse-project-publish-this-file)
(browse-url (ywb-muse-output-file)))
(defun ywb-muse-preview-source ()
"Find the html file"
(interactive)
(muse-project-publish-this-file)
(find-file (ywb-muse-output-file)))
;;}}}
(defun ywb-muse-create-wikisource ()
"Create all wikisource directory using file symbol link"
(interactive)
(dolist (proj muse-project-alist)
(let ((source (expand-file-name (car (cadr proj))))
(wikisource
(expand-file-name (concat (muse-get-keyword :path (nth 2 proj)) "/" "wikisource"))))
(when (and (file-exists-p source)
(not (file-exists-p wikisource)))
(message "Create link %s" wikisource)
(call-process "ln" nil nil nil "-s" source wikisource)))))
;; 增加 --all 参数发布所有项目,除去重复的 project 名
(defun muse-project-batch-publish ()
"Publish Muse files in batch mode."
(let ((muse-batch-publishing-p t)
force)
(if (string= "--force" (or (car command-line-args-left) ""))
(setq force t
command-line-args-left (cdr command-line-args-left)))
(if (string= "--all" (or (car command-line-args-left) ""))
(setq command-line-args-left (nconc (cdr command-line-args-left)
(mapcar 'car muse-project-alist))))
(if command-line-args-left
(dolist (project (delete-dups command-line-args-left))
(message "Publishing project %s ..." project)
(muse-project-publish project force))
(message "No projects specified."))))