1 ;;; org-freemind.el --- Export Org files to freemind
3 ;; Copyright (C) 2009, 2010 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86 (eval-when-compile (require 'cl
))
88 ;; Fix-me: I am not sure these are useful:
90 ;; (defcustom org-freemind-main-fgcolor "black"
91 ;; "Color of main node's text."
95 ;; (defcustom org-freemind-main-color "black"
96 ;; "Background color of main node."
100 ;; (defcustom org-freemind-child-fgcolor "black"
101 ;; "Color of child nodes' text."
105 ;; (defcustom org-freemind-child-color "black"
106 ;; "Background color of child nodes."
110 (defvar org-freemind-node-style nil
"Internal use.")
112 (defcustom org-freemind-node-styles nil
113 "Styles to apply to node.
116 (list :tag
"Node styles for file"
117 (regexp :tag
"File name")
120 (regexp :tag
"Node name regexp")
121 (set :tag
"Node properties"
122 (list :format
"%v" (const :format
"" node-style
)
127 (list :format
"%v" (const :format
"" color
)
128 (color :tag
"Color" :value
"red"))
129 (list :format
"%v" (const :format
"" background-color
)
130 (color :tag
"Background color" :value
"yellow"))
131 (list :format
"%v" (const :format
"" edge-color
)
132 (color :tag
"Edge color" :value
"green"))
133 (list :format
"%v" (const :format
"" edge-style
)
134 (choice :tag
"Edge style" :value bezier
135 (const :tag
"Linear" linear
)
136 (const :tag
"Bezier" bezier
)
137 (const :tag
"Sharp Linear" sharp-linear
)
138 (const :tag
"Sharp Bezier" sharp-bezier
)))
139 (list :format
"%v" (const :format
"" edge-width
)
140 (choice :tag
"Edge width" :value thin
141 (const :tag
"Parent" parent
)
142 (const :tag
"Thin" thin
)
147 (list :format
"%v" (const :format
"" italic
)
148 (const :tag
"Italic font" t
))
149 (list :format
"%v" (const :format
"" bold
)
150 (const :tag
"Bold font" t
))
151 (list :format
"%v" (const :format
"" font-name
)
152 (string :tag
"Font name" :value
"SansSerif"))
153 (list :format
"%v" (const :format
"" font-size
)
154 (integer :tag
"Font size" :value
12)))))))
158 (defun org-export-as-freemind (arg &optional hidden ext-plist
159 to-buffer body-only pub-dir
)
161 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
163 (org-infile-export-plist)))
164 (region-p (org-region-active-p))
165 (rbeg (and region-p
(region-beginning)))
166 (rend (and region-p
(region-end)))
168 (if (plist-get opt-plist
:ignore-subtree-p
)
173 (and (org-at-heading-p)
174 (>= (org-end-of-subtree t t
) rend
))))))
175 (opt-plist (setq org-export-opt-plist
177 (org-export-add-subtree-options opt-plist rbeg
)
179 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
180 (filename (concat (file-name-as-directory
182 (org-export-directory :ascii opt-plist
)))
183 (file-name-sans-extension
185 (org-entry-get (region-beginning)
186 "EXPORT_FILE_NAME" t
))
187 (file-name-nondirectory bfname
)))
189 (when (file-exists-p filename
)
190 (delete-file filename
))
193 (org-freemind-from-org-mode-node (line-number-at-pos rbeg
)
195 (t (org-freemind-from-org-mode bfname filename
)))))
198 (defun org-freemind-show (mm-file)
199 "Show file MM-FILE in FreeMind."
203 (let ((name (read-file-name "FreeMind file: "
205 (if (buffer-file-name)
206 (file-name-nondirectory (buffer-file-name))
208 ;; Fix-me: Is this an Emacs bug?
209 ;; This predicate function is never
212 (string-match "^mm$" (file-name-extension fn
))))))
213 (setq name
(expand-file-name name
))
215 (org-open-file mm-file
))
217 (defconst org-freemind-org-nfix
"--org-mode: ")
219 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
220 ;;; Format converters
222 (defun org-freemind-escape-str-from-org (org-str)
223 "Do some html-escaping of ORG-STR and return the result.
224 The characters \"&<> will be escaped."
225 (let ((chars (append org-str nil
))
232 ((= cc ?
\") """)
236 (t (char-to-string cc
)))
237 ;; Formatting as &#number; is maybe needed
238 ;; according to a bug report from kazuo
239 ;; fujimoto, but I have now instead added a xml
240 ;; processing instruction saying that the mm
243 ;; (format "&#x%x;" (- cc ;; ?\x800))
244 (format "&#x%x;" (encode-char cc
'ucs
))
248 ;;(org-freemind-unescape-str-to-org "mA≌B<C<=")
249 ;;(org-freemind-unescape-str-to-org "<<")
250 (defun org-freemind-unescape-str-to-org (fm-str)
251 "Do some html-unescaping of FM-STR and return the result.
252 This is the opposite of `org-freemind-escape-str-from-org' but it
253 will also unescape &#nn;."
254 (let ((org-str fm-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 ">" ">" org-str
))
259 (setq org-str
(replace-regexp-in-string
260 "&#x\\([a-f0-9]\\{2,4\\}\\);"
263 (+ (string-to-number (match-string 1 m
) 16)
264 0 ;?\x800 ;; What is this for? Encoding?
268 ;; (org-freemind-test-escape)
269 (defun org-freemind-test-escape ()
270 (let* ((str1 "a quote: \", an amp: &, lt: <; over 256: öåäÖÅÄ")
271 (str2 (org-freemind-escape-str-from-org str1
))
272 (str3 (org-freemind-unescape-str-to-org str2
))
274 (unless (string= str1 str3
)
275 (error "str3=%s" str3
))
278 (defun org-freemind-convert-links-from-org (org-str)
279 "Convert org links in ORG-STR to FreeMind links and return the result."
280 (let ((fm-str (replace-regexp-in-string
281 (rx (not (any "[\""))
287 (any "-%.?@a-zA-Z0-9()_/:~=&#"))))
290 (replace-regexp-in-string (rx "[["
295 "<a href=\"\\1\">\\2</a>"
298 ;;(org-freemind-convert-links-to-org "<a href=\"http://www.somewhere/\">link-text</a>")
299 (defun org-freemind-convert-links-to-org (fm-str)
300 "Convert FreeMind links in FM-STR to org links and return the result."
301 (let ((org-str (replace-regexp-in-string
308 (submatch (0+ (not (any "\""))))
312 (submatch (0+ (not (any "<"))))
319 ;;(defun org-freemind-convert-drawers-from-org (text)
322 ;; (org-freemind-test-links)
323 ;; (defun org-freemind-test-links ()
324 ;; (let* ((str1 "[[http://www.somewhere/][link-text]")
325 ;; (str2 (org-freemind-convert-links-from-org str1))
326 ;; (str3 (org-freemind-convert-links-to-org str2))
328 ;; (unless (string= str1 str3)
329 ;; (error "str3=%s" str3))
332 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
335 (defun org-freemind-convert-text-p (text)
336 "Convert TEXT to html with <p> paragraphs."
337 (setq text
(org-freemind-escape-str-from-org text
))
338 (setq text
(replace-regexp-in-string (rx "\n" (0+ blank
) "\n") "</p><p>\n" text
))
339 ;;(setq text (replace-regexp-in-string (rx bol (1+ blank) eol) "" text))
340 ;;(setq text (replace-regexp-in-string (rx bol (1+ blank)) "<br />" text))
341 (setq text
(replace-regexp-in-string "\n" "<br />" text
))
343 (org-freemind-convert-links-from-org text
)
346 (defun org-freemind-org-text-to-freemind-subnode/note
(node-name start end drawers-regexp
)
347 "Convert text part of org node to FreeMind subnode or note.
348 Convert the text part of the org node named NODE-NAME. The text
349 is in the current buffer between START and END. Drawers matching
350 DRAWERS-REGEXP are converted to FreeMind notes."
352 (let ((text (buffer-substring-no-properties start end
))
356 ;;(setq text (org-freemind-escape-str-from-org text))
357 ;; First see if there is something that should be moved to the
360 (while (string-match drawers-regexp text
)
361 (setq drawers
(cons (match-string 0 text
) drawers
))
363 (concat (substring text
0 (match-beginning 0))
364 (substring text
(match-end 0))))
367 (dolist (drawer drawers
)
368 (let ((lines (split-string drawer
"\n")))
370 (setq note-res
(concat
372 org-freemind-org-nfix line
"<br />\n")))
375 (when (> (length note-res
) 0)
376 (setq note-res
(concat
377 "<richcontent TYPE=\"NOTE\"><html>\n"
387 ;; There is always an LF char:
388 (when (> (length text
) 1)
389 (setq node-res
(concat
390 "<node style=\"bubble\" background_color=\"#eeee00\">\n"
391 "<richcontent TYPE=\"NODE\"><html>\n"
393 "<style type=\"text/css\">\n"
395 "p { margin-top: 0 }\n"
400 (let ((begin-html-mark (regexp-quote "#+BEGIN_HTML"))
401 (end-html-mark (regexp-quote "#+END_HTML"))
406 ;; Take care of #+BEGIN_HTML - #+END_HTML
407 (while (string-match begin-html-mark text
)
408 (setq head
(substring text
0 (match-beginning 0)))
409 (setq end-pos-match
(match-end 0))
410 (setq node-res
(concat node-res
411 (org-freemind-convert-text-p head
)))
412 (setq text
(substring text end-pos-match
))
413 (setq end-pos
(string-match end-html-mark text
))
415 (setq end-pos-match
(match-end 0))
416 (message "org-freemind: Missing #+END_HTML")
417 (setq end-pos
(length text
))
418 (setq end-pos-match end-pos
))
419 (setq node-res
(concat node-res
420 (substring text
0 end-pos
)))
421 (setq text
(substring text end-pos-match
)))
422 (setq node-res
(concat node-res
423 (org-freemind-convert-text-p text
))))
424 (setq node-res
(concat
429 ;; Put a note that this is for the parent node
430 "<richcontent TYPE=\"NOTE\"><html>"
435 "-- This is more about \"" node-name
"\" --"
442 (list node-res note-res
))))
444 (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
)
451 (when (string-match "TODO" this-m2
)
452 (setq this-m2
(replace-match "" nil nil this-m2
))
453 (add-to-list 'this-icons
"button_cancel")
454 (setq this-bg-color
"#ffff88")
455 (when (string-match "\\[#\\(.\\)\\]" this-m2
)
456 (let ((prior (string-to-char (match-string 1 this-m2
))))
457 (setq this-m2
(replace-match "" nil nil this-m2
))
460 (add-to-list 'this-icons
"full-1")
461 (setq this-bg-color
"#ff0000"))
463 (add-to-list 'this-icons
"full-2")
464 (setq this-bg-color
"#ffaa00"))
466 (add-to-list 'this-icons
"full-3")
467 (setq this-bg-color
"#ffdd00"))
469 (add-to-list 'this-icons
"full-4")
470 (setq this-bg-color
"#ffff00"))
472 (add-to-list 'this-icons
"full-5"))
474 (add-to-list 'this-icons
"full-6"))
476 (add-to-list 'this-icons
"full-7"))
478 (setq this-m2
(org-trim this-m2
))
479 (setq this-m2-escaped
(org-freemind-escape-str-from-org this-m2
))
480 (let ((node-notes (org-freemind-org-text-to-freemind-subnode/note
485 (setq this-rich-node
(nth 0 node-notes
))
486 (setq this-rich-note
(nth 1 node-notes
)))
487 (with-current-buffer mm-buffer
488 (insert "<node text=\"" this-m2-escaped
"\"")
489 (org-freemind-get-node-style this-m2
)
490 (when (> next-level current-level
)
491 (unless (or this-children-visible
492 next-has-some-visible-child
)
493 (insert " folded=\"true\"")))
494 (when (and (= current-level
(1+ base-level
))
495 (> num-left-nodes
0))
496 (setq num-left-nodes
(1- num-left-nodes
))
497 (insert " position=\"left\""))
499 (insert " background_color=\"" this-bg-color
"\""))
502 (dolist (icon this-icons
)
503 (insert "<icon builtin=\"" icon
"\"/>\n")))
505 (with-current-buffer mm-buffer
506 (when this-rich-note
(insert this-rich-note
))
507 (when this-rich-node
(insert this-rich-node
))))
510 (defun org-freemind-check-overwrite (file interactively
)
511 "Check if file FILE already exists.
512 If FILE does not exists return t.
514 If INTERACTIVELY is non-nil ask if the file should be replaced
515 and return t/nil if it should/should not be replaced.
517 Otherwise give an error say the file exists."
518 (if (file-exists-p file
)
520 (y-or-n-p (format "File %s exists, replace it? " file
))
521 (error "File %s already exists" file
))
524 (defvar org-freemind-node-pattern
(rx bol
530 (defun org-freemind-look-for-visible-child (node-level)
533 (let ((found-visible-child nil
))
534 (while (and (not found-visible-child
)
535 (re-search-forward org-freemind-node-pattern nil t
))
536 (let* ((m1 (match-string-no-properties 1))
538 (if (>= node-level level
)
539 (setq found-visible-child
'none
)
540 (unless (get-char-property (line-beginning-position) 'invisible
)
541 (setq found-visible-child
'found
)))))
542 (eq found-visible-child
'found
)
545 (defun org-freemind-goto-line (line)
546 "Go to line number LINE."
549 (goto-char (point-min))
550 (forward-line (1- line
))))
552 (defun org-freemind-write-mm-buffer (org-buffer mm-buffer node-at-line
)
553 (with-current-buffer org-buffer
554 (dolist (node-style org-freemind-node-styles
)
555 (when (org-string-match-p (car node-style
) buffer-file-name
)
556 (setq org-freemind-node-style
(cadr node-style
))))
557 ;;(message "org-freemind-node-style =%s" org-freemind-node-style)
559 (let* ((drawers (copy-sequence org-drawers
))
565 (odd-only org-odd-levels-only
)
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 (setq next-children-visible
676 (get-char-property (line-end-position) 'invisible
))))
677 (setq next-has-some-visible-child
678 (if next-children-visible t
679 (org-freemind-look-for-visible-child next-level
)))
681 (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
)))
682 (when (if (= num-top1-nodes
1) (> current-level base-level
) t
)
683 (while (>= current-level next-level
)
684 (with-current-buffer mm-buffer
687 (- current-level
(if odd-only
2 1))))))
688 (setq this-node-end
(1+ next-node-end
))
689 (setq this-m2 next-m2
)
690 (setq current-level next-level
)
691 (setq this-children-visible next-children-visible
)
694 ;;; (unless (if node-at-line-last
695 ;;; (>= (point) node-at-line-last)
698 (setq this-m2 next-m2
)
699 (setq current-level next-level
)
700 (setq next-node-start
(if node-at-line-last
701 (1+ node-at-line-last
)
703 (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
))
704 (with-current-buffer mm-buffer
(insert "</node>\n"))
707 (with-current-buffer mm-buffer
708 (while (> current-level base-level
)
711 (- current-level
(if odd-only
2 1)))
713 (with-current-buffer mm-buffer
715 (delete-trailing-whitespace)
716 (goto-char (point-min))
719 (defun org-freemind-get-node-style (node-name)
721 ;;<node BACKGROUND_COLOR="#eeee00" CREATED="1234668815593" MODIFIED="1234668815593" STYLE="bubble">
722 ;;<font BOLD="true" NAME="SansSerif" SIZE="12"/>
725 (dolist (style-list org-freemind-node-style
)
726 (let ((node-regexp (car style-list
)))
727 (message "node-regexp=%s node-name=%s" node-regexp node-name
)
728 (when (org-string-match-p node-regexp node-name
)
729 ;;(setq node-style (org-freemind-do-apply-node-style style-list))
730 (setq node-style
(cadr style-list
))
732 (message "node-style=%s" node-style
)
733 (setq node-styles
(append node-styles node-style
)))
736 (defun org-freemind-do-apply-node-style (style-list)
737 (message "style-list=%S" style-list
)
738 (let ((node-style 'fork
)
740 (background-color "yellow")
746 (font-name "SansSerif")
748 (dolist (style (cadr style-list
))
749 (message " style=%s" style
)
750 (let ((what (car style
)))
752 ((eq what
'node-style
)
753 (setq node-style
(cadr style
)))
755 (setq color
(cadr style
)))
756 ((eq what
'background-color
)
757 (setq background-color
(cadr style
)))
759 ((eq what
'edge-color
)
760 (setq edge-color
(cadr style
)))
762 ((eq what
'edge-style
)
763 (setq edge-style
(cadr style
)))
765 ((eq what
'edge-width
)
766 (setq edge-width
(cadr style
)))
769 (setq italic
(cadr style
)))
772 (setq bold
(cadr style
)))
774 ((eq what
'font-name
)
775 (setq font-name
(cadr style
)))
777 ((eq what
'font-size
)
778 (setq font-size
(cadr style
)))
780 (insert (format " style=\"%s\"" node-style
))
781 (insert (format " color=\"%s\"" color
))
782 (insert (format " background_color=\"%s\"" background-color
))
785 (insert (format " color=\"%s\"" edge-color
))
786 (insert (format " style=\"%s\"" edge-style
))
787 (insert (format " width=\"%s\"" edge-width
))
790 (insert (format " italic=\"%s\"" italic
))
791 (insert (format " bold=\"%s\"" bold
))
792 (insert (format " name=\"%s\"" font-name
))
793 (insert (format " size=\"%s\"" font-size
))
797 (defun org-freemind-from-org-mode-node (node-line mm-file
)
798 "Convert node at line NODE-LINE to the FreeMind file MM-FILE."
801 (unless (org-back-to-heading nil
)
802 (error "Can't find org-mode node start"))
803 (let* ((line (line-number-at-pos))
804 (default-mm-file (concat (if buffer-file-name
805 (file-name-nondirectory buffer-file-name
)
807 "-line-" (number-to-string line
)
809 (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file
)))
810 (list line mm-file
))))
811 (when (org-freemind-check-overwrite mm-file
(interactive-p))
812 (let ((org-buffer (current-buffer))
813 (mm-buffer (find-file-noselect mm-file
)))
814 (org-freemind-write-mm-buffer org-buffer mm-buffer node-line
)
815 (with-current-buffer mm-buffer
817 (when (interactive-p)
818 (switch-to-buffer-other-window mm-buffer
)
819 (when (y-or-n-p "Show in FreeMind? ")
820 (org-freemind-show buffer-file-name
)))))))
823 (defun org-freemind-from-org-mode (org-file mm-file
)
824 "Convert the `org-mode' file ORG-FILE to the FreeMind file MM-FILE."
825 ;; Fix-me: better doc, include recommendations etc.
827 (let* ((org-file buffer-file-name
)
828 (default-mm-file (concat
830 (file-name-nondirectory org-file
)
833 (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file
)))
834 (list org-file mm-file
)))
835 (when (org-freemind-check-overwrite mm-file
(interactive-p))
836 (let ((org-buffer (if org-file
(find-file-noselect org-file
) (current-buffer)))
837 (mm-buffer (find-file-noselect mm-file
)))
838 (org-freemind-write-mm-buffer org-buffer mm-buffer nil
)
839 (with-current-buffer mm-buffer
841 (when (interactive-p)
842 (switch-to-buffer-other-window mm-buffer
)
843 (when (y-or-n-p "Show in FreeMind? ")
844 (org-freemind-show buffer-file-name
)))))))
847 (defun org-freemind-from-org-sparse-tree (org-buffer mm-file
)
848 "Convert visible part of buffer ORG-BUFFER to FreeMind file MM-FILE."
850 (let* ((org-file buffer-file-name
)
851 (default-mm-file (concat
853 (file-name-nondirectory org-file
)
856 (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file
)))
857 (list (current-buffer) mm-file
)))
858 (when (org-freemind-check-overwrite mm-file
(interactive-p))
860 (mm-buffer (find-file-noselect mm-file
)))
861 (save-window-excursion
862 (org-export-visible ?\ nil
)
863 (setq org-buffer
(current-buffer)))
864 (org-freemind-write-mm-buffer org-buffer mm-buffer nil
)
865 (with-current-buffer mm-buffer
867 (when (interactive-p)
868 (switch-to-buffer-other-window mm-buffer
)
869 (when (y-or-n-p "Show in FreeMind? ")
870 (org-freemind-show buffer-file-name
)))))))
873 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
876 ;; (sort '(b a c) 'org-freemind-lt-symbols)
877 (defun org-freemind-lt-symbols (sym-a sym-b
)
878 (string< (symbol-name sym-a
) (symbol-name sym-b
)))
879 ;; (sort '((b . 1) (a . 2) (c . 3)) 'org-freemind-lt-xml-attrs)
880 (defun org-freemind-lt-xml-attrs (attr-a attr-b
)
881 (string< (symbol-name (car attr-a
)) (symbol-name (car attr-b
))))
883 ;; xml-parse-region gives things like
894 ;; (org-freemind-symbols= 'a (car '(A B)))
895 (defsubst org-freemind-symbols
= (sym-a sym-b
)
896 "Return t if downcased names of SYM-A and SYM-B are equal.
897 SYM-A and SYM-B should be symbols."
899 (string= (downcase (symbol-name sym-a
))
900 (downcase (symbol-name sym-b
)))))
902 (defun org-freemind-get-children (parent path
)
903 "Find children node to PARENT from PATH.
904 PATH should be a list of steps, where each step has the form
906 '(NODE-NAME (ATTR-NAME . ATTR-VALUE))"
907 ;; Fix-me: maybe implement op? step: Name, number, attr, attr op val
908 ;; Fix-me: case insensitive version for children?
909 (let* ((children (if (not (listp (car parent
)))
914 (add-to-list 'cs c
)))
918 (step-node (if (listp step
) (car step
) step
))
919 (step-attr-list (when (listp step
) (sort (cdr step
) 'org-freemind-lt-xml-attrs
)))
920 (path-tail (cdr path
))
922 (dolist (child children
)
923 ;; skip xml.el formatting nodes
924 (unless (stringp child
)
926 (when (if (not step-node
)
928 (org-freemind-symbols= step-node
(car child
)))
929 (if (not step-attr-list
)
930 ;;(throw 'path-child child) ;; no attr to care about
931 (add-to-list 'path-children child
)
932 (let* ((child-attr-list (cadr child
))
933 (step-attr-copy (copy-sequence step-attr-list
)))
934 (dolist (child-attr child-attr-list
)
935 ;; Compare attr names:
936 (when (org-freemind-symbols= (caar step-attr-copy
) (car child-attr
))
938 (let ((step-val (cdar step-attr-copy
))
939 (child-val (cdr child-attr
)))
940 (when (if (not step-val
)
942 (string= step-val child-val
))
943 (setq step-attr-copy
(cdr step-attr-copy
))))))
945 (unless step-attr-copy
946 ;;(throw 'path-child child)
947 (add-to-list 'path-children child
)
950 (org-freemind-get-children path-children path-tail
)
953 (defun org-freemind-get-richcontent-node (node)
955 (org-freemind-get-children node
'((richcontent (type .
"NODE")) html body
))))
956 (when (> (length rc-nodes
) 1)
957 (lwarn t
:warning
"Unexpected structure: several <richcontent type=\"NODE\" ...>"))
960 (defun org-freemind-get-richcontent-note (node)
962 (org-freemind-get-children node
'((richcontent (type .
"NOTE")) html body
))))
963 (when (> (length rc-notes
) 1)
964 (lwarn t
:warning
"Unexpected structure: several <richcontent type=\"NOTE\" ...>"))
967 (defun org-freemind-test-get-tree-text ()
968 (let ((node '(p nil
"\n"
975 (org-freemind-get-tree-text node
)))
976 ;; (org-freemind-test-get-tree-text)
978 (defun org-freemind-get-tree-text (node)
985 ;;(a (setq is-link t) )
986 ((h1 h2 h3 h4 h5 h6 p
)
987 ;;(setq ntxt (concat "\n" ntxt))
996 (when (string= n
"\n") (setq n
""))
998 (setq ntxt
(concat ntxt
999 "[[" link
"][" n
"]]"))
1000 (setq ntxt
(concat ntxt n
))))
1002 (if (symbolp (car n
))
1003 (setq ntxt
(concat ntxt
(org-freemind-get-tree-text n
)))
1004 ;; This should be the attributes:
1006 (let ((att (car att-val
))
1007 (val (cdr att-val
)))
1008 (when (eq att
'href
)
1012 (setq ntxt
(concat ntxt
(make-string lf-after ?
\n)))
1013 (setq ntxt
(concat ntxt
" ")))
1014 ;;(setq ntxt (concat ntxt (format "{%s}" n)))
1017 (defun org-freemind-get-richcontent-node-text (node)
1018 "Get the node text as from the richcontent node NODE."
1020 (let* ((rc (org-freemind-get-richcontent-node node
))
1021 (txt (org-freemind-get-tree-text rc
)))
1022 ;;(when txt (setq txt (replace-regexp-in-string (rx (1+ whitespace)) " " txt)))
1026 (defun org-freemind-get-richcontent-note-text (node)
1027 "Get the node text as from the richcontent note NODE."
1029 (let* ((rc (org-freemind-get-richcontent-note node
))
1030 (txt (when rc
(org-freemind-get-tree-text rc
))))
1031 ;;(when txt (setq txt (replace-regexp-in-string (rx (1+ whitespace)) " " txt)))
1035 (defun org-freemind-get-icon-names (node)
1036 (let* ((icon-nodes (org-freemind-get-children node
'((icon ))))
1038 (dolist (icn icon-nodes
)
1039 (setq names
(cons (cdr (assq 'builtin
(cadr icn
))) names
)))
1040 ;; (icon (builtin . "full-1"))
1043 (defun org-freemind-node-to-org (node level skip-levels
)
1044 (let ((qname (car node
))
1045 (attributes (cadr node
))
1047 (note (org-freemind-get-richcontent-note-text node
))
1048 (mark "-- This is more about ")
1049 (icons (org-freemind-get-icon-names node
))
1050 (children (cddr node
)))
1051 (when (< 0 (- level skip-levels
))
1052 (dolist (attrib attributes
)
1054 ('TEXT
(setq text
(cdr attrib
)))
1055 ('text
(setq text
(cdr attrib
)))))
1057 ;; There should be a richcontent node holding the text:
1058 (setq text
(org-freemind-get-richcontent-node-text node
)))
1060 (when (member "full-1" icons
) (setq text
(concat "[#A] " text
)))
1061 (when (member "full-2" icons
) (setq text
(concat "[#B] " text
)))
1062 (when (member "full-3" icons
) (setq text
(concat "[#C] " text
)))
1063 (when (member "full-4" icons
) (setq text
(concat "[#D] " text
)))
1064 (when (member "full-5" icons
) (setq text
(concat "[#E] " text
)))
1065 (when (member "full-6" icons
) (setq text
(concat "[#F] " text
)))
1066 (when (member "full-7" icons
) (setq text
(concat "[#G] " text
)))
1067 (when (member "button_cancel" icons
) (setq text
(concat "TODO " text
)))
1070 (string= mark
(substring note
0 (length mark
))))
1072 (setq text
(replace-regexp-in-string "\n $" "" text
))
1076 (insert (make-string (- level skip-levels
) ?
*) " " text
"\n")
1078 (dolist (child children
)
1079 (unless (or (null child
)
1081 (org-freemind-node-to-org child
(1+ level
) skip-levels
)))))
1083 ;; Fix-me: put back special things, like drawers that are stored in
1084 ;; the notes. Should maybe all notes contents be put in drawers?
1086 (defun org-freemind-to-org-mode (mm-file org-file
)
1087 "Convert FreeMind file MM-FILE to `org-mode' file ORG-FILE."
1090 (let* ((mm-file (buffer-file-name))
1091 (default-org-file (concat (file-name-nondirectory mm-file
) ".org"))
1092 (org-file (read-file-name "Output org-mode file: " nil nil nil default-org-file
)))
1093 (list mm-file org-file
))))
1094 (when (org-freemind-check-overwrite org-file
(interactive-p))
1095 (let ((mm-buffer (find-file-noselect mm-file
))
1096 (org-buffer (find-file-noselect org-file
)))
1097 (with-current-buffer mm-buffer
1098 (let* ((xml-list (xml-parse-file mm-file
))
1099 (top-node (cadr (cddar xml-list
)))
1100 (note (org-freemind-get-richcontent-note-text top-node
))
1103 (string-match (rx bol
"--org-mode: WHOLE FILE" eol
) note
))
1106 (with-current-buffer org-buffer
1108 (org-freemind-node-to-org top-node
1 skip-levels
)
1109 (goto-char (point-min))
1110 (org-set-tags t t
) ;; Align all tags
1112 (switch-to-buffer-other-window org-buffer
)
1115 (provide 'org-freemind
)
1117 ;; arch-tag: e7b0d776-94fd-404a-b35e-0f855fae3627
1119 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1120 ;;; org-freemind.el ends here