From 36c190d0569aa5640ac649ed359cb893872afd2a Mon Sep 17 00:00:00 2001 From: Tslil Clingman Date: Mon, 9 Jul 2018 10:36:13 +0200 Subject: Better defaults, explicit call, fixed history/default --- siege-mode.el | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) (limited to 'siege-mode.el') diff --git a/siege-mode.el b/siege-mode.el index b76b601..9313d3c 100644 --- a/siege-mode.el +++ b/siege-mode.el @@ -1,10 +1,10 @@ ;;; Package --- Surround region with smart delimiters interactively -;; Time-stamp: <2018-07-08 14:37:21 (tslil)> +;; Time-stamp: <2018-07-10 18:33:54 (tslil)> ;; Copyright (c) 2018 tslil clingman ;; Author: tslil clingman -;; Version: 2.0 +;; Version: 2.2 ;; Package-Requires: ((emacs "24.4")) ;; Keywords: region wrap @@ -25,14 +25,15 @@ ;; Lay siege to the region from both sides! ;; -;; When the region is active, all input is redirected to the minibuffer and +;; When the region is active, most input* is redirected to the minibuffer and ;; treated as a delimiter for the region. The input is used as the left ;; delimiter from which the right one is derived using `siege-transform-regexs' ;; applied to tokens generated by `siege-boundary-list' and `siege-block-*'. If ;; such derivations are not desired they may be disabled via "C-c a" in the -;; minibuffer or by default (`siege-default-derive'). +;; minibuffer or by default (`siege-default-derive'). This process may be +;; explicitly invoked by calling `siege-explicit-call', bound to "M-s M-s". ;; -;; All changes are dynamically displayed in the buffer (see +;; ALL changes are dynamically displayed in the buffer (see ;; `siege-preview-face') and may be committed by "Ret" in the minibuffer. It is ;; also possible to commit the input on "space" as set by ;; `siege-default-end-on-space' and toggled by "C-c s" in the minibuffer. @@ -40,6 +41,9 @@ ;; By default siege-mode understands the usual delimeter pairs, as well as latex ;; LaTeX begin/end pairs and left/right pairs. Moreover, for ease of use it will ;; pair both _{ and ^{ with } by default. +;; +;; * siege-mode hooks into `self-insert-command' so that key-presses which would +;; otherwise simply insert a character trigger the delimeter input. ;;; Code: (require 'subr-x) @@ -60,10 +64,12 @@ the left one. The list is traversed in order and every substitution is applied." :group 'siege-mode :type '(repeat (cons string string))) -(defcustom siege-boundary-list '("\\\\left\\\\{" - "\\\\left[({<\\[]" +(defcustom siege-boundary-list '("\\\\left\\\\." + "\\\\left." + "\\\\[[:alnum:]]+" + "\\\\[[:print:]]" "[_^]?{" - "\\Sw" ) + "\\Sw") "List of regexes giving tokens at which the input delimiter should be split \ so that the resulting substrings may be transformed. Note that these are used \ as regex alternative in order and so it is recommended that the last element \ @@ -292,7 +298,7 @@ by regex optional match strings (\\|)" (with-selected-window siege--selected-window (remove-overlays (buffer-end -1) (buffer-end 1) 'sieging t))) -(defun siege--interactive (initial) +(defun siege--interactive (initial &optional default) "Use to enter the interactive delimiter building for region. INITIAL is the string present in the minibuffer, which is not necessarily\ @@ -311,10 +317,14 @@ the default value." (add-hook 'minibuffer-exit-hook #'siege--preview-end nil t) (siege--preview-input)) (read-from-minibuffer - "Enter left delimiter: " + (format "Enter left delimiter%s: " + (if default + (concat " (Default: " default ")") + "")) initial siege-minibuffer-map nil 'siege--hist))) - (pair (siege--matching-pair result))) + (pair (siege--matching-pair + (if (string-equal result "") default result)))) (siege--preview-end) (goto-char start) (insert (car pair)) @@ -332,20 +342,30 @@ is derived according to all siege-* variables. See also \ If the region is not active then ARG is passed on to `self-insert-command'." (interactive "p") - (if (and (region-active-p) (characterp last-command-event)) - (siege--interactive (char-to-string last-command-event)) + (if (and (not (minibufferp)) (region-active-p) (characterp last-input-event)) + (siege--interactive (char-to-string last-input-event)) (self-insert-command arg))) +(defun siege-explicit-call () + "When the region is active, all input is redirected to the minibuffer and \ +treated as a delimiter for the region. By default the input is used as the left\ + delimiter from which the right one is derived according to all siege-* \ +variables. See also `siege--derive-delimiter'." + (interactive) + (if (and (not (minibufferp)) (region-active-p)) + (siege--interactive "" (car siege--hist)))) + (defvar siege-mode-map (let ((map (make-sparse-keymap))) (define-key map [remap self-insert-command] 'siege--self-insert) + (define-key map (kbd "M-s M-s") 'siege-explicit-call) map)) (define-minor-mode siege-mode "Siege minor mode." nil " siege" siege-mode-map - :global true) + :global nil) (provide 'siege-mode) ;;; siege-mode.el ends here -- cgit v1.3.1