doc in emacs

François Pinard pinard at iro.umontreal.ca
Mon Jan 20 08:39:52 EST 2003


[William]

> Is there an easy way to browse the python manual inside emacs ?

There once was an Info rendering of the Python manuals which I found very
convenient at the time, but I do not know if it is kept up-to-date anymore.

Currently, to browse Python manuals, I use `links' for Web access and gave
myself a few Emacs commands to easily launch it, either within Emacs and
externally.  I prefer using it externally when running Emacs under X, within
Emacs otherwise.  In case some people are interested, let me append my
customisation for this, all crude and raw.  Use at your own risk! :-)

Some people prefer Emacs-w3, yet I often find it slow, and in some
circumstances, uneasy to install.



;;; Début de `etc/emacs/commun/browse-url.el'.

;;;; `Browse-URL' package.

(require 'executable)

;; `h' pour `highlight'.
(global-set-key "\C-cWh" 'goto-address)

(defvar browse-url-browser-function)
(defvar browse-url-netscape-arguments)
(defvar browse-url-xterm-program)
(defvar browse-url-xterm-args)
(defvar browse-url-save-file)
(defvar fp-browse-through)
(defvar fp-browse-with)

(setq browse-url-browser-function 'fp-browse-url
      browse-url-netscape-arguments '("-install")
      browse-url-xterm-program "rxvt"
      browse-url-xterm-args '("-geometry" "80x34" "+sb" "-fn" "10x20")
      browse-url-save-file t
      fp-browse-through (if window-system 'xterm 'emacs)
      fp-browse-with (cond ((executable-find "links") "links")
			   ((executable-find "w3m") "w3m")
			   ((executable-find "lynx") "lynx")
			   ((executable-find "netscape") "netscape")))

(global-set-key "\C-cW." 'browse-url-at-point)
(global-set-key "\C-cWb" 'browse-url-of-buffer)
(global-set-key "\C-cWr" 'browse-url-of-region)
(global-set-key "\C-cWu" 'browse-url)
(global-set-key "\C-cWv" 'browse-url-of-file)

(global-set-key "\C-cWn" 'fp-set-browse-through-netscape)
(global-set-key "\C-cWe" 'fp-set-browse-through-emacs)
(global-set-key "\C-cWx" 'fp-set-browse-through-xterm)
(global-set-key "\C-cWl" 'fp-set-browse-with-links)
(global-set-key "\C-cWw" 'fp-set-browse-with-w3m)
(global-set-key "\C-cWy" 'fp-set-browse-with-lynx)

(defun fp-browse-url-dired-mode-routine ()
  (local-set-key "W" 'browse-url-of-dired-file))
(add-hook 'dired-mode-hook 'fp-browse-url-dired-mode-routine)

(defun fp-set-browse-through-netscape ()
  (interactive)
  (unless window-system
    (error "X est requis."))
  (unless (executable-find "netscape")
    (error "Netscape est requis."))
  (setq fp-browse-with "netscape")
  (global-set-key [S-mouse-2] 'browse-url-at-mouse)
  (global-set-key "\C-cWo" 'browse-url-netscape-reload)
  (autoload 'browse-url-netscape-reload "browse-url" nil t)
  (message "Avec Netscape."))
;; REVOIR: Ce qui suit serait mieux, mais alors, browse-url-netscape est
;; appelé avec ("fichier") plutôt que "fichier", ce qui provoque une erreur.
;; (setq browse-url-browser-function '(("^mailto:" . browse-url-mail)
;;				      ("." . browse-url-netscape)))

(defun fp-set-browse-through-emacs ()
  (interactive)
  (setq fp-browse-through 'emacs)
  (message "À l'intérieur de Emacs."))

(defun fp-set-browse-through-xterm ()
  (interactive)
  (unless window-system
    (error "X est requis."))
  (setq fp-browse-through 'xterm)
  (message "Dans une fenêtre séparée."))

(defun fp-set-browse-with-links ()
  (interactive)
  (unless (executable-find "links")
    (error "Links est requis."))
  (setq fp-browse-with "links")
  (message "Avec Links."))

(defun fp-set-browse-with-lynx ()
  (interactive)
  (unless (executable-find "lynx")
    (error "Lynx est requis."))
  (setq fp-browse-with "lynx")
  (message "Avec Lynx."))

(defun fp-set-browse-with-w3m ()
  (interactive)
  (unless (executable-find "w3m")
    (error "W3m est requis."))
  (setq fp-browse-with "w3m")
  (message "Avec W3m."))

(defun fp-browse-url (url &optional new-window)
  (interactive (browse-url-interactive-arg "URL: "))
  (cond ((string-equal fp-browse-with "netscape")
	 (browse-url-netscape url new-window))
	((eq fp-browse-through 'emacs)
	 (fp-browse-url-through-emacs url new-window))
	((eq fp-browse-through 'xterm)
	 (fp-browse-url-through-xterm url new-window))))

(defun fp-browse-url-netscape-reload ()
  (when (string-equal fp-browse-with "netscape")
    (browse-url-netscape-reload)))
(add-hook 'browse-url-of-file-hook 'fp-browse-url-netscape-reload)

;; --- Browser in an xterm ---

(defun fp-browse-url-through-xterm (url &optional new-window)
  ;; new-window ignored
  "Ask the WWW browser to load URL.
Default to the URL around or before point.  A new process is run in an Xterm
window using the Xterm program named by `browse-url-xterm-program' with
possible additional arguments `browse-url-xterm-args'."
  (apply #'start-process `(,(concat fp-browse-with "-" url) nil
			   ,browse-url-xterm-program , at browse-url-xterm-args
			   "-e" ,fp-browse-with ,url)))

;; --- Browser in an Emacs "term" window ---

(defun fp-browse-url-through-emacs (url &optional new-buffer)
  "Ask the WWW browser to load URL.
Default to the URL around or before point.  With a prefix argument, run
a new browser process in a new buffer.

When called interactively, if variable `browse-url-new-window-flag' is
non-nil, load the document in a new browser in a new term window,
otherwise use any existing one.  A non-nil interactive prefix argument
reverses the effect of `browse-url-new-window-flag'.

When called non-interactively, optional second argument NEW-WINDOW is
used instead of `browse-url-new-window-flag'."
  (let* ((system-uses-terminfo t)       ; browser uses terminfo
	 ;; (term-term-name "vt100") ; ??
	 (buf (get-buffer (concat "*" fp-browse-with "*")))
	 (proc (and buf (get-buffer-process buf)))
	 (n browse-url-lynx-input-attempts))
    (if (and (browse-url-maybe-new-window new-buffer) buf)
	;; Rename away the OLD buffer. This isn't very polite, but
	;; term insists on working in a buffer named *terminal* and would
	;; choke on *terminal*<1>.
	(progn (set-buffer buf)
	       (rename-uniquely)))
    (if (or (browse-url-maybe-new-window new-buffer)
	    (not buf)
	    (not proc)
	    (not (memq (process-status proc) '(run stop))))
	;; start a new browser
	(progn
	  (setq buf
		(apply #'make-term
		       `(,fp-browse-with ,fp-browse-with nil
					 , at browse-url-lynx-emacs-args ,url)))
	  (switch-to-buffer buf)
	  (term-char-mode)
	  (set-process-sentinel
	   (get-buffer-process buf)
	   ;; Don't leave around a dead one (especially because of its
	   ;; munged keymap.)
	   (lambda (process event)
	     (if (not (memq (process-status process) '(run stop)))
		 (let ((buf (process-buffer process)))
		   (if buf (kill-buffer buf)))))))
      ;; send the url to browser in the old buffer
      (let ((win (get-buffer-window buf t)))
	(if win
	    (select-window win)
	  (switch-to-buffer buf)))
      (if (eq (following-char) ?_)
	  (cond ((eq browse-url-lynx-input-field 'warn)
		 (error "Please move out of the input field first"))
		((eq browse-url-lynx-input-field 'avoid)
		 (while (and (eq (following-char) ?_) (> n 0))
		   (term-send-down) ; down arrow
		   (sit-for browse-url-lynx-input-delay))
		 (if (eq (following-char) ?_)
		     (error "Cannot move out of the input field, sorry")))))
      (term-send-string proc (concat "g" ; goto
				     "\C-u" ; kill default url
				     url
				     "\r")))))


;;;; Autres outils.

(defun fp-copy-url-in-file (text)
  (interactive "sTexte: ")
  (let ((config (current-window-configuration))
	(url (thing-at-point 'url))
	(file (read-file-name "Vers: " (expand-file-name "~/etc/web/doc"))))
    (find-file file)
    (recursive-edit)
    (newline-and-indent)
    (insert "<a href=\"" url "\">")
    (newline-and-indent)
    (insert (or text url) "</a>")
    (beginning-of-line)
    (xxml-fill-element nil)
    (set-window-configuration config)))
(global-set-key "\C-cWc" 'fp-copy-url-in-file)

(defun fp-browse-maison ()
  (interactive)
  (browse-url-of-file "/home/pinard/WWW/html/index.html"))
(global-set-key "\C-cWm" 'fp-browse-maison)

(defun fp-browse-altavista (query)
  (interactive "sAltavista query: ")
  (let ((url "http://www.altavista.digital.com/"))
    (browse-url (if (string-match "^[ \t]*$" query)
		    url
		  (concat url "cgi-bin/query?pg=aq&what=web&fmt=.&q="
			  (fp-quote-query query) "&r=&d0=&d1=")))))
(global-set-key "\C-cWa" 'fp-browse-altavista)

(defun infoseek (query)
  (interactive "sQuery: ")
  (let ((url "http://www2.infoseek.com"))
    (browse-url (if (string-match "^[ \t]*$" query)
		    url
		  (concat url "/NS/Titles?qt=" (fp-quote-query query))))))

(defun fp-browse-labs (query)
  (interactive "sQuery: ")
  (let ((url "http://labs.google.com"))	; ou labs1 ?
    (browse-url (if (string-match "^[ \t]*$" query)
		    url
		  ;; /sets?hl=en&q1=Tim+Peters&q2=Fredrik+Lundh&q3=Alex+Martelli&q4=Guido+van+Rossum&q5=&btn=Large+Set
		  (concat url (fp-quote-query query))))))


(defun fp-browse-google (query)
  (interactive "sGoogle query: ")
  (let ((url "http://www.google.com"))
    (browse-url (if (string-match "^[ \t]*$" query)
		    url
		  ;; hl=en
		  ;; hl=en&lr=
		  ;; meta=lr%3Dlang_fr%26hl%3Dfr
		  ;; sourceid=navclient
		  ;; sourceid=opera&num=0&ie=utf-8&oe=utf-8
		  (concat url "/search?q=" (fp-quote-query query))))))
(global-set-key "\C-cWg" 'fp-browse-google)

(defun fp-browse-groups (query)
  (interactive "sQuery: ")
  (let ((url "http://groups.google.com"))
    (browse-url (if (string-match "^[ \t]*$" query)
		    (concat url "/advanced_group_search")
		  ;; hl=en
		  ;; as_q=jython%20release&as_ugroup=comp.lang.python&lr=lang_en&hl=en
		  ;; group=comp.lang.python&hl=en
		  ;; group=comp.lang.python.%2A&q=QueryPerformanceCounter
		  ;; hl=en&lr=&safe=off&group=comp.lang.python
		  ;; hl=en&q=current+function+name+python
		  ;; hl=en&selm=2nd70rwjch.fsf%40zsh.cs.rochester.edu
		  ;; q=font+size+idle&hl=en&lr=&ie=UTF-8&scoring=> r&selm=3a942c61_4%40corp.newsfeeds.com&rnum=1
		  ;; selm=9fkubv0i48%40enews2.newsguy.com
		  (concat url "/groups?q=" (fp-quote-query query))))))

(defun fp-quote-query (text)
  (mapconcat '(lambda (character)
		(cond ((= character ? ) "+")
		      ((or (and (>= character ?a) (<= character ?z))
			   (and (>= character ?A) (<= character ?Z))
			   (and (>= character ?0) (<= character ?9)))
		       (char-to-string character))
		      (t (upcase (format "%%%02x" character)))))
	     query
	     ""))

;;; Fin de `etc/emacs/commun/browse-url.el'.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list