Skip to content

Instantly share code, notes, and snippets.

@dsjt
Last active Oct 15, 2021
Embed
What would you like to do?
hideshow config for python
(require 'hideshow)
(define-key hs-minor-mode-map (kbd "C-^") 'hs-toggle-hiding)
(add-hook 'python-mode-hook 'hs-minor-mode)
(defun display-code-line-counts (ov)
(when (eq 'code (overlay-get ov 'hs))
(overlay-put ov 'display
(format " ... [%d]"
(count-lines (overlay-start ov)
(overlay-end ov))))
(overlay-put ov 'face '(:foreground "yellow green"))))
(setq hs-set-up-overlay 'display-code-line-counts)
(defvar my/py-hide-show-keywords '("class" "def" "elif" "else" "except"
"for" "if" "while" "finally" "try" "with"))
(defun my/replace-hs-special ()
(setq hs-special-modes-alist
(remove-if #'(lambda (x) (equal 'python-mode (car x)))
hs-special-modes-alist))
(push (list
'python-mode
(mapconcat #'(lambda (x) (concat "^\\s-*" x "\\>"))
my/py-hide-show-keywords "\\|")
"^\\s-*"
"#"
#'(lambda (x) (python-nav-end-of-block))
nil)
hs-special-modes-alist)
(hs-grok-mode-type))
(add-hook 'python-mode-hook 'my/replace-hs-special)
@Ynjxsjmh

This comment has been minimized.

Copy link

@Ynjxsjmh Ynjxsjmh commented Jul 4, 2021

I have tried it, but it seems it doesn't work. Any idea?

@dsjt

This comment has been minimized.

Copy link
Owner Author

@dsjt dsjt commented Jul 4, 2021

Currently I use a different code. I modified 11th line at `hs-config-for-python.el' (and typo). Try it.
I have no idea without any information about your situaion such as error message.

@Ynjxsjmh

This comment has been minimized.

Copy link

@Ynjxsjmh Ynjxsjmh commented Jul 4, 2021

Still doesn't work on newly added keyword like if, for etc. It doesn't give any warning or error, it just doesn't fold on those keywords.

The value of hs-special-modes-alist is

image

Here is a workable script

https://github.com/braineo/fate-emacs/blob/93879b65dfa2c6459243d2dc3448c87bb2e86f7c/modules/fate-python.el#L27-L43

After comparison, it seems that the problem is the miss of (hs-grok-mode-type).

@dsjt

This comment has been minimized.

Copy link
Owner Author

@dsjt dsjt commented Jul 4, 2021

It seems correct.
In my environment, python-mode-hook invokes #'hs-minor-mode after #'my/replace-hs-special, so it works.
If not, it needs #'hs-grok-mode-type.

I added it. thx

@Ynjxsjmh

This comment has been minimized.

Copy link

@Ynjxsjmh Ynjxsjmh commented Jul 4, 2021

python-mode-hook invokes #'hs-minor-mode after #'my/replace-hs-special

How do you know this, is there any tool to debug this?

@dsjt

This comment has been minimized.

Copy link
Owner Author

@dsjt dsjt commented Jul 4, 2021

It may not be accurate. I just confirmed that when I swapped the order of (add-hook 'python-mode-hook 'my/replace-hs-special) and (add-hook 'python-mode-hook 'hs-minor-mode), it didn't work.

I didn't use any debugging tools.

@Ynjxsjmh

This comment has been minimized.

Copy link

@Ynjxsjmh Ynjxsjmh commented Jul 4, 2021

That's where the problem lies. I actually didn't config (add-hook 'python-mode-hook 'hs-minor-mode) at all.

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