org-freemind.el 45 KB

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