From: Alan M. <ac...@mu...> - 2011-11-28 20:34:24
|
Hello Kai and Peter, On Sun, Nov 27, 2011 at 11:46:19AM +0100, Kai Tetzlaff wrote: > Hi Alan, > Alan Mackenzie <ac...@mu...> writes: > > I think I've got a fix (or perhaps a "fix") for the problem. It > > involves a corrected `fill-paragraph' (called > > `c-external-fill-paragraph') being used in place of the standard one. > > The dodgy bit is overwriting the pertinent element of > > filladapt-function-table with this. > > Could you both try this out, please, and let me know how it works. > > If it works OK and you get back to me very soon, I might be able to > > squeeze the patch into Monday's Emacs-24 pretest. :-) > Hmm, i don't see a difference - sorry. Just to make sure we're looking > at the same problem i've attached an archive containing all files > involved (except for cc-mode itself). After unpacking the archive, > i'm calling Sorry, a bit of carelessness in that patch. The case you'd picked up I'd wrongly directed direct to c-external-fill-paragraph, instead of via filladapt's fill-paragraph. I've tested the following one somewhat better. Please try it, too. *** orig/cc-cmds.el 2011-11-25 10:11:36.000000000 +0000 --- cc-cmds.el 2011-11-28 18:31:35.000000000 +0000 *************** *** 4349,4354 **** --- 4349,4453 ---- (set-marker end nil)))) + (defun c-external-fill-paragraph (&optional justify region) + ;; This function is a copy of Emacs 23 and possibly 24's `fill-paragraph' + ;; with erroneous code removed (rather than corrected). See 4. below. + ;; XEmacs and other GNU Emacsen don't use this. + "Fill paragraph at or after point. + + If JUSTIFY is non-nil (interactively, with prefix argument), justify as well. + If `sentence-end-double-space' is non-nil, then period followed by one + space does not end a sentence, so don't break a line there. + The variable `fill-column' controls the width for filling. + + If `fill-paragraph-function' is non-nil, we call it (passing our + argument to it), and if it returns non-nil, we simply return its value. + + If `fill-paragraph-function' is nil, return the `fill-prefix' used for filling. + + The REGION argument is non-nil if called interactively; in that + case, if Transient Mark mode is enabled and the mark is active, + call `fill-region' to fill each of the paragraphs in the active + region, instead of just filling the current paragraph." + (interactive (progn + (barf-if-buffer-read-only) + (list (if current-prefix-arg 'full) t))) + (or + ;; 1. Fill the region if it is active when called interactively. + (and region transient-mark-mode mark-active + (not (eq (region-beginning) (region-end))) + (or (fill-region (region-beginning) (region-end) justify) t)) + ;; 2. Try fill-paragraph-function. + (and (not (eq fill-paragraph-function t)) + (or fill-paragraph-function + (and (minibufferp (current-buffer)) + (= 1 (point-min)))) + (let ((function (or fill-paragraph-function + ;; In the minibuffer, don't count the width + ;; of the prompt. + 'fill-minibuffer-function)) + ;; If fill-paragraph-function is set, it probably takes care + ;; of comments and stuff. If not, it will have to set + ;; fill-paragraph-handle-comment back to t explicitly or + ;; return nil. + (fill-paragraph-handle-comment nil) + (fill-paragraph-function t)) + (funcall function justify))) + ;; 3. Try our syntax-aware filling code. + (and fill-paragraph-handle-comment + ;; Our code only handles \n-terminated comments right now. + comment-start (equal comment-end "") + (let ((fill-paragraph-handle-comment nil)) + (fill-comment-paragraph justify))) + ;; 4. If it all fails, default to the good ol' text paragraph filling. + (let ((before (point)) + (paragraph-start paragraph-start) + ;; Fill prefix used for filling the paragraph. + fill-pfx) + ;; Try to prevent code sections and comment sections from being + ;; filled together. + ;; NO, NO, NO!! The following removed setting of paragraph-start causes + ;; it to recognise a line comment opener as a paragraph start, thus + ;; preventing contiguous line comments being filled (each one is its own + ;; para.) + ;; Also it causes a single long line comment to be filled without the + ;; comment opener on the 2nd line. :-( + ; (when (and fill-paragraph-handle-comment comment-start-skip) + ; (setq paragraph-start + ; (concat paragraph-start "\\|[ \t]*\\(?:" + ; comment-start-skip "\\)"))) + (save-excursion + ;; To make sure the return value of forward-paragraph is meaningful, + ;; we have to start from the beginning of line, otherwise skipping + ;; past the last few chars of a paragraph-separator would count as + ;; a paragraph (and not skipping any chars at EOB would not count + ;; as a paragraph even if it is). + (move-to-left-margin) + (if (not (zerop (fill-forward-paragraph 1))) + ;; There's no paragraph at or after point: give up. + (setq fill-pfx "") + (let ((end (point)) + (beg (progn (fill-forward-paragraph -1) (point)))) + (goto-char before) + (setq fill-pfx + (if use-hard-newlines + ;; Can't use fill-region-as-paragraph, since this + ;; paragraph may still contain hard newlines. See + ;; fill-region. + (fill-region beg end justify) + (fill-region-as-paragraph beg end justify)))))) + fill-pfx))) + + ;; filladapt is Kyle E. Jones's adaptive filling package available at + ;; http://www.wonderworks.com, or (better) the patched version from the + ;; CC Mode website at http://cc-mode.sourceforge.net/filladapt.php. + (eval-after-load "filladapt" + '(unless (memq 'fill-paragraph-ok c-emacs-features) + (if (boundp 'filladapt-function-table) + (let ((kons (assq 'fill-paragraph filladapt-function-table))) + (if kons + (setcdr kons 'c-external-fill-paragraph)))))) + (defun c-fill-paragraph (&optional arg) "Like \\[fill-paragraph] but handles C and C++ style comments. If any of the current line is a comment or within a comment, fill the *************** *** 4373,4383 **** (let ((fill-paragraph-function ;; Avoid infinite recursion. (if (not (eq fill-paragraph-function 'c-fill-paragraph)) ! fill-paragraph-function)) ! (start-point (point-marker))) ! (c-mask-paragraph ! t nil (lambda () (fill-region-as-paragraph (point-min) (point-max) arg))) ! (goto-char start-point)) ;; Always return t. This has the effect that if filling isn't done ;; above, it isn't done at all, and it's therefore effectively ;; disabled in normal code. --- 4472,4488 ---- (let ((fill-paragraph-function ;; Avoid infinite recursion. (if (not (eq fill-paragraph-function 'c-fill-paragraph)) ! fill-paragraph-function))) ! (c-mask-paragraph t nil ! (if (or (memq 'fill-paragraph-ok c-emacs-features) ! (and (boundp 'filladapt-mode) ! filladapt-mode)) ! ;;; Filladapt mode is a filling package available from http://www.wonderworks.com, ! ;;; or (better) get the patched version for CC Mode from ! ;;; http://cc-mode.sourceforge.net/filladapt.php. ! 'fill-paragraph ! 'c-external-fill-paragraph) ! arg)) ;; Always return t. This has the effect that if filling isn't done ;; above, it isn't done at all, and it's therefore effectively ;; disabled in normal code. *** orig/cc-defs.el 2011-11-25 10:11:36.000000000 +0000 --- cc-defs.el 2011-11-26 21:50:18.000000000 +0000 *************** *** 1863,1868 **** --- 1863,1894 ---- (not changed))) (setq list (cons 'add-hook-local list))) + ;; Does the current (X)Emacs correctly fill paragraphs? + (let ((buf (generate-new-buffer " test3"))) + (save-excursion + (set-buffer buf) + (let ((comment-start-skip "\\(//+\\|/\\*+\\)\\s *") + (paragraph-start "[ \t]*\\(//+\\|\\**\\)[ \t]*$\\|^\f") + (adaptive-fill-regexp + "[ \t]*\\(//+\\|\\**\\)[ \t]*\\([ \t]*\\([-!|#%;>*·•‣⁃◦]+[ \t]*\\)*\\)") + adaptive-fill-function + (adaptive-fill-first-line-regexp "\\`[ \t]*\\(//+\\|\\**\\)[ \t]*\\'")) + (set-syntax-table (make-syntax-table)) + (cond + ;; XEmacs + ((memq '8-bit list) + (modify-syntax-entry ?/ ". 1456") + (modify-syntax-entry ?* ". 23")) + ;; Emacs + ((memq '1-bit list) + (modify-syntax-entry ?/ ". 124b") + (modify-syntax-entry ?* ". 23"))) + (insert "// a\n// b\n") + (goto-char (point-min)) + (fill-paragraph nil) + (if (looking-at "// a b") + (setq list (cons 'fill-paragraph-ok list)))) + (kill-buffer buf))) ;; See if `parse-partial-sexp' returns the eighth element. (if (c-safe (>= (length (save-excursion *************** *** 1890,1895 **** --- 1916,1922 ---- this feature. 'category-properties Syntax routines can add a level of indirection to text properties using the 'category property. + 'fill-paragraph-ok `fill-paragraph' fills C comments correctly. 'gen-comment-delim Generic comment delimiters work (i.e. the syntax class `!'). 'gen-string-delim Generic string delimiters work > BR, > Kai -- Alan Mackenzie (Nuremberg, Germany). |