2

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. – Andreas Röhler Jan 27 '20 at 19:40
  • unfortunately, sage-shell-mode has no active developer - apart from the sagemath community, which lacks emacs experts – Martin Rubey Jan 28 '20 at 06: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. – Andreas Röhler Jan 28 '20 at 09: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. – Martin Rubey Jan 28 '20 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 '20 at 08:23
  • Note that I am not an expert for Sagemath but I know a bit about Elisp (not as much as the masters here on SE though). And I think you would be very lucky if you found anyone here who is expert on both Sagemath and Elisp. So, you need to help us on the Sagemath side. How does it look like what you want to fold? Give a minimal example. – Tobias Jan 30 '20 at 08:28
  • @Tobias: many thanks for having a look, I'll try it today. sage-shell-mode should really be a thin layer over python mode. It has some functionality to send the buffer (i.e., the python file) to sagemath (which essentially is the python interpreter with a module preloaded), execute specific doctests and the like, – Martin Rubey Jan 30 '20 at 10:38
  • So, yes, I only want to activate the hide-show blocks from python mode. – Martin Rubey Jan 30 '20 at 10:39

1 Answers1

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)))))
  (hs-minor-mode))

(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.

Tobias
  • 32,569
  • 1
  • 34
  • 75
  • 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))` – Martin Rubey Jan 30 '20 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 '20 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 '20 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. – Martin Rubey Jan 30 '20 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 '20 at 12:20
  • @MartinRubey I just installed `sage-shell-mode` in Emacs 26.3. Run the above script opened a python file and run `M-x sage-shell:sage-mode`. Afterwards I activated hideshow with `M-x hs-minor-mode` and voila I was able to hide and show blocks with the items of the `Hide/Show` menu. – Tobias Jan 30 '20 at 12:26
  • It doesn't work here :-(, possibly it is because of emacs being 24.5.1. I'll try at home, where I have an up to date emacs. – Martin Rubey Jan 30 '20 at 14:43
  • just tried with emacs 25.2.2, without too much luck. Curiously, an initial comment block seems to be "hidden" (i.e., collapsed into a single line. Is there any way I could debug this? – Martin Rubey Jan 30 '20 at 16:40
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/103902/discussion-between-tobias-and-martin-rubey). – Tobias Jan 30 '20 at 16:55