org-freemind.el 43 KB

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