comparison lisp/progmodes/python.el @ 94139:1e40bf35d15e

(python-mode-map): Use abbrev-table-menu. (python-use-skeletons): Remove, unused. (python-skeletons): Remove. Use the abbrev table instead. (python-mode-abbrev-table): Fix regexp; add enable-function and case-fixed. (def-python-skeleton): Simplify. (python-expand-template): Use the abbrev-table and abbrev-insert. (python-abbrev-pc-hook, python-abbrev-syntax-table, python-pea-hook): Remove. (python-mode): Don't set pre-abbrev-expand-hook.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Thu, 17 Apr 2008 20:09:54 +0000
parents 105ec1146aa7
children b4b1e6b43d64
comparison
equal deleted inserted replaced
94138:b6c3fde83add 94139:1e40bf35d15e
294 :help "Go to end of innermost definition around point"] 294 :help "Go to end of innermost definition around point"]
295 "-" 295 "-"
296 ("Templates..." 296 ("Templates..."
297 :help "Expand templates for compound statements" 297 :help "Expand templates for compound statements"
298 :filter (lambda (&rest junk) 298 :filter (lambda (&rest junk)
299 (mapcar (lambda (elt) 299 (abbrev-table-menu python-mode-abbrev-table)))
300 (vector (car elt) (cdr elt) t))
301 python-skeletons))) ; defined later
302 "-" 300 "-"
303 ["Start interpreter" python-shell 301 ["Start interpreter" python-shell
304 :help "Run `inferior' Python in separate buffer"] 302 :help "Run `inferior' Python in separate buffer"]
305 ["Import/reload file" python-load-file 303 ["Import/reload file" python-load-file
306 :help "Load into inferior Python session"] 304 :help "Load into inferior Python session"]
2200 (when (integerp line) 2198 (when (integerp line)
2201 (goto-line line)))) 2199 (goto-line line))))
2202 2200
2203 ;;;; Skeletons 2201 ;;;; Skeletons
2204 2202
2205 (defcustom python-use-skeletons nil
2206 "Non-nil means template skeletons will be automagically inserted.
2207 This happens when pressing \"if<SPACE>\", for example, to prompt for
2208 the if condition."
2209 :type 'boolean
2210 :group 'python)
2211
2212 (defvar python-skeletons nil
2213 "Alist of named skeletons for Python mode.
2214 Elements are of the form (NAME . EXPANDER-FUNCTION).")
2215
2216 (define-abbrev-table 'python-mode-abbrev-table () 2203 (define-abbrev-table 'python-mode-abbrev-table ()
2217 "Abbrev table for Python mode. 2204 "Abbrev table for Python mode."
2218 The default contents correspond to the elements of `python-skeletons'." 2205 :case-fixed t
2219 ;; Allow / in abbrevs. 2206 ;; Allow / inside abbrevs.
2220 :regexp "\\<\\([[:word:]/]+\\)\\W*") 2207 :regexp "\\(?:^\\|[^/]\\)\\<\\([[:word:]/]+\\)\\W*"
2208 ;; Only expand in code.
2209 :enable-function (lambda () (not (python-in-string/comment))))
2221 2210
2222 (eval-when-compile 2211 (eval-when-compile
2223 ;; Define a user-level skeleton and add it to `python-skeletons' and 2212 ;; Define a user-level skeleton and add it to the abbrev table.
2224 ;; the abbrev table.
2225 (defmacro def-python-skeleton (name &rest elements) 2213 (defmacro def-python-skeleton (name &rest elements)
2226 (let* ((name (symbol-name name)) 2214 (let* ((name (symbol-name name))
2227 (function (intern (concat "python-insert-" name)))) 2215 (function (intern (concat "python-insert-" name))))
2228 `(progn 2216 `(progn
2229 (add-to-list 'python-skeletons ',(cons name function))
2230 ;; Usual technique for inserting a skeleton, but expand 2217 ;; Usual technique for inserting a skeleton, but expand
2231 ;; to the original abbrev instead if in a comment or string. 2218 ;; to the original abbrev instead if in a comment or string.
2232 (define-abbrev python-mode-abbrev-table ,name "" 2219 (define-abbrev python-mode-abbrev-table ,name ""
2233 ;; Quote this to give a readable abbrev table. 2220 ',function
2234 '(lambda ()
2235 (if (python-in-string/comment)
2236 (insert ,name)
2237 (,function)))
2238 nil t) ; system abbrev 2221 nil t) ; system abbrev
2239 (define-skeleton ,function 2222 (define-skeleton ,function
2240 ,(format "Insert Python \"%s\" template." name) 2223 ,(format "Insert Python \"%s\" template." name)
2241 ,@elements))))) 2224 ,@elements)))))
2242 (put 'def-python-skeleton 'lisp-indent-function 2) 2225 (put 'def-python-skeleton 'lisp-indent-function 2)
2325 "Expand template named NAME. 2308 "Expand template named NAME.
2326 Interactively, prompt for the name with completion." 2309 Interactively, prompt for the name with completion."
2327 (interactive 2310 (interactive
2328 (list (completing-read (format "Template to expand (default %s): " 2311 (list (completing-read (format "Template to expand (default %s): "
2329 python-default-template) 2312 python-default-template)
2330 python-skeletons nil t))) 2313 python-mode-abbrev-table nil t nil nil
2314 python-default-template)))
2331 (if (equal "" name) 2315 (if (equal "" name)
2332 (setq name python-default-template) 2316 (setq name python-default-template)
2333 (setq python-default-template name)) 2317 (setq python-default-template name))
2334 (let ((func (cdr (assoc name python-skeletons)))) 2318 (let ((sym (abbrev-symbol name python-mode-abbrev-table)))
2335 (if func 2319 (if sym
2336 (funcall func) 2320 (abbrev-insert sym)
2337 (error "Undefined template: %s" name)))) 2321 (error "Undefined template: %s" name))))
2338 2322
2339 ;;;; Bicycle Repair Man support 2323 ;;;; Bicycle Repair Man support
2340 2324
2341 (autoload 'pymacs-load "pymacs" nil t) 2325 (autoload 'pymacs-load "pymacs" nil t)
2394 (add-hook 'comint-output-filter-functions 'python-pdbtrack-track-stack-file) 2378 (add-hook 'comint-output-filter-functions 'python-pdbtrack-track-stack-file)
2395 2379
2396 (defvar outline-heading-end-regexp) 2380 (defvar outline-heading-end-regexp)
2397 (defvar eldoc-documentation-function) 2381 (defvar eldoc-documentation-function)
2398 (defvar python-mode-running) ;Dynamically scoped var. 2382 (defvar python-mode-running) ;Dynamically scoped var.
2399
2400 ;; Stuff to allow expanding abbrevs with non-word constituents.
2401 (defun python-abbrev-pc-hook ()
2402 "Reset the syntax table after possibly expanding abbrevs."
2403 (remove-hook 'post-command-hook 'python-abbrev-pc-hook t)
2404 (set-syntax-table python-mode-syntax-table))
2405
2406 (defvar python-abbrev-syntax-table
2407 (copy-syntax-table python-mode-syntax-table)
2408 "Syntax table used when expanding abbrevs.")
2409
2410 (defun python-pea-hook ()
2411 "Set the syntax table before possibly expanding abbrevs."
2412 (set-syntax-table python-abbrev-syntax-table)
2413 (add-hook 'post-command-hook 'python-abbrev-pc-hook nil t))
2414 (modify-syntax-entry ?/ "w" python-abbrev-syntax-table)
2415 2383
2416 ;;;###autoload 2384 ;;;###autoload
2417 (define-derived-mode python-mode fundamental-mode "Python" 2385 (define-derived-mode python-mode fundamental-mode "Python"
2418 "Major mode for editing Python files. 2386 "Major mode for editing Python files.
2419 Turns on Font Lock mode unconditionally since it is currently required 2387 Turns on Font Lock mode unconditionally since it is currently required
2501 nil)) 2469 nil))
2502 (set (make-local-variable 'skeleton-further-elements) 2470 (set (make-local-variable 'skeleton-further-elements)
2503 '((< '(backward-delete-char-untabify (min python-indent 2471 '((< '(backward-delete-char-untabify (min python-indent
2504 (current-column)))) 2472 (current-column))))
2505 (^ '(- (1+ (current-indentation)))))) 2473 (^ '(- (1+ (current-indentation))))))
2506 (add-hook 'pre-abbrev-expand-hook 'python-pea-hook nil t)
2507 (if (featurep 'hippie-exp) 2474 (if (featurep 'hippie-exp)
2508 (set (make-local-variable 'hippie-expand-try-functions-list) 2475 (set (make-local-variable 'hippie-expand-try-functions-list)
2509 (cons 'symbol-completion-try-complete 2476 (cons 'symbol-completion-try-complete
2510 hippie-expand-try-functions-list))) 2477 hippie-expand-try-functions-list)))
2511 ;; Python defines TABs as being 8-char wide. 2478 ;; Python defines TABs as being 8-char wide.