dabbrev.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ;; Dynamic abbreviation package for GNU Emacs.
  2. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. ; DABBREVS - "Dynamic abbreviations" hack, originally written by Don Morrison
  18. ; for Twenex Emacs. Converted to mlisp by Russ Fish. Supports the table
  19. ; feature to avoid hitting the same expansion on re-expand, and the search
  20. ; size limit variable. Bugs fixed from the Twenex version are flagged by
  21. ; comments starting with ;;; .
  22. ;
  23. ; converted to elisp by Spencer Thomas.
  24. ; Thoroughly cleaned up by Richard Stallman.
  25. ;
  26. ; If anyone feels like hacking at it, Bob Keller (Keller@Utah-20) first
  27. ; suggested the beast, and has some good ideas for its improvement, but
  28. ; doesn?tknow TECO (the lucky devil...). One thing that should definitely
  29. ; be done is adding the ability to search some other buffer(s) if you can?t
  30. ; find the expansion you want in the current one.
  31. ;; (defun dabbrevs-help ()
  32. ;; "Give help about dabbrevs."
  33. ;; (interactive)
  34. ;; (&info "emacs" "dabbrevs") ; Select the specific info node.
  35. ;; )
  36. (provide 'dabbrevs)
  37. (defvar dabbrevs-limit nil
  38. "*Limits region searched by dabbrevs-expand to that many chars away (local).")
  39. (make-variable-buffer-local 'dabbrevs-limit)
  40. (defvar dabbrevs-backward-only nil
  41. "*If non-NIL, dabbrevs-expand only looks backwards.")
  42. ; State vars for dabbrevs-re-expand.
  43. (defvar last-dabbrevs-table nil
  44. "Table of expansions seen so far. (local)")
  45. (make-variable-buffer-local 'last-dabbrevs-table)
  46. (defvar last-dabbrevs-abbreviation ""
  47. "Last string we tried to expand. Buffer-local.")
  48. (make-variable-buffer-local 'last-dabbrevs-abbreviation)
  49. (defvar last-dabbrevs-direction 0
  50. "Direction of last dabbrevs search. (local)")
  51. (make-variable-buffer-local 'last-dabbrevs-direction)
  52. (defvar last-dabbrevs-abbrev-location nil
  53. "Location last abbreviation began (local).")
  54. (make-variable-buffer-local 'last-dabbrevs-abbrev-location)
  55. (defvar last-dabbrevs-expansion nil
  56. "Last expansion of an abbreviation. (local)")
  57. (make-variable-buffer-local 'last-dabbrevs-expansion)
  58. (defvar last-dabbrevs-expansion-location nil
  59. "Location the last expansion was found. (local)")
  60. (make-variable-buffer-local 'last-dabbrevs-expansion-location)
  61. (defun dabbrev-expand (arg)
  62. "Expand previous word \"dynamically\".
  63. Expands to the most recent, preceding word for which this is a prefix.
  64. If no suitable preceding word is found, words following point are considered.
  65. A positive prefix argument, N, says to take the Nth backward DISTINCT
  66. possibility. A negative argument says search forward. The variable
  67. dabbrev-backward-only may be used to limit the direction of search to
  68. backward if set non-nil.
  69. If the cursor has not moved from the end of the previous expansion and
  70. no argument is given, replace the previously-made expansion
  71. with the next possible expansion not yet tried."
  72. (interactive "*P")
  73. (message "Searching for dynamic expansion...")
  74. (let (abbrev expansion old which loc n pattern)
  75. ;; abbrev -- the abbrev to expand
  76. ;; expansion -- the expansion found (eventually) or nil until then
  77. ;; old -- the text currently in the buffer
  78. ;; (the abbrev, or the previously-made expansion)
  79. ;; loc -- place where expansion is found
  80. ;; (to start search there for next expansion if requested later)
  81. (save-excursion
  82. (if (and (null arg)
  83. (eq last-command this-command)
  84. last-dabbrevs-abbrev-location)
  85. (progn
  86. (setq abbrev last-dabbrevs-abbreviation)
  87. (setq old last-dabbrevs-expansion)
  88. (setq which last-dabbrevs-direction))
  89. (setq which (if (null arg)
  90. (if dabbrevs-backward-only 1 0)
  91. (prefix-numeric-value arg)))
  92. (setq loc (point))
  93. (forward-word -1)
  94. (setq last-dabbrevs-abbrev-location (point)) ; Original location.
  95. (setq abbrev (buffer-substring (point) loc))
  96. (setq old abbrev)
  97. (setq last-dabbrevs-expansion-location nil)
  98. (setq last-dabbrev-table nil)) ; Clear table of things seen.
  99. (setq pattern (concat "\\b" (regexp-quote abbrev) "\\(\\sw\\|\\s_\\)+"))
  100. ;; Try looking backward unless inhibited.
  101. (if (>= which 0)
  102. (progn
  103. (setq n (max 1 which))
  104. (if last-dabbrevs-expansion-location
  105. (goto-char last-dabbrevs-expansion-location))
  106. (while (and (> n 0)
  107. (setq expansion (dabbrevs-search pattern t)))
  108. (setq loc (point-marker))
  109. (setq last-dabbrev-table (cons expansion last-dabbrev-table))
  110. (setq n (1- n)))
  111. (or expansion
  112. (setq last-dabbrevs-expansion-location nil))
  113. (setq last-dabbrevs-direction (min 1 which))))
  114. (if (and (<= which 0) (not expansion)) ; Then look forward.
  115. (progn
  116. (setq n (max 1 (- which)))
  117. (if last-dabbrevs-expansion-location
  118. (goto-char last-dabbrevs-expansion-location))
  119. (while (and (> n 0)
  120. (setq expansion (dabbrevs-search pattern nil)))
  121. (setq loc (point-marker))
  122. (setq last-dabbrev-table (cons expansion last-dabbrev-table))
  123. (setq n (1- n)))
  124. (setq last-dabbrevs-direction -1))))
  125. (message "")
  126. (if (not expansion)
  127. (let ((first (string= abbrev old)))
  128. (setq last-dabbrevs-abbrev-location nil)
  129. (if (not first)
  130. (progn (undo-boundary)
  131. (delete-backward-char (length old))
  132. (insert abbrev)))
  133. (error (if first
  134. "No dynamic expansion for \"%s\" found."
  135. "No further dynamic expansions for \"%s\" found.")
  136. abbrev))
  137. ;; Success: stick it in and return.
  138. (undo-boundary)
  139. (delete-backward-char (length old))
  140. (insert expansion)
  141. ;; Save state for re-expand.
  142. (setq last-dabbrevs-abbreviation abbrev)
  143. (setq last-dabbrevs-expansion expansion)
  144. (setq last-dabbrevs-expansion-location loc))))
  145. ;; Search function used by dabbrevs library.
  146. ;; First arg is string to find as prefix of word. Second arg is
  147. ;; t for reverse search, nil for forward. Variable dabbrevs-limit
  148. ;; controls the maximum search region size.
  149. ;; Table of expansions already seen is examined in buffer last-dabbrev-table,
  150. ;; so that only distinct possibilities are found by dabbrevs-re-expand.
  151. ;; Note that to prevent finding the abbrev itself it must have been
  152. ;; entered in the table.
  153. ;; Value is the expansion, or nil if not found. After a successful
  154. ;; search, point is left right after the expansion found.
  155. (defun dabbrevs-search (pattern reverse)
  156. (let (missing result)
  157. (save-restriction ; Uses restriction for limited searches.
  158. (if dabbrevs-limit
  159. (narrow-to-region last-dabbrevs-abbrev-location
  160. (+ (point)
  161. (* dabbrevs-limit (if reverse -1 1)))))
  162. ;; Keep looking for a distinct expansion.
  163. (setq result nil)
  164. (setq missing nil)
  165. (while (and (not result) (not missing))
  166. ; Look for it, leave loop if search fails.
  167. (setq missing
  168. (not (if reverse
  169. (re-search-backward pattern nil t)
  170. (re-search-forward pattern nil t))))
  171. (if (not missing)
  172. (progn
  173. (setq result (buffer-substring (match-beginning 0)
  174. (match-end 0)))
  175. (let* ((test last-dabbrev-table))
  176. (while (and test (not (string= (car test) result)))
  177. (setq test (cdr test)))
  178. (if test (setq result nil)))))) ; if already in table, ignore
  179. result)))