Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Definition of wrap-region-with should be sensitive to wrapper length #34

Open
apc opened this issue Mar 3, 2022 · 1 comment
Open

Comments

@apc
Copy link

apc commented Mar 3, 2022

From what I can tell, wrap-region doesn't play nicely with multi-character wrappers.

If use something like this:

(wrap-region-add-wrapper "%%" "%%" nil 'text-mode)

and type "%%" when the region consists of "text" and point is after the last "t", the result is

%%text%|%

with point before the last closing "%". From what I can tell, a simple fix would be to change the definition of wrap-region-with to this

(defun wrap-region-with (left right)
  "Wraps region with LEFT and RIGHT."
  (run-hooks 'wrap-region-before-wrap-hook)
  (let ((beg (region-beginning))
        (end (region-end))
        (pos (point))
        (deactivate-mark nil))
    (save-excursion
      (goto-char beg)
      (insert left)
      (goto-char (+ end (length left)))
      (insert right))
    ;; (if (= pos end) (forward-char 1))
    (if (= pos end) (forward-char (length right)))
    (if wrap-region-keep-mark
        (let* ((beg-p (eq beg pos))
               (beg* (+ beg (length left)))
               (end* (+ end (length left))))
          (push-mark (if beg-p end* beg*) nil t)
          (goto-char (if beg-p beg* end*)))
      (deactivate-mark)))
  (run-hooks 'wrap-region-after-wrap-hook))

where the commented out line is in the original definition, and the one immediately below it is the proposed change. Happy to submit a pull request if you think that'd be a good idea.

@rejeep
Copy link
Owner

rejeep commented Mar 3, 2022

To be honest I think you're better of using Smartparens (https://github.com/Fuco1/smartparens). I don't use wrap-region anymore, so I don't really have that strong of an opinion. Perhaps someone else does?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants