ecomplete.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ;;; ecomplete.el --- electric completion of addresses and the like
  2. ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: mail
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile
  19. (require 'cl))
  20. (eval-when-compile
  21. (when (featurep 'xemacs)
  22. ;; The `kbd' macro requires that the `read-kbd-macro' macro is available.
  23. (require 'edmacro)))
  24. (defgroup ecomplete nil
  25. "Electric completion of email addresses and the like."
  26. :group 'mail)
  27. (defcustom ecomplete-database-file "~/.ecompleterc"
  28. "*The name of the file to store the ecomplete data."
  29. :group 'ecomplete
  30. :type 'file)
  31. (defcustom ecomplete-database-file-coding-system 'iso-2022-7bit
  32. "Coding system used for writing the ecomplete database file."
  33. :type '(symbol :tag "Coding system")
  34. :group 'ecomplete)
  35. ;;; Internal variables.
  36. (defvar ecomplete-database nil)
  37. ;;;###autoload
  38. (defun ecomplete-setup ()
  39. (when (file-exists-p ecomplete-database-file)
  40. (with-temp-buffer
  41. (let ((coding-system-for-read ecomplete-database-file-coding-system))
  42. (insert-file-contents ecomplete-database-file)
  43. (setq ecomplete-database (read (current-buffer)))))))
  44. (defun ecomplete-add-item (type key text)
  45. (let ((elems (assq type ecomplete-database))
  46. (now (string-to-number
  47. (format "%.0f" (if (featurep 'emacs)
  48. (float-time)
  49. (require 'gnus-util)
  50. (gnus-float-time)))))
  51. entry)
  52. (unless elems
  53. (push (setq elems (list type)) ecomplete-database))
  54. (if (setq entry (assoc key (cdr elems)))
  55. (setcdr entry (list (1+ (cadr entry)) now text))
  56. (nconc elems (list (list key 1 now text))))))
  57. (defun ecomplete-get-item (type key)
  58. (assoc key (cdr (assq type ecomplete-database))))
  59. (defun ecomplete-save ()
  60. (with-temp-buffer
  61. (let ((coding-system-for-write ecomplete-database-file-coding-system))
  62. (insert "(")
  63. (loop for (type . elems) in ecomplete-database
  64. do
  65. (insert (format "(%s\n" type))
  66. (dolist (entry elems)
  67. (prin1 entry (current-buffer))
  68. (insert "\n"))
  69. (insert ")\n"))
  70. (insert ")")
  71. (write-region (point-min) (point-max)
  72. ecomplete-database-file nil 'silent))))
  73. (defun ecomplete-get-matches (type match)
  74. (let* ((elems (cdr (assq type ecomplete-database)))
  75. (match (regexp-quote match))
  76. (candidates
  77. (sort
  78. (loop for (key count time text) in elems
  79. when (string-match match text)
  80. collect (list count time text))
  81. (lambda (l1 l2)
  82. (> (car l1) (car l2))))))
  83. (when (> (length candidates) 10)
  84. (setcdr (nthcdr 10 candidates) nil))
  85. (unless (zerop (length candidates))
  86. (with-temp-buffer
  87. (dolist (candidate candidates)
  88. (insert (caddr candidate) "\n"))
  89. (goto-char (point-min))
  90. (put-text-property (point) (1+ (point)) 'ecomplete t)
  91. (while (re-search-forward match nil t)
  92. (put-text-property (match-beginning 0) (match-end 0)
  93. 'face 'isearch))
  94. (buffer-string)))))
  95. (defun ecomplete-display-matches (type word &optional choose)
  96. (let* ((matches (ecomplete-get-matches type word))
  97. (line 0)
  98. (max-lines (when matches (- (length (split-string matches "\n")) 2)))
  99. (message-log-max nil)
  100. command highlight)
  101. (if (not matches)
  102. (progn
  103. (message "No ecomplete matches")
  104. nil)
  105. (if (not choose)
  106. (progn
  107. (message "%s" matches)
  108. nil)
  109. (setq highlight (ecomplete-highlight-match-line matches line))
  110. (let ((local-map (make-sparse-keymap))
  111. selected)
  112. (define-key local-map (kbd "RET")
  113. (lambda () (setq selected (nth line (split-string matches "\n")))))
  114. (define-key local-map (kbd "M-n")
  115. (lambda () (setq line (min (1+ line) max-lines))))
  116. (define-key local-map (kbd "M-p")
  117. (lambda () (setq line (max (1- line) 0))))
  118. (let ((overriding-local-map local-map))
  119. (while (and (null selected)
  120. (setq command (read-key-sequence highlight))
  121. (lookup-key local-map command))
  122. (apply (key-binding command) nil)
  123. (setq highlight (ecomplete-highlight-match-line matches line))))
  124. (if selected
  125. (message selected)
  126. (message "Abort"))
  127. selected)))))
  128. (defun ecomplete-highlight-match-line (matches line)
  129. (with-temp-buffer
  130. (insert matches)
  131. (goto-char (point-min))
  132. (forward-line line)
  133. (save-restriction
  134. (narrow-to-region (point) (point-at-eol))
  135. (while (not (eobp))
  136. ;; Put the 'region face on any characters on this line that
  137. ;; aren't already highlighted.
  138. (unless (get-text-property (point) 'face)
  139. (put-text-property (point) (1+ (point)) 'face 'highlight))
  140. (forward-char 1)))
  141. (buffer-string)))
  142. (provide 'ecomplete)
  143. ;;; ecomplete.el ends here