org-freemind.el 50 KB

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