aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2012-05-17 00:03:05 -0300
committerFabián Ezequiel Gallina2012-05-17 00:03:05 -0300
commite2803784cfbd622a60874f3a1d20a0e0decd7161 (patch)
tree068344525fec4af7ae73aa7b5ba16e4d05f5f135 /lisp
parent67845102b2dc195067a6048631a8319d735497a3 (diff)
downloademacs-e2803784cfbd622a60874f3a1d20a0e0decd7161.tar.gz
emacs-e2803784cfbd622a60874f3a1d20a0e0decd7161.zip
Implemented Skeletons after GNU/Emacs python.el
6 basic skeletons are defined: class, def, for, if, try and while. While these skeletons are strongly based on GNU/Emacs' current python.el a better definition macro, a generic template for try/except/finally/else blocks and a cool menu display is included.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/progmodes/python.el158
1 files changed, 156 insertions, 2 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 49b351cc17c..fa00faf1502 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -68,6 +68,13 @@
68;; the shell completion in background so you should run 68;; the shell completion in background so you should run
69;; `python-shell-send-buffer' from time to time to get better results. 69;; `python-shell-send-buffer' from time to time to get better results.
70 70
71;; Skeletons: 6 skeletons are provided for simple inserting of class,
72;; def, for, if, try and while. These skeletons are integrated with
73;; dabbrev. If you have `dabbrev-mode' activated and
74;; `python-skeleton-autoinsert' is set to t, then whenever you type
75;; the name of any of those defined and hit SPC, they will be
76;; automatically expanded.
77
71;; FFAP: You can find the filename for a given module when using ffap 78;; FFAP: You can find the filename for a given module when using ffap
72;; out of the box. This feature needs an inferior python shell 79;; out of the box. This feature needs an inferior python shell
73;; running. 80;; running.
@@ -110,8 +117,6 @@
110 117
111;; Review code and cleanup 118;; Review code and cleanup
112 119
113;; (Perhaps) some skeletons (I never use them because of yasnippet)
114
115;;; Code: 120;;; Code:
116 121
117(require 'comint) 122(require 'comint)
@@ -145,6 +150,13 @@
145 (define-key map "\C-c<" 'python-indent-shift-left) 150 (define-key map "\C-c<" 'python-indent-shift-left)
146 (define-key map "\C-c>" 'python-indent-shift-right) 151 (define-key map "\C-c>" 'python-indent-shift-right)
147 (define-key map ":" 'python-indent-electric-colon) 152 (define-key map ":" 'python-indent-electric-colon)
153 ;; Skeletons
154 (define-key map "\C-c\C-tc" 'python-skeleton-class)
155 (define-key map "\C-c\C-td" 'python-skeleton-def)
156 (define-key map "\C-c\C-tf" 'python-skeleton-for)
157 (define-key map "\C-c\C-ti" 'python-skeleton-if)
158 (define-key map "\C-c\C-tt" 'python-skeleton-try)
159 (define-key map "\C-c\C-tw" 'python-skeleton-while)
148 ;; Shell interaction 160 ;; Shell interaction
149 (define-key map "\C-c\C-s" 'python-shell-send-string) 161 (define-key map "\C-c\C-s" 'python-shell-send-string)
150 (define-key map "\C-c\C-r" 'python-shell-send-region) 162 (define-key map "\C-c\C-r" 'python-shell-send-region)
@@ -176,6 +188,8 @@
176 ["End of def/class" end-of-defun 188 ["End of def/class" end-of-defun
177 :help "Go to end of definition around point"] 189 :help "Go to end of definition around point"]
178 "-" 190 "-"
191 ("Skeletons")
192 "-"
179 ["Start interpreter" run-python 193 ["Start interpreter" run-python
180 :help "Run inferior Python process in a separate buffer"] 194 :help "Run inferior Python process in a separate buffer"]
181 ["Switch to shell" python-shell-switch-to-shell 195 ["Switch to shell" python-shell-switch-to-shell
@@ -1491,6 +1505,138 @@ Optional argument JUSTIFY defines if the paragraph should be justified."
1491 (t t)))) 1505 (t t))))
1492 1506
1493 1507
1508;;; Skeletons
1509
1510(defcustom python-skeleton-autoinsert nil
1511 "Non-nil means template skeletons will be automagically inserted.
1512This happens when pressing \"if<SPACE>\", for example, to prompt for
1513the if condition."
1514 :type 'boolean
1515 :group 'python)
1516
1517(defvar python-skeleton-available '()
1518 "Internal list of available skeletons.")
1519(make-variable-buffer-local 'inferior-python-mode-current-file)
1520
1521(define-abbrev-table 'python-mode-abbrev-table ()
1522 "Abbrev table for Python mode."
1523 :case-fixed t
1524 ;; Allow / inside abbrevs.
1525 :regexp "\\(?:^\\|[^/]\\)\\<\\([[:word:]/]+\\)\\W*"
1526 ;; Only expand in code.
1527 :enable-function (lambda ()
1528 (message "ppss %s" (not (nth 8 (syntax-ppss))))
1529 (message "autoinsert %s" python-skeleton-autoinsert)
1530 (and
1531 (not (nth 8 (syntax-ppss)))
1532 python-skeleton-autoinsert)))
1533
1534(defmacro python-skeleton-define (name doc &rest skel)
1535 "Define a `python-mode' skeleton using NAME DOC and SKEL.
1536The skeleton will be bound to python-skeleton-NAME and will
1537be added to `python-mode-abbrev-table'."
1538 (let* ((name (symbol-name name))
1539 (function-name (intern (concat "python-skeleton-" name))))
1540 (define-abbrev python-mode-abbrev-table name "" function-name)
1541 (setq python-skeleton-available
1542 (cons function-name python-skeleton-available))
1543 `(define-skeleton ,function-name
1544 ,(or doc
1545 (format "Insert %s statement." name))
1546 ,@skel)))
1547
1548(put 'python-skeleton-define 'lisp-indent-function 2)
1549
1550(defmacro python-define-auxiliary-skeleton (name doc &optional &rest skel)
1551 "Define a `python-mode' auxiliary skeleton using NAME DOC and SKEL.
1552The skeleton will be bound to python-skeleton-NAME."
1553 (let* ((name (symbol-name name))
1554 (function-name (intern (concat "python-skeleton--" name)))
1555 (msg (format
1556 "Add '%s' clause? " name)))
1557 (when (not skel)
1558 (setq skel
1559 `(< ,(format "%s:" name) \n \n
1560 > _ \n)))
1561 `(define-skeleton ,function-name
1562 ,(or doc
1563 (format "Auxiliary skeleton for %s statement." name))
1564 nil
1565 (unless (y-or-n-p ,msg)
1566 (signal 'quit t))
1567 ,@skel)))
1568(put 'python-define-auxiliary-skeleton 'lisp-indent-function 2)
1569
1570(python-define-auxiliary-skeleton else nil)
1571
1572(python-define-auxiliary-skeleton except nil)
1573
1574(python-define-auxiliary-skeleton finally nil)
1575
1576(python-skeleton-define if nil
1577 "Condition: "
1578 "if " str ":" \n
1579 _ \n
1580 ("other condition, %s: "
1581 <
1582 "elif " str ":" \n
1583 > _ \n nil)
1584 '(python-skeleton--else) | ^)
1585
1586(python-skeleton-define while nil
1587 "Condition: "
1588 "while " str ":" \n
1589 > _ \n
1590 '(python-skeleton--else) | ^)
1591
1592(python-skeleton-define for nil
1593 "Iteration spec: "
1594 "for " str ":" \n
1595 > _ \n
1596 '(python-skeleton--else) | ^)
1597
1598(python-skeleton-define try nil
1599 nil
1600 "try:" \n
1601 > _ \n
1602 ("Exception, %s: "
1603 <
1604 "except " str ":" \n
1605 > _ \n nil)
1606 resume:
1607 '(python-skeleton--except)
1608 '(python-skeleton--else)
1609 '(python-skeleton--finally) | ^)
1610
1611(python-skeleton-define def nil
1612 "Function name: "
1613 "def " str " (" ("Parameter, %s: "
1614 (unless (equal ?\( (char-before)) ", ")
1615 str) "):" \n
1616 "\"\"\"" - "\"\"\"" \n
1617 > _ \n)
1618
1619(python-skeleton-define class nil
1620 "Class name: "
1621 "class " str " (" ("Inheritance, %s: "
1622 (unless (equal ?\( (char-before)) ", ")
1623 str)
1624 & ")" | -2
1625 ":" \n
1626 "\"\"\"" - "\"\"\"" \n
1627 > _ \n)
1628
1629(defun python-skeleton-add-menu-items ()
1630 "Add menu items to Python->Skeletons menu."
1631 (let ((skeletons (sort python-skeleton-available 'string<))
1632 (items))
1633 (dolist (skeleton skeletons)
1634 (easy-menu-add-item
1635 nil '("Python" "Skeletons")
1636 `[,(format
1637 "Insert %s" (caddr (split-string (symbol-name skeleton) "-")))
1638 ,skeleton t]))))
1639
1494;;; FFAP 1640;;; FFAP
1495 1641
1496(defvar python-ffap-setup-code 1642(defvar python-ffap-setup-code
@@ -1822,6 +1968,12 @@ not inside a defun."
1822 (set (make-local-variable 'add-log-current-defun-function) 1968 (set (make-local-variable 'add-log-current-defun-function)
1823 #'python-info-current-defun) 1969 #'python-info-current-defun)
1824 1970
1971 (set (make-local-variable 'skeleton-further-elements)
1972 '((abbrev-mode nil)
1973 (< '(backward-delete-char-untabify (min python-indent-offset
1974 (current-column))))
1975 (^ '(- (1+ (current-indentation))))))
1976
1825 (set (make-local-variable 'eldoc-documentation-function) 1977 (set (make-local-variable 'eldoc-documentation-function)
1826 #'python-eldoc-function) 1978 #'python-eldoc-function)
1827 1979
@@ -1838,6 +1990,8 @@ not inside a defun."
1838 "`outline-level' function for Python mode." 1990 "`outline-level' function for Python mode."
1839 (1+ (/ (current-indentation) python-indent-offset)))) 1991 (1+ (/ (current-indentation) python-indent-offset))))
1840 1992
1993 (python-skeleton-add-menu-items)
1994
1841 (when python-indent-guess-indent-offset 1995 (when python-indent-guess-indent-offset
1842 (python-indent-guess-indent-offset))) 1996 (python-indent-guess-indent-offset)))
1843 1997