diff options
| author | Stefan Monnier | 1999-11-29 01:31:47 +0000 |
|---|---|---|
| committer | Stefan Monnier | 1999-11-29 01:31:47 +0000 |
| commit | e8fe7d390d032b1b6ceaffacdb4ed5234907dd56 (patch) | |
| tree | 13f027affedf07ad5d0dbd8c9531bdbfe3f004fe | |
| parent | 7a0a180a0a512424ed47360bfc9237e70b3ebab4 (diff) | |
| download | emacs-e8fe7d390d032b1b6ceaffacdb4ed5234907dd56.tar.gz emacs-e8fe7d390d032b1b6ceaffacdb4ed5234907dd56.zip | |
(comment-find): New function.
(indent-for-comment, set-comment-column, kill-comment): use it.
| -rw-r--r-- | lisp/newcomment.el | 207 |
1 files changed, 104 insertions, 103 deletions
diff --git a/lisp/newcomment.el b/lisp/newcomment.el index 748330a599c..901c8e3ef2e 100644 --- a/lisp/newcomment.el +++ b/lisp/newcomment.el | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | ;; Author: Stefan Monnier <monnier@cs.yale.edu> | 5 | ;; Author: Stefan Monnier <monnier@cs.yale.edu> |
| 6 | ;; Keywords: comment uncomment | 6 | ;; Keywords: comment uncomment |
| 7 | ;; Version: $Name: $ | 7 | ;; Version: $Name: $ |
| 8 | ;; Revision: $Id: newcomment.el,v 1.2 1999/11/28 21:33:55 monnier Exp $ | 8 | ;; Revision: $Id: newcomment.el,v 1.3 1999/11/29 00:49:18 monnier Exp $ |
| 9 | 9 | ||
| 10 | ;; This program is free software; you can redistribute it and/or modify | 10 | ;; This program is free software; you can redistribute it and/or modify |
| 11 | ;; it under the terms of the GNU General Public License as published by | 11 | ;; it under the terms of the GNU General Public License as published by |
| @@ -27,10 +27,6 @@ | |||
| 27 | 27 | ||
| 28 | ;;; Bugs: | 28 | ;;; Bugs: |
| 29 | 29 | ||
| 30 | ;; - most of the code is not written (just copied from simple.el) | ||
| 31 | ;; - too many other bugs to mention | ||
| 32 | ;; - if the region does not start at the beginning of a line, | ||
| 33 | ;; comment-region will not align the comment markers properly | ||
| 34 | ;; - comment-multi-line already exists with a different meaning | 30 | ;; - comment-multi-line already exists with a different meaning |
| 35 | ;; and is not orthogonal to comment-extra-lines | 31 | ;; and is not orthogonal to comment-extra-lines |
| 36 | 32 | ||
| @@ -42,6 +38,7 @@ | |||
| 42 | ;; - uncomment-region with a numeric argument | 38 | ;; - uncomment-region with a numeric argument |
| 43 | ;; - uncomment-region with a consp (for blocks) or somehow make the | 39 | ;; - uncomment-region with a consp (for blocks) or somehow make the |
| 44 | ;; deletion of continuation markers less dangerous | 40 | ;; deletion of continuation markers less dangerous |
| 41 | ;; - fix set-comment-column to not use comment-start-skip | ||
| 45 | 42 | ||
| 46 | ;;; Code: | 43 | ;;; Code: |
| 47 | 44 | ||
| @@ -104,6 +101,16 @@ If nil, use `comment-end' instead." | |||
| 104 | string) | 101 | string) |
| 105 | :group 'fill-comments) | 102 | :group 'fill-comments) |
| 106 | 103 | ||
| 104 | (defun comment-find (&optional limit noerror) | ||
| 105 | "Find a comment start between the point and LIMIT. | ||
| 106 | Moves the point to inside the comment and returns the position of the | ||
| 107 | comment-starter. If no comment is found, moves the point to LIMIT | ||
| 108 | and raises an error or returns nil of NOERROR is non-nil." | ||
| 109 | (let ((s (parse-partial-sexp (point) (or limit (point-max)) nil nil nil t))) | ||
| 110 | (if (and (nth 8 s) (not (nth 3 s))) | ||
| 111 | (nth 8 s) | ||
| 112 | (unless noerror (error "No comment"))))) | ||
| 113 | |||
| 107 | (defun indent-for-comment () | 114 | (defun indent-for-comment () |
| 108 | "Indent this line's comment to comment column, or insert an empty comment." | 115 | "Indent this line's comment to comment column, or insert an empty comment." |
| 109 | (interactive "*") | 116 | (interactive "*") |
| @@ -114,26 +121,15 @@ If nil, use `comment-end' instead." | |||
| 114 | (cond | 121 | (cond |
| 115 | ((null starter) | 122 | ((null starter) |
| 116 | (error "No comment syntax defined")) | 123 | (error "No comment syntax defined")) |
| 117 | ((null comment-start-skip) | ||
| 118 | (error "This mode doesn't define `comment-start-skip'")) | ||
| 119 | (t (let* ((eolpos (save-excursion (end-of-line) (point))) | 124 | (t (let* ((eolpos (save-excursion (end-of-line) (point))) |
| 120 | cpos indent begpos) | 125 | cpos indent begpos) |
| 121 | (beginning-of-line) | 126 | (beginning-of-line) |
| 122 | (if (re-search-forward comment-start-skip eolpos 'move) | 127 | (when (setq begpos (comment-find eolpos t)) |
| 123 | (progn (setq cpos (point-marker)) | 128 | (skip-chars-forward |
| 124 | ;; Find the start of the comment delimiter. | 129 | (concat (buffer-substring (1- (point)) (point)) " \t")) |
| 125 | ;; If there were paren-pairs in comment-start-skip, | 130 | (setq cpos (point-marker)) |
| 126 | ;; position at the end of the first pair. | 131 | (goto-char begpos)) |
| 127 | (if (match-end 1) | 132 | (setq begpos (point)) |
| 128 | (goto-char (match-end 1)) | ||
| 129 | ;; If comment-start-skip matched a string with | ||
| 130 | ;; internal whitespace (not final whitespace) then | ||
| 131 | ;; the delimiter start at the end of that | ||
| 132 | ;; whitespace. Otherwise, it starts at the | ||
| 133 | ;; beginning of what was matched. | ||
| 134 | (skip-syntax-backward " " (match-beginning 0)) | ||
| 135 | (skip-syntax-backward "^ " (match-beginning 0))))) | ||
| 136 | (setq begpos (point)) | ||
| 137 | ;; Compute desired indent. | 133 | ;; Compute desired indent. |
| 138 | (if (= (current-column) | 134 | (if (= (current-column) |
| 139 | (setq indent (if comment-indent-hook | 135 | (setq indent (if comment-indent-hook |
| @@ -160,24 +156,20 @@ With just minus as arg, kill any comment on this line. | |||
| 160 | With any other arg, set comment column to indentation of the previous comment | 156 | With any other arg, set comment column to indentation of the previous comment |
| 161 | and then align or create a comment on this line at that column." | 157 | and then align or create a comment on this line at that column." |
| 162 | (interactive "P") | 158 | (interactive "P") |
| 163 | (if (eq arg '-) | 159 | (cond |
| 164 | (kill-comment nil) | 160 | ((eq arg '-) (kill-comment nil)) |
| 165 | (if arg | 161 | (arg |
| 166 | (progn | 162 | (save-excursion |
| 167 | (save-excursion | 163 | (beginning-of-line) |
| 168 | (beginning-of-line) | 164 | (re-search-backward comment-start-skip) |
| 169 | (re-search-backward comment-start-skip) | 165 | (beginning-of-line) |
| 170 | (beginning-of-line) | 166 | (goto-char (comment-find)) |
| 171 | (re-search-forward comment-start-skip) | ||
| 172 | (goto-char (match-beginning 0)) | ||
| 173 | (setq comment-column (current-column)) | ||
| 174 | (message "Comment column set to %d" comment-column)) | ||
| 175 | (indent-for-comment)) | ||
| 176 | (setq comment-column (current-column)) | 167 | (setq comment-column (current-column)) |
| 168 | (message "Comment column set to %d" comment-column)) | ||
| 169 | (indent-for-comment)) | ||
| 170 | (t (setq comment-column (current-column)) | ||
| 177 | (message "Comment column set to %d" comment-column)))) | 171 | (message "Comment column set to %d" comment-column)))) |
| 178 | 172 | ||
| 179 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 180 | |||
| 181 | (defcustom comment-nested nil | 173 | (defcustom comment-nested nil |
| 182 | "Whether the comments can be nested.") | 174 | "Whether the comments can be nested.") |
| 183 | (defcustom comment-continue nil | 175 | (defcustom comment-continue nil |
| @@ -209,7 +201,7 @@ With prefix ARG, kill comments on that many lines starting with this one." | |||
| 209 | (end-of-line) | 201 | (end-of-line) |
| 210 | (setq endc (point)) | 202 | (setq endc (point)) |
| 211 | (beginning-of-line) | 203 | (beginning-of-line) |
| 212 | (let ((cs (nth 8 (parse-partial-sexp (point) endc nil nil nil t)))) | 204 | (let ((cs (comment-find endc t))) |
| 213 | (when cs | 205 | (when cs |
| 214 | (goto-char cs) | 206 | (goto-char cs) |
| 215 | (skip-syntax-backward " ") | 207 | (skip-syntax-backward " ") |
| @@ -270,74 +262,74 @@ ARG is currently ignored." | |||
| 270 | (setq state (parse-partial-sexp | 262 | (setq state (parse-partial-sexp |
| 271 | (point) end | 263 | (point) end |
| 272 | nil nil nil t)) | 264 | nil nil nil t)) |
| 273 | (setq spt (nth 8 state))) | 265 | (setq spt (nth 8 state)) |
| 274 | (unless (nth 3 state) | 266 | (not (nth 3 state))) |
| 275 | (let* ((stxt (buffer-substring spt (point))) | 267 | (let* ((stxt (buffer-substring spt (point))) |
| 276 | ;; find the end of the comment | 268 | ;; find the end of the comment |
| 277 | (ept (progn | 269 | (ept (progn |
| 278 | (when (nth 8 (parse-partial-sexp | 270 | (when (nth 8 (parse-partial-sexp |
| 279 | (point) (point-max) | 271 | (point) (point-max) |
| 280 | nil nil state 'syntax-table)) | 272 | nil nil state 'syntax-table)) |
| 281 | (error "Can't find the comment end")) | 273 | (error "Can't find the comment end")) |
| 282 | (point-marker))) | 274 | (point-marker))) |
| 283 | ;; find the start of the end-comment | 275 | ;; find the start of the end-comment |
| 284 | (_ (while (save-excursion | 276 | (_ (while (save-excursion |
| 277 | (save-restriction | ||
| 278 | (narrow-to-region (point) ept) | ||
| 285 | (nth 8 | 279 | (nth 8 |
| 286 | (save-restriction | 280 | (parse-partial-sexp (point) ept |
| 287 | (narrow-to-region (point) ept) | 281 | nil nil state)))) |
| 288 | (parse-partial-sexp (point) ept | 282 | (backward-char))) |
| 289 | nil nil state)))) | 283 | (etxt (buffer-substring (point) ept)) |
| 290 | (backward-char))) | 284 | (end-quote-re (comment-end-quote-re etxt "\\\\"))) |
| 291 | (etxt (buffer-substring (point) ept)) | 285 | (save-restriction |
| 292 | (end-quote-re (comment-end-quote-re etxt "\\\\"))) | 286 | (narrow-to-region spt ept) |
| 293 | (save-restriction | 287 | ;; remove the end-comment (and leading padding and such) |
| 294 | (narrow-to-region spt ept) | 288 | (unless (string= "\n" etxt) |
| 295 | ;; remove the end-comment (and leading padding and such) | 289 | (beginning-of-line) |
| 296 | (unless (string= "\n" etxt) | 290 | (re-search-forward (concat "\\(^\\s-*\\|\\(" |
| 297 | (beginning-of-line) | 291 | (regexp-quote comment-padding) |
| 298 | (re-search-forward (concat "\\(^\\s-*\\|\\(" | 292 | "\\)?\\)" |
| 299 | (regexp-quote comment-padding) | 293 | (regexp-quote (substring etxt 0 1)) |
| 300 | "\\)?\\)" | 294 | "+" |
| 301 | (regexp-quote (substring etxt 0 1)) | 295 | (regexp-quote (substring etxt 1)) |
| 302 | "+" | 296 | "\\'")) |
| 303 | (regexp-quote (substring etxt 1)) | 297 | (delete-region (match-beginning 0) (match-end 0))) |
| 304 | "\\'")) | 298 | |
| 305 | (delete-region (match-beginning 0) (match-end 0))) | 299 | ;; remove the comment-start |
| 306 | 300 | (goto-char (point-min)) | |
| 307 | ;; remove the comment-start | 301 | (looking-at (concat (regexp-quote stxt) |
| 302 | "+\\(\\s-*$\\|" | ||
| 303 | (regexp-quote comment-padding) | ||
| 304 | "\\)")) | ||
| 305 | (delete-region (match-beginning 0) (match-end 0)) | ||
| 306 | |||
| 307 | ;; unquote any nested end-comment | ||
| 308 | (when end-quote-re | ||
| 308 | (goto-char (point-min)) | 309 | (goto-char (point-min)) |
| 309 | (looking-at (concat (regexp-quote stxt) | 310 | (while (re-search-forward end-quote-re nil t) |
| 310 | "+\\(\\s-*$\\|" | 311 | (delete-region (match-beginning 1) (match-end 1)))) |
| 311 | (regexp-quote comment-padding) | 312 | |
| 312 | "\\)")) | 313 | ;; eliminate continuation markers as well |
| 313 | (delete-region (match-beginning 0) (match-end 0)) | 314 | (let* ((ccs (car comment-continue)) |
| 314 | 315 | (cce (cdr comment-continue)) | |
| 315 | ;; unquote any nested end-comment | 316 | (sre (when (and (stringp ccs) (not (string= "" ccs))) |
| 316 | (when end-quote-re | 317 | (concat |
| 318 | "^\\s-*\\(" (regexp-quote ccs) | ||
| 319 | "+\\(" (regexp-quote comment-padding) | ||
| 320 | "\\)?\\)"))) | ||
| 321 | (ere (when (and (stringp cce) (not (string= "" cce))) | ||
| 322 | (concat | ||
| 323 | "\\(\\(" (regexp-quote comment-padding) | ||
| 324 | "\\)?" (regexp-quote cce) "\\)\\s-*$"))) | ||
| 325 | (re (if (and sre ere) (concat sre "\\|" ere) | ||
| 326 | (or sre ere)))) | ||
| 327 | (when re | ||
| 317 | (goto-char (point-min)) | 328 | (goto-char (point-min)) |
| 318 | (while (re-search-forward end-quote-re nil t) | 329 | (while (re-search-forward re nil t) |
| 319 | (delete-region (match-beginning 1) (match-end 1)))) | 330 | (replace-match "" t t nil (if (match-end 1) 1 3))))) |
| 320 | 331 | ;; go the the end for the next comment | |
| 321 | ;; eliminate continuation markers as well | 332 | (goto-char (point-max)))))))) |
| 322 | (let* ((ccs (car comment-continue)) | ||
| 323 | (cce (cdr comment-continue)) | ||
| 324 | (sre (when (and (stringp ccs) (not (string= "" ccs))) | ||
| 325 | (concat | ||
| 326 | "^\\s-*\\(" (regexp-quote ccs) | ||
| 327 | "+\\(" (regexp-quote comment-padding) | ||
| 328 | "\\)?\\)"))) | ||
| 329 | (ere (when (and (stringp cce) (not (string= "" cce))) | ||
| 330 | (concat | ||
| 331 | "\\(\\(" (regexp-quote comment-padding) | ||
| 332 | "\\)?" (regexp-quote cce) "\\)\\s-*$"))) | ||
| 333 | (re (if (and sre ere) (concat sre "\\|" ere) | ||
| 334 | (or sre ere)))) | ||
| 335 | (when re | ||
| 336 | (goto-char (point-min)) | ||
| 337 | (while (re-search-forward re nil t) | ||
| 338 | (replace-match "" t t nil (if (match-end 1) 1 3))))) | ||
| 339 | ;; go the the end for the next comment | ||
| 340 | (goto-char (point-max))))))))) | ||
| 341 | 333 | ||
| 342 | (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block) | 334 | (defun comment-make-extra-lines (cs ce ccs cce min-indent max-indent &optional block) |
| 343 | (if block | 335 | (if block |
| @@ -536,6 +528,15 @@ The strings used as comment starts are built from | |||
| 536 | 528 | ||
| 537 | ;;; Change Log: | 529 | ;;; Change Log: |
| 538 | ;; $Log: newcomment.el,v $ | 530 | ;; $Log: newcomment.el,v $ |
| 531 | ;; Revision 1.3 1999/11/29 00:49:18 monnier | ||
| 532 | ;; (kill-comment): Fixed by rewriting it with syntax-tables rather than regexps | ||
| 533 | ;; (comment-normalize-vars): Set default (cdr comment-continue) | ||
| 534 | ;; (comment-end-quote-re): new function taken out of `comment-region-internal' | ||
| 535 | ;; (uncomment-region): Rewritten using syntax-tables. Also unquotes | ||
| 536 | ;; nested comment-ends and eliminates continuation markers. | ||
| 537 | ;; (comment-region-internal): Don't create a default for cce. | ||
| 538 | ;; Use `comment-end-quote-re'. | ||
| 539 | ;; | ||
| 539 | ;; Revision 1.2 1999/11/28 21:33:55 monnier | 540 | ;; Revision 1.2 1999/11/28 21:33:55 monnier |
| 540 | ;; (comment-make-extra-lines): Moved out of comment-region-internal. | 541 | ;; (comment-make-extra-lines): Moved out of comment-region-internal. |
| 541 | ;; (comment-with-narrowing): New macro. Provides a way to preserve | 542 | ;; (comment-with-narrowing): New macro. Provides a way to preserve |