auto-overlay-stack-sync.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ;;; auto-overlay-stack-sync.el --- syncronised stacked automatic overlays
  2. ;; Copyright (C) 2006 Toby Cubitt
  3. ;; Author: Toby Cubitt <toby-predictive@dr-qubit.org>
  4. ;; Version: 0.1
  5. ;; Keywords: automatic, overlays, stack, sync
  6. ;; URL: http://www.dr-qubit.org/emacs.php
  7. ;; This file is part of the Emacs Automatic Overlays package.
  8. ;;
  9. ;; This program is free software; you can redistribute it and/or
  10. ;; modify it under the terms of the GNU General Public License
  11. ;; as published by the Free Software Foundation; either version 2
  12. ;; of the License, or (at your option) any later version.
  13. ;;
  14. ;; This program 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. ;;
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with this program; if not, write to the Free Software
  21. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. ;; MA 02110-1301, USA.
  23. ;;; Change Log:
  24. ;;
  25. ;; Version 0.1
  26. ;; * initial version
  27. ;;; Code:
  28. (require 'auto-overlays)
  29. (require 'auto-overlay-stack)
  30. (provide 'auto-overlay-stack-sync)
  31. ;; register stack-sync overlay parsing and suicide functions
  32. (assq-delete-all 'stack-sync auto-overlay-functions)
  33. (push '(stack-sync auto-o-parse-stack-match auto-o-stack-suicide
  34. auto-o-make-stack-sync-match)
  35. auto-overlay-functions)
  36. (defun auto-o-make-stack-sync-match (o-match)
  37. ;; Perform any necessary updates of auto overlays due to a match for a
  38. ;; stack-sync regexp.
  39. ;; add sync function to end of modification-, insert-in-front- and
  40. ;; insert-behind-hooks (after suicide function)
  41. (overlay-put o-match 'modification-hooks
  42. (append (overlay-get o-match 'modification-hooks)
  43. '(auto-o-stack-sync-update)))
  44. (overlay-put o-match 'insert-in-front-hooks
  45. (append (overlay-get o-match 'insert-in-front-hooks)
  46. '(auto-o-stack-sync-update)))
  47. (overlay-put o-match 'insert-behind-hooks
  48. (append (overlay-get o-match 'insert-behind-hooks)
  49. '(auto-o-stack-sync-update)))
  50. ;; make sure new match overlay is synchronised
  51. (auto-o-stack-sync-update o-match t)
  52. )
  53. (defun auto-o-stack-sync-update (o-self modified &rest rest)
  54. ;; Syncronise start and end delimeters. Called by match overlay's
  55. ;; modification-hooks.
  56. (when modified
  57. (if (> auto-o-pending-suicide-count 0)
  58. (add-to-list 'auto-o-pending-post-suicide
  59. (list 'auto-o-stack-sync-update o-self t) 'append)
  60. (let ((edge (if (eq (auto-o-edge o-self) 'start) 'end 'start))
  61. o-parent o-stack o-other str)
  62. ;; if match overlay is still in the buffer (it might have been been
  63. ;; deleted after a suicide), has a parent, the parent is matched at
  64. ;; the other end, and the entire stack is start and end matched...
  65. (when (and (overlay-buffer o-self)
  66. (setq o-parent (overlay-get o-self 'parent))
  67. (setq o-other (overlay-get o-parent edge))
  68. (or (null (setq o-stack
  69. (car (last (auto-o-stack o-self)))))
  70. (and (overlay-get o-stack 'start)
  71. (overlay-get o-stack 'end))))
  72. (save-excursion
  73. (save-match-data
  74. ;; set match data for match overlay's regexp and get string to
  75. ;; copy to other end
  76. (goto-char (overlay-start o-self))
  77. (when (looking-at (auto-o-regexp o-self))
  78. (setq str (match-string (auto-o-regexp-group-nth 1 o-self)))
  79. ;; if string at other end doesn't match, replace it (it's
  80. ;; important to check if it already matches or we get infinite
  81. ;; recursion when it's own modification-hooks are called)
  82. (goto-char (overlay-start o-other))
  83. (when (and (looking-at (auto-o-regexp o-other))
  84. (not (string=
  85. str (match-string (auto-o-regexp-group-nth
  86. 1 o-other)))))
  87. (let ((inhibit-modification-hooks t))
  88. (replace-match str t t nil
  89. (auto-o-regexp-group-nth 1 o-other)))))
  90. )))
  91. )))
  92. )