1 ;;; org-freemind.el --- Export Org files to freemind
3 ;; Copyright (C) 2009 Free Software Foundation, Inc.
5 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; --------------------------------------------------------------------
26 ;; Features that might be required by this library:
28 ;; `backquote', `bytecomp', `cl', `easymenu', `font-lock',
29 ;; `noutline', `org', `org-compat', `org-faces', `org-footnote',
30 ;; `org-list', `org-macs', `org-src', `outline', `syntax',
31 ;; `time-date', `xml'.
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;; This file tries to implement some functions useful for
38 ;; transformation between org-mode and FreeMind files.
40 ;; Here are the commands you can use:
42 ;; M-x `org-freemind-from-org-mode'
43 ;; M-x `org-freemind-from-org-mode-node'
44 ;; M-x `org-freemind-from-org-sparse-tree'
46 ;; M-x `org-freemind-to-org-mode'
48 ;; M-x `org-freemind-show'
50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
54 ;; 2009-02-15: Added check for next level=current+1
55 ;; 2009-02-21: Fixed bug in `org-freemind-to-org-mode'.
56 ;; 2009-10-25: Added support for `org-odd-levels-only'.
57 ;; Added y/n question before showing in FreeMind.
58 ;; 2009-11-04: Added support for #+BEGIN_HTML.
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63 ;; This program is free software; you can redistribute it and/or
64 ;; modify it under the terms of the GNU General Public License as
65 ;; published by the Free Software Foundation; either version 2, or
66 ;; (at your option) any later version.
68 ;; This program is distributed in the hope that it will be useful,
69 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
70 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
71 ;; General Public License for more details.
73 ;; You should have received a copy of the GNU General Public License
74 ;; along with this program; see the file COPYING. If not, write to
75 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
76 ;; Floor, Boston, MA 02110-1301, USA.
78 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
85 (eval-when-compile (require 'cl
))
87 ;; Fix-me: I am not sure these are useful:
89 ;; (defcustom org-freemind-main-fgcolor "black"
90 ;; "Color of main node's text."
94 ;; (defcustom org-freemind-main-color "black"
95 ;; "Background color of main node."
99 ;; (defcustom org-freemind-child-fgcolor "black"
100 ;; "Color of child nodes' text."
104 ;; (defcustom org-freemind-child-color "black"
105 ;; "Background color of child nodes."
109 (defvar org-freemind-node-style nil
"Internal use.")
111 (defcustom org-freemind-node-styles nil
112 "Styles to apply to node.
115 (list :tag
"Node styles for file"
116 (regexp :tag
"File name")
119 (regexp :tag
"Node name regexp")
120 (set :tag
"Node properties"
121 (list :format
"%v" (const :format
"" node-style
)
126 (list :format
"%v" (const :format
"" color
)
127 (color :tag
"Color" :value
"red"))
128 (list :format
"%v" (const :format
"" background-color
)
129 (color :tag
"Background color" :value
"yellow"))
130 (list :format
"%v" (const :format
"" edge-color
)
131 (color :tag
"Edge color" :value
"green"))
132 (list :format
"%v" (const :format
"" edge-style
)
133 (choice :tag
"Edge style" :value bezier
134 (const :tag
"Linear" linear
)
135 (const :tag
"Bezier" bezier
)
136 (const :tag
"Sharp Linear" sharp-linear
)
137 (const :tag
"Sharp Bezier" sharp-bezier
)))
138 (list :format
"%v" (const :format
"" edge-width
)
139 (choice :tag
"Edge width" :value thin
140 (const :tag
"Parent" parent
)
141 (const :tag
"Thin" thin
)
146 (list :format
"%v" (const :format
"" italic
)
147 (const :tag
"Italic font" t
))
148 (list :format
"%v" (const :format
"" bold
)
149 (const :tag
"Bold font" t
))
150 (list :format
"%v" (const :format
"" font-name
)
151 (string :tag
"Font name" :value
"SansSerif"))
152 (list :format
"%v" (const :format
"" font-size
)
153 (integer :tag
"Font size" :value
12)))))))
157 (defun org-export-as-freemind (arg &optional hidden ext-plist
158 to-buffer body-only pub-dir
)
160 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
162 (org-infile-export-plist)))
163 (region-p (org-region-active-p))
164 (rbeg (and region-p
(region-beginning)))
165 (rend (and region-p
(region-end)))
167 (if (plist-get opt-plist
:ignore-subtree-p
)
172 (and (org-at-heading-p)
173 (>= (org-end-of-subtree t t
) rend
))))))
174 (opt-plist (setq org-export-opt-plist
176 (org-export-add-subtree-options opt-plist rbeg
)
178 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
179 (filename (concat (file-name-as-directory
181 (org-export-directory :ascii opt-plist
)))
182 (file-name-sans-extension
184 (org-entry-get (region-beginning)
185 "EXPORT_FILE_NAME" t
))
186 (file-name-nondirectory bfname
)))
188 (when (file-exists-p filename
)
189 (delete-file filename
))
192 (org-freemind-from-org-mode-node (line-number-at-pos rbeg
)
194 (t (org-freemind-from-org-mode bfname filename
)))))
197 (defun org-freemind-show (mm-file)
198 "Show file MM-FILE in Freemind."
202 (let ((name (read-file-name "FreeMind file: "
204 (if (buffer-file-name)
205 (file-name-nondirectory (buffer-file-name))
207 ;; Fix-me: Is this an Emacs bug?
208 ;; This predicate function is never
211 (string-match "^mm$" (file-name-extension fn
))))))
212 (setq name
(expand-file-name name
))
214 (org-open-file mm-file
))
216 (defconst org-freemind-org-nfix
"--org-mode: ")
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;;; Format converters
221 (defun org-freemind-escape-str-from-org (org-str)
222 "Do some html-escaping of ORG-STR and return the result.
223 The characters \"&<> will be escaped."
224 (let ((chars (append org-str nil
))
231 ((= cc ?
\") """)
235 (t (char-to-string cc
)))
236 ;; Formatting as &#number; is maybe needed
237 ;; according to a bug report from kazuo
238 ;; fujimoto, but I have now instead added a xml
239 ;; processing instruction saying that the mm
242 ;; (format "&#x%x;" (- cc ;; ?\x800))
247 ;;(org-freemind-unescape-str-to-org "mA≌B<C<=")
248 ;;(org-freemind-unescape-str-to-org "<<")
249 (defun org-freemind-unescape-str-to-org (fm-str)
250 "Do some html-unescaping of FM-STR and return the result.
251 This is the opposite of `org-freemind-escape-str-from-org' but it
252 will also unescape &#nn;."
253 (let ((org-str fm-str
))
254 (setq org-str
(replace-regexp-in-string """ "\"" org-str
))
255 (setq org-str
(replace-regexp-in-string "&" "&" org-str
))
256 (setq org-str
(replace-regexp-in-string "<" "<" org-str
))
257 (setq org-str
(replace-regexp-in-string ">" ">" org-str
))
258 (setq org-str
(replace-regexp-in-string
259 "&#x\\([a-f0-9]\\{2,4\\}\\);"
262 (+ (string-to-number (match-string 1 m
) 16)
263 0 ;?\x800 ;; What is this for? Encoding?
267 ;; (org-freemind-test-escape)
268 (defun org-freemind-test-escape ()
269 (let* ((str1 "a quote: \", an amp: &, lt: <; over 256: öåäÖÅÄ")
270 (str2 (org-freemind-escape-str-from-org str1
))
271 (str3 (org-freemind-unescape-str-to-org str2
))
273 (unless (string= str1 str3
)
274 (error "str3=%s" str3
))
277 (defun org-freemind-convert-links-from-org (org-str)
278 "Convert org links in ORG-STR to freemind links and return the result."
279 (let ((fm-str (replace-regexp-in-string
280 (rx (not (any "[\""))
286 (any "-%.?@a-zA-Z0-9()_/:~=&#"))))
289 (replace-regexp-in-string (rx "[["
294 "<a href=\"\\1\">\\2</a>"
297 ;;(org-freemind-convert-links-to-org "<a href=\"http://www.somewhere/\">link-text</a>")
298 (defun org-freemind-convert-links-to-org (fm-str)
299 "Convert freemind links in FM-STR to org links and return the result."
300 (let ((org-str (replace-regexp-in-string
307 (submatch (0+ (not (any "\""))))
311 (submatch (0+ (not (any "<"))))
318 ;;(defun org-freemind-convert-drawers-from-org (text)
321 ;; (org-freemind-test-links)
322 ;; (defun org-freemind-test-links ()
323 ;; (let* ((str1 "[[http://www.somewhere/][link-text]")
324 ;; (str2 (org-freemind-convert-links-from-org str1))
325 ;; (str3 (org-freemind-convert-links-to-org str2))
327 ;; (unless (string= str1 str3)
328 ;; (error "str3=%s" str3))
331 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
334 (defun org-freemind-convert-text-p (text)
335 "Convert TEXT to html with <p> paragraphs."
336 (setq text
(org-freemind-escape-str-from-org text
))
337 (setq text
(replace-regexp-in-string (rx "\n" (0+ blank
) "\n") "</p><p>\n" text
))
338 ;;(setq text (replace-regexp-in-string (rx bol (1+ blank) eol) "" text))
339 ;;(setq text (replace-regexp-in-string (rx bol (1+ blank)) "<br />" text))
340 (setq text
(replace-regexp-in-string "\n" "<br />" text
))
342 (org-freemind-convert-links-from-org text
)
345 (defun org-freemind-org-text-to-freemind-subnode/note
(node-name start end drawers-regexp
)
346 "Convert text part of org node to freemind subnode or note.
347 Convert the text part of the org node named NODE-NAME. The text
348 is in the current buffer between START and END. Drawers matching
349 DRAWERS-REGEXP are converted to freemind notes."
351 (let ((text (buffer-substring-no-properties start end
))
355 ;;(setq text (org-freemind-escape-str-from-org text))
356 ;; First see if there is something that should be moved to the
359 (while (string-match drawers-regexp text
)
360 (setq drawers
(cons (match-string 0 text
) drawers
))
362 (concat (substring text
0 (match-beginning 0))
363 (substring text
(match-end 0))))
366 (dolist (drawer drawers
)
367 (let ((lines (split-string drawer
"\n")))
369 (setq note-res
(concat
371 org-freemind-org-nfix line
"<br />\n")))
374 (when (> (length note-res
) 0)
375 (setq note-res
(concat
376 "<richcontent TYPE=\"NOTE\"><html>\n"
386 ;; There is always an LF char:
387 (when (> (length text
) 1)
388 (setq node-res
(concat
389 "<node style=\"bubble\" background_color=\"#eeee00\">\n"
390 "<richcontent TYPE=\"NODE\"><html>\n"
392 "<style type=\"text/css\">\n"
394 "p { margin-top: 0 }\n"
399 (let ((begin-html-mark (regexp-quote "#+BEGIN_HTML"))
400 (end-html-mark (regexp-quote "#+END_HTML"))
405 ;; Take care of #+BEGIN_HTML - #+END_HTML
406 (while (string-match begin-html-mark text
)
407 (setq head
(substring text
0 (match-beginning 0)))
408 (setq end-pos-match
(match-end 0))
409 (setq node-res
(concat node-res
410 (org-freemind-convert-text-p head
)))
411 (setq text
(substring text end-pos-match
))
412 (setq end-pos
(string-match end-html-mark text
))
414 (setq end-pos-match
(match-end 0))
415 (message "org-freemind: Missing #+END_HTML")
416 (setq end-pos
(length text
))
417 (setq end-pos-match end-pos
))
418 (setq node-res
(concat node-res
419 (substring text
0 end-pos
)))
420 (setq text
(substring text end-pos-match
)))
421 (setq node-res
(concat node-res
422 (org-freemind-convert-text-p text
))))
423 (setq node-res
(concat
428 ;; Put a note that this is for the parent node
429 "<richcontent TYPE=\"NOTE\"><html>"
434 "-- This is more about \"" node-name
"\" --"
441 (list node-res note-res
))))
443 (defun org-freemind-write-node (mm-buffer drawers-regexp num-left-nodes base-level current-level next-level this-m2 this-node-end this-children-visible next-node-start next-has-some-visible-child
)
450 (when (string-match "TODO" this-m2
)
451 (setq this-m2
(replace-match "" nil nil this-m2
))
452 (add-to-list 'this-icons
"button_cancel")
453 (setq this-bg-color
"#ffff88")
454 (when (string-match "\\[#\\(.\\)\\]" this-m2
)
455 (let ((prior (string-to-char (match-string 1 this-m2
))))
456 (setq this-m2
(replace-match "" nil nil this-m2
))
459 (add-to-list 'this-icons
"full-1")
460 (setq this-bg-color
"#ff0000"))
462 (add-to-list 'this-icons
"full-2")
463 (setq this-bg-color
"#ffaa00"))
465 (add-to-list 'this-icons
"full-3")
466 (setq this-bg-color
"#ffdd00"))
468 (add-to-list 'this-icons
"full-4")
469 (setq this-bg-color
"#ffff00"))
471 (add-to-list 'this-icons
"full-5"))
473 (add-to-list 'this-icons
"full-6"))
475 (add-to-list 'this-icons
"full-7"))
477 (setq this-m2
(org-trim this-m2
))
478 (setq this-m2-escaped
(org-freemind-escape-str-from-org this-m2
))
479 (let ((node-notes (org-freemind-org-text-to-freemind-subnode/note
484 (setq this-rich-node
(nth 0 node-notes
))
485 (setq this-rich-note
(nth 1 node-notes
)))
486 (with-current-buffer mm-buffer
487 (insert "<node text=\"" this-m2-escaped
"\"")
488 (org-freemind-get-node-style this-m2
)
489 (when (> next-level current-level
)
490 (unless (or this-children-visible
491 next-has-some-visible-child
)
492 (insert " folded=\"true\"")))
493 (when (and (= current-level
(1+ base-level
))
494 (> num-left-nodes
0))
495 (setq num-left-nodes
(1- num-left-nodes
))
496 (insert " position=\"left\""))
498 (insert " background_color=\"" this-bg-color
"\""))
501 (dolist (icon this-icons
)
502 (insert "<icon builtin=\"" icon
"\"/>\n")))
504 (with-current-buffer mm-buffer
505 (when this-rich-note
(insert this-rich-note
))
506 (when this-rich-node
(insert this-rich-node
))))
509 (defun org-freemind-check-overwrite (file interactively
)
510 "Check if file FILE already exists.
511 If FILE does not exists return t.
513 If INTERACTIVELY is non-nil ask if the file should be replaced
514 and return t/nil if it should/should not be replaced.
516 Otherwise give an error say the file exists."
517 (if (file-exists-p file
)
519 (y-or-n-p (format "File %s exists, replace it? " file
))
520 (error "File %s already exists" file
))
523 (defvar org-freemind-node-pattern
(rx bol
529 (defun org-freemind-look-for-visible-child (node-level)
532 (let ((found-visible-child nil
))
533 (while (and (not found-visible-child
)
534 (re-search-forward org-freemind-node-pattern nil t
))
535 (let* ((m1 (match-string-no-properties 1))
537 (if (>= node-level level
)
538 (setq found-visible-child
'none
)
539 (unless (get-char-property (line-beginning-position) 'invisible
)
540 (setq found-visible-child
'found
)))))
541 (eq found-visible-child
'found
)
544 (defun org-freemind-goto-line (line)
545 "Go to line number LINE."
548 (goto-char (point-min))
549 (forward-line (1- line
))))
551 (defun org-freemind-write-mm-buffer (org-buffer mm-buffer node-at-line
)
552 (with-current-buffer org-buffer
553 (dolist (node-style org-freemind-node-styles
)
554 (when (string-match-p (car node-style
) buffer-file-name
)
555 (setq org-freemind-node-style
(cadr node-style
))))
556 ;;(message "org-freemind-node-style =%s" org-freemind-node-style)
558 (let* ((drawers (copy-sequence org-drawers
))
574 (with-current-buffer mm-buffer
576 (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
577 (insert "<map version=\"0.9.0\">\n")
578 (insert "<!-- To view this file, download free mind mapping software FreeMind from http://freemind.sourceforge.net -->\n"))
580 ;; Get special buffer vars:
581 (goto-char (point-min))
582 (while (re-search-forward (rx bol
"#+DRAWERS:") nil t
)
583 (let ((dr-txt (buffer-substring-no-properties (match-end 0) (line-end-position))))
584 (setq drawers
(append drawers
(split-string dr-txt
) nil
))))
586 (concat (rx bol
(0+ blank
) ":")
599 ;; Get number of top nodes and last line for this node
601 (org-freemind-goto-line node-at-line
)
602 (unless (looking-at org-freemind-node-pattern
)
603 (error "No node at line %s" node-at-line
))
604 (setq node-at-line-level
(length (match-string-no-properties 1)))
606 (setq node-at-line-last
608 (while (re-search-forward org-freemind-node-pattern nil t
)
609 (let* ((m1 (match-string-no-properties 1))
611 (if (<= level node-at-line-level
)
614 (throw 'last-line
(1- (point))))
615 (if (= level
(1+ node-at-line-level
))
616 (setq num-top2-nodes
(1+ num-top2-nodes
))))))))
617 (setq current-level node-at-line-level
)
618 (setq num-top1-nodes
1)
619 (org-freemind-goto-line node-at-line
))
621 ;; First get number of top nodes
622 (goto-char (point-min))
623 (while (re-search-forward org-freemind-node-pattern nil t
)
624 (let* ((m1 (match-string-no-properties 1))
627 (setq num-top1-nodes
(1+ num-top1-nodes
))
629 (setq num-top2-nodes
(1+ num-top2-nodes
))))))
630 ;; If there is more than one top node we need to insert a node
631 ;; to keep them together.
632 (goto-char (point-min))
633 (when (> num-top1-nodes
1)
634 (setq num-top2-nodes num-top1-nodes
)
635 (setq current-level
0)
636 (let ((orig-name (if buffer-file-name
637 (file-name-nondirectory (buffer-file-name))
639 (with-current-buffer mm-buffer
640 (insert "<node text=\"" orig-name
"\" background_color=\"#00bfff\">\n"
641 ;; Put a note that this is for the parent node
642 "<richcontent TYPE=\"NOTE\"><html>"
647 org-freemind-org-nfix
"WHOLE FILE"
651 "</richcontent>\n")))))
653 (setq num-left-nodes
(floor num-top2-nodes
2))
654 (setq base-level current-level
)
657 this-children-visible
661 next-has-some-visible-child
662 next-children-visible
665 (re-search-forward org-freemind-node-pattern nil t
)
666 (if node-at-line-last
(<= (point) node-at-line-last
) t
)
668 (let* ((next-m1 (match-string-no-properties 1))
669 (next-node-end (match-end 0))
671 (setq next-node-start
(match-beginning 0))
672 (setq next-m2
(match-string-no-properties 2))
673 (setq next-level
(length next-m1
))
674 (when (> next-level current-level
)
675 (if (not (and org-odd-levels-only
676 (/= (mod current-level
2) 0)
677 (= next-level
(+ 2 current-level
))))
678 (setq skipping-odd nil
)
679 (setq skipping-odd t
)
680 (setq skipped-odd
(1+ skipped-odd
)))
681 (unless (or (= next-level
(1+ current-level
))
683 (if (or org-odd-levels-only
684 (/= next-level
(+ 2 current-level
)))
685 (error "Next level step > +1 for node ending at line %s" (line-number-at-pos))
686 (error "Next level step = +2 for node ending at line %s, forgot org-odd-levels-only?"
687 (line-number-at-pos)))
689 (setq next-children-visible
691 (get-char-property (line-end-position) 'invisible
))))
692 (setq next-has-some-visible-child
693 (if next-children-visible t
694 (org-freemind-look-for-visible-child next-level
)))
696 (setq num-left-nodes
(org-freemind-write-node mm-buffer drawers-regexp num-left-nodes base-level current-level next-level this-m2 this-node-end this-children-visible next-node-start next-has-some-visible-child
)))
697 (when (if (= num-top1-nodes
1) (> current-level base-level
) t
)
698 (while (>= current-level next-level
)
699 (with-current-buffer mm-buffer
701 (setq current-level
(1- current-level
))
702 (when (< 0 skipped-odd
)
703 (setq skipped-odd
(1- skipped-odd
))
704 (setq current-level
(1- current-level
)))
706 (setq this-node-end
(1+ next-node-end
))
707 (setq this-m2 next-m2
)
708 (setq current-level next-level
)
709 (setq this-children-visible next-children-visible
)
712 ;;; (unless (if node-at-line-last
713 ;;; (>= (point) node-at-line-last)
716 (setq this-m2 next-m2
)
717 (setq current-level next-level
)
718 (setq next-node-start
(if node-at-line-last
719 (1+ node-at-line-last
)
721 (setq num-left-nodes
(org-freemind-write-node mm-buffer drawers-regexp num-left-nodes base-level current-level next-level this-m2 this-node-end this-children-visible next-node-start next-has-some-visible-child
))
722 (with-current-buffer mm-buffer
(insert "</node>\n"))
725 (with-current-buffer mm-buffer
726 (while (> current-level base-level
)
728 (setq current-level
(1- current-level
))
730 (with-current-buffer mm-buffer
732 (delete-trailing-whitespace)
733 (goto-char (point-min))
736 (defun org-freemind-get-node-style (node-name)
738 ;;<node BACKGROUND_COLOR="#eeee00" CREATED="1234668815593" MODIFIED="1234668815593" STYLE="bubble">
739 ;;<font BOLD="true" NAME="SansSerif" SIZE="12"/>
742 (dolist (style-list org-freemind-node-style
)
743 (let ((node-regexp (car style-list
)))
744 (message "node-regexp=%s node-name=%s" node-regexp node-name
)
745 (when (string-match-p node-regexp node-name
)
746 ;;(setq node-style (org-freemind-do-apply-node-style style-list))
747 (setq node-style
(cadr style-list
))
749 (message "node-style=%s" node-style
)
750 (setq node-styles
(append node-styles node-style
)))
753 (defun org-freemind-do-apply-node-style (style-list)
754 (message "style-list=%S" style-list
)
755 (let ((node-style 'fork
)
757 (background-color "yellow")
763 (font-name "SansSerif")
765 (dolist (style (cadr style-list
))
766 (message " style=%s" style
)
767 (let ((what (car style
)))
769 ((eq what
'node-style
)
770 (setq node-style
(cadr style
)))
772 (setq color
(cadr style
)))
773 ((eq what
'background-color
)
774 (setq background-color
(cadr style
)))
776 ((eq what
'edge-color
)
777 (setq edge-color
(cadr style
)))
779 ((eq what
'edge-style
)
780 (setq edge-style
(cadr style
)))
782 ((eq what
'edge-width
)
783 (setq edge-width
(cadr style
)))
786 (setq italic
(cadr style
)))
789 (setq bold
(cadr style
)))
791 ((eq what
'font-name
)
792 (setq font-name
(cadr style
)))
794 ((eq what
'font-size
)
795 (setq font-size
(cadr style
)))
797 (insert (format " style=\"%s\"" node-style
))
798 (insert (format " color=\"%s\"" color
))
799 (insert (format " background_color=\"%s\"" background-color
))
802 (insert (format " color=\"%s\"" edge-color
))
803 (insert (format " style=\"%s\"" edge-style
))
804 (insert (format " width=\"%s\"" edge-width
))
807 (insert (format " italic=\"%s\"" italic
))
808 (insert (format " bold=\"%s\"" bold
))
809 (insert (format " name=\"%s\"" font-name
))
810 (insert (format " size=\"%s\"" font-size
))
814 (defun org-freemind-from-org-mode-node (node-line mm-file
)
815 "Convert node at line NODE-LINE to the FreeMind file MM-FILE."
818 (unless (org-back-to-heading nil
)
819 (error "Can't find org-mode node start"))
820 (let* ((line (line-number-at-pos))
821 (default-mm-file (concat (if buffer-file-name
822 (file-name-nondirectory buffer-file-name
)
824 "-line-" (number-to-string line
)
826 (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file
)))
827 (list line mm-file
))))
828 (when (org-freemind-check-overwrite mm-file
(called-interactively-p))
829 (let ((org-buffer (current-buffer))
830 (mm-buffer (find-file-noselect mm-file
)))
831 (org-freemind-write-mm-buffer org-buffer mm-buffer node-line
)
832 (with-current-buffer mm-buffer
834 (when (called-interactively-p)
835 (switch-to-buffer-other-window mm-buffer
)
836 (when (y-or-n-p "Show in FreeMind? ")
837 (org-freemind-show buffer-file-name
)))))))
840 (defun org-freemind-from-org-mode (org-file mm-file
)
841 "Convert the `org-mode' file ORG-FILE to the FreeMind file MM-FILE."
842 ;; Fix-me: better doc, include recommendations etc.
844 (let* ((org-file buffer-file-name
)
845 (default-mm-file (concat
847 (file-name-nondirectory org-file
)
850 (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file
)))
851 (list org-file mm-file
)))
852 (when (org-freemind-check-overwrite mm-file
(called-interactively-p))
853 (let ((org-buffer (if org-file
(find-file-noselect org-file
) (current-buffer)))
854 (mm-buffer (find-file-noselect mm-file
)))
855 (org-freemind-write-mm-buffer org-buffer mm-buffer nil
)
856 (with-current-buffer mm-buffer
858 (when (called-interactively-p)
859 (switch-to-buffer-other-window mm-buffer
)
860 (when (y-or-n-p "Show in FreeMind? ")
861 (org-freemind-show buffer-file-name
)))))))
864 (defun org-freemind-from-org-sparse-tree (org-buffer mm-file
)
865 "Convert visible part of buffer ORG-BUFFER to FreeMind file MM-FILE."
867 (let* ((org-file buffer-file-name
)
868 (default-mm-file (concat
870 (file-name-nondirectory org-file
)
873 (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file
)))
874 (list (current-buffer) mm-file
)))
875 (when (org-freemind-check-overwrite mm-file
(called-interactively-p))
877 (mm-buffer (find-file-noselect mm-file
)))
878 (save-window-excursion
879 (org-export-visible ?\ nil
)
880 (setq org-buffer
(current-buffer)))
881 (org-freemind-write-mm-buffer org-buffer mm-buffer nil
)
882 (with-current-buffer mm-buffer
884 (when (called-interactively-p)
885 (switch-to-buffer-other-window mm-buffer
)
886 (when (y-or-n-p "Show in FreeMind? ")
887 (org-freemind-show buffer-file-name
)))))))
890 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
893 ;; (sort '(b a c) 'org-freemind-lt-symbols)
894 (defun org-freemind-lt-symbols (sym-a sym-b
)
895 (string< (symbol-name sym-a
) (symbol-name sym-b
)))
896 ;; (sort '((b . 1) (a . 2) (c . 3)) 'org-freemind-lt-xml-attrs)
897 (defun org-freemind-lt-xml-attrs (attr-a attr-b
)
898 (string< (symbol-name (car attr-a
)) (symbol-name (car attr-b
))))
900 ;; xml-parse-region gives things like
911 ;; (org-freemind-symbols= 'a (car '(A B)))
912 (defsubst org-freemind-symbols
= (sym-a sym-b
)
913 "Return t if downcased names of SYM-A and SYM-B are equal.
914 SYM-A and SYM-B should be symbols."
916 (string= (downcase (symbol-name sym-a
))
917 (downcase (symbol-name sym-b
)))))
919 (defun org-freemind-get-children (parent path
)
920 "Find children node to PARENT from PATH.
921 PATH should be a list of steps, where each step has the form
923 '(NODE-NAME (ATTR-NAME . ATTR-VALUE))"
924 ;; Fix-me: maybe implement op? step: Name, number, attr, attr op val
925 ;; Fix-me: case insensitive version for children?
926 (let* ((children (if (not (listp (car parent
)))
931 (add-to-list 'cs c
)))
935 (step-node (if (listp step
) (car step
) step
))
936 (step-attr-list (when (listp step
) (sort (cdr step
) 'org-freemind-lt-xml-attrs
)))
937 (path-tail (cdr path
))
939 (dolist (child children
)
940 ;; skip xml.el formatting nodes
941 (unless (stringp child
)
943 (when (if (not step-node
)
945 (org-freemind-symbols= step-node
(car child
)))
946 (if (not step-attr-list
)
947 ;;(throw 'path-child child) ;; no attr to care about
948 (add-to-list 'path-children child
)
949 (let* ((child-attr-list (cadr child
))
950 (step-attr-copy (copy-sequence step-attr-list
)))
951 (dolist (child-attr child-attr-list
)
952 ;; Compare attr names:
953 (when (org-freemind-symbols= (caar step-attr-copy
) (car child-attr
))
955 (let ((step-val (cdar step-attr-copy
))
956 (child-val (cdr child-attr
)))
957 (when (if (not step-val
)
959 (string= step-val child-val
))
960 (setq step-attr-copy
(cdr step-attr-copy
))))))
962 (unless step-attr-copy
963 ;;(throw 'path-child child)
964 (add-to-list 'path-children child
)
967 (org-freemind-get-children path-children path-tail
)
970 (defun org-freemind-get-richcontent-node (node)
972 (org-freemind-get-children node
'((richcontent (type .
"NODE")) html body
))))
973 (when (> (length rc-nodes
) 1)
974 (lwarn t
:warning
"Unexpected structure: several <richcontent type=\"NODE\" ...>"))
977 (defun org-freemind-get-richcontent-note (node)
979 (org-freemind-get-children node
'((richcontent (type .
"NOTE")) html body
))))
980 (when (> (length rc-notes
) 1)
981 (lwarn t
:warning
"Unexpected structure: several <richcontent type=\"NOTE\" ...>"))
984 (defun org-freemind-test-get-tree-text ()
985 (let ((node '(p nil
"\n"
992 (org-freemind-get-tree-text node
)))
993 ;; (org-freemind-test-get-tree-text)
995 (defun org-freemind-get-tree-text (node)
1002 ;;(a (setq is-link t) )
1003 ((h1 h2 h3 h4 h5 h6 p
)
1004 ;;(setq ntxt (concat "\n" ntxt))
1013 (when (string= n
"\n") (setq n
""))
1015 (setq ntxt
(concat ntxt
1016 "[[" link
"][" n
"]]"))
1017 (setq ntxt
(concat ntxt n
))))
1019 (if (symbolp (car n
))
1020 (setq ntxt
(concat ntxt
(org-freemind-get-tree-text n
)))
1021 ;; This should be the attributes:
1023 (let ((att (car att-val
))
1024 (val (cdr att-val
)))
1025 (when (eq att
'href
)
1029 (setq ntxt
(concat ntxt
(make-string lf-after ?
\n)))
1030 (setq ntxt
(concat ntxt
" ")))
1031 ;;(setq ntxt (concat ntxt (format "{%s}" n)))
1034 (defun org-freemind-get-richcontent-node-text (node)
1035 "Get the node text as from the richcontent node NODE."
1037 (let* ((rc (org-freemind-get-richcontent-node node
))
1038 (txt (org-freemind-get-tree-text rc
)))
1039 ;;(when txt (setq txt (replace-regexp-in-string (rx (1+ whitespace)) " " txt)))
1043 (defun org-freemind-get-richcontent-note-text (node)
1044 "Get the node text as from the richcontent note NODE."
1046 (let* ((rc (org-freemind-get-richcontent-note node
))
1047 (txt (when rc
(org-freemind-get-tree-text rc
))))
1048 ;;(when txt (setq txt (replace-regexp-in-string (rx (1+ whitespace)) " " txt)))
1052 (defun org-freemind-get-icon-names (node)
1053 (let* ((icon-nodes (org-freemind-get-children node
'((icon ))))
1055 (dolist (icn icon-nodes
)
1056 (setq names
(cons (cdr (assq 'builtin
(cadr icn
))) names
)))
1057 ;; (icon (builtin . "full-1"))
1060 (defun org-freemind-node-to-org (node level skip-levels
)
1061 (let ((qname (car node
))
1062 (attributes (cadr node
))
1064 (note (org-freemind-get-richcontent-note-text node
))
1065 (mark "-- This is more about ")
1066 (icons (org-freemind-get-icon-names node
))
1067 (children (cddr node
)))
1068 (when (< 0 (- level skip-levels
))
1069 (dolist (attrib attributes
)
1071 ('TEXT
(setq text
(cdr attrib
)))
1072 ('text
(setq text
(cdr attrib
)))))
1074 ;; There should be a richcontent node holding the text:
1075 (setq text
(org-freemind-get-richcontent-node-text node
)))
1077 (when (member "full-1" icons
) (setq text
(concat "[#A] " text
)))
1078 (when (member "full-2" icons
) (setq text
(concat "[#B] " text
)))
1079 (when (member "full-3" icons
) (setq text
(concat "[#C] " text
)))
1080 (when (member "full-4" icons
) (setq text
(concat "[#D] " text
)))
1081 (when (member "full-5" icons
) (setq text
(concat "[#E] " text
)))
1082 (when (member "full-6" icons
) (setq text
(concat "[#F] " text
)))
1083 (when (member "full-7" icons
) (setq text
(concat "[#G] " text
)))
1084 (when (member "button_cancel" icons
) (setq text
(concat "TODO " text
)))
1087 (string= mark
(substring note
0 (length mark
))))
1089 (setq text
(replace-regexp-in-string "\n $" "" text
))
1093 (insert (make-string (- level skip-levels
) ?
*) " " text
"\n")
1095 (dolist (child children
)
1096 (unless (or (null child
)
1098 (org-freemind-node-to-org child
(1+ level
) skip-levels
)))))
1100 ;; Fix-me: put back special things, like drawers that are stored in
1101 ;; the notes. Should maybe all notes contents be put in drawers?
1103 (defun org-freemind-to-org-mode (mm-file org-file
)
1104 "Convert FreeMind file MM-FILE to `org-mode' file ORG-FILE."
1107 (let* ((mm-file (buffer-file-name))
1108 (default-org-file (concat (file-name-nondirectory mm-file
) ".org"))
1109 (org-file (read-file-name "Output org-mode file: " nil nil nil default-org-file
)))
1110 (list mm-file org-file
))))
1111 (when (org-freemind-check-overwrite org-file
(called-interactively-p))
1112 (let ((mm-buffer (find-file-noselect mm-file
))
1113 (org-buffer (find-file-noselect org-file
)))
1114 (with-current-buffer mm-buffer
1115 (let* ((xml-list (xml-parse-file mm-file
))
1116 (top-node (cadr (cddar xml-list
)))
1117 (note (org-freemind-get-richcontent-note-text top-node
))
1120 (string-match (rx bol
"--org-mode: WHOLE FILE" eol
) note
))
1123 (with-current-buffer org-buffer
1125 (org-freemind-node-to-org top-node
1 skip-levels
)
1126 (goto-char (point-min))
1127 (org-set-tags t t
) ;; Align all tags
1129 (switch-to-buffer-other-window org-buffer
)
1132 (provide 'org-freemind
)
1134 ;; arch-tag: e7b0d776-94fd-404a-b35e-0f855fae3627
1136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1137 ;;; org-freemind.el ends here