kmacro-x

a fork of vifon's kmacro-x
git clone https://git.trogloxene.org/kmacro-x.git
Log | Files | Refs | README | LICENSE

kmacro-x-mc-legacy.el (4078B)


      1 ;;; kmacro-x-mc-legacy.el --- The old implementation of the multiple cursors  -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2022-2023  Wojciech Siewierski
      4 
      5 ;; Author: Wojciech Siewierski
      6 ;; URL: https://github.com/vifon/kmacro-x.el
      7 ;; Keywords: convenience
      8 ;; Version: 0.9
      9 
     10 ;; This program is free software; you can redistribute it and/or modify
     11 ;; it under the terms of the GNU General Public License as published by
     12 ;; the Free Software Foundation, either version 3 of the License, or
     13 ;; (at your option) any later version.
     14 
     15 ;; This program is distributed in the hope that it will be useful,
     16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18 ;; GNU General Public License for more details.
     19 
     20 ;; You should have received a copy of the GNU General Public License
     21 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     22 
     23 ;;; Commentary:
     24 
     25 ;; The obsolete version of the multiple cursors based of keyboard
     26 ;; macros.  Less robust but with different semantics.  No longer
     27 ;; maintained but might be useful.
     28 
     29 ;;; Code:
     30 
     31 (require 'kmacro-x-mc)
     32 
     33 (defgroup kmacro-x-mc-legacy nil
     34   "Multiple cursors implemented with keyboard macros (legacy)."
     35   :group 'kmacro-x-mc)
     36 
     37 (defface kmacro-x-mc-legacy-highlight-face
     38   '((t (:inherit hi-yellow)))  ;`highlight-regexp' hardcodes `hi-yellow'
     39   "The face used by `kmacro-x-mc-legacy-region'.")
     40 
     41 (defcustom kmacro-x-mc-legacy-region-sequence-fmt "C-s %s C-r RET 2*C-s RET"
     42   "The sequence of keys used by `kmacro-x-mc-legacy-region'.
     43 
     44 This sequence should search for the next occurrence of the query
     45 and leave the region in a predictable state for the user to use.
     46 
     47 The search query will be spliced into it using `format', and so
     48 it should contain exactly one `%s' placeholder."
     49   :type 'string)
     50 
     51 
     52 ;;;###autoload
     53 (defun kmacro-x-mc-legacy-region (start end &optional highlight)
     54   "Record a keyboard macro emulating multiple cursors.
     55 
     56 The kmacro will first search for current region and then execute
     57 the rest of the recorded kmacro.  During the kmacro execution,
     58 mark is always at the beginning of the match and point is at
     59 the end.
     60 
     61 START and END mark the region.
     62 
     63 If HIGHLIGHT is non-nil (or with the prefix argument when using
     64 interactively) highlight the query using `highlight-regexp'.
     65 Use `\\[unhighlight-regexp]' to remove the highlight later."
     66   (interactive "r\nP")
     67   (unless (and (mark)
     68                (= start (region-beginning))
     69                (= end (region-end)))
     70     ;; Ensure a consistent behavior when called interactively and when
     71     ;; called from Elisp with arguments not being the current region.
     72     ;; Considering the interactive nature of the kmacros, tampering
     73     ;; with the user's region seems very much justified.
     74     (push-mark start)
     75     (goto-char end))
     76   (let ((query (buffer-substring-no-properties start end)))
     77     (when (< (point) (mark))
     78       (exchange-point-and-mark))
     79     (deactivate-mark)
     80     (kmacro-push-ring)
     81     (setq last-kbd-macro
     82           (read-kbd-macro (format kmacro-x-mc-legacy-region-sequence-fmt
     83                                   (format-kbd-macro query))))
     84     (when highlight
     85       (highlight-regexp (regexp-quote query)
     86                         'kmacro-x-mc-legacy-highlight-face))
     87     (start-kbd-macro 'append 'no-exec)))
     88 
     89 ;;;###autoload
     90 (defun kmacro-x-mc-legacy (&optional highlight)
     91   "A more user friendly version of `kmacro-x-mc-legacy-region'.
     92 
     93 If selection is active, it does the same thing as
     94 `kmacro-x-mc-legacy-region'.  If there is no active selection, it uses
     95 the symbol at point instead.
     96 
     97 See `kmacro-x-mc-legacy-region' for the HIGHLIGHT argument."
     98   (interactive "P")
     99   (let ((bounds (if (use-region-p)
    100                     (cons (region-beginning) (region-end))
    101                   (bounds-of-thing-at-point 'symbol))))
    102     (unless bounds
    103       (error "%s" "No region or symbol to act on"))
    104     (let ((start (car bounds))
    105           (end (cdr bounds)))
    106       (kmacro-x-mc-legacy-region start end highlight))))
    107 
    108 
    109 (provide 'kmacro-x-mc-legacy)
    110 ;;; kmacro-x-mc-legacy.el ends here