diff options
| author | Richard M. Stallman | 1994-02-02 00:05:42 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1994-02-02 00:05:42 +0000 |
| commit | f28039bb00590ee7798d7397473b571a3f977173 (patch) | |
| tree | b9f83a43325e2c0d18d216bd08423af0e5191b23 | |
| parent | d9f082eccae83275db39962ea7bbe958510fcb08 (diff) | |
| download | emacs-f28039bb00590ee7798d7397473b571a3f977173.tar.gz emacs-f28039bb00590ee7798d7397473b571a3f977173.zip | |
(comment-region): Handle comment-end deletion for C-u.
Don't let short lines confuse the comment-end deletion.
(comment-region): Just C-u prefix means uncomment lines.
| -rw-r--r-- | lisp/simple.el | 76 |
1 files changed, 52 insertions, 24 deletions
diff --git a/lisp/simple.el b/lisp/simple.el index fc4ac793eab..738f12e7410 100644 --- a/lisp/simple.el +++ b/lisp/simple.el | |||
| @@ -1800,7 +1800,9 @@ With argument, kill comments on that many lines starting with this one." | |||
| 1800 | (setq count (1- count))))) | 1800 | (setq count (1- count))))) |
| 1801 | 1801 | ||
| 1802 | (defun comment-region (beg end &optional arg) | 1802 | (defun comment-region (beg end &optional arg) |
| 1803 | "Comment the region; third arg numeric means use ARG comment characters. | 1803 | "Comment or uncomment each line in the region. |
| 1804 | With just C-u prefix arg, uncomment each line in region. | ||
| 1805 | Numeric prefix arg ARG means use ARG comment characters. | ||
| 1804 | If ARG is negative, delete that many comment characters instead. | 1806 | If ARG is negative, delete that many comment characters instead. |
| 1805 | Comments are terminated on each line, even for syntax in which newline does | 1807 | Comments are terminated on each line, even for syntax in which newline does |
| 1806 | not end the comment. Blank lines do not get comments." | 1808 | not end the comment. Blank lines do not get comments." |
| @@ -1808,36 +1810,62 @@ not end the comment. Blank lines do not get comments." | |||
| 1808 | ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x | 1810 | ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x |
| 1809 | ;; is easy enough. No option is made here for other than commenting | 1811 | ;; is easy enough. No option is made here for other than commenting |
| 1810 | ;; every line. | 1812 | ;; every line. |
| 1811 | (interactive "r\np") | 1813 | (interactive "r\nP") |
| 1812 | (or comment-start (error "No comment syntax is defined")) | 1814 | (or comment-start (error "No comment syntax is defined")) |
| 1813 | (if (> beg end) (let (mid) (setq mid beg beg end end mid))) | 1815 | (if (> beg end) (let (mid) (setq mid beg beg end end mid))) |
| 1814 | (save-excursion | 1816 | (save-excursion |
| 1815 | (save-restriction | 1817 | (save-restriction |
| 1816 | (let ((cs comment-start) (ce comment-end)) | 1818 | (let ((cs comment-start) (ce comment-end) |
| 1817 | (cond ((not arg) (setq arg 1)) | 1819 | numarg) |
| 1818 | ((> arg 1) | 1820 | (if (consp arg) (setq numarg t) |
| 1819 | (while (> (setq arg (1- arg)) 0) | 1821 | (setq numarg (prefix-numeric-value arg)) |
| 1820 | (setq cs (concat cs comment-start) | 1822 | ;; For positive arg > 1, replicate the comment delims now, |
| 1821 | ce (concat ce comment-end))))) | 1823 | ;; then insert the replicated strings just once. |
| 1824 | (while (> numarg 1) | ||
| 1825 | (setq cs (concat cs comment-start) | ||
| 1826 | ce (concat ce comment-end)) | ||
| 1827 | (setq numarg (1- numarg)))) | ||
| 1828 | ;; Loop over all lines from BEG to END. | ||
| 1822 | (narrow-to-region beg end) | 1829 | (narrow-to-region beg end) |
| 1823 | (goto-char beg) | 1830 | (goto-char beg) |
| 1824 | (while (not (eobp)) | 1831 | (while (not (eobp)) |
| 1825 | (if (< arg 0) | 1832 | (if (or (eq numarg t) (< numarg 0)) |
| 1826 | (let ((count arg)) | 1833 | (progn |
| 1827 | (while (and (> 1 (setq count (1+ count))) | 1834 | ;; Delete comment start from beginning of line. |
| 1828 | (looking-at (regexp-quote cs))) | 1835 | (if (eq numarg t) |
| 1829 | (delete-char (length cs))) | 1836 | (while (looking-at (regexp-quote cs)) |
| 1830 | (if (string= "" ce) () | 1837 | (delete-char (length cs))) |
| 1831 | (setq count arg) | 1838 | (let ((count numarg)) |
| 1832 | (while (> 1 (setq count (1+ count))) | 1839 | (while (and (> 1 (setq count (1+ count))) |
| 1833 | (end-of-line) | 1840 | (looking-at (regexp-quote cs))) |
| 1834 | ;; this is questionable if comment-end ends in whitespace | 1841 | (delete-char (length cs))))) |
| 1835 | ;; that is pretty brain-damaged though | 1842 | ;; Delete comment end from end of line. |
| 1836 | (skip-chars-backward " \t") | 1843 | (if (string= "" ce) |
| 1837 | (backward-char (length ce)) | 1844 | nil |
| 1838 | (if (looking-at (regexp-quote ce)) | 1845 | (if (eq numarg t) |
| 1839 | (delete-char (length ce))))) | 1846 | (progn |
| 1847 | (end-of-line) | ||
| 1848 | ;; This is questionable if comment-end ends in | ||
| 1849 | ;; whitespace. That is pretty brain-damaged, | ||
| 1850 | ;; though. | ||
| 1851 | (skip-chars-backward " \t") | ||
| 1852 | (if (and (>= (- (point) (point-min)) (length ce)) | ||
| 1853 | (save-excursion | ||
| 1854 | (backward-char (length ce)) | ||
| 1855 | (looking-at (regexp-quote ce)))) | ||
| 1856 | (delete-char (- (length ce))))) | ||
| 1857 | (setq count numarg) | ||
| 1858 | (while (> 1 (setq count (1+ count))) | ||
| 1859 | (end-of-line) | ||
| 1860 | ;; this is questionable if comment-end ends in whitespace | ||
| 1861 | ;; that is pretty brain-damaged though | ||
| 1862 | (skip-chars-backward " \t") | ||
| 1863 | (save-excursion | ||
| 1864 | (backward-char (length ce)) | ||
| 1865 | (if (looking-at (regexp-quote ce)) | ||
| 1866 | (delete-char (length ce))))))) | ||
| 1840 | (forward-line 1)) | 1867 | (forward-line 1)) |
| 1868 | ;; Insert at beginning and at end. | ||
| 1841 | (if (looking-at "[ \t]*$") () | 1869 | (if (looking-at "[ \t]*$") () |
| 1842 | (insert cs) | 1870 | (insert cs) |
| 1843 | (if (string= "" ce) () | 1871 | (if (string= "" ce) () |
| @@ -1849,7 +1877,7 @@ not end the comment. Blank lines do not get comments." | |||
| 1849 | "Move backward until encountering the end of a word. | 1877 | "Move backward until encountering the end of a word. |
| 1850 | With argument, do this that many times. | 1878 | With argument, do this that many times. |
| 1851 | In programs, it is faster to call `forward-word' with negative arg." | 1879 | In programs, it is faster to call `forward-word' with negative arg." |
| 1852 | (interactive "p") | 1880 | (INTERACTIVE "p") |
| 1853 | (forward-word (- arg))) | 1881 | (forward-word (- arg))) |
| 1854 | 1882 | ||
| 1855 | (defun mark-word (arg) | 1883 | (defun mark-word (arg) |