I would like to use emacs' hide-show mode (to collapse class and function definitions) with Sho Takemori's sage-shell-mode (for the SageMath computer algebra system) which derives from python-mode.

Although emacs does not complain when I do M-x hs-minor-mode, it does not work, in particular, M-x hs-hide-all does not appear to be doing anything.

sage-shell-mode is available at https://github.com/sagemath/sage-shell-mode

  • Please consider filing a bug-report resp. feature-request at the developers site. Jan 27, 2020 at 19:40
  • unfortunately, sage-shell-mode has no active developer - apart from the sagemath community, which lacks emacs experts Jan 28, 2020 at 6:08
  • Seeing there for instance: @fchapoton fchapoton merged commit 1549f72 into sagemath:master on 3 Nov 2019 - so some people seem being around. Still better than providing patches here. Jan 28, 2020 at 9:44
  • Yes fchapoton is one of the sage developers but as far as I know he is not an expert on emacs. In any case he knows about this post, so he would answer if he could. Jan 28, 2020 at 16:25
  • Would it help to activate the hide-show blocks from Python mode in sage-shell-mode? hide-show is a bit dumb in that regard. It does an (assoc major-mode hs-special-modes-alist) and not a (cl-assoc-if (lambda (mode) (derived-mode-p major-mode mode)) hs-special-modes-alist). So, if you want to adopt the python-mode hide-show blocks for sage-shell-mode, we need to copy the entries of the python-mode cons from hs-special-modes-alist to a new entry with sage-shell-mode as car. (A copy is actually not necessary the right cdr link would do.)
    – Tobias
    Jan 30, 2020 at 8:23

1 Answer 1


I already filled a feature request for recognizing derived modes through hideshow.

Until this feature request is handled you can use the following workaround in your init file:

(defun my-sage-initialize-hs ()
  "Initialize `hs-special-mode-alist' for `sage-shell:sage-mode'.
Note: Function `python-mode' must be run at least once to make this work."
  (unless (assoc 'sage-shell:sage-mode hs-special-modes-alist)
    (add-to-list 'hs-special-modes-alist
         (cons 'sage-shell:sage-mode
               (cdr (assoc 'python-mode hs-special-modes-alist)))))

(add-hook 'sage-shell:sage-mode-hook #'my-sage-initialize-hs)

The workaround links the settings for python-mode in hs-special-modes-alist to a new entry for sage-shell-mode.

Tested with Emacs 26.3, and sage-shell-mode-20191103.1040.

When the feature request is accepted and built into hideshow.el you do not need that workaround anymore since sage-shell-mode is really derived from python-mode.

  • Unfortunately, it doesn't seem to have any effect. I also tried replacing sage-shell-mode' with 'sage-shell:sage-mode` in your snippet, because in sage-shell-mode.el it says (define-derived-mode sage-shell:sage-mode python-mode "Sage" (setq sage-shell-edit:-eldoc-orig-func eldoc-documentation-function) (set (make-local-variable 'eldoc-documentation-function) #'sage-shell-edit:eldoc-function) (add-hook 'completion-at-point-functions 'sage-shell-edit:completion-at-point-func nil t)) Jan 30, 2020 at 11:07
  • @MartinRubey Okay I think we get this working... When you are editing a sage shell script what does M-: major-mode say? Did you restart emacs after adding the corrected version to your init file?
    – Tobias
    Jan 30, 2020 at 11:10
  • @MartinRubey Oh, I had to correct "python-mode" to "python". Could you try again with the new version? We can also use the feature name 'python. I've modified the answer in that way.
    – Tobias
    Jan 30, 2020 at 11:20
  • No, it doesn't work with python either (I restarted emacs). M-:major-mode says sage-shell:sage-mode. The output of M-:(cadr hs-special-modes-alist) is a bi suspicious though, it is just (sage-shell:sage-mode), I expected it to be a cons. Jan 30, 2020 at 12:15
  • @MartinRubey The output of (assoc 'sage-shell:sage-mode hs-special-modes-alist) should be a list with sage-shell:sage-mode as first element. Did you switch on hs-minor-mode before experimenting with hiding blocks?
    – Tobias
    Jan 30, 2020 at 12:20

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.