comparison lisp/progmodes/cc-cmds.el @ 44802:cb8f23e350ff

(c-mask-comment): A better way to ensure point doesn't move around unexpectedly. Also handles the paragraph fill case.
author Martin Stjernholm <mast@lysator.liu.se>
date Wed, 24 Apr 2002 01:27:15 +0000
parents 4c09bfb5fbbf
children 0d8b17d428b5
comparison
equal deleted inserted replaced
44801:b986daa6d686 44802:cb8f23e350ff
2759 ;; prefix at all and we should just fill it as 2759 ;; prefix at all and we should just fill it as
2760 ;; normal text. 2760 ;; normal text.
2761 '("" . 0)))))) 2761 '("" . 0))))))
2762 )) 2762 ))
2763 2763
2764 (defun c-mask-comment (fill-mode apply-outside-literal fun &rest args) 2764 (defun c-mask-comment (fill-paragraph apply-outside-literal fun &rest args)
2765 ;; Calls FUN with ARGS ar arguments. If point is inside a comment, 2765 ;; Calls FUN with ARGS ar arguments. If point is inside a comment,
2766 ;; the comment starter and ender are masked and the buffer is 2766 ;; the comment starter and ender are masked and the buffer is
2767 ;; narrowed to make it look like a normal paragraph during the call. 2767 ;; narrowed to make it look like a normal paragraph during the call.
2768 ;;
2769 ;; FILL-PARAGRAPH is non-nil if called for paragraph filling. The
2770 ;; position of point is then less significant when doing masking and
2771 ;; narrowing.
2768 (let (fill 2772 (let (fill
2769 ;; beg and end limits the region to narrow. end is a marker. 2773 ;; beg and end limits the region to narrow. end is a marker.
2770 beg end 2774 beg end
2771 ;; tmp-pre and tmp-post mark strings that are temporarily 2775 ;; tmp-pre and tmp-post mark strings that are temporarily
2772 ;; inserted at the start and end of the region. tmp-pre is a 2776 ;; inserted at the start and end of the region. tmp-pre is a
2788 (setq buffer-undo-list (cons (point) buffer-undo-list))) 2792 (setq buffer-undo-list (cons (point) buffer-undo-list)))
2789 (save-restriction 2793 (save-restriction
2790 ;; Widen to catch comment limits correctly. 2794 ;; Widen to catch comment limits correctly.
2791 (widen) 2795 (widen)
2792 (unless c-lit-limits 2796 (unless c-lit-limits
2793 (setq c-lit-limits (c-literal-limits nil fill-mode))) 2797 (setq c-lit-limits (c-literal-limits nil fill-paragraph)))
2794 (setq c-lit-limits (c-collect-line-comments c-lit-limits)) 2798 (setq c-lit-limits (c-collect-line-comments c-lit-limits))
2795 (unless c-lit-type 2799 (unless c-lit-type
2796 (setq c-lit-type (c-literal-type c-lit-limits)))) 2800 (setq c-lit-type (c-literal-type c-lit-limits))))
2797 (save-excursion 2801 (save-excursion
2798 (unless (c-safe (backward-char) 2802 (unless (c-safe (backward-char)
2838 (and (looking-at (concat "[ \t]*\\(" 2842 (and (looking-at (concat "[ \t]*\\("
2839 c-current-comment-prefix 2843 c-current-comment-prefix
2840 "\\)\\*/")) 2844 "\\)\\*/"))
2841 (eq (cdr c-lit-limits) (match-end 0)) 2845 (eq (cdr c-lit-limits) (match-end 0))
2842 ;; Leave the comment ender on its own line. 2846 ;; Leave the comment ender on its own line.
2843 (set-marker end (max (point) here)))) 2847 (set-marker end (point))))
2844 (when fill-mode 2848 (when fill-paragraph
2845 ;; The comment ender should hang. Replace all cruft 2849 ;; The comment ender should hang. Replace all cruft
2846 ;; between it and the last word with one or two 'x' 2850 ;; between it and the last word with one or two 'x'
2847 ;; and include it in the region. We'll change them 2851 ;; and include it in the region. We'll change them
2848 ;; back to spaces afterwards. 2852 ;; back to spaces afterwards.
2849 (let* ((ender-start (save-excursion 2853 (let* ((ender-start (save-excursion
2898 )))) 2902 ))))
2899 (when (<= beg (car c-lit-limits)) 2903 (when (<= beg (car c-lit-limits))
2900 ;; The region includes the comment starter. 2904 ;; The region includes the comment starter.
2901 (save-excursion 2905 (save-excursion
2902 (goto-char (car c-lit-limits)) 2906 (goto-char (car c-lit-limits))
2903 (when (and (looking-at comment-start-skip) 2907 (if (looking-at (concat "\\(" comment-start-skip "\\)$"))
2904 (> here (match-end 0))) 2908 ;; Begin with the next line.
2905 (if (eq (match-end 0) (c-point 'eol)) 2909 (setq beg (c-point 'bonl))
2906 ;; Begin with the next line. 2910 ;; Fake the fill prefix in the first line.
2907 (setq beg (c-point 'bonl)) 2911 (setq tmp-pre t)))))
2908 ;; Fake the fill prefix in the first line.
2909 (setq tmp-pre t))))))
2910 ((eq c-lit-type 'string) ; String. 2912 ((eq c-lit-type 'string) ; String.
2911 (save-excursion 2913 (save-excursion
2912 (when (>= end (cdr c-lit-limits)) 2914 (when (>= end (cdr c-lit-limits))
2913 (goto-char (1- (cdr c-lit-limits))) 2915 (goto-char (1- (cdr c-lit-limits)))
2914 (setq tmp-post (point-marker)) 2916 (setq tmp-post (point-marker))
2982 "\\`[ \t]*//" 2984 "\\`[ \t]*//"
2983 (or (fill-context-prefix beg end) 2985 (or (fill-context-prefix beg end)
2984 "")))) 2986 ""))))
2985 (car (or fill (c-guess-fill-prefix 2987 (car (or fill (c-guess-fill-prefix
2986 c-lit-limits c-lit-type)))))) 2988 c-lit-limits c-lit-type))))))
2987 ;; Don't remember why I added this, but it doesn't 2989 ;; Save the relative position of point if it's
2988 ;; work correctly since `here' can point anywhere 2990 ;; outside the region we're going to narrow. Want
2989 ;; after the deletes and inserts above. 2991 ;; to restore it in that case, but otherwise it
2990 ;(point-rel (cond ((< here beg) (- here beg)) 2992 ;; should be moved according to the called
2991 ; ((> here end) (- here end)))) 2993 ;; function.
2992 ) 2994 (point-rel (cond ((< (point) beg) (- (point) beg))
2995 ((> (point) end) (- (point) end)))))
2993 ;; Preparations finally done! Now we can call the 2996 ;; Preparations finally done! Now we can call the
2994 ;; actual function. 2997 ;; actual function.
2995 (prog1 2998 (prog1
2996 (save-restriction 2999 (save-restriction
2997 (narrow-to-region beg end) 3000 (narrow-to-region beg end)
2998 (apply fun args)) 3001 (apply fun args))
2999 ;(if point-rel 3002 (if point-rel
3000 ; ;; Restore point if it was outside the region. 3003 ;; Restore point if it was outside the region.
3001 ; (if (< point-rel 0) 3004 (if (< point-rel 0)
3002 ; (goto-char (+ beg point-rel)) 3005 (goto-char (+ beg point-rel))
3003 ; (goto-char (+ end point-rel)))) 3006 (goto-char (+ end point-rel))))))
3004 ))
3005 (when apply-outside-literal 3007 (when apply-outside-literal
3006 (apply fun args)))) 3008 (apply fun args))))
3007 (when (consp tmp-pre) 3009 (when (consp tmp-pre)
3008 (delete-region (car tmp-pre) (cdr tmp-pre))) 3010 (delete-region (car tmp-pre) (cdr tmp-pre)))
3009 (when tmp-post 3011 (when tmp-post