[Python-checkins] python/dist/src/Misc python-mode.el,4.10,4.11

bwarsaw@sourceforge.net bwarsaw@sourceforge.net
Mon, 22 Apr 2002 10:15:22 -0700


Update of /cvsroot/python/python/dist/src/Misc
In directory usw-pr-cvs1:/tmp/cvs-serv15084

Modified Files:
	python-mode.el 
Log Message:
Skip Montanaro's contribution (slightly mod'd by Barry) to provide a
"help-on-symbol-at-point" feature which uses pydoc to provide help on
the symbol under point, if available.

Mods include some name changes, a port to Emacs, binding the command
to C-c C-h, and providing a more informative error message if the
symbol's help can't be found (through use of a nasty bare except).

Note also that py-describe-mode has been moved off of C-c C-h m; it's
now just available on C-c ?

Closes SF patch #545439.


Index: python-mode.el
===================================================================
RCS file: /cvsroot/python/python/dist/src/Misc/python-mode.el,v
retrieving revision 4.10
retrieving revision 4.11
diff -C2 -d -r4.10 -r4.11
*** python-mode.el	22 Apr 2002 16:23:29 -0000	4.10
--- python-mode.el	22 Apr 2002 17:15:19 -0000	4.11
***************
*** 499,503 ****
    (define-key py-mode-map "\C-c#"     'py-comment-region)
    (define-key py-mode-map "\C-c?"     'py-describe-mode)
!   (define-key py-mode-map "\C-c\C-hm" 'py-describe-mode)
    (define-key py-mode-map "\e\C-a"    'py-beginning-of-def-or-class)
    (define-key py-mode-map "\e\C-e"    'py-end-of-def-or-class)
--- 499,503 ----
    (define-key py-mode-map "\C-c#"     'py-comment-region)
    (define-key py-mode-map "\C-c?"     'py-describe-mode)
!   (define-key py-mode-map "\C-c\C-h"  'py-help-at-point)
    (define-key py-mode-map "\e\C-a"    'py-beginning-of-def-or-class)
    (define-key py-mode-map "\e\C-e"    'py-end-of-def-or-class)
***************
*** 554,559 ****
  (defvar py-mode-syntax-table nil
    "Syntax table used in `python-mode' buffers.")
! (if py-mode-syntax-table
!     nil
    (setq py-mode-syntax-table (make-syntax-table))
    (modify-syntax-entry ?\( "()" py-mode-syntax-table)
--- 554,558 ----
  (defvar py-mode-syntax-table nil
    "Syntax table used in `python-mode' buffers.")
! (when (not py-mode-syntax-table)
    (setq py-mode-syntax-table (make-syntax-table))
    (modify-syntax-entry ?\( "()" py-mode-syntax-table)
***************
*** 596,603 ****
    )
  
  
  
  ;; Utilities
- 
  (defmacro py-safe (&rest body)
    "Safely execute BODY, return nil if an error occurred."
--- 595,611 ----
    )
  
+ ;; An auxiliary syntax table which places underscore and dot in the
+ ;; symbol class for simplicity
+ (defvar py-dotted-expression-syntax-table nil
+   "Syntax table used to identify Python dotted expressions.")
+ (when (not py-dotted-expression-syntax-table)
+   (setq py-dotted-expression-syntax-table
+ 	(copy-syntax-table py-mode-syntax-table))
+   (modify-syntax-entry ?_ "_" py-dotted-expression-syntax-table)
+   (modify-syntax-entry ?. "_" py-dotted-expression-syntax-table))
+ 
  
  
  ;; Utilities
  (defmacro py-safe (&rest body)
    "Safely execute BODY, return nil if an error occurred."
***************
*** 2599,2602 ****
--- 2607,2652 ----
    (interactive)
    (py-pdbtrack-toggle-stack-tracking 0))
+ 
+ 
+ 
+ ;; Skip's python-help commands. The guts of this function is stolen
+ ;; from XEmacs's symbol-near-point, but without the useless
+ ;; regexp-quote call on the results, nor the interactive bit.  Also,
+ ;; we've added the temporary syntax table setting, which Skip
+ ;; originally had broken out into a separate function.  Note that
+ ;; Emacs doesn't have the original function.
+ (defun py-symbol-near-point ()
+   "Return the first textual item to the nearest point."
+   ;; alg stolen from etag.el
+   (save-excursion
+     (with-syntax-table py-dotted-expression-syntax-table
+       (if (or (bobp) (not (memq (char-syntax (char-before)) '(?w ?_))))
+ 	  (while (not (looking-at "\\sw\\|\\s_\\|\\'"))
+ 	    (forward-char 1)))
+       (while (looking-at "\\sw\\|\\s_")
+ 	(forward-char 1))
+       (if (re-search-backward "\\sw\\|\\s_" nil t)
+ 	  (progn (forward-char 1)
+ 		 (buffer-substring (point)
+ 				   (progn (forward-sexp -1)
+ 					  (while (looking-at "\\s'")
+ 					    (forward-char 1))
+ 					  (point))))
+ 	nil))))
+ 
+ (defun py-help-at-point ()
+   "Get help from Python based on the symbol nearest point."
+   (interactive)
+   (let* ((sym (py-symbol-near-point))
+ 	 (base (substring sym 0 (or (search "." sym :from-end t) 0)))
+ 	 cmd)
+     (if (not (equal base ""))
+         (setq cmd (concat "import " base "\n")))
+     (setq cmd (concat "import pydoc\n"
+                       cmd
+ 		      "try: pydoc.help(" sym ")\n"
+ 		      "except: print 'No help available on:', \"" sym "\""))
+     (message cmd)
+     (py-execute-string cmd)))