.emacs-my
changeset 1702 52368d6da1d4
parent 1701 c1ddee0b6366
child 1703 003cda885eeb
equal deleted inserted replaced
1701:c1ddee0b6366 1702:52368d6da1d4
  3067 (setq python-shell-interpreter "python3")
  3067 (setq python-shell-interpreter "python3")
  3068 (setq python-shell-interpreter-args "-i")
  3068 (setq python-shell-interpreter-args "-i")
  3069 
  3069 
  3070 (setq python-indent-offset 4)
  3070 (setq python-indent-offset 4)
  3071 
  3071 
  3072 (defvar my-python/eldoc-setup-code
  3072 (defun my-python/describe-at-point (symbol process)
  3073   "def __PYDOC_get_full_help(obj):
       
  3074     try:
       
  3075         import inspect
       
  3076         try:
       
  3077             str_type = basestring
       
  3078         except NameError:
       
  3079             str_type = str
       
  3080         if isinstance(obj, str_type):
       
  3081             obj = eval(obj, globals())
       
  3082         doc = inspect.getdoc(obj)
       
  3083         if not doc and callable(obj):
       
  3084             target = None
       
  3085             if inspect.isclass(obj) and hasattr(obj, '__init__'):
       
  3086                 target = obj.__init__
       
  3087                 objtype = 'class'
       
  3088             else:
       
  3089                 target = obj
       
  3090                 objtype = 'def'
       
  3091             if target:
       
  3092                 args = inspect.formatargspec(
       
  3093                     *inspect.getargspec(target)
       
  3094                 )
       
  3095                 name = obj.__name__
       
  3096                 doc = '{objtype} {name}{args}'.format(
       
  3097                     objtype=objtype, name=name, args=args
       
  3098                 )
       
  3099     except:
       
  3100         doc = ''
       
  3101     return doc"
       
  3102   "Python code to setup documentation retrieval.")
       
  3103 
       
  3104 (defvar my-python/eldoc-string-code "__PYDOC_get_full_help('''%s''')"
       
  3105   "Python code used to get a string with the documentation of an object.")
       
  3106 
       
  3107 (defun my-python/eldoc-at-point (&optional symbol)
       
  3108   "Show full docs for symbol at point."
  3073   "Show full docs for symbol at point."
  3109   (interactive
  3074   (interactive (list (python-info-current-symbol)
  3110    (let ((symbol (python-info-current-symbol t))
  3075                      (python-shell-get-process)))
  3111          (enable-recursive-minibuffers t))
  3076   (let ( (cmd (concat "help('" symbol "')\n")) )
  3112      (list (read-string (if symbol
  3077     (switch-to-buffer (get-buffer-create (format "*Python-doc: %s*" symbol)))
  3113                             (format "Describe symbol (default %s): " symbol)
  3078     (fundamental-mode)
  3114                           "Describe symbol: ")
       
  3115                         nil nil symbol))))
       
  3116   (let ( (python-eldoc-string-code my-python/eldoc-string-code)
       
  3117          (python-eldoc-setup-code my-python/eldoc-setup-code) )
       
  3118     (switch-to-buffer (get-buffer-create "*Python-doc*"))
       
  3119     (read-only-mode -1)
  3079     (read-only-mode -1)
  3120     (buffer-disable-undo)
  3080     (buffer-disable-undo)
  3121     (erase-buffer)
  3081     (erase-buffer)
  3122     (insert (python-eldoc--get-doc-at-point symbol))
  3082     (insert (python-shell-send-string-no-output cmd process))
  3123     (goto-char (point-min))
  3083     (setq-local delete-trailing-lines t)
  3124     (when (re-search-forward "^u?['\"]" (line-end-position) t)
  3084     (delete-trailing-whitespace)
  3125       (replace-match ""))
       
  3126     (while (re-search-forward "\\\\n" nil t)
       
  3127       (replace-match "\n"))
       
  3128     (goto-char (point-min))
       
  3129     (while (re-search-forward "\\\\'" nil t)
       
  3130       (replace-match "['\"]"))
       
  3131     (goto-char (point-max))
       
  3132     (when (eq ?' (char-before))
       
  3133       (delete-char -1))
       
  3134     (read-only-mode 1)
       
  3135     (goto-char (point-min))))
       
  3136 
       
  3137 (defvar my-python/eldoc-string-code2 "help(%s); pass"
       
  3138   "Python code used to get a string with the documentation of an object.")
       
  3139 
       
  3140 (defun my-python/eldoc-at-point2 (&optional symbol)
       
  3141   "Show full docs for symbol at point."
       
  3142   (interactive
       
  3143    (let ((symbol (python-info-current-symbol t))
       
  3144          (enable-recursive-minibuffers t))
       
  3145      (list (read-string (if symbol
       
  3146                             (format "Describe symbol (default %s): " symbol)
       
  3147                           "Describe symbol: ")
       
  3148                         nil nil symbol))))
       
  3149   (let ( (pyproc (python-shell-get-process)) )
       
  3150     (switch-to-buffer (get-buffer-create "*Python-doc*"))
       
  3151     (read-only-mode -1)
       
  3152     (buffer-disable-undo)
       
  3153     (erase-buffer)
       
  3154     (insert (python-shell-send-string-no-output (format my-python/eldoc-string-code2 symbol) pyproc))
       
  3155     (goto-char (point-min))
       
  3156     (when (re-search-forward "^u?'" (line-end-position) t)
       
  3157       (replace-match ""))
       
  3158     (while (re-search-forward "\\\\n" nil t)
       
  3159       (replace-match "\n"))
       
  3160     (goto-char (point-min))
       
  3161     (while (re-search-forward "\\\\'" nil t)
       
  3162       (replace-match "'"))
       
  3163     (goto-char (point-max))
       
  3164     (when (eq ?' (char-before))
       
  3165       (delete-char -1))
       
  3166     (read-only-mode 1)
  3085     (read-only-mode 1)
  3167     (goto-char (point-min))))
  3086     (goto-char (point-min))))
  3168 
  3087 
  3169 (defun my-python/send-paragraph ()
  3088 (defun my-python/send-paragraph ()
  3170   (interactive)
  3089   (interactive)
  3172     (mark-paragraph)
  3091     (mark-paragraph)
  3173     (python-shell-send-region (point) (mark))))
  3092     (python-shell-send-region (point) (mark))))
  3174 
  3093 
  3175 ;; For built-in python.el
  3094 ;; For built-in python.el
  3176 (my--eval-after-load python
  3095 (my--eval-after-load python
  3177   ;; (add-to-list 'python-shell-setup-codes 'my-python/eldoc-setup-code) ; Used inside (python-shell-send-setup-code)
  3096   ;; Shadows primitive built-in `python-describe-at-point'.
  3178   (define-key python-mode-map "\C-c\C-d" 'my-python/eldoc-at-point)
  3097   (define-key python-mode-map "\C-c\C-d" 'my-python/describe-at-point)
  3179   (define-key python-mode-map "\C-c\C-g" 'my-python/eldoc-at-point2)
       
  3180   (define-key python-mode-map [?\C-c ?\C-h] 'my-python/send-paragraph))
  3098   (define-key python-mode-map [?\C-c ?\C-h] 'my-python/send-paragraph))
  3181 
  3099 
  3182 (defgroup my-python nil
  3100 (defgroup my-python nil
  3183   "My Python extentions in Emacs."
  3101   "My Python extentions in Emacs."
  3184   :group 'python)
  3102   :group 'python)