org-freemind.el 48 KB

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