# HG changeset patch # User Oleksandr Gavenko # Date 1610924375 -7200 # Node ID 52368d6da1d4829dcca49bb0316df2be0f2c0944 # Parent c1ddee0b6366105fda1842c335d16a9c0489055c Simplified code for displaying Python help. Shadow default C-c C-d key binding with enhanced implementation. Fixed hanging of Python process during initial loading (due to extra code put into python-shell-setup-codes). diff -r c1ddee0b6366 -r 52368d6da1d4 .emacs-my --- a/.emacs-my Sun Jan 17 12:47:26 2021 +0200 +++ b/.emacs-my Mon Jan 18 00:59:35 2021 +0200 @@ -3069,100 +3069,19 @@ (setq python-indent-offset 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.") - -(defun my-python/eldoc-at-point (&optional symbol) +(defun my-python/describe-at-point (symbol process) "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*")) + (interactive (list (python-info-current-symbol) + (python-shell-get-process))) + (let ( (cmd (concat "help('" symbol "')\n")) ) + (switch-to-buffer (get-buffer-create (format "*Python-doc: %s*" symbol))) + (fundamental-mode) (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-min)) - (while (re-search-forward "\\\\'" nil t) - (replace-match "['\"]")) - (goto-char (point-max)) - (when (eq ?' (char-before)) - (delete-char -1)) - (read-only-mode 1) - (goto-char (point-min)))) - -(defvar my-python/eldoc-string-code2 "help(%s); pass" - "Python code used to get a string with the documentation of an object.") - -(defun my-python/eldoc-at-point2 (&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 ( (pyproc (python-shell-get-process)) ) - (switch-to-buffer (get-buffer-create "*Python-doc*")) - (read-only-mode -1) - (buffer-disable-undo) - (erase-buffer) - (insert (python-shell-send-string-no-output (format my-python/eldoc-string-code2 symbol) pyproc)) - (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-min)) - (while (re-search-forward "\\\\'" nil t) - (replace-match "'")) - (goto-char (point-max)) - (when (eq ?' (char-before)) - (delete-char -1)) + (insert (python-shell-send-string-no-output cmd process)) + (setq-local delete-trailing-lines t) + (delete-trailing-whitespace) (read-only-mode 1) (goto-char (point-min)))) @@ -3174,9 +3093,8 @@ ;; 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) - (define-key python-mode-map "\C-c\C-d" 'my-python/eldoc-at-point) - (define-key python-mode-map "\C-c\C-g" 'my-python/eldoc-at-point2) + ;; Shadows primitive built-in `python-describe-at-point'. + (define-key python-mode-map "\C-c\C-d" 'my-python/describe-at-point) (define-key python-mode-map [?\C-c ?\C-h] 'my-python/send-paragraph)) (defgroup my-python nil