PageRenderTime 102ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/predictive/predictive-auto-overlay-auto-dict.el

https://github.com/shanbhardwaj/emacsonrails
Emacs Lisp | 203 lines | 91 code | 44 blank | 68 comment | 1 complexity | ec482bce62563b3b6febc2515b37b9a3 MD5 | raw file
  1. ;;; predictive-auto-overlay-auto-dict.el --- automatic overlays with automatic
  2. ;;; dictionary update
  3. ;; Copyright (C) 2008 Toby Cubitt
  4. ;; Author: Toby Cubitt <toby-predictive@dr-qubit.org>
  5. ;; Version: 0.2
  6. ;; Keywords: predictive, automatic, overlays, dictionary, auto-dict
  7. ;; URL: http://www.dr-qubit.org/emacs.php
  8. ;; This file is part of the Emacs Predictive Completion Mode package.
  9. ;;
  10. ;; This program is free software; you can redistribute it and/or
  11. ;; modify it under the terms of the GNU General Public License
  12. ;; as published by the Free Software Foundation; either version 2
  13. ;; of the License, or (at your option) any later version.
  14. ;;
  15. ;; This program is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;;
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with this program; if not, write to the Free Software
  22. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. ;; MA 02110-1301, USA.
  24. ;;; Change Log:
  25. ;;
  26. ;; Version 0.2
  27. ;; * moved utility functions from predictive-latex.el
  28. ;;
  29. ;; Version 0.1.1
  30. ;; * add required `require's
  31. ;;
  32. ;; Version 0.1
  33. ;; * initial release
  34. ;;; Code:
  35. (require 'auto-overlays)
  36. (require 'auto-overlay-word)
  37. (require 'dict-tree)
  38. (require 'predictive)
  39. (provide 'predictive-auto-overlay-auto-dict)
  40. ;; set auto-dict overlay parsing and suicide functions, and indicate class
  41. ;; requires separate start and end regexps
  42. (put 'predictive-auto-dict 'auto-overlay-parse-function
  43. 'predictive-parse-auto-dict-match)
  44. (put 'predictive-auto-dict 'auto-overlay-suicide-function
  45. 'predictive-auto-dict-suicide)
  46. (defun predictive-parse-auto-dict-match (o-match)
  47. ;; Create a new word overlay, and add its contents to a dictionary
  48. ;; create new word overlay
  49. (let ((o-new (auto-o-parse-word-match o-match))
  50. word dict)
  51. ;; extract word and get dict
  52. (setq word (buffer-substring-no-properties
  53. (overlay-get o-match 'delim-start)
  54. (overlay-get o-match 'delim-end)))
  55. (setq dict (overlay-get o-new 'auto-dict))
  56. ;; save word and dict in overlay properties
  57. (overlay-put o-match 'word word)
  58. (overlay-put o-match 'auto-dict dict)
  59. ;; add change function to overlay modification hooks
  60. (overlay-put o-new 'modification-hooks
  61. (cons 'predictive-schedule-auto-dict-update
  62. (overlay-get o-new 'modification-hooks)))
  63. (overlay-put o-new 'insert-in-front-hooks
  64. (cons 'predictive-schedule-auto-dict-update
  65. (overlay-get o-new 'insert-in-front-hooks)))
  66. (overlay-put o-new 'insert-behind-hooks
  67. (cons 'predictive-schedule-auto-dict-update
  68. (overlay-get o-new 'insert-behind-hooks)))
  69. ;; add word to dictionary
  70. (unless (dictree-p dict) (setq dict (eval dict)))
  71. (predictive-add-to-dict dict word 0)
  72. ;; return the new overlay
  73. o-new)
  74. )
  75. (defun predictive-auto-dict-suicide (o-match)
  76. ;; Delete the word overlay, and delete the word from the dictionary
  77. (let ((word (overlay-get o-match 'word))
  78. (dict (overlay-get o-match 'auto-dict)))
  79. ;; delete the overlay
  80. (auto-o-delete-overlay (overlay-get o-match 'parent))
  81. ;; delete the word from the dictionary
  82. (unless (dictree-p dict) (setq dict (eval dict)))
  83. (dictree-delete dict word))
  84. )
  85. (defun predictive-schedule-auto-dict-update
  86. (o-self modified &rest unused)
  87. ;; All auto-dict overlay modification hooks are set to this function, which
  88. ;; schedules `predictive-auto-dict-update' to run after any suicide
  89. ;; functions have been called
  90. (unless modified
  91. (add-to-list 'auto-o-pending-post-suicide
  92. (list 'predictive-auto-dict-update o-self)))
  93. )
  94. (defun predictive-auto-dict-update (o-self)
  95. ;; Update the auto-dict with new word. Runs after modifications.
  96. (let ((dict (overlay-get (overlay-get o-self 'start) 'auto-dict))
  97. word)
  98. (unless (dictree-p dict) (setq dict (eval dict)))
  99. ;; delete old word from label dictionary
  100. (dictree-delete dict (overlay-get (overlay-get o-self 'start) 'word))
  101. ;; if overlay has not been deleted...
  102. (when (overlay-buffer o-self)
  103. ;; extract word
  104. (setq word (buffer-substring-no-properties
  105. (overlay-start o-self) (overlay-end o-self)))
  106. ;; save label in overlay property
  107. (overlay-put (overlay-get o-self 'start) 'word word)
  108. ;; add new label to dictionary
  109. (predictive-add-to-dict dict word 0)))
  110. )
  111. ;;; =================================================================
  112. ;;; Utility functions for automatically generated dictionaries
  113. (defmacro predictive-auto-dict-name (name)
  114. ;; Return a dictionary name constructed from NAME and the buffer name
  115. `(intern
  116. (concat "dict-" ,name "-"
  117. (file-name-sans-extension
  118. (file-name-nondirectory (buffer-file-name))))))
  119. (defun predictive-load-auto-dict (name)
  120. "Load/create a NAME dictionary for the current buffer."
  121. (let ((dict (intern (concat "predictive-" name "-dict")))
  122. dictname filename)
  123. (cond
  124. ;; if buffer is associated with a file...
  125. ((buffer-file-name)
  126. (setq dictname (predictive-auto-dict-name name))
  127. (setq filename
  128. (concat (file-name-directory (buffer-file-name))
  129. predictive-auxiliary-file-location
  130. (symbol-name dictname) ".elc"))
  131. ;; create directory for dictionary file if necessary
  132. (predictive-create-auxiliary-file-location)
  133. ;; if a dictionary isn't loaded, load or create it
  134. (unless (featurep dictname)
  135. (if (not (file-exists-p filename))
  136. (predictive-create-dict dictname filename)
  137. (load filename)
  138. (predictive-load-dict dictname)
  139. ;; FIXME: probably shouldn't be using an internal dict-tree.el
  140. ;; function
  141. (dictree--set-filename (eval dictname) filename)))
  142. ;; set the NAME dictionary to the loaded/new dictionary
  143. (set dict (eval dictname)))
  144. ;; if buffer is not associated with a file,
  145. (t
  146. (set dict (predictive-create-dict))
  147. (setq dict (eval dict))
  148. ;; FIXME: shouldn't be using internal dict-tree.el functions. Probably
  149. ;; need to make `predictive-create-dict' interface more flexible.
  150. (dictree--set-name dict name)
  151. (dictree--set-autosave dict nil))
  152. ))
  153. )
  154. (defun predictive-unload-auto-dict (name)
  155. "Unload and possibly save the current buffer's NAME dictionary."
  156. (let ((dict (eval (intern (concat "predictive-" name "-dict")))))
  157. (dictree-unload (if (dictree-p dict) dict (eval dict))))
  158. )
  159. ;;; predictive-auto-overlay-auto-dict.el ends here