1

I am currently trying to make Elpy into my ideal Python IDE, and besides inline images in the console, I am mostly there (I don't need much).

C-c C-d (M-x elpy-doc) brings up documentation for the object at point when editing a .py file, and after some troubleshooting I have gotten this to work, mostly, for imported modules as well as built-in functions. However, it turns out that scipy documentation is written in ReST and requires rst-mode for syntax highlighting. I want to automatically enable rst-mode whenever I open SciPy documentation via elpy-doc, or generally ReST documentation. However, I cannot automate this because the buffer documentation is displayed in is not connected to a file. It does not seem possible to set any options related to elpy-doc to achieve such a thing.

Does this make sense, and if so, is there a way to accomplish it?

2 Answers 2

2

You can advise elpy-doc to achieve this goal. This runs a function after elpy-doc to turn on rst-mode in the Python Doc buffer it creates, but only when the word scipy is in the buffer.

(defun turn-on-rst-mode ()
  (with-current-buffer "*Python Doc*"
    (goto-char (point-min))
    (when (search-forward "scipy")
      (rst-mode))))

(advice-add #'elpy-doc :after #'turn-on-rst-mode)
2
  • Thanks! Marked as answer. How would I go about adding other words to also achieve the same result? Purely out of curiosity, can you provide a regexp to the (when (search-forward line?
    – ygtozc
    Commented Sep 25, 2021 at 0:11
  • 1
    Search-forward looks for a string. Use re-search-forward if you want a regexp. Then you could match other patterns. Commented Sep 25, 2021 at 11:32
1

A quick-and-dirty keyboard macro called pydoc-rst bound to C-x C-k 1 and C-x C-k R for this purpose:

(kmacro-lambda-form [?\C-c ?\C-d ?\C-x ?o ?\M-x ?r ?s ?t ?- ?m ?o ?d ?e return ?\C-x ?o] 0 "%d"))

(global-set-key [24 11 82] 'pydoc-rst)

(global-set-key [24 11 49] 'pydoc-rst)

Let me know if this seems wrong somehow.

4
  • 1
    Is there any reason to use the vector form of the keybindings? I'd write (global-set-key (kbd "C-x C-k 1") 'pydoc-rst).
    – NickD
    Commented Sep 23, 2021 at 16:01
  • 1
    And since it's a toggle, I'd economize on keybindings and define just one of them.
    – NickD
    Commented Sep 23, 2021 at 16:08
  • @NickD I used M-x insert-kbd-macro, which provides keybindings in vector form when given a prefix argument. (Without a prefix argument, it only saves the macro by name.) This is the best method I have found for saving macros with keybindings so far. In retrospect I use C-x C-k 1 more often so I would delete the other one if I knew which line referred to which keybinding.
    – ygtozc
    Commented Sep 23, 2021 at 21:35
  • 1
    Do (string-to-vector (kbd "\C-x C-k 1")) and you'll find out! Basically the integers in the vector are the ASCII codes of the characters in the string, so "1" <--> 49 and "R" <--> 82 (and "C-x" <--> 24 and "C-k" <--> 11).
    – NickD
    Commented Sep 24, 2021 at 0:17

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.