org-freemind.el 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. ;;; org-freemind.el --- Export Org files to freemind
  2. ;; Copyright (C) 2009 Free Software Foundation, Inc.
  3. ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.33a
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;; --------------------------------------------------------------------
  21. ;; Features that might be required by this library:
  22. ;;
  23. ;; `backquote', `bytecomp', `cl', `easymenu', `font-lock',
  24. ;; `noutline', `org', `org-compat', `org-faces', `org-footnote',
  25. ;; `org-list', `org-macs', `org-src', `outline', `syntax',
  26. ;; `time-date', `xml'.
  27. ;;
  28. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  29. ;;
  30. ;;; Commentary:
  31. ;;
  32. ;; This file tries to implement some functions useful for
  33. ;; transformation between org-mode and FreeMind files.
  34. ;;
  35. ;; Here are the commands you can use:
  36. ;;
  37. ;; M-x `org-freemind-from-org-mode'
  38. ;; M-x `org-freemind-from-org-mode-node'
  39. ;; M-x `org-freemind-from-org-sparse-tree'
  40. ;;
  41. ;; M-x `org-freemind-to-org-mode'
  42. ;;
  43. ;; M-x `org-freemind-show'
  44. ;;
  45. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  46. ;;
  47. ;;; Change log:
  48. ;;
  49. ;; 2009-02-15: Added check for next level=current+1
  50. ;; 2009-02-21: Fixed bug in `org-freemind-to-org-mode'.
  51. ;; 2009-10-25: Added support for `org-odd-levels-only'.
  52. ;; Added y/n question before showing in FreeMind.
  53. ;; 2009-11-04: Added support for #+BEGIN_HTML.
  54. ;;
  55. ;;
  56. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  57. ;;
  58. ;; This program is free software; you can redistribute it and/or
  59. ;; modify it under the terms of the GNU General Public License as
  60. ;; published by the Free Software Foundation; either version 2, or
  61. ;; (at your option) any later version.
  62. ;;
  63. ;; This program is distributed in the hope that it will be useful,
  64. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  65. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  66. ;; General Public License for more details.
  67. ;;
  68. ;; You should have received a copy of the GNU General Public License
  69. ;; along with this program; see the file COPYING. If not, write to
  70. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  71. ;; Floor, Boston, MA 02110-1301, USA.
  72. ;;
  73. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  74. ;;
  75. ;;; Code:
  76. (require 'xml)
  77. (require 'org)
  78. (require 'org-exp)
  79. (eval-when-compile (require 'cl))
  80. ;; Fix-me: I am not sure these are useful:
  81. ;;
  82. ;; (defcustom org-freemind-main-fgcolor "black"
  83. ;; "Color of main node's text."
  84. ;; :type 'color
  85. ;; :group 'freemind)
  86. ;; (defcustom org-freemind-main-color "black"
  87. ;; "Background color of main node."
  88. ;; :type 'color
  89. ;; :group 'freemind)
  90. ;; (defcustom org-freemind-child-fgcolor "black"
  91. ;; "Color of child nodes' text."
  92. ;; :type 'color
  93. ;; :group 'freemind)
  94. ;; (defcustom org-freemind-child-color "black"
  95. ;; "Background color of child nodes."
  96. ;; :type 'color
  97. ;; :group 'freemind)
  98. (defvar org-freemind-node-style nil "Internal use.")
  99. (defcustom org-freemind-node-styles nil
  100. "Styles to apply to node.
  101. NOT READY YET."
  102. :type '(repeat
  103. (list :tag "Node styles for file"
  104. (regexp :tag "File name")
  105. (repeat
  106. (list :tag "Node"
  107. (regexp :tag "Node name regexp")
  108. (set :tag "Node properties"
  109. (list :format "%v" (const :format "" node-style)
  110. (choice :tag "Style"
  111. :value bubble
  112. (const bubble)
  113. (const fork)))
  114. (list :format "%v" (const :format "" color)
  115. (color :tag "Color" :value "red"))
  116. (list :format "%v" (const :format "" background-color)
  117. (color :tag "Background color" :value "yellow"))
  118. (list :format "%v" (const :format "" edge-color)
  119. (color :tag "Edge color" :value "green"))
  120. (list :format "%v" (const :format "" edge-style)
  121. (choice :tag "Edge style" :value bezier
  122. (const :tag "Linear" linear)
  123. (const :tag "Bezier" bezier)
  124. (const :tag "Sharp Linear" sharp-linear)
  125. (const :tag "Sharp Bezier" sharp-bezier)))
  126. (list :format "%v" (const :format "" edge-width)
  127. (choice :tag "Edge width" :value thin
  128. (const :tag "Parent" parent)
  129. (const :tag "Thin" thin)
  130. (const 1)
  131. (const 2)
  132. (const 4)
  133. (const 8)))
  134. (list :format "%v" (const :format "" italic)
  135. (const :tag "Italic font" t))
  136. (list :format "%v" (const :format "" bold)
  137. (const :tag "Bold font" t))
  138. (list :format "%v" (const :format "" font-name)
  139. (string :tag "Font name" :value "SansSerif"))
  140. (list :format "%v" (const :format "" font-size)
  141. (integer :tag "Font size" :value 12)))))))
  142. :group 'freemind)
  143. ;;;###autoload
  144. (defun org-export-as-freemind (arg &optional hidden ext-plist
  145. to-buffer body-only pub-dir)
  146. (interactive "P")
  147. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  148. ext-plist
  149. (org-infile-export-plist)))
  150. (region-p (org-region-active-p))
  151. (rbeg (and region-p (region-beginning)))
  152. (rend (and region-p (region-end)))
  153. (subtree-p
  154. (if (plist-get opt-plist :ignore-subtree-p)
  155. nil
  156. (when region-p
  157. (save-excursion
  158. (goto-char rbeg)
  159. (and (org-at-heading-p)
  160. (>= (org-end-of-subtree t t) rend))))))
  161. (opt-plist (setq org-export-opt-plist
  162. (if subtree-p
  163. (org-export-add-subtree-options opt-plist rbeg)
  164. opt-plist)))
  165. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  166. (filename (concat (file-name-as-directory
  167. (or pub-dir
  168. (org-export-directory :ascii opt-plist)))
  169. (file-name-sans-extension
  170. (or (and subtree-p
  171. (org-entry-get (region-beginning)
  172. "EXPORT_FILE_NAME" t))
  173. (file-name-nondirectory bfname)))
  174. ".mm")))
  175. (when (file-exists-p filename)
  176. (delete-file filename))
  177. (cond
  178. (subtree-p
  179. (org-freemind-from-org-mode-node (line-number-at-pos rbeg)
  180. filename))
  181. (t (org-freemind-from-org-mode bfname filename)))))
  182. ;;;###autoload
  183. (defun org-freemind-show (mm-file)
  184. "Show file MM-FILE in Freemind."
  185. (interactive
  186. (list
  187. (save-match-data
  188. (let ((name (read-file-name "FreeMind file: "
  189. nil nil nil
  190. (if (buffer-file-name)
  191. (file-name-nondirectory (buffer-file-name))
  192. "")
  193. ;; Fix-me: Is this an Emacs bug?
  194. ;; This predicate function is never
  195. ;; called.
  196. (lambda (fn)
  197. (string-match "^mm$" (file-name-extension fn))))))
  198. (setq name (expand-file-name name))
  199. name))))
  200. (org-open-file mm-file))
  201. (defconst org-freemind-org-nfix "--org-mode: ")
  202. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  203. ;;; Format converters
  204. (defun org-freemind-escape-str-from-org (org-str)
  205. "Do some html-escaping of ORG-STR and return the result.
  206. The characters \"&<> will be escaped."
  207. (let ((chars (append org-str nil))
  208. (fm-str ""))
  209. (dolist (cc chars)
  210. (setq fm-str
  211. (concat fm-str
  212. (if (< cc 256)
  213. (cond
  214. ((= cc ?\") "&quot;")
  215. ((= cc ?\&) "&amp;")
  216. ((= cc ?\<) "&lt;")
  217. ((= cc ?\>) "&gt;")
  218. (t (char-to-string cc)))
  219. ;; Formatting as &#number; is maybe needed
  220. ;; according to a bug report from kazuo
  221. ;; fujimoto, but I have now instead added a xml
  222. ;; processing instruction saying that the mm
  223. ;; file is utf-8:
  224. ;;
  225. ;; (format "&#x%x;" (- cc ;; ?\x800))
  226. (char-to-string cc)
  227. ))))
  228. fm-str))
  229. ;;(org-freemind-unescape-str-to-org "&#x6d;A&#x224C;B&lt;C&#x3C;&#x3D;")
  230. ;;(org-freemind-unescape-str-to-org "&#x3C;&lt;")
  231. (defun org-freemind-unescape-str-to-org (fm-str)
  232. "Do some html-unescaping of FM-STR and return the result.
  233. This is the opposite of `org-freemind-escape-str-from-org' but it
  234. will also unescape &#nn;."
  235. (let ((org-str fm-str))
  236. (setq org-str (replace-regexp-in-string "&quot;" "\"" org-str))
  237. (setq org-str (replace-regexp-in-string "&amp;" "&" org-str))
  238. (setq org-str (replace-regexp-in-string "&lt;" "<" org-str))
  239. (setq org-str (replace-regexp-in-string "&gt;" ">" org-str))
  240. (setq org-str (replace-regexp-in-string
  241. "&#x\\([a-f0-9]\\{2,4\\}\\);"
  242. (lambda (m)
  243. (char-to-string
  244. (+ (string-to-number (match-string 1 m) 16)
  245. 0 ;?\x800 ;; What is this for? Encoding?
  246. )))
  247. org-str))))
  248. ;; (org-freemind-test-escape)
  249. (defun org-freemind-test-escape ()
  250. (let* ((str1 "a quote: \", an amp: &, lt: <; over 256: öåäÖÅÄ")
  251. (str2 (org-freemind-escape-str-from-org str1))
  252. (str3 (org-freemind-unescape-str-to-org str2))
  253. )
  254. (unless (string= str1 str3)
  255. (error "str3=%s" str3))
  256. ))
  257. (defun org-freemind-convert-links-from-org (org-str)
  258. "Convert org links in ORG-STR to freemind links and return the result."
  259. (let ((fm-str (replace-regexp-in-string
  260. (rx (not (any "[\""))
  261. (submatch
  262. "http"
  263. (opt ?\s)
  264. "://"
  265. (1+
  266. (any "-%.?@a-zA-Z0-9()_/:~=&#"))))
  267. "[[\\1][\\1]]"
  268. org-str)))
  269. (replace-regexp-in-string (rx "[["
  270. (submatch (*? nonl))
  271. "]["
  272. (submatch (*? nonl))
  273. "]]")
  274. "<a href=\"\\1\">\\2</a>"
  275. fm-str)))
  276. ;;(org-freemind-convert-links-to-org "<a href=\"http://www.somewhere/\">link-text</a>")
  277. (defun org-freemind-convert-links-to-org (fm-str)
  278. "Convert freemind links in FM-STR to org links and return the result."
  279. (let ((org-str (replace-regexp-in-string
  280. (rx "<a"
  281. space
  282. (0+
  283. (0+ (not (any ">")))
  284. space)
  285. "href=\""
  286. (submatch (0+ (not (any "\""))))
  287. "\""
  288. (0+ (not (any ">")))
  289. ">"
  290. (submatch (0+ (not (any "<"))))
  291. "</a>")
  292. "[[\\1][\\2]]"
  293. fm-str)))
  294. org-str))
  295. ;; Fix-me:
  296. ;;(defun org-freemind-convert-drawers-from-org (text)
  297. ;; )
  298. ;; (org-freemind-test-links)
  299. ;; (defun org-freemind-test-links ()
  300. ;; (let* ((str1 "[[http://www.somewhere/][link-text]")
  301. ;; (str2 (org-freemind-convert-links-from-org str1))
  302. ;; (str3 (org-freemind-convert-links-to-org str2))
  303. ;; )
  304. ;; (unless (string= str1 str3)
  305. ;; (error "str3=%s" str3))
  306. ;; ))
  307. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  308. ;;; Org => FreeMind
  309. (defun org-freemind-convert-text-p (text)
  310. "Convert TEXT to html with <p> paragraphs."
  311. (setq text (org-freemind-escape-str-from-org text))
  312. (setq text (replace-regexp-in-string (rx "\n" (0+ blank) "\n") "</p><p>\n" text))
  313. ;;(setq text (replace-regexp-in-string (rx bol (1+ blank) eol) "" text))
  314. ;;(setq text (replace-regexp-in-string (rx bol (1+ blank)) "<br />" text))
  315. (setq text (replace-regexp-in-string "\n" "<br />" text))
  316. (concat "<p>"
  317. (org-freemind-convert-links-from-org text)
  318. "</p>\n"))
  319. (defun org-freemind-org-text-to-freemind-subnode/note (node-name start end drawers-regexp)
  320. "Convert text part of org node to freemind subnode or note.
  321. Convert the text part of the org node named NODE-NAME. The text
  322. is in the current buffer between START and END. Drawers matching
  323. DRAWERS-REGEXP are converted to freemind notes."
  324. ;; fix-me: doc
  325. (let ((text (buffer-substring-no-properties start end))
  326. (node-res "")
  327. (note-res ""))
  328. (save-match-data
  329. ;;(setq text (org-freemind-escape-str-from-org text))
  330. ;; First see if there is something that should be moved to the
  331. ;; note part:
  332. (let (drawers)
  333. (while (string-match drawers-regexp text)
  334. (setq drawers (cons (match-string 0 text) drawers))
  335. (setq text
  336. (concat (substring text 0 (match-beginning 0))
  337. (substring text (match-end 0))))
  338. )
  339. (when drawers
  340. (dolist (drawer drawers)
  341. (let ((lines (split-string drawer "\n")))
  342. (dolist (line lines)
  343. (setq note-res (concat
  344. note-res
  345. org-freemind-org-nfix line "<br />\n")))
  346. ))))
  347. (when (> (length note-res) 0)
  348. (setq note-res (concat
  349. "<richcontent TYPE=\"NOTE\"><html>\n"
  350. "<head>\n"
  351. "</head>\n"
  352. "<body>\n"
  353. note-res
  354. "</body>\n"
  355. "</html>\n"
  356. "</richcontent>\n"))
  357. )
  358. ;; There is always an LF char:
  359. (when (> (length text) 1)
  360. (setq node-res (concat
  361. "<node style=\"bubble\" background_color=\"#eeee00\">\n"
  362. "<richcontent TYPE=\"NODE\"><html>\n"
  363. "<head>\n"
  364. "<style type=\"text/css\">\n"
  365. "<!--\n"
  366. "p { margin-top: 0 }\n"
  367. "-->\n"
  368. "</style>\n"
  369. "</head>\n"
  370. "<body>\n"))
  371. (let ((begin-html-mark (regexp-quote "#+BEGIN_HTML"))
  372. (end-html-mark (regexp-quote "#+END_HTML"))
  373. head
  374. end-pos
  375. end-pos-match
  376. )
  377. ;; Take care of #+BEGIN_HTML - #+END_HTML
  378. (while (string-match begin-html-mark text)
  379. (setq head (substring text 0 (match-beginning 0)))
  380. (setq end-pos-match (match-end 0))
  381. (setq node-res (concat node-res
  382. (org-freemind-convert-text-p head)))
  383. (setq text (substring text end-pos-match))
  384. (setq end-pos (string-match end-html-mark text))
  385. (if end-pos
  386. (setq end-pos-match (match-end 0))
  387. (message "org-freemind: Missing #+END_HTML")
  388. (setq end-pos (length text))
  389. (setq end-pos-match end-pos))
  390. (setq node-res (concat node-res
  391. (substring text 0 end-pos)))
  392. (setq text (substring text end-pos-match)))
  393. (setq node-res (concat node-res
  394. (org-freemind-convert-text-p text))))
  395. (setq node-res (concat
  396. node-res
  397. "</body>\n"
  398. "</html>\n"
  399. "</richcontent>\n"
  400. ;; Put a note that this is for the parent node
  401. "<richcontent TYPE=\"NOTE\"><html>"
  402. "<head>"
  403. "</head>"
  404. "<body>"
  405. "<p>"
  406. "-- This is more about \"" node-name "\" --"
  407. "</p>"
  408. "</body>"
  409. "</html>"
  410. "</richcontent>\n"
  411. "</node>\n" ;; ok
  412. )))
  413. (list node-res note-res))))
  414. (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)
  415. (let* (this-icons
  416. this-bg-color
  417. this-m2-escaped
  418. this-rich-node
  419. this-rich-note
  420. )
  421. (when (string-match "TODO" this-m2)
  422. (setq this-m2 (replace-match "" nil nil this-m2))
  423. (add-to-list 'this-icons "button_cancel")
  424. (setq this-bg-color "#ffff88")
  425. (when (string-match "\\[#\\(.\\)\\]" this-m2)
  426. (let ((prior (string-to-char (match-string 1 this-m2))))
  427. (setq this-m2 (replace-match "" nil nil this-m2))
  428. (cond
  429. ((= prior ?A)
  430. (add-to-list 'this-icons "full-1")
  431. (setq this-bg-color "#ff0000"))
  432. ((= prior ?B)
  433. (add-to-list 'this-icons "full-2")
  434. (setq this-bg-color "#ffaa00"))
  435. ((= prior ?C)
  436. (add-to-list 'this-icons "full-3")
  437. (setq this-bg-color "#ffdd00"))
  438. ((= prior ?D)
  439. (add-to-list 'this-icons "full-4")
  440. (setq this-bg-color "#ffff00"))
  441. ((= prior ?E)
  442. (add-to-list 'this-icons "full-5"))
  443. ((= prior ?F)
  444. (add-to-list 'this-icons "full-6"))
  445. ((= prior ?G)
  446. (add-to-list 'this-icons "full-7"))
  447. ))))
  448. (setq this-m2 (org-trim this-m2))
  449. (setq this-m2-escaped (org-freemind-escape-str-from-org this-m2))
  450. (let ((node-notes (org-freemind-org-text-to-freemind-subnode/note
  451. this-m2-escaped
  452. this-node-end
  453. (1- next-node-start)
  454. drawers-regexp)))
  455. (setq this-rich-node (nth 0 node-notes))
  456. (setq this-rich-note (nth 1 node-notes)))
  457. (with-current-buffer mm-buffer
  458. (insert "<node text=\"" this-m2-escaped "\"")
  459. (org-freemind-get-node-style this-m2)
  460. (when (> next-level current-level)
  461. (unless (or this-children-visible
  462. next-has-some-visible-child)
  463. (insert " folded=\"true\"")))
  464. (when (and (= current-level (1+ base-level))
  465. (> num-left-nodes 0))
  466. (setq num-left-nodes (1- num-left-nodes))
  467. (insert " position=\"left\""))
  468. (when this-bg-color
  469. (insert " background_color=\"" this-bg-color "\""))
  470. (insert ">\n")
  471. (when this-icons
  472. (dolist (icon this-icons)
  473. (insert "<icon builtin=\"" icon "\"/>\n")))
  474. )
  475. (with-current-buffer mm-buffer
  476. (when this-rich-note (insert this-rich-note))
  477. (when this-rich-node (insert this-rich-node))))
  478. num-left-nodes)
  479. (defun org-freemind-check-overwrite (file interactively)
  480. "Check if file FILE already exists.
  481. If FILE does not exists return t.
  482. If INTERACTIVELY is non-nil ask if the file should be replaced
  483. and return t/nil if it should/should not be replaced.
  484. Otherwise give an error say the file exists."
  485. (if (file-exists-p file)
  486. (if interactively
  487. (y-or-n-p (format "File %s exists, replace it? " file))
  488. (error "File %s already exists" file))
  489. t))
  490. (defvar org-freemind-node-pattern (rx bol
  491. (submatch (1+ "*"))
  492. (1+ space)
  493. (submatch (*? nonl))
  494. eol))
  495. (defun org-freemind-look-for-visible-child (node-level)
  496. (save-excursion
  497. (save-match-data
  498. (let ((found-visible-child nil))
  499. (while (and (not found-visible-child)
  500. (re-search-forward org-freemind-node-pattern nil t))
  501. (let* ((m1 (match-string-no-properties 1))
  502. (level (length m1)))
  503. (if (>= node-level level)
  504. (setq found-visible-child 'none)
  505. (unless (get-char-property (line-beginning-position) 'invisible)
  506. (setq found-visible-child 'found)))))
  507. (eq found-visible-child 'found)
  508. ))))
  509. (defun org-freemind-goto-line (line)
  510. "Go to line number LINE."
  511. (save-restriction
  512. (widen)
  513. (goto-char (point-min))
  514. (forward-line (1- line))))
  515. (defun org-freemind-write-mm-buffer (org-buffer mm-buffer node-at-line)
  516. (with-current-buffer org-buffer
  517. (dolist (node-style org-freemind-node-styles)
  518. (when (string-match-p (car node-style) buffer-file-name)
  519. (setq org-freemind-node-style (cadr node-style))))
  520. ;;(message "org-freemind-node-style =%s" org-freemind-node-style)
  521. (save-match-data
  522. (let* ((drawers (copy-sequence org-drawers))
  523. drawers-regexp
  524. (num-top1-nodes 0)
  525. (num-top2-nodes 0)
  526. num-left-nodes
  527. (unclosed-nodes 0)
  528. (first-time t)
  529. (current-level 1)
  530. base-level
  531. skipping-odd
  532. (skipped-odd 0)
  533. prev-node-end
  534. rich-text
  535. unfinished-tag
  536. node-at-line-level
  537. node-at-line-last)
  538. (with-current-buffer mm-buffer
  539. (erase-buffer)
  540. (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
  541. (insert "<map version=\"0.9.0\">\n")
  542. (insert "<!-- To view this file, download free mind mapping software FreeMind from http://freemind.sourceforge.net -->\n"))
  543. (save-excursion
  544. ;; Get special buffer vars:
  545. (goto-char (point-min))
  546. (while (re-search-forward (rx bol "#+DRAWERS:") nil t)
  547. (let ((dr-txt (buffer-substring-no-properties (match-end 0) (line-end-position))))
  548. (setq drawers (append drawers (split-string dr-txt) nil))))
  549. (setq drawers-regexp
  550. (concat (rx bol (0+ blank) ":")
  551. (regexp-opt drawers)
  552. (rx ":" (0+ blank)
  553. "\n"
  554. (*? anything)
  555. "\n"
  556. (0+ blank)
  557. ":END:"
  558. (0+ blank)
  559. eol)
  560. ))
  561. (if node-at-line
  562. ;; Get number of top nodes and last line for this node
  563. (progn
  564. (org-freemind-goto-line node-at-line)
  565. (unless (looking-at org-freemind-node-pattern)
  566. (error "No node at line %s" node-at-line))
  567. (setq node-at-line-level (length (match-string-no-properties 1)))
  568. (forward-line)
  569. (setq node-at-line-last
  570. (catch 'last-line
  571. (while (re-search-forward org-freemind-node-pattern nil t)
  572. (let* ((m1 (match-string-no-properties 1))
  573. (level (length m1)))
  574. (if (<= level node-at-line-level)
  575. (progn
  576. (beginning-of-line)
  577. (throw 'last-line (1- (point))))
  578. (if (= level (1+ node-at-line-level))
  579. (setq num-top2-nodes (1+ num-top2-nodes))))))))
  580. (setq current-level node-at-line-level)
  581. (setq num-top1-nodes 1)
  582. (org-freemind-goto-line node-at-line))
  583. ;; First get number of top nodes
  584. (goto-char (point-min))
  585. (while (re-search-forward org-freemind-node-pattern nil t)
  586. (let* ((m1 (match-string-no-properties 1))
  587. (level (length m1)))
  588. (if (= level 1)
  589. (setq num-top1-nodes (1+ num-top1-nodes))
  590. (if (= level 2)
  591. (setq num-top2-nodes (1+ num-top2-nodes))))))
  592. ;; If there is more than one top node we need to insert a node
  593. ;; to keep them together.
  594. (goto-char (point-min))
  595. (when (> num-top1-nodes 1)
  596. (setq num-top2-nodes num-top1-nodes)
  597. (setq current-level 0)
  598. (let ((orig-name (if buffer-file-name
  599. (file-name-nondirectory (buffer-file-name))
  600. (buffer-name))))
  601. (with-current-buffer mm-buffer
  602. (insert "<node text=\"" orig-name "\" background_color=\"#00bfff\">\n"
  603. ;; Put a note that this is for the parent node
  604. "<richcontent TYPE=\"NOTE\"><html>"
  605. "<head>"
  606. "</head>"
  607. "<body>"
  608. "<p>"
  609. org-freemind-org-nfix "WHOLE FILE"
  610. "</p>"
  611. "</body>"
  612. "</html>"
  613. "</richcontent>\n")))))
  614. (setq num-left-nodes (floor num-top2-nodes 2))
  615. (setq base-level current-level)
  616. (let (this-m2
  617. this-node-end
  618. this-children-visible
  619. next-m2
  620. next-node-start
  621. next-level
  622. next-has-some-visible-child
  623. next-children-visible
  624. )
  625. (while (and
  626. (re-search-forward org-freemind-node-pattern nil t)
  627. (if node-at-line-last (<= (point) node-at-line-last) t)
  628. )
  629. (let* ((next-m1 (match-string-no-properties 1))
  630. (next-node-end (match-end 0))
  631. )
  632. (setq next-node-start (match-beginning 0))
  633. (setq next-m2 (match-string-no-properties 2))
  634. (setq next-level (length next-m1))
  635. (when (> next-level current-level)
  636. (if (not (and org-odd-levels-only
  637. (/= (mod current-level 2) 0)
  638. (= next-level (+ 2 current-level))))
  639. (setq skipping-odd nil)
  640. (setq skipping-odd t)
  641. (setq skipped-odd (1+ skipped-odd)))
  642. (unless (or (= next-level (1+ current-level))
  643. skipping-odd)
  644. (if (or org-odd-levels-only
  645. (/= next-level (+ 2 current-level)))
  646. (error "Next level step > +1 for node ending at line %s" (line-number-at-pos))
  647. (error "Next level step = +2 for node ending at line %s, forgot org-odd-levels-only?"
  648. (line-number-at-pos)))
  649. ))
  650. (setq next-children-visible
  651. (not (eq 'outline
  652. (get-char-property (line-end-position) 'invisible))))
  653. (setq next-has-some-visible-child
  654. (if next-children-visible t
  655. (org-freemind-look-for-visible-child next-level)))
  656. (when this-m2
  657. (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)))
  658. (when (if (= num-top1-nodes 1) (> current-level base-level) t)
  659. (while (>= current-level next-level)
  660. (with-current-buffer mm-buffer
  661. (insert "</node>\n")
  662. (setq current-level (1- current-level))
  663. (when (< 0 skipped-odd)
  664. (setq skipped-odd (1- skipped-odd))
  665. (setq current-level (1- current-level)))
  666. )))
  667. (setq this-node-end (1+ next-node-end))
  668. (setq this-m2 next-m2)
  669. (setq current-level next-level)
  670. (setq this-children-visible next-children-visible)
  671. (forward-char)
  672. ))
  673. ;;; (unless (if node-at-line-last
  674. ;;; (>= (point) node-at-line-last)
  675. ;;; nil)
  676. ;; Write last node:
  677. (setq this-m2 next-m2)
  678. (setq current-level next-level)
  679. (setq next-node-start (if node-at-line-last
  680. (1+ node-at-line-last)
  681. (point-max)))
  682. (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))
  683. (with-current-buffer mm-buffer (insert "</node>\n"))
  684. ;)
  685. )
  686. (with-current-buffer mm-buffer
  687. (while (> current-level base-level)
  688. (insert "</node>\n")
  689. (setq current-level (1- current-level))
  690. ))
  691. (with-current-buffer mm-buffer
  692. (insert "</map>")
  693. (delete-trailing-whitespace)
  694. (goto-char (point-min))
  695. ))))))
  696. (defun org-freemind-get-node-style (node-name)
  697. "NOT READY YET."
  698. ;;<node BACKGROUND_COLOR="#eeee00" CREATED="1234668815593" MODIFIED="1234668815593" STYLE="bubble">
  699. ;;<font BOLD="true" NAME="SansSerif" SIZE="12"/>
  700. (let (node-styles
  701. node-style)
  702. (dolist (style-list org-freemind-node-style)
  703. (let ((node-regexp (car style-list)))
  704. (message "node-regexp=%s node-name=%s" node-regexp node-name)
  705. (when (string-match-p node-regexp node-name)
  706. ;;(setq node-style (org-freemind-do-apply-node-style style-list))
  707. (setq node-style (cadr style-list))
  708. (when node-style
  709. (message "node-style=%s" node-style)
  710. (setq node-styles (append node-styles node-style)))
  711. )))))
  712. (defun org-freemind-do-apply-node-style (style-list)
  713. (message "style-list=%S" style-list)
  714. (let ((node-style 'fork)
  715. (color "red")
  716. (background-color "yellow")
  717. (edge-color "green")
  718. (edge-style 'bezier)
  719. (edge-width 'thin)
  720. (italic t)
  721. (bold t)
  722. (font-name "SansSerif")
  723. (font-size 12))
  724. (dolist (style (cadr style-list))
  725. (message " style=%s" style)
  726. (let ((what (car style)))
  727. (cond
  728. ((eq what 'node-style)
  729. (setq node-style (cadr style)))
  730. ((eq what 'color)
  731. (setq color (cadr style)))
  732. ((eq what 'background-color)
  733. (setq background-color (cadr style)))
  734. ((eq what 'edge-color)
  735. (setq edge-color (cadr style)))
  736. ((eq what 'edge-style)
  737. (setq edge-style (cadr style)))
  738. ((eq what 'edge-width)
  739. (setq edge-width (cadr style)))
  740. ((eq what 'italic)
  741. (setq italic (cadr style)))
  742. ((eq what 'bold)
  743. (setq bold (cadr style)))
  744. ((eq what 'font-name)
  745. (setq font-name (cadr style)))
  746. ((eq what 'font-size)
  747. (setq font-size (cadr style)))
  748. )
  749. (insert (format " style=\"%s\"" node-style))
  750. (insert (format " color=\"%s\"" color))
  751. (insert (format " background_color=\"%s\"" background-color))
  752. (insert ">\n")
  753. (insert "<edge")
  754. (insert (format " color=\"%s\"" edge-color))
  755. (insert (format " style=\"%s\"" edge-style))
  756. (insert (format " width=\"%s\"" edge-width))
  757. (insert "/>\n")
  758. (insert "<font")
  759. (insert (format " italic=\"%s\"" italic))
  760. (insert (format " bold=\"%s\"" bold))
  761. (insert (format " name=\"%s\"" font-name))
  762. (insert (format " size=\"%s\"" font-size))
  763. ))))
  764. ;;;###autoload
  765. (defun org-freemind-from-org-mode-node (node-line mm-file)
  766. "Convert node at line NODE-LINE to the FreeMind file MM-FILE."
  767. (interactive
  768. (progn
  769. (unless (org-back-to-heading nil)
  770. (error "Can't find org-mode node start"))
  771. (let* ((line (line-number-at-pos))
  772. (default-mm-file (concat (if buffer-file-name
  773. (file-name-nondirectory buffer-file-name)
  774. "nofile")
  775. "-line-" (number-to-string line)
  776. ".mm"))
  777. (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file)))
  778. (list line mm-file))))
  779. (when (org-freemind-check-overwrite mm-file (called-interactively-p))
  780. (let ((org-buffer (current-buffer))
  781. (mm-buffer (find-file-noselect mm-file)))
  782. (org-freemind-write-mm-buffer org-buffer mm-buffer node-line)
  783. (with-current-buffer mm-buffer
  784. (basic-save-buffer)
  785. (when (called-interactively-p)
  786. (switch-to-buffer-other-window mm-buffer)
  787. (when (y-or-n-p "Show in FreeMind? ")
  788. (org-freemind-show buffer-file-name)))))))
  789. ;;;###autoload
  790. (defun org-freemind-from-org-mode (org-file mm-file)
  791. "Convert the `org-mode' file ORG-FILE to the FreeMind file MM-FILE."
  792. ;; Fix-me: better doc, include recommendations etc.
  793. (interactive
  794. (let* ((org-file buffer-file-name)
  795. (default-mm-file (concat
  796. (if org-file
  797. (file-name-nondirectory org-file)
  798. "nofile")
  799. ".mm"))
  800. (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file)))
  801. (list org-file mm-file)))
  802. (when (org-freemind-check-overwrite mm-file (called-interactively-p))
  803. (let ((org-buffer (if org-file (find-file-noselect org-file) (current-buffer)))
  804. (mm-buffer (find-file-noselect mm-file)))
  805. (org-freemind-write-mm-buffer org-buffer mm-buffer nil)
  806. (with-current-buffer mm-buffer
  807. (basic-save-buffer)
  808. (when (called-interactively-p)
  809. (switch-to-buffer-other-window mm-buffer)
  810. (when (y-or-n-p "Show in FreeMind? ")
  811. (org-freemind-show buffer-file-name)))))))
  812. ;;;###autoload
  813. (defun org-freemind-from-org-sparse-tree (org-buffer mm-file)
  814. "Convert visible part of buffer ORG-BUFFER to FreeMind file MM-FILE."
  815. (interactive
  816. (let* ((org-file buffer-file-name)
  817. (default-mm-file (concat
  818. (if org-file
  819. (file-name-nondirectory org-file)
  820. "nofile")
  821. "-sparse.mm"))
  822. (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file)))
  823. (list (current-buffer) mm-file)))
  824. (when (org-freemind-check-overwrite mm-file (called-interactively-p))
  825. (let (org-buffer
  826. (mm-buffer (find-file-noselect mm-file)))
  827. (save-window-excursion
  828. (org-export-visible ?\ nil)
  829. (setq org-buffer (current-buffer)))
  830. (org-freemind-write-mm-buffer org-buffer mm-buffer nil)
  831. (with-current-buffer mm-buffer
  832. (basic-save-buffer)
  833. (when (called-interactively-p)
  834. (switch-to-buffer-other-window mm-buffer)
  835. (when (y-or-n-p "Show in FreeMind? ")
  836. (org-freemind-show buffer-file-name)))))))
  837. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  838. ;;; FreeMind => Org
  839. ;; (sort '(b a c) 'org-freemind-lt-symbols)
  840. (defun org-freemind-lt-symbols (sym-a sym-b)
  841. (string< (symbol-name sym-a) (symbol-name sym-b)))
  842. ;; (sort '((b . 1) (a . 2) (c . 3)) 'org-freemind-lt-xml-attrs)
  843. (defun org-freemind-lt-xml-attrs (attr-a attr-b)
  844. (string< (symbol-name (car attr-a)) (symbol-name (car attr-b))))
  845. ;; xml-parse-region gives things like
  846. ;; ((p nil "\n"
  847. ;; (a
  848. ;; ((href . "link"))
  849. ;; "text")
  850. ;; "\n"
  851. ;; (b nil "hej")
  852. ;; "\n"))
  853. ;; '(a . nil)
  854. ;; (org-freemind-symbols= 'a (car '(A B)))
  855. (defsubst org-freemind-symbols= (sym-a sym-b)
  856. "Return t if downcased names of SYM-A and SYM-B are equal.
  857. SYM-A and SYM-B should be symbols."
  858. (or (eq sym-a sym-b)
  859. (string= (downcase (symbol-name sym-a))
  860. (downcase (symbol-name sym-b)))))
  861. (defun org-freemind-get-children (parent path)
  862. "Find children node to PARENT from PATH.
  863. PATH should be a list of steps, where each step has the form
  864. '(NODE-NAME (ATTR-NAME . ATTR-VALUE))"
  865. ;; Fix-me: maybe implement op? step: Name, number, attr, attr op val
  866. ;; Fix-me: case insensitive version for children?
  867. (let* ((children (if (not (listp (car parent)))
  868. (cddr parent)
  869. (let (cs)
  870. (dolist (p parent)
  871. (dolist (c (cddr p))
  872. (add-to-list 'cs c)))
  873. cs)
  874. ))
  875. (step (car path))
  876. (step-node (if (listp step) (car step) step))
  877. (step-attr-list (when (listp step) (sort (cdr step) 'org-freemind-lt-xml-attrs)))
  878. (path-tail (cdr path))
  879. path-children)
  880. (dolist (child children)
  881. ;; skip xml.el formatting nodes
  882. (unless (stringp child)
  883. ;; compare node name
  884. (when (if (not step-node)
  885. t ;; any node name
  886. (org-freemind-symbols= step-node (car child)))
  887. (if (not step-attr-list)
  888. ;;(throw 'path-child child) ;; no attr to care about
  889. (add-to-list 'path-children child)
  890. (let* ((child-attr-list (cadr child))
  891. (step-attr-copy (copy-sequence step-attr-list)))
  892. (dolist (child-attr child-attr-list)
  893. ;; Compare attr names:
  894. (when (org-freemind-symbols= (caar step-attr-copy) (car child-attr))
  895. ;; Compare values:
  896. (let ((step-val (cdar step-attr-copy))
  897. (child-val (cdr child-attr)))
  898. (when (if (not step-val)
  899. t ;; any value
  900. (string= step-val child-val))
  901. (setq step-attr-copy (cdr step-attr-copy))))))
  902. ;; Did we find all?
  903. (unless step-attr-copy
  904. ;;(throw 'path-child child)
  905. (add-to-list 'path-children child)
  906. ))))))
  907. (if path-tail
  908. (org-freemind-get-children path-children path-tail)
  909. path-children)))
  910. (defun org-freemind-get-richcontent-node (node)
  911. (let ((rc-nodes
  912. (org-freemind-get-children node '((richcontent (type . "NODE")) html body))))
  913. (when (> (length rc-nodes) 1)
  914. (lwarn t :warning "Unexpected structure: several <richcontent type=\"NODE\" ...>"))
  915. (car rc-nodes)))
  916. (defun org-freemind-get-richcontent-note (node)
  917. (let ((rc-notes
  918. (org-freemind-get-children node '((richcontent (type . "NOTE")) html body))))
  919. (when (> (length rc-notes) 1)
  920. (lwarn t :warning "Unexpected structure: several <richcontent type=\"NOTE\" ...>"))
  921. (car rc-notes)))
  922. (defun org-freemind-test-get-tree-text ()
  923. (let ((node '(p nil "\n"
  924. (a
  925. ((href . "link"))
  926. "text")
  927. "\n"
  928. (b nil "hej")
  929. "\n")))
  930. (org-freemind-get-tree-text node)))
  931. ;; (org-freemind-test-get-tree-text)
  932. (defun org-freemind-get-tree-text (node)
  933. (when node
  934. (let ((ntxt "")
  935. (link nil)
  936. (lf-after nil))
  937. (dolist (n node)
  938. (case n
  939. ;;(a (setq is-link t) )
  940. ((h1 h2 h3 h4 h5 h6 p)
  941. ;;(setq ntxt (concat "\n" ntxt))
  942. (setq lf-after 2)
  943. )
  944. (br
  945. (setq lf-after 1)
  946. )
  947. (t
  948. (cond
  949. ((stringp n)
  950. (when (string= n "\n") (setq n ""))
  951. (if link
  952. (setq ntxt (concat ntxt
  953. "[[" link "][" n "]]"))
  954. (setq ntxt (concat ntxt n))))
  955. ((and n (listp n))
  956. (if (symbolp (car n))
  957. (setq ntxt (concat ntxt (org-freemind-get-tree-text n)))
  958. ;; This should be the attributes:
  959. (dolist (att-val n)
  960. (let ((att (car att-val))
  961. (val (cdr att-val)))
  962. (when (eq att 'href)
  963. (setq link val)))))
  964. )))))
  965. (if lf-after
  966. (setq ntxt (concat ntxt (make-string lf-after ?\n)))
  967. (setq ntxt (concat ntxt " ")))
  968. ;;(setq ntxt (concat ntxt (format "{%s}" n)))
  969. ntxt)))
  970. (defun org-freemind-get-richcontent-node-text (node)
  971. "Get the node text as from the richcontent node NODE."
  972. (save-match-data
  973. (let* ((rc (org-freemind-get-richcontent-node node))
  974. (txt (org-freemind-get-tree-text rc)))
  975. ;;(when txt (setq txt (replace-regexp-in-string (rx (1+ whitespace)) " " txt)))
  976. txt
  977. )))
  978. (defun org-freemind-get-richcontent-note-text (node)
  979. "Get the node text as from the richcontent note NODE."
  980. (save-match-data
  981. (let* ((rc (org-freemind-get-richcontent-note node))
  982. (txt (when rc (org-freemind-get-tree-text rc))))
  983. ;;(when txt (setq txt (replace-regexp-in-string (rx (1+ whitespace)) " " txt)))
  984. txt
  985. )))
  986. (defun org-freemind-get-icon-names (node)
  987. (let* ((icon-nodes (org-freemind-get-children node '((icon ))))
  988. names)
  989. (dolist (icn icon-nodes)
  990. (setq names (cons (cdr (assq 'builtin (cadr icn))) names)))
  991. ;; (icon (builtin . "full-1"))
  992. names))
  993. (defun org-freemind-node-to-org (node level skip-levels)
  994. (let ((qname (car node))
  995. (attributes (cadr node))
  996. text
  997. (note (org-freemind-get-richcontent-note-text node))
  998. (mark "-- This is more about ")
  999. (icons (org-freemind-get-icon-names node))
  1000. (children (cddr node)))
  1001. (when (< 0 (- level skip-levels))
  1002. (dolist (attrib attributes)
  1003. (case (car attrib)
  1004. ('TEXT (setq text (cdr attrib)))
  1005. ('text (setq text (cdr attrib)))))
  1006. (unless text
  1007. ;; There should be a richcontent node holding the text:
  1008. (setq text (org-freemind-get-richcontent-node-text node)))
  1009. (when icons
  1010. (when (member "full-1" icons) (setq text (concat "[#A] " text)))
  1011. (when (member "full-2" icons) (setq text (concat "[#B] " text)))
  1012. (when (member "full-3" icons) (setq text (concat "[#C] " text)))
  1013. (when (member "full-4" icons) (setq text (concat "[#D] " text)))
  1014. (when (member "full-5" icons) (setq text (concat "[#E] " text)))
  1015. (when (member "full-6" icons) (setq text (concat "[#F] " text)))
  1016. (when (member "full-7" icons) (setq text (concat "[#G] " text)))
  1017. (when (member "button_cancel" icons) (setq text (concat "TODO " text)))
  1018. )
  1019. (if (and note
  1020. (string= mark (substring note 0 (length mark))))
  1021. (progn
  1022. (setq text (replace-regexp-in-string "\n $" "" text))
  1023. (insert text))
  1024. (case qname
  1025. ('node
  1026. (insert (make-string (- level skip-levels) ?*) " " text "\n")
  1027. ))))
  1028. (dolist (child children)
  1029. (unless (or (null child)
  1030. (stringp child))
  1031. (org-freemind-node-to-org child (1+ level) skip-levels)))))
  1032. ;; Fix-me: put back special things, like drawers that are stored in
  1033. ;; the notes. Should maybe all notes contents be put in drawers?
  1034. ;;;###autoload
  1035. (defun org-freemind-to-org-mode (mm-file org-file)
  1036. "Convert FreeMind file MM-FILE to `org-mode' file ORG-FILE."
  1037. (interactive
  1038. (save-match-data
  1039. (let* ((mm-file (buffer-file-name))
  1040. (default-org-file (concat (file-name-nondirectory mm-file) ".org"))
  1041. (org-file (read-file-name "Output org-mode file: " nil nil nil default-org-file)))
  1042. (list mm-file org-file))))
  1043. (when (org-freemind-check-overwrite org-file (called-interactively-p))
  1044. (let ((mm-buffer (find-file-noselect mm-file))
  1045. (org-buffer (find-file-noselect org-file)))
  1046. (with-current-buffer mm-buffer
  1047. (let* ((xml-list (xml-parse-file mm-file))
  1048. (top-node (cadr (cddar xml-list)))
  1049. (note (org-freemind-get-richcontent-note-text top-node))
  1050. (skip-levels
  1051. (if (and note
  1052. (string-match (rx bol "--org-mode: WHOLE FILE" eol) note))
  1053. 1
  1054. 0)))
  1055. (with-current-buffer org-buffer
  1056. (erase-buffer)
  1057. (org-freemind-node-to-org top-node 1 skip-levels)
  1058. (goto-char (point-min))
  1059. (org-set-tags t t) ;; Align all tags
  1060. )
  1061. (switch-to-buffer-other-window org-buffer)
  1062. )))))
  1063. (provide 'org-freemind)
  1064. ;; arch-tag: e7b0d776-94fd-404a-b35e-0f855fae3627
  1065. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1066. ;;; org-freemind.el ends here