diff options
| author | Alan Mackenzie | 2017-06-15 20:47:11 +0000 |
|---|---|---|
| committer | Alan Mackenzie | 2017-06-15 21:03:03 +0000 |
| commit | 7a2038d7c887e4fa08a91950a7494d1dd20c39e1 (patch) | |
| tree | 3aca6adb27e83c129c9a2f2cebffef55e679fcde /lisp | |
| parent | 21d10e59f89a5bb72829ffb8ebe4463ba4fac124 (diff) | |
| download | emacs-7a2038d7c887e4fa08a91950a7494d1dd20c39e1.tar.gz emacs-7a2038d7c887e4fa08a91950a7494d1dd20c39e1.zip | |
Create a toggle between block and line comments in CC Mode.
Also (unrelated change) initialize the modes' keymaps at each loading.
* lisp/progmodes/cc-cmds.el (c-update-modeline): amend for the new information
on the modeline.
(c-block-comment-flag): New variable.
(c-toggle-comment-style): New function.
* lisp/progmodes/cc-langs.el (c-block-comment-starter)
(c-line-comment-starter): Make them c-lang-defvars.
(c-block-comment-is-default): New c-lang-defvar.
(comment-start, comment-end): Make the default values dependent on
c-block-comment-is-default.
* lisp/progmodes/cc-mode.el (c-mode-base-map): Define C-c C-k in this map.
(c-basic-common-init): Initialize c-block-comment-flag.
(c-mode-map, c++-mode-map, objc-mode-map, java-mode-map, idl-mode-map)
(pike-mode-map, awk-mode-map): Make entries in these key maps each time the
mode is loaded rather than just once per Emacs session.
* doc/misc/cc-mode.texi (Comment Commands): Introduce the notion of comment
style.
(Minor Modes): Define comment style. Describe how comment style influences
the information displayed on the modeline. Document c-toggle-comment-style.
(FAQ): Add a question about toggling the comment style.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/progmodes/cc-cmds.el | 37 | ||||
| -rw-r--r-- | lisp/progmodes/cc-langs.el | 31 | ||||
| -rw-r--r-- | lisp/progmodes/cc-mode.el | 54 |
3 files changed, 79 insertions, 43 deletions
diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el index 9c0798e7529..6250725c179 100644 --- a/lisp/progmodes/cc-cmds.el +++ b/lisp/progmodes/cc-cmds.el | |||
| @@ -252,7 +252,8 @@ With universal argument, inserts the analysis as a comment on that line." | |||
| 252 | 252 | ||
| 253 | ;; Minor mode functions. | 253 | ;; Minor mode functions. |
| 254 | (defun c-update-modeline () | 254 | (defun c-update-modeline () |
| 255 | (let ((fmt (format "/%s%s%s%s" | 255 | (let ((fmt (format "/%s%s%s%s%s" |
| 256 | (if c-block-comment-flag "*" "/") | ||
| 256 | (if c-electric-flag "l" "") | 257 | (if c-electric-flag "l" "") |
| 257 | (if (and c-electric-flag c-auto-newline) | 258 | (if (and c-electric-flag c-auto-newline) |
| 258 | "a" "") | 259 | "a" "") |
| @@ -270,9 +271,6 @@ With universal argument, inserts the analysis as a comment on that line." | |||
| 270 | (bare-mode-name (if (string-match "\\(^[^/]*\\)/" mode-name) | 271 | (bare-mode-name (if (string-match "\\(^[^/]*\\)/" mode-name) |
| 271 | (match-string 1 mode-name) | 272 | (match-string 1 mode-name) |
| 272 | mode-name))) | 273 | mode-name))) |
| 273 | ;; (setq c-submode-indicators | ||
| 274 | ;; (if (> (length fmt) 1) | ||
| 275 | ;; fmt)) | ||
| 276 | (setq mode-name | 274 | (setq mode-name |
| 277 | (if (> (length fmt) 1) | 275 | (if (> (length fmt) 1) |
| 278 | (concat bare-mode-name fmt) | 276 | (concat bare-mode-name fmt) |
| @@ -362,6 +360,37 @@ left out." | |||
| 362 | (electric-indent-local-mode (if c-electric-flag 1 0))) | 360 | (electric-indent-local-mode (if c-electric-flag 1 0))) |
| 363 | (c-keep-region-active)) | 361 | (c-keep-region-active)) |
| 364 | 362 | ||
| 363 | ;; `c-block-comment-flag' gets initialized to the current mode's default in | ||
| 364 | ;; c-basic-common-init. | ||
| 365 | (defvar c-block-comment-flag nil) | ||
| 366 | (make-variable-buffer-local 'c-block-comment-flag) | ||
| 367 | |||
| 368 | (defun c-toggle-comment-style (&optional arg) | ||
| 369 | "Toggle the comment style between block and line comments. | ||
| 370 | Optional numeric ARG, if supplied, switches to block comment | ||
| 371 | style when positive, to line comment style when negative, and | ||
| 372 | just toggles it when zero or left out. | ||
| 373 | |||
| 374 | This action does nothing when the mode only has one comment style." | ||
| 375 | (interactive "P") | ||
| 376 | (setq c-block-comment-flag | ||
| 377 | (cond | ||
| 378 | ((and c-line-comment-starter c-block-comment-starter) | ||
| 379 | (c-calculate-state arg c-block-comment-flag)) | ||
| 380 | (c-line-comment-starter nil) | ||
| 381 | (t t))) | ||
| 382 | (setq comment-start | ||
| 383 | (concat (if c-block-comment-flag | ||
| 384 | c-block-comment-starter | ||
| 385 | c-line-comment-starter) | ||
| 386 | " ")) | ||
| 387 | (setq comment-end | ||
| 388 | (if c-block-comment-flag | ||
| 389 | (concat " " c-block-comment-ender) | ||
| 390 | "")) | ||
| 391 | (c-update-modeline) | ||
| 392 | (c-keep-region-active)) | ||
| 393 | |||
| 365 | 394 | ||
| 366 | ;; Electric keys | 395 | ;; Electric keys |
| 367 | 396 | ||
diff --git a/lisp/progmodes/cc-langs.el b/lisp/progmodes/cc-langs.el index 84d4eab75af..a9d5ac34ad4 100644 --- a/lisp/progmodes/cc-langs.el +++ b/lisp/progmodes/cc-langs.el | |||
| @@ -1449,6 +1449,7 @@ comment style. Other stuff like the syntax table must also be set up | |||
| 1449 | properly." | 1449 | properly." |
| 1450 | t "/*" | 1450 | t "/*" |
| 1451 | awk nil) | 1451 | awk nil) |
| 1452 | (c-lang-defvar c-block-comment-starter (c-lang-const c-block-comment-starter)) | ||
| 1452 | 1453 | ||
| 1453 | (c-lang-defconst c-block-comment-ender | 1454 | (c-lang-defconst c-block-comment-ender |
| 1454 | "String that ends block comments, or nil if such don't exist. | 1455 | "String that ends block comments, or nil if such don't exist. |
| @@ -1458,6 +1459,7 @@ comment style. Other stuff like the syntax table must also be set up | |||
| 1458 | properly." | 1459 | properly." |
| 1459 | t "*/" | 1460 | t "*/" |
| 1460 | awk nil) | 1461 | awk nil) |
| 1462 | (c-lang-defvar c-block-comment-ender (c-lang-const c-block-comment-ender)) | ||
| 1461 | 1463 | ||
| 1462 | (c-lang-defconst c-block-comment-ender-regexp | 1464 | (c-lang-defconst c-block-comment-ender-regexp |
| 1463 | ;; Regexp which matches the end of a block comment (if such exists in the | 1465 | ;; Regexp which matches the end of a block comment (if such exists in the |
| @@ -1515,27 +1517,30 @@ properly." | |||
| 1515 | (c-lang-defvar c-doc-comment-start-regexp | 1517 | (c-lang-defvar c-doc-comment-start-regexp |
| 1516 | (c-lang-const c-doc-comment-start-regexp)) | 1518 | (c-lang-const c-doc-comment-start-regexp)) |
| 1517 | 1519 | ||
| 1520 | (c-lang-defconst c-block-comment-is-default | ||
| 1521 | "Non-nil when the default comment style is block comment." | ||
| 1522 | ;; Note to maintainers of derived modes: You are responsible for ensuring | ||
| 1523 | ;; the pertinent `c-block-comment-{starter,ender}' or | ||
| 1524 | ;; `c-line-comment-{starter,ender}' are non-nil. | ||
| 1525 | t nil | ||
| 1526 | c t) | ||
| 1527 | (c-lang-defvar c-block-comment-is-default | ||
| 1528 | (c-lang-const c-block-comment-is-default)) | ||
| 1529 | |||
| 1518 | (c-lang-defconst comment-start | 1530 | (c-lang-defconst comment-start |
| 1519 | "String that starts comments inserted with M-; etc. | 1531 | "String that starts comments inserted with M-; etc. |
| 1520 | `comment-start' is initialized from this." | 1532 | `comment-start' is initialized from this." |
| 1521 | ;; Default: Prefer line comments to block comments, and pad with a space. | 1533 | t (concat |
| 1522 | t (concat (or (c-lang-const c-line-comment-starter) | 1534 | (if (c-lang-const c-block-comment-is-default) |
| 1523 | (c-lang-const c-block-comment-starter)) | 1535 | (c-lang-const c-block-comment-starter) |
| 1524 | " ") | 1536 | (c-lang-const c-line-comment-starter)) |
| 1525 | ;; In C we still default to the block comment style since line | 1537 | " ")) |
| 1526 | ;; comments aren't entirely portable. | ||
| 1527 | c "/* ") | ||
| 1528 | (c-lang-setvar comment-start (c-lang-const comment-start)) | 1538 | (c-lang-setvar comment-start (c-lang-const comment-start)) |
| 1529 | 1539 | ||
| 1530 | (c-lang-defconst comment-end | 1540 | (c-lang-defconst comment-end |
| 1531 | "String that ends comments inserted with M-; etc. | 1541 | "String that ends comments inserted with M-; etc. |
| 1532 | `comment-end' is initialized from this." | 1542 | `comment-end' is initialized from this." |
| 1533 | ;; Default: Use block comment style if comment-start uses block | 1543 | t (if (c-lang-const c-block-comment-is-default) |
| 1534 | ;; comments, and pad with a space in that case. | ||
| 1535 | t (if (string-match (concat "\\`\\(" | ||
| 1536 | (c-lang-const c-block-comment-start-regexp) | ||
| 1537 | "\\)") | ||
| 1538 | (c-lang-const comment-start)) | ||
| 1539 | (concat " " (c-lang-const c-block-comment-ender)) | 1544 | (concat " " (c-lang-const c-block-comment-ender)) |
| 1540 | "")) | 1545 | "")) |
| 1541 | (c-lang-setvar comment-end (c-lang-const comment-end)) | 1546 | (c-lang-setvar comment-end (c-lang-const comment-end)) |
diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el index 20c63d4dbe2..a501ebba256 100644 --- a/lisp/progmodes/cc-mode.el +++ b/lisp/progmodes/cc-mode.el | |||
| @@ -398,7 +398,7 @@ control). See \"cc-mode.el\" for more info." | |||
| 398 | ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version) | 398 | ;;(define-key c-mode-base-map "\C-c\C-v" 'c-version) |
| 399 | ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22. | 399 | ;; (define-key c-mode-base-map "\C-c\C-y" 'c-toggle-hungry-state) Commented out by ACM, 2005-11-22. |
| 400 | (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode) | 400 | (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode) |
| 401 | ) | 401 | (define-key c-mode-base-map "\C-c\C-k" 'c-toggle-comment-style)) |
| 402 | 402 | ||
| 403 | ;; We don't require the outline package, but we configure it a bit anyway. | 403 | ;; We don't require the outline package, but we configure it a bit anyway. |
| 404 | (cc-bytecomp-defvar outline-level) | 404 | (cc-bytecomp-defvar outline-level) |
| @@ -547,7 +547,7 @@ that requires a literal mode spec at compile time." | |||
| 547 | (setq yank-handled-properties (remq yank-cat-handler | 547 | (setq yank-handled-properties (remq yank-cat-handler |
| 548 | yank-handled-properties))))) | 548 | yank-handled-properties))))) |
| 549 | 549 | ||
| 550 | ;; For the benefit of adaptive file, which otherwise mis-fills. | 550 | ;; For the benefit of adaptive fill, which otherwise mis-fills. |
| 551 | (setq fill-paragraph-handle-comment nil) | 551 | (setq fill-paragraph-handle-comment nil) |
| 552 | 552 | ||
| 553 | ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a | 553 | ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a |
| @@ -623,6 +623,8 @@ that requires a literal mode spec at compile time." | |||
| 623 | 623 | ||
| 624 | ;; setup the comment indent variable in a Emacs version portable way | 624 | ;; setup the comment indent variable in a Emacs version portable way |
| 625 | (set (make-local-variable 'comment-indent-function) 'c-comment-indent) | 625 | (set (make-local-variable 'comment-indent-function) 'c-comment-indent) |
| 626 | ;; What sort of comments are default for M-;? | ||
| 627 | (setq c-block-comment-flag c-block-comment-is-default) | ||
| 626 | 628 | ||
| 627 | ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from | 629 | ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from |
| 628 | ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode' | 630 | ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode' |
| @@ -1621,10 +1623,10 @@ This function is called from `c-common-init', once per mode initialization." | |||
| 1621 | 1623 | ||
| 1622 | (defvar c-mode-map | 1624 | (defvar c-mode-map |
| 1623 | (let ((map (c-make-inherited-keymap))) | 1625 | (let ((map (c-make-inherited-keymap))) |
| 1624 | ;; Add bindings which are only useful for C. | ||
| 1625 | (define-key map "\C-c\C-e" 'c-macro-expand) | ||
| 1626 | map) | 1626 | map) |
| 1627 | "Keymap used in c-mode buffers.") | 1627 | "Keymap used in c-mode buffers.") |
| 1628 | ;; Add bindings which are only useful for C. | ||
| 1629 | (define-key c-mode-map "\C-c\C-e" 'c-macro-expand) | ||
| 1628 | 1630 | ||
| 1629 | 1631 | ||
| 1630 | (easy-menu-define c-c-menu c-mode-map "C Mode Commands" | 1632 | (easy-menu-define c-c-menu c-mode-map "C Mode Commands" |
| @@ -1737,13 +1739,13 @@ the code is C or C++ and based on that chooses whether to enable | |||
| 1737 | 1739 | ||
| 1738 | (defvar c++-mode-map | 1740 | (defvar c++-mode-map |
| 1739 | (let ((map (c-make-inherited-keymap))) | 1741 | (let ((map (c-make-inherited-keymap))) |
| 1740 | ;; Add bindings which are only useful for C++. | ||
| 1741 | (define-key map "\C-c\C-e" 'c-macro-expand) | ||
| 1742 | (define-key map "\C-c:" 'c-scope-operator) | ||
| 1743 | (define-key map "<" 'c-electric-lt-gt) | ||
| 1744 | (define-key map ">" 'c-electric-lt-gt) | ||
| 1745 | map) | 1742 | map) |
| 1746 | "Keymap used in c++-mode buffers.") | 1743 | "Keymap used in c++-mode buffers.") |
| 1744 | ;; Add bindings which are only useful for C++. | ||
| 1745 | (define-key c++-mode-map "\C-c\C-e" 'c-macro-expand) | ||
| 1746 | (define-key c++-mode-map "\C-c:" 'c-scope-operator) | ||
| 1747 | (define-key c++-mode-map "<" 'c-electric-lt-gt) | ||
| 1748 | (define-key c++-mode-map ">" 'c-electric-lt-gt) | ||
| 1747 | 1749 | ||
| 1748 | (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands" | 1750 | (easy-menu-define c-c++-menu c++-mode-map "C++ Mode Commands" |
| 1749 | (cons "C++" (c-lang-const c-mode-menu c++))) | 1751 | (cons "C++" (c-lang-const c-mode-menu c++))) |
| @@ -1789,10 +1791,10 @@ Key bindings: | |||
| 1789 | 1791 | ||
| 1790 | (defvar objc-mode-map | 1792 | (defvar objc-mode-map |
| 1791 | (let ((map (c-make-inherited-keymap))) | 1793 | (let ((map (c-make-inherited-keymap))) |
| 1792 | ;; Add bindings which are only useful for Objective-C. | ||
| 1793 | (define-key map "\C-c\C-e" 'c-macro-expand) | ||
| 1794 | map) | 1794 | map) |
| 1795 | "Keymap used in objc-mode buffers.") | 1795 | "Keymap used in objc-mode buffers.") |
| 1796 | ;; Add bindings which are only useful for Objective-C. | ||
| 1797 | (define-key objc-mode-map "\C-c\C-e" 'c-macro-expand) | ||
| 1796 | 1798 | ||
| 1797 | (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands" | 1799 | (easy-menu-define c-objc-menu objc-mode-map "ObjC Mode Commands" |
| 1798 | (cons "ObjC" (c-lang-const c-mode-menu objc))) | 1800 | (cons "ObjC" (c-lang-const c-mode-menu objc))) |
| @@ -1842,9 +1844,9 @@ Key bindings: | |||
| 1842 | 1844 | ||
| 1843 | (defvar java-mode-map | 1845 | (defvar java-mode-map |
| 1844 | (let ((map (c-make-inherited-keymap))) | 1846 | (let ((map (c-make-inherited-keymap))) |
| 1845 | ;; Add bindings which are only useful for Java. | ||
| 1846 | map) | 1847 | map) |
| 1847 | "Keymap used in java-mode buffers.") | 1848 | "Keymap used in java-mode buffers.") |
| 1849 | ;; Add bindings which are only useful for Java. | ||
| 1848 | 1850 | ||
| 1849 | ;; Regexp trying to describe the beginning of a Java top-level | 1851 | ;; Regexp trying to describe the beginning of a Java top-level |
| 1850 | ;; definition. This is not used by CC Mode, nor is it maintained | 1852 | ;; definition. This is not used by CC Mode, nor is it maintained |
| @@ -1895,9 +1897,9 @@ Key bindings: | |||
| 1895 | 1897 | ||
| 1896 | (defvar idl-mode-map | 1898 | (defvar idl-mode-map |
| 1897 | (let ((map (c-make-inherited-keymap))) | 1899 | (let ((map (c-make-inherited-keymap))) |
| 1898 | ;; Add bindings which are only useful for IDL. | ||
| 1899 | map) | 1900 | map) |
| 1900 | "Keymap used in idl-mode buffers.") | 1901 | "Keymap used in idl-mode buffers.") |
| 1902 | ;; Add bindings which are only useful for IDL. | ||
| 1901 | 1903 | ||
| 1902 | (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands" | 1904 | (easy-menu-define c-idl-menu idl-mode-map "IDL Mode Commands" |
| 1903 | (cons "IDL" (c-lang-const c-mode-menu idl))) | 1905 | (cons "IDL" (c-lang-const c-mode-menu idl))) |
| @@ -1942,10 +1944,10 @@ Key bindings: | |||
| 1942 | 1944 | ||
| 1943 | (defvar pike-mode-map | 1945 | (defvar pike-mode-map |
| 1944 | (let ((map (c-make-inherited-keymap))) | 1946 | (let ((map (c-make-inherited-keymap))) |
| 1945 | ;; Additional bindings. | ||
| 1946 | (define-key map "\C-c\C-e" 'c-macro-expand) | ||
| 1947 | map) | 1947 | map) |
| 1948 | "Keymap used in pike-mode buffers.") | 1948 | "Keymap used in pike-mode buffers.") |
| 1949 | ;; Additional bindings. | ||
| 1950 | (define-key pike-mode-map "\C-c\C-e" 'c-macro-expand) | ||
| 1949 | 1951 | ||
| 1950 | (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands" | 1952 | (easy-menu-define c-pike-menu pike-mode-map "Pike Mode Commands" |
| 1951 | (cons "Pike" (c-lang-const c-mode-menu pike))) | 1953 | (cons "Pike" (c-lang-const c-mode-menu pike))) |
| @@ -1994,19 +1996,19 @@ Key bindings: | |||
| 1994 | 1996 | ||
| 1995 | (defvar awk-mode-map | 1997 | (defvar awk-mode-map |
| 1996 | (let ((map (c-make-inherited-keymap))) | 1998 | (let ((map (c-make-inherited-keymap))) |
| 1997 | ;; Add bindings which are only useful for awk. | ||
| 1998 | (define-key map "#" 'self-insert-command);Overrides electric parent binding. | ||
| 1999 | (define-key map "/" 'self-insert-command);Overrides electric parent binding. | ||
| 2000 | (define-key map "*" 'self-insert-command);Overrides electric parent binding. | ||
| 2001 | (define-key map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk. | ||
| 2002 | (define-key map "\C-c\C-p" 'undefined) | ||
| 2003 | (define-key map "\C-c\C-u" 'undefined) | ||
| 2004 | (define-key map "\M-a" 'c-beginning-of-statement) ; 2003/10/7 | ||
| 2005 | (define-key map "\M-e" 'c-end-of-statement) ; 2003/10/7 | ||
| 2006 | (define-key map "\C-\M-a" 'c-awk-beginning-of-defun) | ||
| 2007 | (define-key map "\C-\M-e" 'c-awk-end-of-defun) | ||
| 2008 | map) | 1999 | map) |
| 2009 | "Keymap used in awk-mode buffers.") | 2000 | "Keymap used in awk-mode buffers.") |
| 2001 | ;; Add bindings which are only useful for awk. | ||
| 2002 | (define-key awk-mode-map "#" 'self-insert-command);Overrides electric parent binding. | ||
| 2003 | (define-key awk-mode-map "/" 'self-insert-command);Overrides electric parent binding. | ||
| 2004 | (define-key awk-mode-map "*" 'self-insert-command);Overrides electric parent binding. | ||
| 2005 | (define-key awk-mode-map "\C-c\C-n" 'undefined) ; #if doesn't exist in awk. | ||
| 2006 | (define-key awk-mode-map "\C-c\C-p" 'undefined) | ||
| 2007 | (define-key awk-mode-map "\C-c\C-u" 'undefined) | ||
| 2008 | (define-key awk-mode-map "\M-a" 'c-beginning-of-statement) ; 2003/10/7 | ||
| 2009 | (define-key awk-mode-map "\M-e" 'c-end-of-statement) ; 2003/10/7 | ||
| 2010 | (define-key awk-mode-map "\C-\M-a" 'c-awk-beginning-of-defun) | ||
| 2011 | (define-key awk-mode-map "\C-\M-e" 'c-awk-end-of-defun) | ||
| 2010 | 2012 | ||
| 2011 | (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands" | 2013 | (easy-menu-define c-awk-menu awk-mode-map "AWK Mode Commands" |
| 2012 | (cons "AWK" (c-lang-const c-mode-menu awk))) | 2014 | (cons "AWK" (c-lang-const c-mode-menu awk))) |