PageRenderTime 27ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/lisp/org-wikinodes.el

https://github.com/alexko/org-mode
Emacs Lisp | 337 lines | 252 code | 44 blank | 41 comment | 8 complexity | 7e091fe10f53a6f246fcbfab19ae782f MD5 | raw file
  1. ;;; org-wikinodes.el --- Wiki-like CamelCase links to outline nodes
  2. ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  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. (require 'org)
  22. (eval-when-compile
  23. (require 'cl))
  24. (defgroup org-wikinodes nil
  25. "Wiki-like CamelCase links words to outline nodes in Org mode."
  26. :tag "Org WikiNodes"
  27. :group 'org)
  28. (defconst org-wikinodes-camel-regexp "\\<[A-Z]+[a-z]+[A-Z]+[a-z]+[a-zA-Z]*\\>"
  29. "Regular expression matching CamelCase words.")
  30. (defcustom org-wikinodes-active t
  31. "Should CamelCase links be active in the current file?"
  32. :group 'org-wikinodes
  33. :type 'boolean)
  34. (put 'org-wikinodes-active 'safe-local-variable 'booleanp)
  35. (defcustom org-wikinodes-scope 'file
  36. "The scope of searches for wiki targets.
  37. Allowed values are:
  38. file Search for targets in the current file only
  39. directory Search for targets in all org files in the current directory"
  40. :group 'org-wikinodes
  41. :type '(choice
  42. (const :tag "Find targets in current file" file)
  43. (const :tag "Find targets in current directory" directory)))
  44. (defcustom org-wikinodes-create-targets 'query
  45. "Non-nil means create Wiki target when following a wiki link fails.
  46. Allowed values are:
  47. nil never create node, just throw an error if the target does not exist
  48. query ask the user what to do
  49. t create the node in the current buffer
  50. \"file.org\" create the node in the file \"file.org\", in the same directory
  51. If you are using wiki links across files, you need to set `org-wikinodes-scope'
  52. to `directory'."
  53. :group 'org-wikinodes
  54. :type '(choice
  55. (const :tag "Never automatically create node" nil)
  56. (const :tag "In current file" t)
  57. (file :tag "In one special file\n")
  58. (const :tag "Query the user" query)))
  59. ;;; Link activation
  60. (defun org-wikinodes-activate-links (limit)
  61. "Activate CamelCase words as links to Wiki targets."
  62. (when org-wikinodes-active
  63. (let (case-fold-search)
  64. (if (re-search-forward org-wikinodes-camel-regexp limit t)
  65. (if (equal (char-after (point-at-bol)) ?*)
  66. (progn
  67. ;; in heading - deactivate flyspell
  68. (org-remove-flyspell-overlays-in (match-beginning 0)
  69. (match-end 0))
  70. (add-text-properties (match-beginning 0) (match-end 0)
  71. '(org-no-flyspell t))
  72. t)
  73. ;; this is a wiki link
  74. (org-remove-flyspell-overlays-in (match-beginning 0)
  75. (match-end 0))
  76. (add-text-properties (match-beginning 0) (match-end 0)
  77. (list 'mouse-face 'highlight
  78. 'face 'org-link
  79. 'keymap org-mouse-map
  80. 'help-echo "Wiki Link"))
  81. t)))))
  82. ;;; Following links and creating non-existing target nodes
  83. (defun org-wikinodes-open-at-point ()
  84. "Check if the cursor is on a Wiki link and follow the link.
  85. This function goes into `org-open-at-point-functions'."
  86. (and org-wikinodes-active
  87. (not (org-on-heading-p))
  88. (let (case-fold-search) (org-in-regexp org-wikinodes-camel-regexp))
  89. (progn (org-wikinodes-follow-link (match-string 0)) t)))
  90. (defun org-wikinodes-follow-link (target)
  91. "Follow a wiki link to TARGET.
  92. This need to be found as an exact headline match, either in the current
  93. buffer, or in any .org file in the current directory, depending on the
  94. variable `org-wikinodes-scope'.
  95. If a target headline is not found, it may be created according to the
  96. setting of `org-wikinodes-create-targets'."
  97. (if current-prefix-arg (org-wikinodes-clear-direcory-targets-cache))
  98. (let ((create org-wikinodes-create-targets)
  99. visiting buffer m pos file rpl)
  100. (setq pos
  101. (or (org-find-exact-headline-in-buffer target (current-buffer))
  102. (and (eq org-wikinodes-scope 'directory)
  103. (setq file (org-wikinodes-which-file target))
  104. (org-find-exact-headline-in-buffer
  105. target (or (get-file-buffer file)
  106. (find-file-noselect file))))))
  107. (if pos
  108. (progn
  109. (org-mark-ring-push (point))
  110. (org-goto-marker-or-bmk pos)
  111. (move-marker pos nil))
  112. (when (eq create 'query)
  113. (if (eq org-wikinodes-scope 'directory)
  114. (progn
  115. (message "Node \"%s\" does not exist. Should it be created?
  116. \[RET] in this buffer [TAB] in another file [q]uit" target)
  117. (setq rpl (read-char-exclusive))
  118. (cond
  119. ((member rpl '(?\C-g ?q)) (error "Abort"))
  120. ((equal rpl ?\C-m) (setq create t))
  121. ((equal rpl ?\C-i)
  122. (setq create (file-name-nondirectory
  123. (read-file-name "Create in file: "))))
  124. (t (error "Invalid selection"))))
  125. (if (y-or-n-p (format "Create new node \"%s\" in current buffer? "
  126. target))
  127. (setq create t)
  128. (error "Abort"))))
  129. (cond
  130. ((not create)
  131. ;; We are not allowed to create the new node
  132. (error "No match for link to \"%s\"" target))
  133. ((stringp create)
  134. ;; Make new node in another file
  135. (org-mark-ring-push (point))
  136. (org-pop-to-buffer-same-window (find-file-noselect create))
  137. (goto-char (point-max))
  138. (or (bolp) (newline))
  139. (insert "\n* " target "\n")
  140. (backward-char 1)
  141. (org-wikinodes-add-target-to-cache target)
  142. (message "New Wiki target `%s' created in file \"%s\""
  143. target create))
  144. (t
  145. ;; Make new node in current buffer
  146. (org-mark-ring-push (point))
  147. (goto-char (point-max))
  148. (or (bolp) (newline))
  149. (insert "* " target "\n")
  150. (backward-char 1)
  151. (org-wikinodes-add-target-to-cache target)
  152. (message "New Wiki target `%s' created in current buffer"
  153. target))))))
  154. ;;; The target cache
  155. (defvar org-wikinodes-directory-targets-cache nil)
  156. (defun org-wikinodes-clear-cache-when-on-target ()
  157. "When on a headline that is a Wiki target, clear the cache."
  158. (when (and (org-on-heading-p)
  159. (org-in-regexp (format org-complex-heading-regexp-format
  160. org-wikinodes-camel-regexp))
  161. (org-in-regexp org-wikinodes-camel-regexp))
  162. (org-wikinodes-clear-direcory-targets-cache)
  163. t))
  164. (defun org-wikinodes-clear-direcory-targets-cache ()
  165. "Clear the cache where to find wiki targets."
  166. (interactive)
  167. (setq org-wikinodes-directory-targets-cache nil)
  168. (message "Wiki target cache cleared, so that it will update when used again"))
  169. (defun org-wikinodes-get-targets ()
  170. "Return a list of all wiki targets in the current buffer."
  171. (let ((re (format org-complex-heading-regexp-format
  172. org-wikinodes-camel-regexp))
  173. (case-fold-search nil)
  174. targets)
  175. (save-excursion
  176. (save-restriction
  177. (widen)
  178. (goto-char (point-min))
  179. (while (re-search-forward re nil t)
  180. (push (org-match-string-no-properties 4) targets))))
  181. (nreverse targets)))
  182. (defun org-wikinodes-get-links-for-directory (dir)
  183. "Return an alist that connects wiki links to files in directory DIR."
  184. (let ((files (directory-files dir nil "\\`[^.#].*\\.org\\'"))
  185. (org-inhibit-startup t)
  186. target-file-alist file visiting m buffer)
  187. (while (setq file (pop files))
  188. (setq visiting (org-find-base-buffer-visiting file))
  189. (setq buffer (or visiting (find-file-noselect file)))
  190. (with-current-buffer buffer
  191. (mapc
  192. (lambda (target)
  193. (setq target-file-alist (cons (cons target file) target-file-alist)))
  194. (org-wikinodes-get-targets)))
  195. (or visiting (kill-buffer buffer)))
  196. target-file-alist))
  197. (defun org-wikinodes-add-target-to-cache (target &optional file)
  198. (setq file (or file buffer-file-name (error "No file for new wiki target")))
  199. (set-text-properties 0 (length target) nil target)
  200. (let ((dir (file-name-directory (expand-file-name file)))
  201. a)
  202. (setq a (assoc dir org-wikinodes-directory-targets-cache))
  203. (if a
  204. ;; Push the new target onto the existing list
  205. (push (cons target (expand-file-name file)) (cdr a))
  206. ;; Call org-wikinodes-which-file so that the cache will be filled
  207. (org-wikinodes-which-file target dir))))
  208. (defun org-wikinodes-which-file (target &optional directory)
  209. "Return the file for wiki headline TARGET DIRECTORY.
  210. If there is no such wiki target, return nil."
  211. (setq directory (expand-file-name (or directory default-directory)))
  212. (unless (assoc directory org-wikinodes-directory-targets-cache)
  213. (push (cons directory (org-wikinodes-get-links-for-directory directory))
  214. org-wikinodes-directory-targets-cache))
  215. (cdr (assoc target (cdr (assoc directory
  216. org-wikinodes-directory-targets-cache)))))
  217. ;;; Exporting Wiki links
  218. (defvar target)
  219. (defvar target-alist)
  220. (defvar last-section-target)
  221. (defvar org-export-target-aliases)
  222. (defun org-wikinodes-set-wiki-targets-during-export ()
  223. (let ((line (buffer-substring (point-at-bol) (point-at-eol)))
  224. (case-fold-search nil)
  225. wtarget a)
  226. (when (string-match (format org-complex-heading-regexp-format
  227. org-wikinodes-camel-regexp)
  228. line)
  229. (setq wtarget (match-string 4 line))
  230. (push (cons wtarget target) target-alist)
  231. (setq a (or (assoc last-section-target org-export-target-aliases)
  232. (progn
  233. (push (list last-section-target)
  234. org-export-target-aliases)
  235. (car org-export-target-aliases))))
  236. (push (caar target-alist) (cdr a)))))
  237. (defvar org-current-export-file)
  238. (defun org-wikinodes-process-links-for-export ()
  239. "Process Wiki links in the export preprocess buffer.
  240. Try to find target matches in the wiki scope and replace CamelCase words
  241. with working links."
  242. (let ((re org-wikinodes-camel-regexp)
  243. (case-fold-search nil)
  244. link file)
  245. (goto-char (point-min))
  246. (while (re-search-forward re nil t)
  247. (org-if-unprotected-at (match-beginning 0)
  248. (unless (save-match-data
  249. (or (org-on-heading-p)
  250. (org-in-regexp org-bracket-link-regexp)
  251. (org-in-regexp org-plain-link-re)
  252. (org-in-regexp "<<[^<>]+>>")))
  253. (setq link (match-string 0))
  254. (delete-region (match-beginning 0) (match-end 0))
  255. (save-match-data
  256. (cond
  257. ((org-find-exact-headline-in-buffer link (current-buffer))
  258. ;; Found in current buffer
  259. (insert (format "[[#%s][%s]]" link link)))
  260. ((eq org-wikinodes-scope 'file)
  261. ;; No match in file, and other files are not allowed
  262. (insert (format "%s" link)))
  263. ((setq file
  264. (and (org-string-nw-p org-current-export-file)
  265. (org-wikinodes-which-file
  266. link (file-name-directory org-current-export-file))))
  267. ;; Match in another file in the current directory
  268. (insert (format "[[file:%s::%s][%s]]" file link link)))
  269. (t ;; No match for this link
  270. (insert (format "%s" link))))))))))
  271. ;;; Hook the WikiNode mechanism into Org
  272. ;; `C-c C-o' should follow wiki links
  273. (add-hook 'org-open-at-point-functions 'org-wikinodes-open-at-point)
  274. ;; `C-c C-c' should clear the cache
  275. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-wikinodes-clear-cache-when-on-target)
  276. ;; Make Wiki haeding create additional link names for headlines
  277. (add-hook 'org-export-define-heading-targets-headline-hook
  278. 'org-wikinodes-set-wiki-targets-during-export)
  279. ;; Turn Wiki links into links the exporter will treat correctly
  280. (add-hook 'org-export-preprocess-after-radio-targets-hook
  281. 'org-wikinodes-process-links-for-export)
  282. ;; Activate CamelCase words as part of Org mode font lock
  283. (defun org-wikinodes-add-to-font-lock-keywords ()
  284. "Add wikinode CamelCase highlighting to `org-font-lock-extra-keywords'."
  285. (let ((m (member '(org-activate-plain-links) org-font-lock-extra-keywords)))
  286. (if m
  287. (setcdr m (cons '(org-wikinodes-activate-links) (cdr m)))
  288. (message
  289. "Failed to add wikinodes to `org-font-lock-extra-keywords'."))))
  290. (add-hook 'org-font-lock-set-keywords-hook
  291. 'org-wikinodes-add-to-font-lock-keywords)
  292. (provide 'org-wikinodes)
  293. ;;; org-wikinodes.el ends here