Show full docs for symbol at point for Python mode.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Sun, 14 Feb 2016 20:04:20 +0200
changeset 1340 daa03efc12b9
parent 1339 d276e7a0d704
child 1341 227f15d381aa
Show full docs for symbol at point for Python mode.
.emacs-my
--- a/.emacs-my	Sat Feb 13 00:04:34 2016 +0200
+++ b/.emacs-my	Sun Feb 14 20:04:20 2016 +0200
@@ -2745,8 +2745,72 @@
 
 (setq python-indent 4)
 
+(defvar my/python-eldoc-setup-code
+  "def __PYDOC_get_full_help(obj):
+    try:
+        import inspect
+        try:
+            str_type = basestring
+        except NameError:
+            str_type = str
+        if isinstance(obj, str_type):
+            obj = eval(obj, globals())
+        doc = inspect.getdoc(obj)
+        if not doc and callable(obj):
+            target = None
+            if inspect.isclass(obj) and hasattr(obj, '__init__'):
+                target = obj.__init__
+                objtype = 'class'
+            else:
+                target = obj
+                objtype = 'def'
+            if target:
+                args = inspect.formatargspec(
+                    *inspect.getargspec(target)
+                )
+                name = obj.__name__
+                doc = '{objtype} {name}{args}'.format(
+                    objtype=objtype, name=name, args=args
+                )
+    except:
+        doc = ''
+    return doc"
+  "Python code to setup documentation retrieval.")
+
+(defvar my/python-eldoc-string-code "__PYDOC_get_full_help('''%s''')"
+  "Python code used to get a string with the documentation of an object.")
+
 ;; For built-in python.el
 (my--eval-after-load python
+  (add-to-list 'python-shell-setup-codes 'my/python-eldoc-setup-code) ; Used inside (python-shell-send-setup-code)
+  (defun my/python-eldoc-at-point (&optional symbol)
+    "Show full docs for symbol at point."
+    (interactive
+     (let ((symbol (python-info-current-symbol t))
+           (enable-recursive-minibuffers t))
+       (list (read-string (if symbol
+                              (format "Describe symbol (default %s): " symbol)
+                            "Describe symbol: ")
+                          nil nil symbol))))
+    (let ( (python-eldoc-string-code my/python-eldoc-string-code)
+           (python-eldoc-setup-code my/python-eldoc-setup-code) )
+      (switch-to-buffer (get-buffer-create "*Python-doc*"))
+      (read-only-mode -1)
+      (buffer-disable-undo)
+      (erase-buffer)
+      (insert (python-eldoc--get-doc-at-point symbol))
+      (goto-char (point-min))
+      (when (re-search-forward "^u?'" (line-end-position) t)
+        (replace-match ""))
+      (while (re-search-forward "\\\\n" nil t)
+        (replace-match "\n"))
+      (goto-char (point-max))
+      (when (eq ?' (char-before))
+        (delete-char -1))
+      (read-only-mode 1)
+      (goto-char (point-min))))
+  (define-key python-mode-map "\C-c\C-d" 'my/python-eldoc-at-point)
+
   (defun my-python/send-paragraph ()
     (interactive)
     (save-excursion
@@ -2754,6 +2818,7 @@
       (python-shell-send-region (point) (mark))))
   (define-key python-mode-map [?\C-c ?\C-h] 'my-python/send-paragraph))
 
+
 ;; For 3rd party python-mode.el.
 (my--eval-after-load python-mode
   (when (and (boundp 'py-version) (equal py-version "5.1.0"))