org-freemind.el 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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)))
  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. )
  424. ;; There is always an LF char:
  425. (when (> (length text) 1)
  426. (setq node-res (concat
  427. "<node style=\"bubble\" background_color=\"#eeee00\">\n"
  428. "<richcontent TYPE=\"NODE\"><html>\n"
  429. "<head>\n"
  430. (if (= 0 (length org-freemind-node-css-style))
  431. ""
  432. (concat
  433. "<style type=\"text/css\">\n"
  434. "<!--\n"
  435. org-freemind-node-css-style
  436. "-->\n"
  437. "</style>\n"))
  438. "</head>\n"
  439. "<body>\n"))
  440. (let ((begin-html-mark (regexp-quote "#+BEGIN_HTML"))
  441. (end-html-mark (regexp-quote "#+END_HTML"))
  442. head
  443. end-pos
  444. end-pos-match
  445. )
  446. ;; Take care of #+BEGIN_HTML - #+END_HTML
  447. (while (string-match begin-html-mark text)
  448. (setq head (substring text 0 (match-beginning 0)))
  449. (setq end-pos-match (match-end 0))
  450. (setq node-res (concat node-res
  451. (org-freemind-convert-text-p head)))
  452. (setq text (substring text end-pos-match))
  453. (setq end-pos (string-match end-html-mark text))
  454. (if end-pos
  455. (setq end-pos-match (match-end 0))
  456. (message "org-freemind: Missing #+END_HTML")
  457. (setq end-pos (length text))
  458. (setq end-pos-match end-pos))
  459. (setq node-res (concat node-res
  460. (substring text 0 end-pos)))
  461. (setq text (substring text end-pos-match)))
  462. (setq node-res (concat node-res
  463. (org-freemind-convert-text-p text))))
  464. (setq node-res (concat
  465. node-res
  466. "</body>\n"
  467. "</html>\n"
  468. "</richcontent>\n"
  469. ;; Put a note that this is for the parent node
  470. ;; "<richcontent TYPE=\"NOTE\"><html>"
  471. ;; "<head>"
  472. ;; "</head>"
  473. ;; "<body>"
  474. ;; "<p>"
  475. ;; "-- This is more about \"" node-name "\" --"
  476. ;; "</p>"
  477. ;; "</body>"
  478. ;; "</html>"
  479. ;; "</richcontent>\n"
  480. note-res
  481. "</node>\n" ;; ok
  482. )))
  483. (list node-res note-res))))
  484. (defun org-freemind-write-node (mm-buffer drawers-regexp
  485. num-left-nodes base-level
  486. current-level next-level this-m2
  487. this-node-end
  488. this-children-visible
  489. next-node-start
  490. next-has-some-visible-child)
  491. (let* (this-icons
  492. this-bg-color
  493. this-m2-escaped
  494. this-rich-node
  495. this-rich-note
  496. )
  497. (when (string-match "TODO" this-m2)
  498. (setq this-m2 (replace-match "" nil nil this-m2))
  499. (add-to-list 'this-icons "button_cancel")
  500. (setq this-bg-color "#ffff88")
  501. (when (string-match "\\[#\\(.\\)\\]" this-m2)
  502. (let ((prior (string-to-char (match-string 1 this-m2))))
  503. (setq this-m2 (replace-match "" nil nil this-m2))
  504. (cond
  505. ((= prior ?A)
  506. (add-to-list 'this-icons "full-1")
  507. (setq this-bg-color "#ff0000"))
  508. ((= prior ?B)
  509. (add-to-list 'this-icons "full-2")
  510. (setq this-bg-color "#ffaa00"))
  511. ((= prior ?C)
  512. (add-to-list 'this-icons "full-3")
  513. (setq this-bg-color "#ffdd00"))
  514. ((= prior ?D)
  515. (add-to-list 'this-icons "full-4")
  516. (setq this-bg-color "#ffff00"))
  517. ((= prior ?E)
  518. (add-to-list 'this-icons "full-5"))
  519. ((= prior ?F)
  520. (add-to-list 'this-icons "full-6"))
  521. ((= prior ?G)
  522. (add-to-list 'this-icons "full-7"))
  523. ))))
  524. (setq this-m2 (org-trim this-m2))
  525. (setq this-m2-escaped (org-freemind-escape-str-from-org this-m2))
  526. (let ((node-notes (org-freemind-org-text-to-freemind-subnode/note
  527. this-m2-escaped
  528. this-node-end
  529. (1- next-node-start)
  530. drawers-regexp)))
  531. (setq this-rich-node (nth 0 node-notes))
  532. (setq this-rich-note (nth 1 node-notes)))
  533. (with-current-buffer mm-buffer
  534. (insert "<node text=\"" this-m2-escaped "\"")
  535. (org-freemind-get-node-style this-m2)
  536. (when (> next-level current-level)
  537. (unless (or this-children-visible
  538. next-has-some-visible-child)
  539. (insert " folded=\"true\"")))
  540. (when (and (= current-level (1+ base-level))
  541. (> num-left-nodes 0))
  542. (setq num-left-nodes (1- num-left-nodes))
  543. (insert " position=\"left\""))
  544. (when this-bg-color
  545. (insert " background_color=\"" this-bg-color "\""))
  546. (insert ">\n")
  547. (when this-icons
  548. (dolist (icon this-icons)
  549. (insert "<icon builtin=\"" icon "\"/>\n")))
  550. )
  551. (with-current-buffer mm-buffer
  552. ;;(when this-rich-note (insert this-rich-note))
  553. (when this-rich-node (insert this-rich-node))))
  554. num-left-nodes)
  555. (defun org-freemind-check-overwrite (file interactively)
  556. "Check if file FILE already exists.
  557. If FILE does not exists return t.
  558. If INTERACTIVELY is non-nil ask if the file should be replaced
  559. and return t/nil if it should/should not be replaced.
  560. Otherwise give an error say the file exists."
  561. (if (file-exists-p file)
  562. (if interactively
  563. (y-or-n-p (format "File %s exists, replace it? " file))
  564. (error "File %s already exists" file))
  565. t))
  566. (defvar org-freemind-node-pattern
  567. ;;(rx bol
  568. ;; (submatch (1+ "*"))
  569. ;; (1+ space)
  570. ;; (submatch (*? nonl))
  571. ;; eol)
  572. "^\\(\\*+\\)[[:space:]]+\\(.*?\\)$")
  573. (defun org-freemind-look-for-visible-child (node-level)
  574. (save-excursion
  575. (save-match-data
  576. (let ((found-visible-child nil))
  577. (while (and (not found-visible-child)
  578. (re-search-forward org-freemind-node-pattern nil t))
  579. (let* ((m1 (match-string-no-properties 1))
  580. (level (length m1)))
  581. (if (>= node-level level)
  582. (setq found-visible-child 'none)
  583. (unless (get-char-property (line-beginning-position) 'invisible)
  584. (setq found-visible-child 'found)))))
  585. (eq found-visible-child 'found)
  586. ))))
  587. (defun org-freemind-goto-line (line)
  588. "Go to line number LINE."
  589. (save-restriction
  590. (widen)
  591. (goto-char (point-min))
  592. (forward-line (1- line))))
  593. (defun org-freemind-write-mm-buffer (org-buffer mm-buffer node-at-line)
  594. (with-current-buffer org-buffer
  595. (dolist (node-style org-freemind-node-styles)
  596. (when (org-string-match-p (car node-style) buffer-file-name)
  597. (setq org-freemind-node-style (cadr node-style))))
  598. ;;(message "org-freemind-node-style =%s" org-freemind-node-style)
  599. (save-match-data
  600. (let* ((drawers (copy-sequence org-drawers))
  601. drawers-regexp
  602. (num-top1-nodes 0)
  603. (num-top2-nodes 0)
  604. num-left-nodes
  605. (unclosed-nodes 0)
  606. (odd-only org-odd-levels-only)
  607. (first-time t)
  608. (current-level 1)
  609. base-level
  610. prev-node-end
  611. rich-text
  612. unfinished-tag
  613. node-at-line-level
  614. node-at-line-last)
  615. (with-current-buffer mm-buffer
  616. (erase-buffer)
  617. (setq buffer-file-coding-system 'utf-8)
  618. ;; Fix-me: Currently Freemind (ver 0.9.0 RC9) does not support this:
  619. ;;(insert "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
  620. (insert "<map version=\"0.9.0\">\n")
  621. (insert "<!-- To view this file, download free mind mapping software FreeMind from http://freemind.sourceforge.net -->\n"))
  622. (save-excursion
  623. ;; Get special buffer vars:
  624. (goto-char (point-min))
  625. (message "Writing Freemind file...")
  626. (while (re-search-forward "^#\\+DRAWERS:" nil t)
  627. (let ((dr-txt (buffer-substring-no-properties (match-end 0) (line-end-position))))
  628. (setq drawers (append drawers (split-string dr-txt) nil))))
  629. (setq drawers-regexp
  630. (concat "^[[:blank:]]*:"
  631. (regexp-opt drawers)
  632. ;;(rx ":" (0+ blank)
  633. ;; "\n"
  634. ;; (*? anything)
  635. ;; "\n"
  636. ;; (0+ blank)
  637. ;; ":END:"
  638. ;; (0+ blank)
  639. ;; eol)
  640. ":[[:blank:]]*\n\\(?:.\\|\n\\)*?\n[[:blank:]]*:END:[[:blank:]]*$"
  641. ))
  642. (if node-at-line
  643. ;; Get number of top nodes and last line for this node
  644. (progn
  645. (org-freemind-goto-line node-at-line)
  646. (unless (looking-at org-freemind-node-pattern)
  647. (error "No node at line %s" node-at-line))
  648. (setq node-at-line-level (length (match-string-no-properties 1)))
  649. (forward-line)
  650. (setq node-at-line-last
  651. (catch 'last-line
  652. (while (re-search-forward org-freemind-node-pattern nil t)
  653. (let* ((m1 (match-string-no-properties 1))
  654. (level (length m1)))
  655. (if (<= level node-at-line-level)
  656. (progn
  657. (beginning-of-line)
  658. (throw 'last-line (1- (point))))
  659. (if (= level (1+ node-at-line-level))
  660. (setq num-top2-nodes (1+ num-top2-nodes))))))))
  661. (setq current-level node-at-line-level)
  662. (setq num-top1-nodes 1)
  663. (org-freemind-goto-line node-at-line))
  664. ;; First get number of top nodes
  665. (goto-char (point-min))
  666. (while (re-search-forward org-freemind-node-pattern nil t)
  667. (let* ((m1 (match-string-no-properties 1))
  668. (level (length m1)))
  669. (if (= level 1)
  670. (setq num-top1-nodes (1+ num-top1-nodes))
  671. (if (= level 2)
  672. (setq num-top2-nodes (1+ num-top2-nodes))))))
  673. ;; If there is more than one top node we need to insert a node
  674. ;; to keep them together.
  675. (goto-char (point-min))
  676. (when (> num-top1-nodes 1)
  677. (setq num-top2-nodes num-top1-nodes)
  678. (setq current-level 0)
  679. (let ((orig-name (if buffer-file-name
  680. (file-name-nondirectory (buffer-file-name))
  681. (buffer-name))))
  682. (with-current-buffer mm-buffer
  683. (insert "<node text=\"" orig-name "\" background_color=\"#00bfff\">\n"
  684. ;; Put a note that this is for the parent node
  685. "<richcontent TYPE=\"NOTE\"><html>"
  686. "<head>"
  687. "</head>"
  688. "<body>"
  689. "<p>"
  690. org-freemind-org-nfix "WHOLE FILE"
  691. "</p>"
  692. "</body>"
  693. "</html>"
  694. "</richcontent>\n")))))
  695. (setq num-left-nodes (floor num-top2-nodes 2))
  696. (setq base-level current-level)
  697. (let (this-m2
  698. this-node-end
  699. this-children-visible
  700. next-m2
  701. next-node-start
  702. next-level
  703. next-has-some-visible-child
  704. next-children-visible
  705. )
  706. (while (and
  707. (re-search-forward org-freemind-node-pattern nil t)
  708. (if node-at-line-last (<= (point) node-at-line-last) t)
  709. )
  710. (let* ((next-m1 (match-string-no-properties 1))
  711. (next-node-end (match-end 0))
  712. )
  713. (setq next-node-start (match-beginning 0))
  714. (setq next-m2 (match-string-no-properties 2))
  715. (setq next-level (length next-m1))
  716. (setq next-children-visible
  717. (not (eq 'outline
  718. (get-char-property (line-end-position) 'invisible))))
  719. (setq next-has-some-visible-child
  720. (if next-children-visible t
  721. (org-freemind-look-for-visible-child next-level)))
  722. (when this-m2
  723. (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)))
  724. (when (if (= num-top1-nodes 1) (> current-level base-level) t)
  725. (while (>= current-level next-level)
  726. (with-current-buffer mm-buffer
  727. (insert "</node>\n")
  728. (setq current-level
  729. (- current-level (if odd-only 2 1))))))
  730. (setq this-node-end (1+ next-node-end))
  731. (setq this-m2 next-m2)
  732. (setq current-level next-level)
  733. (setq this-children-visible next-children-visible)
  734. (forward-char)
  735. ))
  736. ;;; (unless (if node-at-line-last
  737. ;;; (>= (point) node-at-line-last)
  738. ;;; nil)
  739. ;; Write last node:
  740. (setq this-m2 next-m2)
  741. (setq current-level next-level)
  742. (setq next-node-start (if node-at-line-last
  743. (1+ node-at-line-last)
  744. (point-max)))
  745. (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))
  746. (with-current-buffer mm-buffer (insert "</node>\n"))
  747. ;)
  748. )
  749. (with-current-buffer mm-buffer
  750. (while (> current-level base-level)
  751. (insert "</node>\n")
  752. (setq current-level
  753. (- current-level (if odd-only 2 1)))
  754. ))
  755. (with-current-buffer mm-buffer
  756. (insert "</map>")
  757. (delete-trailing-whitespace)
  758. (goto-char (point-min))
  759. ))))))
  760. (defun org-freemind-get-node-style (node-name)
  761. "NOT READY YET."
  762. ;;<node BACKGROUND_COLOR="#eeee00" CREATED="1234668815593" MODIFIED="1234668815593" STYLE="bubble">
  763. ;;<font BOLD="true" NAME="SansSerif" SIZE="12"/>
  764. (let (node-styles
  765. node-style)
  766. (dolist (style-list org-freemind-node-style)
  767. (let ((node-regexp (car style-list)))
  768. (message "node-regexp=%s node-name=%s" node-regexp node-name)
  769. (when (org-string-match-p node-regexp node-name)
  770. ;;(setq node-style (org-freemind-do-apply-node-style style-list))
  771. (setq node-style (cadr style-list))
  772. (when node-style
  773. (message "node-style=%s" node-style)
  774. (setq node-styles (append node-styles node-style)))
  775. )))))
  776. (defun org-freemind-do-apply-node-style (style-list)
  777. (message "style-list=%S" style-list)
  778. (let ((node-style 'fork)
  779. (color "red")
  780. (background-color "yellow")
  781. (edge-color "green")
  782. (edge-style 'bezier)
  783. (edge-width 'thin)
  784. (italic t)
  785. (bold t)
  786. (font-name "SansSerif")
  787. (font-size 12))
  788. (dolist (style (cadr style-list))
  789. (message " style=%s" style)
  790. (let ((what (car style)))
  791. (cond
  792. ((eq what 'node-style)
  793. (setq node-style (cadr style)))
  794. ((eq what 'color)
  795. (setq color (cadr style)))
  796. ((eq what 'background-color)
  797. (setq background-color (cadr style)))
  798. ((eq what 'edge-color)
  799. (setq edge-color (cadr style)))
  800. ((eq what 'edge-style)
  801. (setq edge-style (cadr style)))
  802. ((eq what 'edge-width)
  803. (setq edge-width (cadr style)))
  804. ((eq what 'italic)
  805. (setq italic (cadr style)))
  806. ((eq what 'bold)
  807. (setq bold (cadr style)))
  808. ((eq what 'font-name)
  809. (setq font-name (cadr style)))
  810. ((eq what 'font-size)
  811. (setq font-size (cadr style)))
  812. )
  813. (insert (format " style=\"%s\"" node-style))
  814. (insert (format " color=\"%s\"" color))
  815. (insert (format " background_color=\"%s\"" background-color))
  816. (insert ">\n")
  817. (insert "<edge")
  818. (insert (format " color=\"%s\"" edge-color))
  819. (insert (format " style=\"%s\"" edge-style))
  820. (insert (format " width=\"%s\"" edge-width))
  821. (insert "/>\n")
  822. (insert "<font")
  823. (insert (format " italic=\"%s\"" italic))
  824. (insert (format " bold=\"%s\"" bold))
  825. (insert (format " name=\"%s\"" font-name))
  826. (insert (format " size=\"%s\"" font-size))
  827. ))))
  828. ;;;###autoload
  829. (defun org-freemind-from-org-mode-node (node-line mm-file)
  830. "Convert node at line NODE-LINE to the FreeMind file MM-FILE.
  831. See `org-freemind-from-org-mode' for more information."
  832. (interactive
  833. (progn
  834. (unless (org-back-to-heading nil)
  835. (error "Can't find org-mode node start"))
  836. (let* ((line (line-number-at-pos))
  837. (default-mm-file (concat (if buffer-file-name
  838. (file-name-nondirectory buffer-file-name)
  839. "nofile")
  840. "-line-" (number-to-string line)
  841. ".mm"))
  842. (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file)))
  843. (list line mm-file))))
  844. (when (org-freemind-check-overwrite mm-file (org-called-interactively-p 'any))
  845. (let ((org-buffer (current-buffer))
  846. (mm-buffer (find-file-noselect mm-file)))
  847. (org-freemind-write-mm-buffer org-buffer mm-buffer node-line)
  848. (with-current-buffer mm-buffer
  849. (basic-save-buffer)
  850. (when (org-called-interactively-p 'any)
  851. (switch-to-buffer-other-window mm-buffer)
  852. (when (y-or-n-p "Show in FreeMind? ")
  853. (org-freemind-show buffer-file-name)))))))
  854. ;;;###autoload
  855. (defun org-freemind-from-org-mode (org-file mm-file)
  856. "Convert the `org-mode' file ORG-FILE to the FreeMind file MM-FILE.
  857. All the nodes will be opened or closed in Freemind just as you
  858. have them in `org-mode'.
  859. Note that exporting to Freemind also gives you an alternative way
  860. to export from `org-mode' to html. You can create a dynamic html
  861. version of the your org file, by first exporting to Freemind and
  862. then exporting from Freemind to html. The 'As
  863. XHTML (JavaScript)' version in Freemind works very well \(and you
  864. can use a CSS stylesheet to style it)."
  865. ;; Fix-me: better doc, include recommendations etc.
  866. (interactive
  867. (let* ((org-file buffer-file-name)
  868. (default-mm-file (concat
  869. (if org-file
  870. (file-name-nondirectory org-file)
  871. "nofile")
  872. ".mm"))
  873. (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file)))
  874. (list org-file mm-file)))
  875. (when (org-freemind-check-overwrite mm-file (org-called-interactively-p 'any))
  876. (let ((org-buffer (if org-file (find-file-noselect org-file) (current-buffer)))
  877. (mm-buffer (find-file-noselect mm-file)))
  878. (org-freemind-write-mm-buffer org-buffer mm-buffer nil)
  879. (with-current-buffer mm-buffer
  880. (basic-save-buffer)
  881. (when (org-called-interactively-p 'any)
  882. (switch-to-buffer-other-window mm-buffer)
  883. (when (y-or-n-p "Show in FreeMind? ")
  884. (org-freemind-show buffer-file-name)))))))
  885. ;;;###autoload
  886. (defun org-freemind-from-org-sparse-tree (org-buffer mm-file)
  887. "Convert visible part of buffer ORG-BUFFER to FreeMind file MM-FILE."
  888. (interactive
  889. (let* ((org-file buffer-file-name)
  890. (default-mm-file (concat
  891. (if org-file
  892. (file-name-nondirectory org-file)
  893. "nofile")
  894. "-sparse.mm"))
  895. (mm-file (read-file-name "Output FreeMind file: " nil nil nil default-mm-file)))
  896. (list (current-buffer) mm-file)))
  897. (when (org-freemind-check-overwrite mm-file (org-called-interactively-p 'any))
  898. (let (org-buffer
  899. (mm-buffer (find-file-noselect mm-file)))
  900. (save-window-excursion
  901. (org-export-visible ?\ nil)
  902. (setq org-buffer (current-buffer)))
  903. (org-freemind-write-mm-buffer org-buffer mm-buffer nil)
  904. (with-current-buffer mm-buffer
  905. (basic-save-buffer)
  906. (when (org-called-interactively-p 'any)
  907. (switch-to-buffer-other-window mm-buffer)
  908. (when (y-or-n-p "Show in FreeMind? ")
  909. (org-freemind-show buffer-file-name)))))))
  910. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  911. ;;; FreeMind => Org
  912. ;; (sort '(b a c) 'org-freemind-lt-symbols)
  913. (defun org-freemind-lt-symbols (sym-a sym-b)
  914. (string< (symbol-name sym-a) (symbol-name sym-b)))
  915. ;; (sort '((b . 1) (a . 2) (c . 3)) 'org-freemind-lt-xml-attrs)
  916. (defun org-freemind-lt-xml-attrs (attr-a attr-b)
  917. (string< (symbol-name (car attr-a)) (symbol-name (car attr-b))))
  918. ;; xml-parse-region gives things like
  919. ;; ((p nil "\n"
  920. ;; (a
  921. ;; ((href . "link"))
  922. ;; "text")
  923. ;; "\n"
  924. ;; (b nil "hej")
  925. ;; "\n"))
  926. ;; '(a . nil)
  927. ;; (org-freemind-symbols= 'a (car '(A B)))
  928. (defsubst org-freemind-symbols= (sym-a sym-b)
  929. "Return t if downcased names of SYM-A and SYM-B are equal.
  930. SYM-A and SYM-B should be symbols."
  931. (or (eq sym-a sym-b)
  932. (string= (downcase (symbol-name sym-a))
  933. (downcase (symbol-name sym-b)))))
  934. (defun org-freemind-get-children (parent path)
  935. "Find children node to PARENT from PATH.
  936. PATH should be a list of steps, where each step has the form
  937. '(NODE-NAME (ATTR-NAME . ATTR-VALUE))"
  938. ;; Fix-me: maybe implement op? step: Name, number, attr, attr op val
  939. ;; Fix-me: case insensitive version for children?
  940. (let* ((children (if (not (listp (car parent)))
  941. (cddr parent)
  942. (let (cs)
  943. (dolist (p parent)
  944. (dolist (c (cddr p))
  945. (add-to-list 'cs c)))
  946. cs)
  947. ))
  948. (step (car path))
  949. (step-node (if (listp step) (car step) step))
  950. (step-attr-list (when (listp step) (sort (cdr step) 'org-freemind-lt-xml-attrs)))
  951. (path-tail (cdr path))
  952. path-children)
  953. (dolist (child children)
  954. ;; skip xml.el formatting nodes
  955. (unless (stringp child)
  956. ;; compare node name
  957. (when (if (not step-node)
  958. t ;; any node name
  959. (org-freemind-symbols= step-node (car child)))
  960. (if (not step-attr-list)
  961. ;;(throw 'path-child child) ;; no attr to care about
  962. (add-to-list 'path-children child)
  963. (let* ((child-attr-list (cadr child))
  964. (step-attr-copy (copy-sequence step-attr-list)))
  965. (dolist (child-attr child-attr-list)
  966. ;; Compare attr names:
  967. (when (org-freemind-symbols= (caar step-attr-copy) (car child-attr))
  968. ;; Compare values:
  969. (let ((step-val (cdar step-attr-copy))
  970. (child-val (cdr child-attr)))
  971. (when (if (not step-val)
  972. t ;; any value
  973. (string= step-val child-val))
  974. (setq step-attr-copy (cdr step-attr-copy))))))
  975. ;; Did we find all?
  976. (unless step-attr-copy
  977. ;;(throw 'path-child child)
  978. (add-to-list 'path-children child)
  979. ))))))
  980. (if path-tail
  981. (org-freemind-get-children path-children path-tail)
  982. path-children)))
  983. (defun org-freemind-get-richcontent-node (node)
  984. (let ((rc-nodes
  985. (org-freemind-get-children node '((richcontent (type . "NODE")) html body))))
  986. (when (> (length rc-nodes) 1)
  987. (lwarn t :warning "Unexpected structure: several <richcontent type=\"NODE\" ...>"))
  988. (car rc-nodes)))
  989. (defun org-freemind-get-richcontent-note (node)
  990. (let ((rc-notes
  991. (org-freemind-get-children node '((richcontent (type . "NOTE")) html body))))
  992. (when (> (length rc-notes) 1)
  993. (lwarn t :warning "Unexpected structure: several <richcontent type=\"NOTE\" ...>"))
  994. (car rc-notes)))
  995. (defun org-freemind-test-get-tree-text ()
  996. (let ((node '(p nil "\n"
  997. (a
  998. ((href . "link"))
  999. "text")
  1000. "\n"
  1001. (b nil "hej")
  1002. "\n")))
  1003. (org-freemind-get-tree-text node)))
  1004. ;; (org-freemind-test-get-tree-text)
  1005. (defun org-freemind-get-tree-text (node)
  1006. (when node
  1007. (let ((ntxt "")
  1008. (link nil)
  1009. (lf-after nil))
  1010. (dolist (n node)
  1011. (case n
  1012. ;;(a (setq is-link t) )
  1013. ((h1 h2 h3 h4 h5 h6 p)
  1014. ;;(setq ntxt (concat "\n" ntxt))
  1015. (setq lf-after 2)
  1016. )
  1017. (br
  1018. (setq lf-after 1)
  1019. )
  1020. (t
  1021. (cond
  1022. ((stringp n)
  1023. (when (string= n "\n") (setq n ""))
  1024. (if link
  1025. (setq ntxt (concat ntxt
  1026. "[[" link "][" n "]]"))
  1027. (setq ntxt (concat ntxt n))))
  1028. ((and n (listp n))
  1029. (if (symbolp (car n))
  1030. (setq ntxt (concat ntxt (org-freemind-get-tree-text n)))
  1031. ;; This should be the attributes:
  1032. (dolist (att-val n)
  1033. (let ((att (car att-val))
  1034. (val (cdr att-val)))
  1035. (when (eq att 'href)
  1036. (setq link val)))))
  1037. )))))
  1038. (if lf-after
  1039. (setq ntxt (concat ntxt (make-string lf-after ?\n)))
  1040. (setq ntxt (concat ntxt " ")))
  1041. ;;(setq ntxt (concat ntxt (format "{%s}" n)))
  1042. ntxt)))
  1043. (defun org-freemind-get-richcontent-node-text (node)
  1044. "Get the node text as from the richcontent node NODE."
  1045. (save-match-data
  1046. (let* ((rc (org-freemind-get-richcontent-node node))
  1047. (txt (org-freemind-get-tree-text rc)))
  1048. ;;(when txt (setq txt (replace-regexp-in-string "[[:space:]]+" " " txt)))
  1049. txt
  1050. )))
  1051. (defun org-freemind-get-richcontent-note-text (node)
  1052. "Get the node text as from the richcontent note NODE."
  1053. (save-match-data
  1054. (let* ((rc (org-freemind-get-richcontent-note node))
  1055. (txt (when rc (org-freemind-get-tree-text rc))))
  1056. ;;(when txt (setq txt (replace-regexp-in-string "[[:space:]]+" " " txt)))
  1057. txt
  1058. )))
  1059. (defun org-freemind-get-icon-names (node)
  1060. (let* ((icon-nodes (org-freemind-get-children node '((icon ))))
  1061. names)
  1062. (dolist (icn icon-nodes)
  1063. (setq names (cons (cdr (assq 'builtin (cadr icn))) names)))
  1064. ;; (icon (builtin . "full-1"))
  1065. names))
  1066. (defun org-freemind-node-to-org (node level skip-levels)
  1067. (let ((qname (car node))
  1068. (attributes (cadr node))
  1069. text
  1070. ;; Fix-me: note is never inserted
  1071. (note (org-freemind-get-richcontent-note-text node))
  1072. (mark "-- This is more about ")
  1073. (icons (org-freemind-get-icon-names node))
  1074. (children (cddr node)))
  1075. (when (< 0 (- level skip-levels))
  1076. (dolist (attrib attributes)
  1077. (case (car attrib)
  1078. ('TEXT (setq text (cdr attrib)))
  1079. ('text (setq text (cdr attrib)))))
  1080. (unless text
  1081. ;; There should be a richcontent node holding the text:
  1082. (setq text (org-freemind-get-richcontent-node-text node)))
  1083. (when icons
  1084. (when (member "full-1" icons) (setq text (concat "[#A] " text)))
  1085. (when (member "full-2" icons) (setq text (concat "[#B] " text)))
  1086. (when (member "full-3" icons) (setq text (concat "[#C] " text)))
  1087. (when (member "full-4" icons) (setq text (concat "[#D] " text)))
  1088. (when (member "full-5" icons) (setq text (concat "[#E] " text)))
  1089. (when (member "full-6" icons) (setq text (concat "[#F] " text)))
  1090. (when (member "full-7" icons) (setq text (concat "[#G] " text)))
  1091. (when (member "button_cancel" icons) (setq text (concat "TODO " text)))
  1092. )
  1093. (if (and note
  1094. (string= mark (substring note 0 (length mark))))
  1095. (progn
  1096. (setq text (replace-regexp-in-string "\n $" "" text))
  1097. (insert text))
  1098. (case qname
  1099. ('node
  1100. (insert (make-string (- level skip-levels) ?*) " " text "\n")
  1101. (when note
  1102. (insert ":COMMENT:\n" note "\n:END:\n"))
  1103. ))))
  1104. (dolist (child children)
  1105. (unless (or (null child)
  1106. (stringp child))
  1107. (org-freemind-node-to-org child (1+ level) skip-levels)))))
  1108. ;; Fix-me: put back special things, like drawers that are stored in
  1109. ;; the notes. Should maybe all notes contents be put in drawers?
  1110. ;;;###autoload
  1111. (defun org-freemind-to-org-mode (mm-file org-file)
  1112. "Convert FreeMind file MM-FILE to `org-mode' file ORG-FILE."
  1113. (interactive
  1114. (save-match-data
  1115. (let* ((mm-file (buffer-file-name))
  1116. (default-org-file (concat (file-name-nondirectory mm-file) ".org"))
  1117. (org-file (read-file-name "Output org-mode file: " nil nil nil default-org-file)))
  1118. (list mm-file org-file))))
  1119. (when (org-freemind-check-overwrite org-file (org-called-interactively-p 'any))
  1120. (let ((mm-buffer (find-file-noselect mm-file))
  1121. (org-buffer (find-file-noselect org-file)))
  1122. (with-current-buffer mm-buffer
  1123. (let* ((xml-list (xml-parse-file mm-file))
  1124. (top-node (cadr (cddar xml-list)))
  1125. (note (org-freemind-get-richcontent-note-text top-node))
  1126. (skip-levels
  1127. (if (and note
  1128. (string-match "^--org-mode: WHOLE FILE$" note))
  1129. 1
  1130. 0)))
  1131. (with-current-buffer org-buffer
  1132. (erase-buffer)
  1133. (org-freemind-node-to-org top-node 1 skip-levels)
  1134. (goto-char (point-min))
  1135. (org-set-tags t t) ;; Align all tags
  1136. )
  1137. (switch-to-buffer-other-window org-buffer)
  1138. )))))
  1139. (provide 'org-freemind)
  1140. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1141. ;;; org-freemind.el ends here