;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
(error nil))))))))
(defun python-comment-line-p ()
- "Return non-nil iff current line has only a comment."
+ "Return non-nil if current line has only a comment."
(save-excursion
(end-of-line)
(when (eq 'comment (syntax-ppss-context (syntax-ppss)))
(looking-at (rx (or (syntax comment-start) line-end))))))
(defun python-blank-line-p ()
- "Return non-nil iff current line is blank."
+ "Return non-nil if current line is blank."
(save-excursion
(beginning-of-line)
(looking-at "\\s-*$")))
"Skip out of any nested brackets.
Skip forward if FORWARD is non-nil, else backward.
If SYNTAX is non-nil it is the state returned by `syntax-ppss' at point.
-Return non-nil iff skipping was done."
+Return non-nil if skipping was done."
(let ((depth (syntax-ppss-depth (or syntax (syntax-ppss))))
(forward (if forward -1 1)))
(unless (zerop depth)
(throw 'done t)))))))
(setq arg (1- arg)))
(zerop arg)))
-\f
+
+(defvar python-which-func-length-limit 40
+ "Non-strict length limit for `python-which-func' output.")
+
+(defun python-which-func ()
+ (let ((function-name (python-current-defun python-which-func-length-limit)))
+ (set-text-properties 0 (length function-name) nil function-name)
+ function-name))
+
+
;;;; Imenu.
(defvar python-recursing)
(define-key map "\C-c\C-l" 'python-load-file)
(define-key map "\C-c\C-v" 'python-check)
;; Note that we _can_ still use these commands which send to the
- ;; Python process even at the prompt iff we have a normal prompt,
+ ;; Python process even at the prompt provided we have a normal prompt,
;; i.e. '>>> ' and not '... '. See the comment before
;; python-send-region. Fixme: uncomment these if we address that.
(path (getenv "PYTHONPATH"))
(process-environment ; to import emacs.py
(cons (concat "PYTHONPATH=" data-directory
- (if path (concat ":" path)))
+ (if path (concat path-separator path)))
process-environment)))
(apply 'make-comint-in-buffer "Python"
(if new (generate-new-buffer "*Python*") "*Python*")
(1+ (/ (current-indentation) python-indent)))
;; Fixme: Consider top-level assignments, imports, &c.
-(defun python-current-defun ()
+(defun python-current-defun (&optional length-limit)
"`add-log-current-defun-function' for Python."
(save-excursion
;; Move up the tree of nested `class' and `def' blocks until we
;; get to zero indentation, accumulating the defined names.
- (let ((start t)
- accum)
- (while (or start (> (current-indentation) 0))
- (setq start nil)
- (python-beginning-of-block)
- (end-of-line)
- (beginning-of-defun)
- (if (looking-at (rx (0+ space) (or "def" "class") (1+ space)
- (group (1+ (or word (syntax symbol))))))
- (push (match-string 1) accum)))
- (if accum (mapconcat 'identity accum ".")))))
+ (let ((accum)
+ (length -1))
+ (catch 'done
+ (while (or (null length-limit)
+ (null (cdr accum))
+ (< length length-limit))
+ (setq start nil)
+ (let ((started-from (point)))
+ (python-beginning-of-block)
+ (end-of-line)
+ (beginning-of-defun)
+ (when (= (point) started-from)
+ (throw 'done nil)))
+ (when (looking-at (rx (0+ space) (or "def" "class") (1+ space)
+ (group (1+ (or word (syntax symbol))))))
+ (push (match-string 1) accum)
+ (setq length (+ length 1 (length (car accum)))))
+ (when (= (current-indentation) 0)
+ (throw 'done nil))))
+ (when accum
+ (when (and length-limit (> length length-limit))
+ (setcar accum ".."))
+ (mapconcat 'identity accum ".")))))
(defun python-mark-block ()
"Mark the block around point.
(interactive)
(let ((window (get-buffer-window "*Completions*")))
(if (and (eq last-command this-command)
- window (window-live-p window) (window-buffer window)
+ (window-live-p window) (window-buffer window)
(buffer-name (window-buffer window)))
(with-current-buffer (window-buffer window)
(if (pos-visible-in-window-p (point-max) window)
(set (make-local-variable 'beginning-of-defun-function)
'python-beginning-of-defun)
(set (make-local-variable 'end-of-defun-function) 'python-end-of-defun)
+ (add-hook 'which-func-functions 'python-which-func nil t)
(setq imenu-create-index-function #'python-imenu-create-index)
(set (make-local-variable 'eldoc-documentation-function)
#'python-eldoc-function)