org-freemind.el 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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.32trans
  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-subree-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. (defvar str)
  232. (defun org-freemind-unescape-str-to-org (fm-str)
  233. "Do some html-unescaping of FM-STR and return the result.
  234. This is the opposite of `org-freemind-escape-str-from-org' but it
  235. will also unescape &#nn;."
  236. (let ((org-str fm-str))
  237. (setq org-str (replace-regexp-in-string "&quot;" "\"" org-str))
  238. (setq org-str (replace-regexp-in-string "&amp;" "&" org-str))
  239. (setq org-str (replace-regexp-in-string "&lt;" "<" org-str))
  240. (setq org-str (replace-regexp-in-string "&gt;" ">" org-str))
  241. (setq org-str (replace-regexp-in-string
  242. "&#x\\([a-f0-9]\\{2,4\\}\\);"
  243. (lambda (m)
  244. (char-to-string
  245. ;; Note: str is scoped dynamically from
  246. ;; `replace-regexp-in-string'.
  247. (+ (string-to-number (match-string 1 str) 16)
  248. 0 ;?\x800 ;; What is this for? Encoding?
  249. )))
  250. org-str))))
  251. ;; (org-freemind-test-escape)
  252. (defun org-freemind-test-escape ()
  253. (let* ((str1 "a quote: \", an amp: &, lt: <; over 256: öåäÖÅÄ")
  254. (str2 (org-freemind-escape-str-from-org str1))
  255. (str3 (org-freemind-unescape-str-to-org str2))
  256. )
  257. (unless (string= str1 str3)
  258. (error "str3=%s" str3))
  259. ))
  260. (defun org-freemind-convert-links-from-org (org-str)
  261. "Convert org links in ORG-STR to freemind links and return the result."
  262. (let ((fm-str (replace-regexp-in-string
  263. (rx (not (any "[\""))
  264. (submatch
  265. "http"
  266. (opt ?\s)
  267. "://"
  268. (1+
  269. (any "-%.?@a-zA-Z0-9()_/:~=&#"))))
  270. "[[\\1][\\1]]"
  271. org-str)))
  272. (replace-regexp-in-string (rx "[["
  273. (submatch (*? nonl))
  274. "]["
  275. (submatch (*? nonl))
  276. "]]")
  277. "<a href=\"\\1\">\\2</a>"
  278. fm-str)))
  279. ;;(org-freemind-convert-links-to-org "<a href=\"http://www.somewhere/\">link-text</a>")
  280. (defun org-freemind-convert-links-to-org (fm-str)
  281. "Convert freemind links in FM-STR to org links and return the result."
  282. (let ((org-str (replace-regexp-in-string
  283. (rx "<a"
  284. space
  285. (0+
  286. (0+ (not (any ">")))
  287. space)
  288. "href=\""
  289. (submatch (0+ (not (any "\""))))
  290. "\""
  291. (0+ (not (any ">")))
  292. ">"
  293. (submatch (0+ (not (any "<"))))
  294. "</a>")
  295. "[[\\1][\\2]]"
  296. fm-str)))
  297. org-str))
  298. ;; Fix-me:
  299. ;;(defun org-freemind-convert-drawers-from-org (text)
  300. ;; )
  301. ;; (org-freemind-test-links)
  302. ;; (defun org-freemind-test-links ()
  303. ;; (let* ((str1 "[[http://www.somewhere/][link-text]")
  304. ;; (str2 (org-freemind-convert-links-from-org str1))
  305. ;; (str3 (org-freemind-convert-links-to-org str2))
  306. ;; )
  307. ;; (unless (string= str1 str3)
  308. ;; (error "str3=%s" str3))
  309. ;; ))
  310. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  311. ;;; Org => FreeMind
  312. (defun org-freemind-convert-text-p (text)
  313. "Convert TEXT to html with <p> paragraphs."
  314. (setq text (org-freemind-escape-str-from-org text))
  315. (setq text (replace-regexp-in-string (rx "\n" (0+ blank) "\n") "</p><p>\n" text))
  316. ;;(setq text (replace-regexp-in-string (rx bol (1+ blank) eol) "" text))
  317. ;;(setq text (replace-regexp-in-string (rx bol (1+ blank)) "<br />" text))
  318. (setq text (replace-regexp-in-string "\n" "<br />" text))
  319. (concat "<p>"
  320. (org-freemind-convert-links-from-org text)
  321. "</p>\n"))
  322. (defun org-freemind-org-text-to-freemind-subnode/note (node-name start end drawers-regexp)
  323. "Convert text part of org node to freemind subnode or note.
  324. Convert the text part of the org node named NODE-NAME. The text
  325. is in the current buffer between START and END. Drawers matching
  326. DRAWERS-REGEXP are converted to freemind notes."
  327. ;; fix-me: doc
  328. (let ((text (buffer-substring-no-properties start end))
  329. (node-res "")
  330. (note-res ""))
  331. (save-match-data
  332. ;;(setq text (org-freemind-escape-str-from-org text))
  333. ;; First see if there is something that should be moved to the
  334. ;; note part:
  335. (let (drawers)
  336. (while (string-match drawers-regexp text)
  337. (setq drawers (cons (match-string 0 text) drawers))
  338. (setq text
  339. (concat (substring text 0 (match-beginning 0))
  340. (substring text (match-end 0))))
  341. )
  342. (when drawers
  343. (dolist (drawer drawers)
  344. (let ((lines (split-string drawer "\n")))
  345. (dolist (line lines)
  346. (setq note-res (concat
  347. note-res
  348. org-freemind-org-nfix line "<br />\n")))
  349. ))))
  350. (when (> (length note-res) 0)
  351. (setq note-res (concat
  352. "<richcontent TYPE=\"NOTE\"><html>\n"
  353. "<head>\n"
  354. "</head>\n"
  355. "<body>\n"
  356. note-res
  357. "</body>\n"
  358. "</html>\n"
  359. "</richcontent>\n"))
  360. )
  361. ;; There is always an LF char:
  362. (when (> (length text) 1)
  363. (setq node-res (concat
  364. "<node style=\"bubble\" background_color=\"#eeee00\">\n"
  365. "<richcontent TYPE=\"NODE\"><html>\n"
  366. "<head>\n"
  367. "<style type=\"text/css\">\n"
  368. "<!--\n"
  369. "p { margin-top: 0 }\n"
  370. "-->\n"
  371. "</style>\n"
  372. "</head>\n"
  373. "<body>\n"))
  374. (let ((begin-html-mark (regexp-quote "#+BEGIN_HTML"))
  375. (end-html-mark (regexp-quote "#+END_HTML"))
  376. head
  377. end-pos
  378. end-pos-match
  379. )
  380. ;; Take care of #+BEGIN_HTML - #+END_HTML
  381. (while (string-match begin-html-mark text)
  382. (setq head (substring text 0 (match-beginning 0)))
  383. (setq end-pos-match (match-end 0))
  384. (setq node-res (concat node-res
  385. (org-freemind-convert-text-p head)))
  386. (setq text (substring text end-pos-match))
  387. (setq end-pos (string-match end-html-mark text))
  388. (if end-pos
  389. (setq end-pos-match (match-end 0))
  390. (message "org-freemind: Missing #+END_HTML")
  391. (setq end-pos (length text))
  392. (setq end-pos-match end-pos))
  393. (setq node-res (concat node-res
  394. (substring text 0 end-pos)))
  395. (setq text (substring text end-pos-match)))
  396. (setq node-res (concat node-res
  397. (org-freemind-convert-text-p text))))
  398. (setq node-res (concat
  399. node-res
  400. "</body>\n"
  401. "</html>\n"
  402. "</richcontent>\n"
  403. ;; Put a note that this is for the parent node
  404. "<richcontent TYPE=\"NOTE\"><html>"
  405. "<head>"
  406. "</head>"
  407. "<body>"
  408. "<p>"
  409. "-- This is more about \"" node-name "\" --"
  410. "</p>"
  411. "</body>"
  412. "</html>"
  413. "</richcontent>\n"
  414. "</node>\n" ;; ok
  415. )))
  416. (list node-res note-res))))
  417. (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)
  418. (let* (this-icons
  419. this-bg-color
  420. this-m2-escaped
  421. this-rich-node
  422. this-rich-note
  423. )
  424. (when (string-match "TODO" this-m2)
  425. (setq this-m2 (replace-match "" nil nil this-m2))
  426. (add-to-list 'this-icons "button_cancel")
  427. (setq this-bg-color "#ffff88")
  428. (when (string-match "\\[#\\(.\\)\\]" this-m2)
  429. (let ((prior (string-to-char (match-string 1 this-m2))))
  430. (setq this-m2 (replace-match "" nil nil this-m2))
  431. (cond
  432. ((= prior ?A)
  433. (add-to-list 'this-icons "full-1")
  434. (setq this-bg-color "#ff0000"))
  435. ((= prior ?B)
  436. (add-to-list 'this-icons "full-2")
  437. (setq this-bg-color "#ffaa00"))
  438. ((= prior ?C)
  439. (add-to-list 'this-icons "full-3")
  440. (setq this-bg-color "#ffdd00"))
  441. ((= prior ?D)
  442. (add-to-list 'this-icons "full-4")
  443. (setq this-bg-color "#ffff00"))
  444. ((= prior ?E)
  445. (add-to-list 'this-icons "full-5"))
  446. ((= prior ?F)
  447. (add-to-list 'this-icons "full-6"))
  448. ((= prior ?G)
  449. (add-to-list 'this-icons "full-7"))
  450. ))))
  451. (setq this-m2 (org-trim this-m2))
  452. (setq this-m2-escaped (org-freemind-escape-str-from-org this-m2))
  453. (let ((node-notes (org-freemind-org-text-to-freemind-subnode/note
  454. this-m2-escaped
  455. this-node-end
  456. (1- next-node-start)
  457. drawers-regexp)))
  458. (setq this-rich-node (nth 0 node-notes))
  459. (setq this-rich-note (nth 1 node-notes)))
  460. (with-current-buffer mm-buffer
  461. (insert "<node text=\"" this-m2-escaped "\"")
  462. (org-freemind-get-node-style this-m2)
  463. (when (> next-level current-level)
  464. (unless (or this-children-visible
  465. next-has-some-visible-child)
  466. (insert " folded=\"true\"")))
  467. (when (and (= current-level (1+ base-level))
  468. (> num-left-nodes 0))
  469. (setq num-left-nodes (1- num-left-nodes))
  470. (insert " position=\"left\""))
  471. (when this-bg-color
  472. (insert " background_color=\"" this-bg-color "\""))
  473. (insert ">\n")
  474. (when this-icons
  475. (dolist (icon this-icons)
  476. (insert "<icon builtin=\"" icon "\"/>\n")))
  477. )
  478. (with-current-buffer mm-buffer
  479. (when this-rich-note (insert this-rich-note))
  480. (when this-rich-node (insert this-rich-node))))
  481. num-left-nodes)
  482. (defun org-freemind-check-overwrite (file interactively)
  483. "Check if file FILE already exists.
  484. If FILE does not exists return t.
  485. If INTERACTIVELY is non-nil ask if the file should be replaced
  486. and return t/nil if it should/should not be replaced.
  487. Otherwise give an error say the file exists."
  488. (if (file-exists-p file)
  489. (if interactively
  490. (y-or-n-p (format "File %s exists, replace it? " file))
  491. (error "File %s already exists" file))
  492. t))
  493. (defvar org-freemind-node-pattern (rx bol
  494. (submatch (1+ "*"))
  495. (1+ space)
  496. (submatch (*? nonl))
  497. eol))
  498. (defun org-freemind-look-for-visible-child (node-level)
  499. (save-excursion
  500. (save-match-data
  501. (let ((found-visible-child nil))
  502. (while (and (not found-visible-child)
  503. (re-search-forward org-freemind-node-pattern nil t))
  504. (let* ((m1 (match-string-no-properties 1))
  505. (level (length m1)))
  506. (if (>= node-level level)
  507. (setq found-visible-child 'none)
  508. (unless (get-char-property (line-beginning-position) 'invisible)
  509. (setq found-visible-child 'found)))))
  510. (eq found-visible-child 'found)
  511. ))))
  512. (defun org-freemind-goto-line (line)
  513. "Go to line number LINE."
  514. (save-restriction
  515. (widen)
  516. (goto-char (point-min))
  517. (forward-line (1- line))))
  518. (defun org-freemind-write-mm-buffer (org-buffer mm-buffer node-at-line)
  519. (with-current-buffer org-buffer
  520. (dolist (node-style org-freemind-node-styles)
  521. (when (string-match-p (car node-style) buffer-file-name)
  522. (setq org-freemind-node-style (cadr node-style))))
  523. ;;(message "org-freemind-node-style =%s" org-freemind-node-style)
  524. (save-match-data
  525. (let* ((drawers (copy-sequence org-drawers))
  526. drawers-regexp
  527. (num-top1-nodes 0)
  528. (num-top2-nodes 0)
  529. num-left-nodes
  530. (unclosed-nodes 0)
  531. (first-time t)
  532. (current-level 1)
  533. base-level
  534. skipping-odd
  535. (skipped-odd 0)
  536. prev-node-end
  537. rich-text
  538. unfinished-tag
  539. node-at-line-level
  540. node-at-line-last)
  541. (with-current-buffer mm-buffer
  542. (erase-buffer)
  543. (insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
  544. (insert "<map version=\"0.9.0\">\n")
  545. (insert "<!-- To view this file, download free mind mapping software FreeMind from http://freemind.sourceforge.net -->\n"))
  546. (save-excursion
  547. ;; Get special buffer vars:
  548. (goto-char (point-min))
  549. (while (re-search-forward (rx bol "#+DRAWERS:") nil t)
  550. (let ((dr-txt (buffer-substring-no-properties (match-end 0) (line-end-position))))
  551. (setq drawers (append drawers (split-string dr-txt) nil))))
  552. (setq drawers-regexp
  553. (concat (rx bol (0+ blank) ":")
  554. (regexp-opt drawers)
  555. (rx ":" (0+ blank)
  556. "\n"
  557. (*? anything)
  558. "\n"
  559. (0+ blank)
  560. ":END:"
  561. (0+ blank)
  562. eol)
  563. ))
  564. (if node-at-line
  565. ;; Get number of top nodes and last line for this node
  566. (progn
  567. (org-freemind-goto-line node-at-line)
  568. (unless (looking-at org-freemind-node-pattern)
  569. (error "No node at line %s" node-at-line))
  570. (setq node-at-line-level (length (match-string-no-properties 1)))
  571. (forward-line)
  572. (setq node-at-line-last
  573. (catch 'last-line
  574. (while (re-search-forward org-freemind-node-pattern nil t)
  575. (let* ((m1 (match-string-no-properties 1))
  576. (level (length m1)))
  577. (if (<= level node-at-line-level)
  578. (progn
  579. (beginning-of-line)
  580. (throw 'last-line (1- (point))))
  581. (if (= level (1+ node-at-line-level))
  582. (setq num-top2-nodes (1+ num-top2-nodes))))))))
  583. (setq current-level node-at-line-level)
  584. (setq num-top1-nodes 1)
  585. (org-freemind-goto-line node-at-line))
  586. ;; First get number of top nodes
  587. (goto-char (point-min))
  588. (while (re-search-forward org-freemind-node-pattern nil t)
  589. (let* ((m1 (match-string-no-properties 1))
  590. (level (length m1)))
  591. (if (= level 1)
  592. (setq num-top1-nodes (1+ num-top1-nodes))
  593. (if (= level 2)
  594. (setq num-top2-nodes (1+ num-top2-nodes))))))
  595. ;; If there is more than one top node we need to insert a node
  596. ;; to keep them together.
  597. (goto-char (point-min))
  598. (when (> num-top1-nodes 1)
  599. (setq num-top2-nodes num-top1-nodes)
  600. (setq current-level 0)
  601. (let ((orig-name (if buffer-file-name
  602. (file-name-nondirectory (buffer-file-name))
  603. (buffer-name))))
  604. (with-current-buffer mm-buffer
  605. (insert "<node text=\"" orig-name "\" background_color=\"#00bfff\">\n"
  606. ;; Put a note that this is for the parent node
  607. "<richcontent TYPE=\"NOTE\"><html>"
  608. "<head>"
  609. "</head>"
  610. "<body>"
  611. "<p>"
  612. org-freemind-org-nfix "WHOLE FILE"
  613. "</p>"
  614. "</body>"
  615. "</html>"
  616. "</richcontent>\n")))))
  617. (setq num-left-nodes (floor num-top2-nodes 2))
  618. (setq base-level current-level)
  619. (let (this-m2
  620. this-node-end
  621. this-children-visible
  622. next-m2
  623. next-node-start
  624. next-level
  625. next-has-some-visible-child
  626. next-children-visible
  627. )
  628. (while (and
  629. (re-search-forward org-freemind-node-pattern nil t)
  630. (if node-at-line-last (<= (point) node-at-line-last) t)
  631. )
  632. (let* ((next-m1 (match-string-no-properties 1))
  633. (next-node-end (match-end 0))
  634. )
  635. (setq next-node-start (match-beginning 0))
  636. (setq next-m2 (match-string-no-properties 2))
  637. (setq next-level (length next-m1))
  638. (when (> next-level current-level)
  639. (if (not (and org-odd-levels-only
  640. (/= (mod current-level 2) 0)
  641. (= next-level (+ 2 current-level))))
  642. (setq skipping-odd nil)
  643. (setq skipping-odd t)
  644. (setq skipped-odd (1+ skipped-odd)))
  645. (unless (or (= next-level (1+ current-level))
  646. skipping-odd)
  647. (if (or org-odd-levels-only
  648. (/= next-level (+ 2 current-level)))
  649. (error "Next level step > +1 for node ending at line %s" (line-number-at-pos))
  650. (error "Next level step = +2 for node ending at line %s, forgot org-odd-levels-only?"
  651. (line-number-at-pos)))
  652. ))
  653. (setq next-children-visible
  654. (not (eq 'outline
  655. (get-char-property (line-end-position) 'invisible))))
  656. (setq next-has-some-visible-child
  657. (if next-children-visible t
  658. (org-freemind-look-for-visible-child next-level)))
  659. (when this-m2
  660. (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)))
  661. (when (if (= num-top1-nodes 1) (> current-level base-level) t)
  662. (while (>= current-level next-level)
  663. (with-current-buffer mm-buffer
  664. (insert "</node>\n")
  665. (setq current-level (1- current-level))
  666. (when (< 0 skipped-odd)
  667. (setq skipped-odd (1- skipped-odd))
  668. (setq current-level (1- current-level)))
  669. )))
  670. (setq this-node-end (1+ next-node-end))
  671. (setq this-m2 next-m2)
  672. (setq current-level next-level)
  673. (setq this-children-visible next-children-visible)
  674. (forward-char)
  675. ))
  676. ;;; (unless (if node-at-line-last
  677. ;;; (>= (point) node-at-line-last)
  678. ;;; nil)
  679. ;; Write last node:
  680. (setq this-m2 next-m2)
  681. (setq current-level next-level)
  682. (setq next-node-start (if node-at-line-last
  683. (1+ node-at-line-last)
  684. (point-max)))
  685. (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))
  686. (with-current-buffer mm-buffer (insert "</node>\n"))
  687. ;)
  688. )
  689. (with-current-buffer mm-buffer
  690. (while (> current-level base-level)
  691. (insert "</node>\n")
  692. (setq current-level (1- current-level))
  693. ))
  694. (with-current-buffer mm-buffer
  695. (insert "</map>")
  696. (delete-trailing-whitespace)
  697. (goto-char (point-min))
  698. ))))))
  699. (defun org-freemind-get-node-style (node-name)
  700. "NOT READY YET."
  701. ;;<node BACKGROUND_COLOR="#eeee00" CREATED="1234668815593" MODIFIED="1234668815593" STYLE="bubble">
  702. ;;<font BOLD="true" NAME="SansSerif" SIZE="12"/>
  703. (let (node-styles
  704. node-style)
  705. (dolist (style-list org-freemind-node-style)
  706. (let ((node-regexp (car style-list)))
  707. (message "node-regexp=%s node-name=%s" node-regexp node-name)
  708. (when (string-match-p node-regexp node-name)
  709. ;;(setq node-style (org-freemind-do-apply-node-style style-list))
  710. (setq node-style (cadr style-list))
  711. (when node-style
  712. (message "node-style=%s" node-style)
  713. (setq node-styles (append node-styles node-style)))
  714. )))))
  715. (defun org-freemind-do-apply-node-style (style-list)
  716. (message "style-list=%S" style-list)
  717. (let ((node-style 'fork)
  718. (color "red")
  719. (background-color "yellow")
  720. (edge-color "green")
  721. (edge-style 'bezier)
  722. (edge-width 'thin)
  723. (italic t)
  724. (bold t)
  725. (font-name "SansSerif")
  726. (font-size 12))
  727. (dolist (style (cadr style-list))
  728. (message " style=%s" style)
  729. (let ((what (car style)))
  730. (cond
  731. ((eq what 'node-style)
  732. (setq node-style (cadr style)))
  733. ((eq what 'color)
  734. (setq color (cadr style)))
  735. ((eq what 'background-color)
  736. (setq background-color (cadr style)))
  737. ((eq what 'edge-color)
  738. (setq edge-color (cadr style)))
  739. ((eq what 'edge-style)
  740. (setq edge-style (cadr style)))
  741. ((eq what 'edge-width)
  742. (setq edge-width (cadr style)))
  743. ((eq what 'italic)
  744. (setq italic (cadr style)))
  745. ((eq what 'bold)
  746. (setq bold (cadr style)))
  747. ((eq what 'font-name)
  748. (setq font-name (cadr style)))
  749. ((eq what 'font-size)
  750. (setq font-size (cadr style)))
  751. )
  752. (insert (format " style=\"%s\"" node-style))
  753. (insert (format " color=\"%s\"" color))
  754. (insert (format " background_color=\"%s\"" background-color))
  755. (insert ">\n")
  756. (insert "<edge")
  757. (insert (format " color=\"%s\"" edge-color))
  758. (insert (format " style=\"%s\"" edge-style))
  759. (insert (format " width=\"%s\"" edge-width))
  760. (insert "/>\n")
  761. (insert "<font")
  762. (insert (format " italic=\"%s\"" italic))
  763. (insert (format " bold=\"%s\"" bold))
  764. (insert (format " name=\"%s\"" font-name))
  765. (insert (format " size=\"%s\"" font-size))
  766. ))))
  767. ;;;###autoload
  768. (defun org-freemind-from-org-mode-node (node-line mm-file)
  769. "Convert node at line NODE-LINE to the FreeMind file MM-FILE."
  770. (interactive
  771. (progn
  772. (unless (org-back-to-heading nil)
  773. (error "Can't find org-mode node start"))
  774. (let* ((line (line-number-at-pos))
  775. (default-mm-file (concat (if buffer-file-name
  776. (file-name-nondirectory buffer-file-name)
  777. "nofile")
  778. "-line-" (number-to-string line)
  779. ".mm"))
  780. (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file)))
  781. (list line mm-file))))
  782. (when (org-freemind-check-overwrite mm-file (called-interactively-p))
  783. (let ((org-buffer (current-buffer))
  784. (mm-buffer (find-file-noselect mm-file)))
  785. (org-freemind-write-mm-buffer org-buffer mm-buffer node-line)
  786. (with-current-buffer mm-buffer
  787. (basic-save-buffer)
  788. (when (called-interactively-p)
  789. (switch-to-buffer-other-window mm-buffer)
  790. (when (y-or-n-p "Show in FreeMind? ")
  791. (org-freemind-show buffer-file-name)))))))
  792. ;;;###autoload
  793. (defun org-freemind-from-org-mode (org-file mm-file)
  794. "Convert the `org-mode' file ORG-FILE to the FreeMind file MM-FILE."
  795. ;; Fix-me: better doc, include recommendations etc.
  796. (interactive
  797. (let* ((org-file buffer-file-name)
  798. (default-mm-file (concat
  799. (if org-file
  800. (file-name-nondirectory org-file)
  801. "nofile")
  802. ".mm"))
  803. (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file)))
  804. (list org-file mm-file)))
  805. (when (org-freemind-check-overwrite mm-file (called-interactively-p))
  806. (let ((org-buffer (if org-file (find-file-noselect org-file) (current-buffer)))
  807. (mm-buffer (find-file-noselect mm-file)))
  808. (org-freemind-write-mm-buffer org-buffer mm-buffer nil)
  809. (with-current-buffer mm-buffer
  810. (basic-save-buffer)
  811. (when (called-interactively-p)
  812. (switch-to-buffer-other-window mm-buffer)
  813. (when (y-or-n-p "Show in FreeMind? ")
  814. (org-freemind-show buffer-file-name)))))))
  815. ;;;###autoload
  816. (defun org-freemind-from-org-sparse-tree (org-buffer mm-file)
  817. "Convert visible part of buffer ORG-BUFFER to FreeMind file MM-FILE."
  818. (interactive
  819. (let* ((org-file buffer-file-name)
  820. (default-mm-file (concat
  821. (if org-file
  822. (file-name-nondirectory org-file)
  823. "nofile")
  824. "-sparse.mm"))
  825. (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file)))
  826. (list (current-buffer) mm-file)))
  827. (when (org-freemind-check-overwrite mm-file (called-interactively-p))
  828. (let (org-buffer
  829. (mm-buffer (find-file-noselect mm-file)))
  830. (save-window-excursion
  831. (org-export-visible ?\ nil)
  832. (setq org-buffer (current-buffer)))
  833. (org-freemind-write-mm-buffer org-buffer mm-buffer nil)
  834. (with-current-buffer mm-buffer
  835. (basic-save-buffer)
  836. (when (called-interactively-p)
  837. (switch-to-buffer-other-window mm-buffer)
  838. (when (y-or-n-p "Show in FreeMind? ")
  839. (org-freemind-show buffer-file-name)))))))
  840. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  841. ;;; FreeMind => Org
  842. ;; (sort '(b a c) 'org-freemind-lt-symbols)
  843. (defun org-freemind-lt-symbols (sym-a sym-b)
  844. (string< (symbol-name sym-a) (symbol-name sym-b)))
  845. ;; (sort '((b . 1) (a . 2) (c . 3)) 'org-freemind-lt-xml-attrs)
  846. (defun org-freemind-lt-xml-attrs (attr-a attr-b)
  847. (string< (symbol-name (car attr-a)) (symbol-name (car attr-b))))
  848. ;; xml-parse-region gives things like
  849. ;; ((p nil "\n"
  850. ;; (a
  851. ;; ((href . "link"))
  852. ;; "text")
  853. ;; "\n"
  854. ;; (b nil "hej")
  855. ;; "\n"))
  856. ;; '(a . nil)
  857. ;; (org-freemind-symbols= 'a (car '(A B)))
  858. (defsubst org-freemind-symbols= (sym-a sym-b)
  859. "Return t if downcased names of SYM-A and SYM-B are equal.
  860. SYM-A and SYM-B should be symbols."
  861. (or (eq sym-a sym-b)
  862. (string= (downcase (symbol-name sym-a))
  863. (downcase (symbol-name sym-b)))))
  864. (defun org-freemind-get-children (parent path)
  865. "Find children node to PARENT from PATH.
  866. PATH should be a list of steps, where each step has the form
  867. '(NODE-NAME (ATTR-NAME . ATTR-VALUE))"
  868. ;; Fix-me: maybe implement op? step: Name, number, attr, attr op val
  869. ;; Fix-me: case insensitive version for children?
  870. (let* ((children (if (not (listp (car parent)))
  871. (cddr parent)
  872. (let (cs)
  873. (dolist (p parent)
  874. (dolist (c (cddr p))
  875. (add-to-list 'cs c)))
  876. cs)
  877. ))
  878. (step (car path))
  879. (step-node (if (listp step) (car step) step))
  880. (step-attr-list (when (listp step) (sort (cdr step) 'org-freemind-lt-xml-attrs)))
  881. (path-tail (cdr path))
  882. path-children)
  883. (dolist (child children)
  884. ;; skip xml.el formatting nodes
  885. (unless (stringp child)
  886. ;; compare node name
  887. (when (if (not step-node)
  888. t ;; any node name
  889. (org-freemind-symbols= step-node (car child)))
  890. (if (not step-attr-list)
  891. ;;(throw 'path-child child) ;; no attr to care about
  892. (add-to-list 'path-children child)
  893. (let* ((child-attr-list (cadr child))
  894. (step-attr-copy (copy-sequence step-attr-list)))
  895. (dolist (child-attr child-attr-list)
  896. ;; Compare attr names:
  897. (when (org-freemind-symbols= (caar step-attr-copy) (car child-attr))
  898. ;; Compare values:
  899. (let ((step-val (cdar step-attr-copy))
  900. (child-val (cdr child-attr)))
  901. (when (if (not step-val)
  902. t ;; any value
  903. (string= step-val child-val))
  904. (setq step-attr-copy (cdr step-attr-copy))))))
  905. ;; Did we find all?
  906. (unless step-attr-copy
  907. ;;(throw 'path-child child)
  908. (add-to-list 'path-children child)
  909. ))))))
  910. (if path-tail
  911. (org-freemind-get-children path-children path-tail)
  912. path-children)))
  913. (defun org-freemind-get-richcontent-node (node)
  914. (let ((rc-nodes
  915. (org-freemind-get-children node '((richcontent (type . "NODE")) html body))))
  916. (when (> (length rc-nodes) 1)
  917. (lwarn t :warning "Unexpected structure: several <richcontent type=\"NODE\" ...>"))
  918. (car rc-nodes)))
  919. (defun org-freemind-get-richcontent-note (node)
  920. (let ((rc-notes
  921. (org-freemind-get-children node '((richcontent (type . "NOTE")) html body))))
  922. (when (> (length rc-notes) 1)
  923. (lwarn t :warning "Unexpected structure: several <richcontent type=\"NOTE\" ...>"))
  924. (car rc-notes)))
  925. (defun org-freemind-test-get-tree-text ()
  926. (let ((node '(p nil "\n"
  927. (a
  928. ((href . "link"))
  929. "text")
  930. "\n"
  931. (b nil "hej")
  932. "\n")))
  933. (org-freemind-get-tree-text node)))
  934. ;; (org-freemind-test-get-tree-text)
  935. (defun org-freemind-get-tree-text (node)
  936. (when node
  937. (let ((ntxt "")
  938. (link nil)
  939. (lf-after nil))
  940. (dolist (n node)
  941. (case n
  942. ;;(a (setq is-link t) )
  943. ((h1 h2 h3 h4 h5 h6 p)
  944. ;;(setq ntxt (concat "\n" ntxt))
  945. (setq lf-after 2)
  946. )
  947. (br
  948. (setq lf-after 1)
  949. )
  950. (t
  951. (cond
  952. ((stringp n)
  953. (when (string= n "\n") (setq n ""))
  954. (if link
  955. (setq ntxt (concat ntxt
  956. "[[" link "][" n "]]"))
  957. (setq ntxt (concat ntxt n))))
  958. ((and n (listp n))
  959. (if (symbolp (car n))
  960. (setq ntxt (concat ntxt (org-freemind-get-tree-text n)))
  961. ;; This should be the attributes:
  962. (dolist (att-val n)
  963. (let ((att (car att-val))
  964. (val (cdr att-val)))
  965. (when (eq att 'href)
  966. (setq link val)))))
  967. )))))
  968. (if lf-after
  969. (setq ntxt (concat ntxt (make-string lf-after ?\n)))
  970. (setq ntxt (concat ntxt " ")))
  971. ;;(setq ntxt (concat ntxt (format "{%s}" n)))
  972. ntxt)))
  973. (defun org-freemind-get-richcontent-node-text (node)
  974. "Get the node text as from the richcontent node NODE."
  975. (save-match-data
  976. (let* ((rc (org-freemind-get-richcontent-node node))
  977. (txt (org-freemind-get-tree-text rc)))
  978. ;;(when txt (setq txt (replace-regexp-in-string (rx (1+ whitespace)) " " txt)))
  979. txt
  980. )))
  981. (defun org-freemind-get-richcontent-note-text (node)
  982. "Get the node text as from the richcontent note NODE."
  983. (save-match-data
  984. (let* ((rc (org-freemind-get-richcontent-note node))
  985. (txt (when rc (org-freemind-get-tree-text rc))))
  986. ;;(when txt (setq txt (replace-regexp-in-string (rx (1+ whitespace)) " " txt)))
  987. txt
  988. )))
  989. (defun org-freemind-get-icon-names (node)
  990. (let* ((icon-nodes (org-freemind-get-children node '((icon ))))
  991. names)
  992. (dolist (icn icon-nodes)
  993. (setq names (cons (cdr (assq 'builtin (cadr icn))) names)))
  994. ;; (icon (builtin . "full-1"))
  995. names))
  996. (defun org-freemind-node-to-org (node level skip-levels)
  997. (let ((qname (car node))
  998. (attributes (cadr node))
  999. text
  1000. (note (org-freemind-get-richcontent-note-text node))
  1001. (mark "-- This is more about ")
  1002. (icons (org-freemind-get-icon-names node))
  1003. (children (cddr node)))
  1004. (when (< 0 (- level skip-levels))
  1005. (dolist (attrib attributes)
  1006. (case (car attrib)
  1007. ('TEXT (setq text (cdr attrib)))
  1008. ('text (setq text (cdr attrib)))))
  1009. (unless text
  1010. ;; There should be a richcontent node holding the text:
  1011. (setq text (org-freemind-get-richcontent-node-text node)))
  1012. (when icons
  1013. (when (member "full-1" icons) (setq text (concat "[#A] " text)))
  1014. (when (member "full-2" icons) (setq text (concat "[#B] " text)))
  1015. (when (member "full-3" icons) (setq text (concat "[#C] " text)))
  1016. (when (member "full-4" icons) (setq text (concat "[#D] " text)))
  1017. (when (member "full-5" icons) (setq text (concat "[#E] " text)))
  1018. (when (member "full-6" icons) (setq text (concat "[#F] " text)))
  1019. (when (member "full-7" icons) (setq text (concat "[#G] " text)))
  1020. (when (member "button_cancel" icons) (setq text (concat "TODO " text)))
  1021. )
  1022. (if (and note
  1023. (string= mark (substring note 0 (length mark))))
  1024. (progn
  1025. (setq text (replace-regexp-in-string "\n $" "" text))
  1026. (insert text))
  1027. (case qname
  1028. ('node
  1029. (insert (make-string (- level skip-levels) ?*) " " text "\n")
  1030. ))))
  1031. (dolist (child children)
  1032. (unless (or (null child)
  1033. (stringp child))
  1034. (org-freemind-node-to-org child (1+ level) skip-levels)))))
  1035. ;; Fix-me: put back special things, like drawers that are stored in
  1036. ;; the notes. Should maybe all notes contents be put in drawers?
  1037. ;;;###autoload
  1038. (defun org-freemind-to-org-mode (mm-file org-file)
  1039. "Convert FreeMind file MM-FILE to `org-mode' file ORG-FILE."
  1040. (interactive
  1041. (save-match-data
  1042. (let* ((mm-file (buffer-file-name))
  1043. (default-org-file (concat (file-name-nondirectory mm-file) ".org"))
  1044. (org-file (read-file-name "Output org-mode file: " nil nil nil default-org-file)))
  1045. (list mm-file org-file))))
  1046. (when (org-freemind-check-overwrite org-file (called-interactively-p))
  1047. (let ((mm-buffer (find-file-noselect mm-file))
  1048. (org-buffer (find-file-noselect org-file)))
  1049. (with-current-buffer mm-buffer
  1050. (let* ((xml-list (xml-parse-file mm-file))
  1051. (top-node (cadr (cddar xml-list)))
  1052. (note (org-freemind-get-richcontent-note-text top-node))
  1053. (skip-levels
  1054. (if (and note
  1055. (string-match (rx bol "--org-mode: WHOLE FILE" eol) note))
  1056. 1
  1057. 0)))
  1058. (with-current-buffer org-buffer
  1059. (erase-buffer)
  1060. (org-freemind-node-to-org top-node 1 skip-levels)
  1061. (goto-char (point-min))
  1062. (org-set-tags t t) ;; Align all tags
  1063. )
  1064. (switch-to-buffer-other-window org-buffer)
  1065. )))))
  1066. (provide 'org-freemind)
  1067. ;; arch-tag: e7b0d776-94fd-404a-b35e-0f855fae3627
  1068. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1069. ;;; org-freemind.el ends here