Last active
October 10, 2020 19:20
-
-
Save uucidl/8ca5ec04b97dc2b460efee74fca4537c to your computer and use it in GitHub Desktop.
Let Emacs simulate Acme by creating clickable regexp links in the buffer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Simulates Acme by creating clickable regexp links in the buffer. | |
;; | |
;; You can use it for instance to create an index: | |
;; | |
;; :/uu-re-link-mode/ : entry point. | |
;; | |
(defcustom uu-re-link-re ":/\\(.+\\)/" "Regular expression for regular expression links.") | |
(defcustom uu-re-link-re-initial "[[:space:]]:/" nil) | |
(defcustom uu-re-link-isearch t "Use isearch to find the expression interactively.") | |
(defun uu-re-text-search-forward () | |
"moves the point to the next regexp in buffer." | |
(interactive) | |
(if (re-search-forward uu-re-link-re nil t) | |
(match-beginning 0))) | |
(defun uu-re-text-search-backward () | |
(if (re-search-backward uu-re-link-re nil t) | |
(match-beginning 0))) | |
(defun uu-re-link--mouse-click (event) | |
"Follow the regular expression search." | |
(interactive "e") | |
(let ((window (posn-window (event-end event))) | |
(pos (posn-point (event-end event)))) | |
;; move pos to beginning of link | |
(with-current-buffer (window-buffer window) | |
(let ((re)) | |
(goto-char pos) | |
(save-excursion | |
(if (progn (re-search-backward uu-re-link-re-initial) (uu-re-text-search-forward)) | |
(setq re (buffer-substring-no-properties (match-beginning 1) (match-end 1))))) | |
(if (not uu-re-link-isearch) | |
(re-search-forward re) | |
(progn | |
(isearch-mode t t) | |
(let ((isearch-regexp nil)) | |
(isearch-yank-string re)) | |
(isearch-repeat-forward))))))) | |
(setq uu-re-text-map | |
(let ((map (make-sparse-keymap))) | |
(define-key map [mouse-2] 'uu-re-link--mouse-click) | |
(define-key map (kbd "<C-mouse-1>") 'uu-re-link--mouse-click) | |
(define-key map [follow-link] t) | |
map)) | |
(defun uu-re-link--make-links () | |
(save-excursion | |
(goto-char (point-min)) | |
(while (uu-re-text-search-forward) | |
(let ((link-start (match-beginning 1)) | |
(link-end (match-end 1))) | |
(with-silent-modifications | |
(add-text-properties | |
link-start link-end | |
'(mouse-face highlight | |
help-echo "mouse-2: search forward for regular expression.")) | |
(put-text-property link-start link-end | |
'keymap uu-re-text-map)))))) | |
(defun uu-re-link--after-change (begin end length) | |
(uu-re-link--make-links)) | |
(define-minor-mode uu-re-link-mode | |
"Mode in which :/.../ defines a clickable regular expression." | |
:group uu | |
:init-value nil :lighter nil :keymap nil | |
(uu-re-link--make-links) | |
(add-hook 'after-change-functions #'uu-re-link--after-change t t)) | |
(define-globalized-minor-mode global-uu-re-link-mode uu-re-link-mode (lambda () (uu-re-link-mode))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment