comparison lisp/emacs-lisp/lisp-mode.el @ 48127:ebfdb0351da6

(lisp-fill-paragraph): Use fill-comment-paragraph.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Sun, 03 Nov 2002 03:43:49 +0000
parents df3f64f68cfe
children 4dd5da1ea3dc
comparison
equal deleted inserted replaced
48126:92c696f8509e 48127:ebfdb0351da6
1058 "Like \\[fill-paragraph], but handle Emacs Lisp comments. 1058 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
1059 If any of the current line is a comment, fill the comment or the 1059 If any of the current line is a comment, fill the comment or the
1060 paragraph of it that point is in, preserving the comment's indentation 1060 paragraph of it that point is in, preserving the comment's indentation
1061 and initial semicolons." 1061 and initial semicolons."
1062 (interactive "P") 1062 (interactive "P")
1063 (let ( 1063 (or (fill-comment-paragraph justify)
1064 ;; Non-nil if the current line contains a comment. 1064 ;; `paragraph-start' is set here (not in the buffer-local
1065 has-comment 1065 ;; variable so that `forward-paragraph' et al work as
1066 1066 ;; expected) so that filling (doc) strings works sensibly.
1067 ;; Non-nil if the current line contains code and a comment. 1067 ;; Adding the opening paren to avoid the following sexp being
1068 has-code-and-comment 1068 ;; filled means that sexps generally aren't filled as normal
1069 1069 ;; text, which is probably sensible. The `;' and `:' stop the
1070 ;; If has-comment, the appropriate fill-prefix for the comment. 1070 ;; filled para at following comment lines and keywords
1071 comment-fill-prefix 1071 ;; (typically in `defcustom').
1072 ) 1072 (let ((paragraph-start (concat paragraph-start
1073 1073 "\\|\\s-*[\(;:\"]"))
1074 ;; Figure out what kind of comment we are looking at. 1074 ;; Avoid filling the first line of docstring.
1075 (save-excursion 1075 (paragraph-separate
1076 (beginning-of-line) 1076 (concat paragraph-separate "\\|\\s-*\".*\\.$")))
1077 (cond 1077 (fill-paragraph justify))
1078 1078 ;; Never return nil.
1079 ;; A line with nothing but a comment on it? 1079 t))
1080 ((looking-at "[ \t]*;[; \t]*")
1081 (setq has-comment t
1082 comment-fill-prefix (match-string 0)))
1083
1084 ;; A line with some code, followed by a comment? Remember that the
1085 ;; semi which starts the comment shouldn't be part of a string or
1086 ;; character.
1087 ((let ((state (syntax-ppss (line-end-position))))
1088 (when (nth 4 state)
1089 (goto-char (nth 8 state))
1090 (looking-at ";+[\t ]*")))
1091 (setq has-comment t has-code-and-comment t)
1092 (setq comment-fill-prefix
1093 (concat (make-string (/ (current-column) tab-width) ?\t)
1094 (make-string (% (current-column) tab-width) ?\ )
1095 (match-string 0))))))
1096
1097 (if (not has-comment)
1098 ;; `paragraph-start' is set here (not in the buffer-local
1099 ;; variable so that `forward-paragraph' et al work as
1100 ;; expected) so that filling (doc) strings works sensibly.
1101 ;; Adding the opening paren to avoid the following sexp being
1102 ;; filled means that sexps generally aren't filled as normal
1103 ;; text, which is probably sensible. The `;' and `:' stop the
1104 ;; filled para at following comment lines and keywords
1105 ;; (typically in `defcustom').
1106 (let ((paragraph-start (concat paragraph-start
1107 "\\|\\s-*[\(;:\"]"))
1108 ;; Avoid filling the first line of docstring.
1109 (paragraph-separate
1110 (concat paragraph-separate "\\|\\s-*\".*\\.$")))
1111 (fill-paragraph justify))
1112
1113 ;; Narrow to include only the comment, and then fill the region.
1114 (save-excursion
1115 (save-restriction
1116 (beginning-of-line)
1117 (narrow-to-region
1118 ;; Find the first line we should include in the region to fill.
1119 (save-excursion
1120 (while (and (zerop (forward-line -1))
1121 (looking-at "[ \t]*;")))
1122 ;; We may have gone too far. Go forward again.
1123 (or (looking-at ".*;")
1124 (forward-line 1))
1125 (point))
1126 ;; Find the beginning of the first line past the region to fill.
1127 (save-excursion
1128 (while (progn (forward-line 1)
1129 (looking-at "[ \t]*;")))
1130 (point)))
1131
1132 ;; Lines with only semicolons on them can be paragraph boundaries.
1133 (let* ((paragraph-separate (concat paragraph-separate "\\|[ \t;]*$"))
1134 (paragraph-ignore-fill-prefix nil)
1135 (fill-prefix comment-fill-prefix)
1136 (after-line (if has-code-and-comment
1137 (line-beginning-position 2)))
1138 (end (progn
1139 (forward-paragraph)
1140 (or (bolp) (newline 1))
1141 (point)))
1142 ;; If this comment starts on a line with code,
1143 ;; include that like in the filling.
1144 (beg (progn (backward-paragraph)
1145 (if (eq (point) after-line)
1146 (forward-line -1))
1147 (point))))
1148 (fill-region-as-paragraph beg end
1149 justify nil
1150 (save-excursion
1151 (goto-char beg)
1152 (if (looking-at fill-prefix)
1153 nil
1154 (re-search-forward comment-start-skip))))))))
1155 t))
1156 1080
1157 (defun indent-code-rigidly (start end arg &optional nochange-regexp) 1081 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1158 "Indent all lines of code, starting in the region, sideways by ARG columns. 1082 "Indent all lines of code, starting in the region, sideways by ARG columns.
1159 Does not affect lines starting inside comments or strings, assuming that 1083 Does not affect lines starting inside comments or strings, assuming that
1160 the start of the region is not inside them. 1084 the start of the region is not inside them.