Skip to content

Restore original indentation #46

Description

@arkhan

Greetings, I'm currently using edit-indirect. I want to transfer my settings to use separateedit, but I notice that it doesn't work the same way. The idea is to edit and restore the original indentation when finished. I've attached the edit-indirect settings:

(setup (:pkg expand-region))

(setup (:pkg edit-indirect)
  (:require s
            dash
            expand-region)
  (:global "C-c '" edit-indirect-or-org-edit-special)

  (defun edit-indirect-or-org-edit-special ()
    "Edit region with `org-edit-special' in Org mode, otherwise use `conf-edit-indirect-region'."
    (interactive)
    (if (derived-mode-p 'org-mode)
        (call-interactively 'org-edit-special)
      (call-interactively 'conf:edit-indirect-region)))

  (defvar conf:select-a-major-mode-last-selected nil)
  (defvar conf:edit-indirect--left-margin 0)
  (defvar edit-indirect-guess-mode-function #'edit-indirect-default-guess-mode)

  (defun conf:select-a-major-mode ()
    "Interactively select a major mode and return it as a string."
    (let* ((def (or
                 conf:select-a-major-mode-last-selected
                 (symbol-name initial-major-mode)))
           (choice (completing-read "major mode: "
                                    (apropos-internal "-mode$")
                                    nil nil nil nil
                                    def)))
      (setq conf:select-a-major-mode-last-selected choice)))

  (defun conf:edit-indirect-region ()
    (interactive)
    (er/mark-inside-quotes)
    (unless (region-active-p) (user-error "No region selected"))
    (save-excursion
      (let* ((begin (region-beginning))
             (end (region-end))
             (mode (conf:select-a-major-mode))
             (edit-indirect-guess-mode-function
              (lambda (_parent _beg _end)
                (funcall (intern mode)))))
        (edit-indirect-region begin end 'display-buffer))))

  (defun conf:compute-left-margin (code)
    "Compute left margin of a string of CODE."
    (-min
     (-map #'(lambda (line) (length (car (s-match "^\\s-*" line))))
           (-remove 's-blank? (s-lines code)))))

  (defun conf:after-indirect-edit-remove-left-margin ()
    "Remove left-margin and save it into a local variable."
    (let ((lm (conf:compute-left-margin (buffer-substring (point-min) (point-max)))))
      (indent-rigidly (point-min) (point-max) (* -1 lm))
      (setq-local conf:edit-indirect--left-margin lm)))

  (defun conf:after-indirect-edit-restore-left-margin ()
    "Restore left-margin before commiting."
    (indent-rigidly (point-min) (point-max) conf:edit-indirect--left-margin))

  (:with-hook edit-indirect-after-creation-hook
    (:hook conf:after-indirect-edit-remove-left-margin))
  (:with-hook edit-indirect-before-commit-hook
    (:hook conf:after-indirect-edit-restore-left-margin
           delete-trailing-whitespace)))

and this is the separedit configuration:

(setup (:pkg language-detection))

(setup (:pkg separedit)
  (:require separedit)
  (:global "C-c '" #'separedit)
  (:with-hook separedit-buffer-creation-hook
    (:hook (lambda ()
             (when (derived-mode-p 'separedit-single-quote-string-mode
                                   'separedit-double-quote-string-mode)
               (let* ((language (language-detection-buffer))
                      (mode (intern (concat (symbol-name language) "-mode")))
                      (header-line-format-back header-line-format))
                 (if (fboundp mode)
                     (progn
                         (funcall mode)
                         (setq-local header-line-format header-line-format-back)))))))))

Additionally, I attach a video of the behavior of the two packages

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions