Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.9 KiB

4 years ago
  1. ;;; highlight-uses-mode.el --- Mode for highlighting uses -*- lexical-binding: t -*-
  2. ;; Copyright (c) 2014 Chris Done. All rights reserved.
  3. ;; This file is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation; either version 3, or (at your option)
  6. ;; any later version.
  7. ;; This file is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. (require 'cl-lib)
  15. (defvar highlight-uses-mode-map
  16. (let ((map (make-sparse-keymap)))
  17. (define-key map (kbd "TAB") 'highlight-uses-mode-next)
  18. (define-key map (kbd "S-TAB") 'highlight-uses-mode-prev)
  19. (define-key map (kbd "<backtab>") 'highlight-uses-mode-prev)
  20. (define-key map (kbd "RET") 'highlight-uses-mode-stop-here)
  21. (define-key map (kbd "C-g") 'highlight-uses-mode)
  22. map)
  23. "Keymap for using `highlight-uses-mode'.")
  24. (defvar-local highlight-uses-mode-point nil)
  25. ;;;###autoload
  26. (define-minor-mode highlight-uses-mode
  27. "Minor mode for highlighting and jumping between uses."
  28. :lighter " Uses"
  29. :keymap highlight-uses-mode-map
  30. (if highlight-uses-mode
  31. (setq highlight-uses-mode-point (point))
  32. (when highlight-uses-mode-point
  33. (goto-char highlight-uses-mode-point)))
  34. (remove-overlays (point-min) (point-max) 'highlight-uses-mode-highlight t))
  35. (defun highlight-uses-mode-replace ()
  36. "Replace all highlighted instances in the buffer with something
  37. else."
  38. (interactive)
  39. (save-excursion
  40. (goto-char (point-min))
  41. (let ((o (highlight-uses-mode-next)))
  42. (when o
  43. (let ((replacement (read-from-minibuffer (format "Replace uses %s with: "
  44. (buffer-substring
  45. (overlay-start o)
  46. (overlay-end o))))))
  47. (while o
  48. (goto-char (overlay-start o))
  49. (delete-region (overlay-start o)
  50. (overlay-end o))
  51. (insert replacement)
  52. (setq o (highlight-uses-mode-next))))))))
  53. (defun highlight-uses-mode-stop-here ()
  54. "Stop at this point."
  55. (interactive)
  56. (setq highlight-uses-mode-point (point))
  57. (highlight-uses-mode -1))
  58. (defun highlight-uses-mode-next ()
  59. "Jump to next result."
  60. (interactive)
  61. (let ((os (sort (cl-remove-if (lambda (o)
  62. (or (<= (overlay-start o) (point))
  63. (not (overlay-get o 'highlight-uses-mode-highlight))))
  64. (overlays-in (point) (point-max)))
  65. (lambda (a b)
  66. (< (overlay-start a)
  67. (overlay-start b))))))
  68. (when os
  69. (goto-char (overlay-start (car os)))
  70. (car os))))
  71. (defun highlight-uses-mode-prev ()
  72. "Jump to previous result."
  73. (interactive)
  74. (let ((os (sort (cl-remove-if (lambda (o)
  75. (or (>= (overlay-end o) (point))
  76. (not (overlay-get o 'highlight-uses-mode-highlight))))
  77. (overlays-in (point-min) (point)))
  78. (lambda (a b)
  79. (> (overlay-start a)
  80. (overlay-start b))))))
  81. (when os
  82. (goto-char (overlay-start (car os)))
  83. (car os))))
  84. (defun highlight-uses-mode-highlight (start end)
  85. "Make a highlight overlay at the given span."
  86. (let ((o (make-overlay start end)))
  87. (overlay-put o 'priority 999)
  88. (overlay-put o 'face 'isearch)
  89. (overlay-put o 'highlight-uses-mode-highlight t)))
  90. (provide 'highlight-uses-mode)