comparison lisp/progmodes/python.el @ 54888:89ed55db7532

(python-orig-start-line, python-orig-file): Remove. (python-orig-start): New var. (python-input-filter, python-compilation-line-number) (python-send-region, python-load-file): Use it.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Wed, 14 Apr 2004 20:31:53 +0000
parents 476bf87a815c
children 109b2bf180dd
comparison
equal deleted inserted replaced
54887:a19fbbfac280 54888:89ed55db7532
603 start of buffer." 603 start of buffer."
604 (let ((ci (current-indentation)) 604 (let ((ci (current-indentation))
605 (def-re (rx (and line-start (0+ space) (or "def" "class") 605 (def-re (rx (and line-start (0+ space) (or "def" "class")
606 (1+ space) 606 (1+ space)
607 (group (1+ (or word (syntax symbol))))))) 607 (group (1+ (or word (syntax symbol)))))))
608 point found lep def-line) 608 found lep def-line)
609 (if (python-comment-line-p) 609 (if (python-comment-line-p)
610 (setq ci most-positive-fixnum)) 610 (setq ci most-positive-fixnum))
611 (while (and (not (bobp)) (not found)) 611 (while (and (not (bobp)) (not found))
612 ;; Treat bol at beginning of function as outside function so 612 ;; Treat bol at beginning of function as outside function so
613 ;; that successive C-M-a makes progress backwards. 613 ;; that successive C-M-a makes progress backwards.
820 (setq arg (1- arg))) 820 (setq arg (1- arg)))
821 (zerop arg)) 821 (zerop arg))
822 822
823 ;;;; Imenu. 823 ;;;; Imenu.
824 824
825 (defvar python-recursing)
825 (defun python-imenu-create-index () 826 (defun python-imenu-create-index ()
826 "`imenu-create-index-function' for Python. 827 "`imenu-create-index-function' for Python.
827 828
828 Makes nested Imenu menus from nested `class' and `def' statements. 829 Makes nested Imenu menus from nested `class' and `def' statements.
829 The nested menus are headed by an item referencing the outer 830 The nested menus are headed by an item referencing the outer
830 definition; it has a space prepended to the name so that it sorts 831 definition; it has a space prepended to the name so that it sorts
831 first with `imenu--sort-by-name'." 832 first with `imenu--sort-by-name'."
832 (unless (boundp 'recursing) ; dynamically bound below 833 (unless (boundp 'python-recursing) ; dynamically bound below
833 (goto-char (point-min))) ; normal call from Imenu 834 (goto-char (point-min))) ; normal call from Imenu
834 (let (index-alist ; accumulated value to return 835 (let (index-alist ; accumulated value to return
835 name is-class pos) 836 name)
836 (while (re-search-forward 837 (while (re-search-forward
837 (rx (and line-start (0+ space) ; leading space 838 (rx (and line-start (0+ space) ; leading space
838 (or (group "def") (group "class")) ; type 839 (or (group "def") (group "class")) ; type
839 (1+ space) (group (1+ (or word ?_))))) ; name 840 (1+ space) (group (1+ (or word ?_))))) ; name
840 nil t) 841 nil t)
843 (name (match-string-no-properties 3))) 844 (name (match-string-no-properties 3)))
844 (if (match-beginning 2) ; def or class? 845 (if (match-beginning 2) ; def or class?
845 (setq name (concat "class " name))) 846 (setq name (concat "class " name)))
846 (save-restriction 847 (save-restriction
847 (narrow-to-defun) 848 (narrow-to-defun)
848 (let* ((recursing t) 849 (let* ((python-recursing t)
849 (sublist (python-imenu-create-index))) 850 (sublist (python-imenu-create-index)))
850 (if sublist 851 (if sublist
851 (progn (push (cons (concat " " name) pos) sublist) 852 (progn (push (cons (concat " " name) pos) sublist)
852 (push (cons name sublist) index-alist)) 853 (push (cons name sublist) index-alist))
853 (push (cons name pos) index-alist))))))) 854 (push (cons name pos) index-alist)))))))
1006 "*Input matching this regexp is not saved on the history list. 1007 "*Input matching this regexp is not saved on the history list.
1007 Default ignores all inputs of 0, 1, or 2 non-blank characters." 1008 Default ignores all inputs of 0, 1, or 2 non-blank characters."
1008 :type 'regexp 1009 :type 'regexp
1009 :group 'python) 1010 :group 'python)
1010 1011
1011 (defvar python-orig-start-line nil 1012 (defvar python-orig-start nil
1012 "Line number at start of region sent to inferior Python.") 1013 "Marker to the start of the region passed to the inferior Python.
1013 1014 It can also be a filename.")
1014 (defvar python-orig-file nil
1015 "File name to associate with errors found in inferior Python.")
1016 1015
1017 (defun python-input-filter (str) 1016 (defun python-input-filter (str)
1018 "`comint-input-filter' function for inferior Python. 1017 "`comint-input-filter' function for inferior Python.
1019 Don't save anything for STR matching `inferior-python-filter-regexp'. 1018 Don't save anything for STR matching `inferior-python-filter-regexp'.
1020 Also resets variables for adjusting error messages." 1019 Also resets variables for adjusting error messages."
1021 (setq python-orig-file nil 1020 (setq python-orig-start nil)
1022 python-orig-start-line 1)
1023 (not (string-match inferior-python-filter-regexp str))) 1021 (not (string-match inferior-python-filter-regexp str)))
1024 1022
1025 ;; Fixme: Loses with quoted whitespace. 1023 ;; Fixme: Loses with quoted whitespace.
1026 (defun python-args-to-list (string) 1024 (defun python-args-to-list (string)
1027 (let ((where (string-match "[ \t]" string))) 1025 (let ((where (string-match "[ \t]" string)))
1033 (if pos (python-args-to-list (substring string pos)))))))) 1031 (if pos (python-args-to-list (substring string pos))))))))
1034 1032
1035 (defun python-compilation-line-number (file col) 1033 (defun python-compilation-line-number (file col)
1036 "Return error descriptor of error found for FILE, column COL. 1034 "Return error descriptor of error found for FILE, column COL.
1037 Used as line-number hook function in `python-compilation-regexp-alist'." 1035 Used as line-number hook function in `python-compilation-regexp-alist'."
1038 (let ((line (save-excursion 1036 (let ((line (string-to-number (match-string 2))))
1039 (goto-char (match-beginning 2)) 1037 (cons (point-marker)
1040 (read (current-buffer))))) 1038 (if (and (markerp python-orig-start)
1041 (list (point-marker) (if python-orig-file 1039 (marker-buffer python-orig-start))
1042 (list python-orig-file default-directory) 1040 (with-current-buffer (marker-buffer python-orig-start)
1043 file) 1041 (goto-char python-orig-start)
1044 (+ line (1- python-orig-start-line)) 1042 (forward-line (1- line)))
1045 nil))) 1043 (list (if (stringp python-orig-start) python-orig-start file)
1044 line nil)))))
1046 1045
1047 (defvar python-preoutput-result nil 1046 (defvar python-preoutput-result nil
1048 "Data from output line last `_emacs_out' line seen by the preoutput filter.") 1047 "Data from output line last `_emacs_out' line seen by the preoutput filter.")
1049 1048
1050 (defvar python-preoutput-continuation nil 1049 (defvar python-preoutput-continuation nil
1148 ;; reference to the temporary file to the source. Fixme: 1147 ;; reference to the temporary file to the source. Fixme:
1149 ;; comint-filter the first two lines of the traceback? 1148 ;; comint-filter the first two lines of the traceback?
1150 (interactive "r") 1149 (interactive "r")
1151 (let* ((f (make-temp-file "py")) 1150 (let* ((f (make-temp-file "py"))
1152 (command (format "_emacs_execfile(%S)" f)) 1151 (command (format "_emacs_execfile(%S)" f))
1153 (orig-file (buffer-file-name)) 1152 (orig-start (copy-marker start)))
1154 (orig-line (save-restriction
1155 (widen)
1156 (line-number-at-pos start))))
1157 (if (save-excursion 1153 (if (save-excursion
1158 (goto-char start) 1154 (goto-char start)
1159 (/= 0 (current-indentation))) ; need dummy block 1155 (/= 0 (current-indentation))) ; need dummy block
1160 (write-region "if True:\n" nil f nil 'nomsg)) 1156 (write-region "if True:\n" nil f nil 'nomsg))
1161 (write-region start end f t 'nomsg) 1157 (write-region start end f t 'nomsg)
1162 (when python-buffer 1158 (when python-buffer
1163 (with-current-buffer python-buffer 1159 (with-current-buffer python-buffer
1164 (let ((end (marker-position (process-mark (python-proc))))) 1160 (let ((end (marker-position (process-mark (python-proc)))))
1165 (set (make-local-variable 'python-orig-file) orig-file) 1161 (set (make-local-variable 'python-orig-start) orig-start)
1166 (set (make-local-variable 'python-orig-start-line) orig-line)
1167 (set (make-local-variable 'compilation-error-list) nil) 1162 (set (make-local-variable 'compilation-error-list) nil)
1168 (let ((comint-input-filter-functions 1163 (let ((comint-input-filter-functions
1169 (delete 'python-input-filter comint-input-filter-functions))) 1164 (delete 'python-input-filter comint-input-filter-functions)))
1170 (python-send-string command)) 1165 (python-send-string command))
1171 (set-marker compilation-parsing-end end) 1166 (set-marker compilation-parsing-end end)
1242 (python-send-string 1237 (python-send-string
1243 (if (string-match "\\.py\\'" file-name) 1238 (if (string-match "\\.py\\'" file-name)
1244 ;; Fixme: make sure the directory is in the path list 1239 ;; Fixme: make sure the directory is in the path list
1245 (let ((module (file-name-sans-extension 1240 (let ((module (file-name-sans-extension
1246 (file-name-nondirectory file-name)))) 1241 (file-name-nondirectory file-name))))
1247 (set (make-local-variable 'python-orig-file) nil) 1242 (set (make-local-variable 'python-orig-start) nil)
1248 (set (make-local-variable 'python-orig-start-line) nil)
1249 (format "\ 1243 (format "\
1250 if globals().has_key(%S): reload(%s) 1244 if globals().has_key(%S): reload(%s)
1251 else: import %s 1245 else: import %s
1252 " module module module)) 1246 " module module module))
1253 (set (make-local-variable 'python-orig-file) file-name) 1247 (set (make-local-variable 'python-orig-start) file-name)
1254 (set (make-local-variable 'python-orig-start-line) 1)
1255 (format "execfile('%s')" file-name)))) 1248 (format "execfile('%s')" file-name))))
1256 (set-marker compilation-parsing-end end) 1249 (set-marker compilation-parsing-end end)
1257 (setq compilation-last-buffer (current-buffer)))))) 1250 (setq compilation-last-buffer (current-buffer))))))
1258 1251
1259 ;; Fixme: Should this start a process if there isn't one? (Unlike cmuscheme.) 1252 ;; Fixme: Should this start a process if there isn't one? (Unlike cmuscheme.)
1334 (let* ((version (let ((s (shell-command-to-string (concat python-command 1327 (let* ((version (let ((s (shell-command-to-string (concat python-command
1335 " -V")))) 1328 " -V"))))
1336 (string-match "^Python \\([0-9]+\\.[0-9]+\\>\\)" s) 1329 (string-match "^Python \\([0-9]+\\.[0-9]+\\>\\)" s)
1337 (match-string 1 s))) 1330 (match-string 1 s)))
1338 ;; Whether info files have a Python version suffix, e.g. in Debian. 1331 ;; Whether info files have a Python version suffix, e.g. in Debian.
1339 (versioned 1332 (versioned
1340 (with-temp-buffer 1333 (with-temp-buffer
1341 (Info-mode) 1334 (Info-mode)
1342 (condition-case () 1335 (condition-case ()
1336 ;; Don't use `info' because it would pop-up a *info* buffer.
1343 (Info-goto-node (format "(python%s-lib)Miscellaneous Index" 1337 (Info-goto-node (format "(python%s-lib)Miscellaneous Index"
1344 version)) 1338 version))
1345 (error nil))))) 1339 (error nil)))))
1346 (info-lookup-maybe-add-help 1340 (info-lookup-maybe-add-help
1347 :mode 'python-mode 1341 :mode 'python-mode
1527 1521
1528 ;;;; Modes. 1522 ;;;; Modes.
1529 1523
1530 (defvar outline-heading-end-regexp) 1524 (defvar outline-heading-end-regexp)
1531 (defvar eldoc-print-current-symbol-info-function) 1525 (defvar eldoc-print-current-symbol-info-function)
1532 1526 (defvar python-mode-running)
1533 ;;;###autoload 1527 ;;;###autoload
1534 (define-derived-mode python-mode fundamental-mode "Python" 1528 (define-derived-mode python-mode fundamental-mode "Python"
1535 "Major mode for editing Python files. 1529 "Major mode for editing Python files.
1536 Turns on Font Lock mode unconditionally since it is required for correct 1530 Turns on Font Lock mode unconditionally since it is required for correct
1537 parsing of the source. 1531 parsing of the source.