1

I'm working on a plotly dash app where there are a number of nested blocks of code like the following:

dbc.Row(
        [
            dbc.Col(
                [
                <some long code>
                ]
            ),
            dbc.Col(
                [
                <another piece of code>
                ]
            ),
        ]
    ),

I would like to be able to fold the code between two matching bracket or parentheses symbols. My init file has the following configuration for hs (taken from the hide show emacswiki page) and it does what I want if I switch to R-mode (i.e. fold the code using C-+ when the cursor is at a symbol) . Then, of course, all the python functionalities are off. Is there a way to fold the code between brackets within python mode. Ideally a custom mode/function that I can toggle when working with dash apps. Thanks

(defun toggle-selective-display (column)
(interactive "P")
(set-selective-display
 (or column
     (unless selective-display
       (1+ (current-column))))))

(defun toggle-hiding (column)
(interactive "P")
(if hs-minor-mode
    (if (condition-case nil
            (hs-toggle-hiding)
          (error t))
        (hs-show-all))
  (toggle-selective-display column)))

;; Define Keybindings for hiding/showing code
(global-set-key (kbd "C-+") 'toggle-hiding)
(global-set-key (kbd "C-\\") 'toggle-selective-display)
(add-hook 'ess-r-mode-hook (lambda () (hs-minor-mode 1)))
(add-hook 'python-mode-hook (lambda () (hs-minor-mode 1)))
1
  • I've never gotten Python folding to work the way I want with hideshow or origami.el. When I need it, I reach for marker based folding with folding.el. The only thing I ever got working was folding general code blocks as in emacs.stackexchange.com/a/50144/12713. My only suggestions would be to try the other python mode (melpa.org/#/python-mode) with hideshow or maybe there's a Treesitter based solution out there, though I'm ignorant of the current state of Treesitter and Emacs (other than it's "in" Emacs 29). Hopefully someone else will will have a better answer.
    – nega
    Jul 20, 2023 at 18:54

1 Answer 1

0

It worked out of the box with yafolding. Just enabled it and code can be folded with C-intro even lists with a large number of elements :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.