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

Already on GitHub? Sign in to your account

Collapse/fold code blocks? #128

Open
brendan-r opened this Issue Mar 7, 2017 · 4 comments

Comments

Projects
None yet
3 participants

brendan-r commented Mar 7, 2017 edited

Hello there,

First, thanks for polymode!

Is it possible to collapse/fold code-blocks, like markdown-mode allows with headings? Rstudio currently does this, for a reference implementation.

The use-case is that it can get very hairy to proof-read or even to navigate Rmd documents which have a lot of code in them. It's a very useful feature in Rstudio.

P.S. do let me know if this is best asked in markdown-mode.

Best,

Brendan

Owner

vspinu commented Mar 7, 2017

I think this is a good idea. And no, markdown wont' be able to handle those chunks because they are out of its reach once you are in a chunk. It should be a key available in the whole buffer on polymode M-n map.

brendan-r commented May 24, 2017 edited

Thought about taking a run at a PR for this, but I'm afraid my elisp fu is non-existent. Still, the following links might be useful; the built-in hide-show (code folding), outline-mode (which seems to detect 'headings'; a given regex), and the packages origami and yasfolding.

This would be incredibly useful for me. I have found polymode to be very useful for writing academic papers, but if I was able to collapse code blocks it would make life a great deal easier. Unfortunately my elisp is rudimentary at best, but willing to help if I can

brendan-r commented Aug 24, 2017 edited

Okay, yet to be thoroughly tested, but I think I've hacked together a solution using the fold-this package.

It's in my dotemacs here, and below:

(defun rmd-fold-block ()
  "Fold the contents of the current R block, in an Rmarkdown file (can be undone
   with fold-this-unfold-at-point)"
  (interactive)
  (and (eq (oref pm/chunkmode :mode) 'r-mode)
       (pm-with-narrowed-to-span nil
         (goto-char (point-min))
         (forward-line)
         (fold-this (point) (point-max)))))

(defun rmd-fold-all-blocks (arg)
  "Fold all R blocks in an Rmarkdown file (can be undone with
   fold-this-unfold-all)"
  ;; Interactive, with a prefix argument
  (interactive "P")
  (save-restriction
    (widen)
    (save-excursion
      (pm-map-over-spans
       'rmd-fold-block (point-min)
       ;; adjust this point to fold prior regions
       (if arg (point) (point-max))))))

It was cobbled together without much understanding, based on a stack-overflow answer about evaluating polymode regions. I'm sure there may be a sleeker solution, but it seems to work at the moment!

So, I'm personally sorted (and happy to close the issue if you like), though I still think this would make a great 'native' feature for polymode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment