.emacs-my
changeset 1340 daa03efc12b9
parent 1339 d276e7a0d704
child 1341 227f15d381aa
equal deleted inserted replaced
1339:d276e7a0d704 1340:daa03efc12b9
  2743 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2743 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2744 (message "python")
  2744 (message "python")
  2745 
  2745 
  2746 (setq python-indent 4)
  2746 (setq python-indent 4)
  2747 
  2747 
       
  2748 (defvar my/python-eldoc-setup-code
       
  2749   "def __PYDOC_get_full_help(obj):
       
  2750     try:
       
  2751         import inspect
       
  2752         try:
       
  2753             str_type = basestring
       
  2754         except NameError:
       
  2755             str_type = str
       
  2756         if isinstance(obj, str_type):
       
  2757             obj = eval(obj, globals())
       
  2758         doc = inspect.getdoc(obj)
       
  2759         if not doc and callable(obj):
       
  2760             target = None
       
  2761             if inspect.isclass(obj) and hasattr(obj, '__init__'):
       
  2762                 target = obj.__init__
       
  2763                 objtype = 'class'
       
  2764             else:
       
  2765                 target = obj
       
  2766                 objtype = 'def'
       
  2767             if target:
       
  2768                 args = inspect.formatargspec(
       
  2769                     *inspect.getargspec(target)
       
  2770                 )
       
  2771                 name = obj.__name__
       
  2772                 doc = '{objtype} {name}{args}'.format(
       
  2773                     objtype=objtype, name=name, args=args
       
  2774                 )
       
  2775     except:
       
  2776         doc = ''
       
  2777     return doc"
       
  2778   "Python code to setup documentation retrieval.")
       
  2779 
       
  2780 (defvar my/python-eldoc-string-code "__PYDOC_get_full_help('''%s''')"
       
  2781   "Python code used to get a string with the documentation of an object.")
       
  2782 
  2748 ;; For built-in python.el
  2783 ;; For built-in python.el
  2749 (my--eval-after-load python
  2784 (my--eval-after-load python
       
  2785   (add-to-list 'python-shell-setup-codes 'my/python-eldoc-setup-code) ; Used inside (python-shell-send-setup-code)
       
  2786   (defun my/python-eldoc-at-point (&optional symbol)
       
  2787     "Show full docs for symbol at point."
       
  2788     (interactive
       
  2789      (let ((symbol (python-info-current-symbol t))
       
  2790            (enable-recursive-minibuffers t))
       
  2791        (list (read-string (if symbol
       
  2792                               (format "Describe symbol (default %s): " symbol)
       
  2793                             "Describe symbol: ")
       
  2794                           nil nil symbol))))
       
  2795     (let ( (python-eldoc-string-code my/python-eldoc-string-code)
       
  2796            (python-eldoc-setup-code my/python-eldoc-setup-code) )
       
  2797       (switch-to-buffer (get-buffer-create "*Python-doc*"))
       
  2798       (read-only-mode -1)
       
  2799       (buffer-disable-undo)
       
  2800       (erase-buffer)
       
  2801       (insert (python-eldoc--get-doc-at-point symbol))
       
  2802       (goto-char (point-min))
       
  2803       (when (re-search-forward "^u?'" (line-end-position) t)
       
  2804         (replace-match ""))
       
  2805       (while (re-search-forward "\\\\n" nil t)
       
  2806         (replace-match "\n"))
       
  2807       (goto-char (point-max))
       
  2808       (when (eq ?' (char-before))
       
  2809         (delete-char -1))
       
  2810       (read-only-mode 1)
       
  2811       (goto-char (point-min))))
       
  2812   (define-key python-mode-map "\C-c\C-d" 'my/python-eldoc-at-point)
       
  2813 
  2750   (defun my-python/send-paragraph ()
  2814   (defun my-python/send-paragraph ()
  2751     (interactive)
  2815     (interactive)
  2752     (save-excursion
  2816     (save-excursion
  2753       (mark-paragraph)
  2817       (mark-paragraph)
  2754       (python-shell-send-region (point) (mark))))
  2818       (python-shell-send-region (point) (mark))))
  2755   (define-key python-mode-map [?\C-c ?\C-h] 'my-python/send-paragraph))
  2819   (define-key python-mode-map [?\C-c ?\C-h] 'my-python/send-paragraph))
       
  2820 
  2756 
  2821 
  2757 ;; For 3rd party python-mode.el.
  2822 ;; For 3rd party python-mode.el.
  2758 (my--eval-after-load python-mode
  2823 (my--eval-after-load python-mode
  2759   (when (and (boundp 'py-version) (equal py-version "5.1.0"))
  2824   (when (and (boundp 'py-version) (equal py-version "5.1.0"))
  2760     ;; (py-toggle-shells 'cpython)
  2825     ;; (py-toggle-shells 'cpython)