org-freemind.el 48 KB

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