Fanael / edit-indirect

Edit regions in separate buffers
99 stars 23 forks source link

How is the core of this entire package better than this snippet of code ? #11

Closed maindoor closed 5 years ago

maindoor commented 5 years ago

Not a troll, genuine question. See this piece of code and lets discuss how this package does a lot better job the code here:

 (defun narrow-or-widen-dwim (p)
   "Widen if buffer is narrowed, narrow-dwim otherwise.
   Dwim means: region, org-src-block, org-subtree, or
   defun, whichever applies first. Narrowing to
   org-src-block actually calls `org-edit-src-code'.

   With prefix P, don't widen, just narrow even if buffer
   is already narrowed."
   (interactive "P")
   (declare (interactive-only))
   (cond ((and (buffer-narrowed-p) (not p)) (widen))
         ((region-active-p)
          (narrow-to-region (region-beginning)
                            (region-end)))
         ((derived-mode-p 'org-mode)
          ;; `org-edit-src-code' is not a real narrowing
          ;; command. Remove this first conditional if
          ;; you don't want it.
          (cond ((ignore-errors (org-edit-src-code) t)
                 (delete-other-windows))
                ((ignore-errors (org-narrow-to-block) t))
                (t (org-narrow-to-subtree))))
         ((derived-mode-p 'latex-mode)
          (LaTeX-narrow-to-environment))
         (t (narrow-to-defun))))

I've been using this piece of code for a long time and I'd like for it to be integrated into a package like yours. This code does the right thing when I am inside a C function to narrow it down to just the function, preserving font-lock and also works on a region. Your package has hooks support, but font-lock isn't enabled by default and maybe more, I just looked at the description. Can you use portions of the code and make better defaults ?

Fanael commented 5 years ago

This package is meant to replicate org-edit-src-code outside of org-mode, for situations like for example modifying SQL statements (using sql-mode) embedded directly in source code in another language. It's not, and wasn't meant to be, an alternative to narrowing.

font-lock isn't enabled

It "is", but the default edit-indirect-guess-mode-function just calls normal-mode, which most of the time results in fundamental-mode. Knowing which major mode to enable for a given region requires domain-specific knowledge, which is why the edit-indirect-guess-mode-function hook exists: so you can use that knowledge to better determine which major mode to use in the indirect buffer.