diff options
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 61 |
1 files changed, 14 insertions, 47 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index e9f3dafa2f0..15dc73daba4 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -296,9 +296,7 @@ Used for syntactic keywords. N is the match number (1, 2 or 3)." | |||
| 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"] |
| @@ -2202,39 +2200,24 @@ Interactively, prompt for name." | |||
| 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) |
| @@ -2327,13 +2310,14 @@ 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 |
| @@ -2397,22 +2381,6 @@ without confirmation." | |||
| 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 | 2383 | ||
| 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 | |||
| 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. |
| @@ -2503,7 +2471,6 @@ with skeleton expansions for compound statement templates. | |||
| 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 |