AmaiKinono / puni

Structured editing (soft deletion, expression navigating & manipulating) that supports many major modes out of the box.
GNU General Public License v3.0
395 stars 20 forks source link

puni-wrap-pair function #47

Open tpeacock19 opened 1 year ago

tpeacock19 commented 1 year ago

I currently use the below configuration to emulate insert-pair from lisp.el with puni-wrap-next-sexps. I figured it may be useful to add to puni itself. I'm happy to open a PR, but wanted to get your opinion first. This allows for the delimiter to be defined after calling the function, so there would be less need for puni-wrap-{round,square,curly,angle}.

Note: I currently use insert-pair-alist, but perhaps adding an alist specific to puni would be better.

(setf insert-pair-alist '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>)
                               (?\" ?\") (?\' ?\') (?\` ?\') (?r "(" ")")
                               (?R "( " " )") (?c "{" "}") (?C "{ " " }")
                               (?b "[" "]") (?B "[ " " ]") (?o "<" ">")
                               (?O "< " " >") (?s "\"" "\"") (?t "\'" "\'")
                               (?v "`" "`")))

(defun +puni-wrap-pair (&optional n open close)
  (interactive "P")
  (let ((pair (or (assq last-command-event insert-pair-alist)
                  (assq (event-basic-type last-command-event)
                        insert-pair-alist))))
    (if pair
        (if (nth 2 pair)
            (setq open (nth 1 pair) close (nth 2 pair))
          (setq open (nth 0 pair) close (nth 1 pair)))))
  (puni-wrap-next-sexps
   (puni--parse-interactive-argument-for-wrap n)
   open close))

(defvar-keymap insert-pair-map
    :prefix 'insert-pair-map
    "<t>" #'+puni-wrap-pair)

(keymap-global-set "C-S-w" insert-pair-map)
AmaiKinono commented 1 year ago

Sorry for the late response.

I'm not sure if people will actually use +puni-wrap-pair often, based on my own habit:

But if you are sure some users will found it useful, please go ahead.

hkjels commented 2 months ago
(defun puni-wrap (char)
    "Wrap the region with the given CHAR."
    (cond ((or (eq char ?\)) (eq char ?\()) (puni-wrap-round))
      ((or (eq char ?\]) (eq char ?\[)) (puni-wrap-square))
      ((or (eq char ?\}) (eq char ?\{)) (puni-wrap-curly))
      (t (save-excursion
           (goto-char (region-end))
           (insert char)
           (goto-char (region-beginning))
           (insert char)))))

(defun meow-wrap-with-pair ()
    "Prompt user to enter a character to wrap the active region, and use `puni-wrap`."
    (interactive)
    (if (use-region-p)
    (let ((char (read-char "Enter the character to wrap with: ")))
          (puni-wrap char))
      (message "No active region")))

I'm using this myself. Just sharing for others to steal