1 ;;; multistring.el --- editing multiline strings.
3 ;; Copyright (C) 2000, 2006 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free
17 ;;;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 ;;; Author: Mikael Djurfeldt <djurfeldt@nada.kth.se>
24 ;; Commands for editing multiline ANSI C compatible string literals.
28 (defun ms-ansify-string (arg)
29 "Convert a string literal spanning multiple lines into multiple literals.
30 With no argument, convert the single string at point to multiple strings.
31 With an argument, convert multiple strings to a single one.
33 ANSI C doesn't allow a string literal to span multiple lines. This
34 function makes editing of multiline strings easier.
36 The programmer can edit a string spanning multiple lines and then use
37 this function to convert it into multiple literals representing the
38 original string through the ANSI C string concatenation feature:
40 \"A string consisting of
46 \"A string consisting of\n\"
53 (ms-narrow-canonicalize-ansi-c-string)
56 (setq beg
(point-min))
57 (setq end
(point-max)))
59 (c-indent-region beg end
)))))
61 (defun ms-pack-region (from to
&optional unpack-flag
)
62 "Pack paragraphs into single lines and remove one newline after paragraphs.
63 With no argument, do the conversion.
64 With an argument, do the reverse.
66 When doing the reverse conversion, \\[fill-region] is used to break up
67 the text into multiple lines."
71 (narrow-to-region from to
)
74 (ms-unpack-buffer)))))
76 (defun ms-pack-ansify-string (arg)
77 "Pack text in a string literal and convert into multiple literals.
78 With no argument, do the conversion.
79 With an argument, do the reverse.
81 This command has the combined effect of \\[ms-pack-region] and
82 \\[ms-ansify-string]. It is typically used if you want to store
83 entire paragraphs without newlines in an ANSI C literal, but want to
84 split it into multiple literals in order for the program text to look
85 sensible. Using the reverse command, you can \"unpack\" the text,
86 edit it and repack the text using the forward conversion."
91 (ms-narrow-canonicalize-ansi-c-string)
93 (ms-break-string " " "" "\\n")
95 (setq beg
(point-min))
96 (setq end
(point-max)))
98 (c-indent-region beg end
)))))
100 (defun ms-pack-buffer ()
101 "Pack paragraphs into single lines and remove one newline after paragraphs."
103 (goto-char (point-min))
104 (skip-chars-forward "^\n")
107 (skip-chars-forward "\n")
108 (skip-chars-forward "^\n")))
110 (defun ms-unpack-buffer ()
111 "Break single lines into paragraphs and add an extra newline between each."
113 (goto-char (point-min))
114 (skip-chars-forward "^\n")
117 (skip-chars-forward "\n")
118 (skip-chars-forward "^\n"))
119 (fill-region (point-min) (point-max) nil t
))
121 (defconst ms-whitespace
" \t\n")
122 (defconst ms-string-beginning
"\"")
123 (defconst ms-string-end
"\\(\\\\*\\)\"")
124 (defconst ms-quoted-newline
"\\(\\\\*\\)\\(\\\\n\\)")
126 (defun ms-in-string-p ()
127 (eq (c-in-literal) 'string
))
129 (defun ms-narrow-canonicalize-ansi-c-string ()
130 ;; Find and check reference point
131 (cond ((ms-in-string-p))
132 ((eq (char-after) ?
\") (forward-char))
133 (t (error "Not in string.")))
136 (ms-beginning-of-string)
138 ;; Extend string backwards
139 (while (ms-extend-backwards)
145 ;; Extend string forwards
146 (while (ms-extend-forwards)
149 (narrow-to-region beg end
)
150 ;; Convert \n into explicit newlines
151 (ms-convert-quoted-newlines))))
153 (defun ms-beginning-of-string ()
154 (let ((pos (search-backward ms-string-beginning nil t
)))
157 (eq (char-before) ?
\\))
158 (setq pos
(search-backward ms-string-beginning nil t
)))
164 (defun ms-extend-backwards ()
167 (skip-chars-backward ms-whitespace
)
168 (if (eq (char-before) ?
\")
171 (delete-region (point) end
)
172 (ms-beginning-of-string)))))
174 (defun ms-end-of-string ()
175 (let ((pos (search-forward-regexp ms-string-end nil t
)))
176 (while (and pos
(= (logand (- (match-end 1) (match-beginning 1)) 1) 1))
177 (setq pos
(search-forward-regexp ms-string-end nil t
)))
183 (defun ms-extend-forwards ()
184 (let ((start (point)))
186 (skip-chars-forward ms-whitespace
)
187 (if (eq (char-after) ?
\")
190 (delete-region start
(point))
191 (ms-end-of-string)))))
193 (defun ms-convert-quoted-newlines ()
194 (goto-char (point-min))
195 (while (search-forward-regexp ms-quoted-newline nil t
)
196 (if (= (logand (- (match-end 1) (match-beginning 1)) 1) 0)
197 (replace-match "\n" nil t nil
2))))
199 (defun ms-break-string (&optional single-term multi-term-1 multi-term-n
)
200 (let ((single-term (or single-term
"\\n"))
201 (multi-term-1 (or multi-term-1
"\\n"))
202 (multi-term-n (or multi-term-n
"\\n")))
203 (goto-char (point-min))
204 (skip-chars-forward "^\n")
207 (if (not (eq (char-after) ?
\n))
209 (insert multi-term-1
)
210 (while (eq (char-after) ?
\n)
212 (insert multi-term-n
)))
214 (skip-chars-forward "^\n"))))
216 (eval-after-load "cc-mode"
218 (define-key c-mode-base-map
"\C-ca" 'ms-ansify-string
)
219 (define-key c-mode-base-map
"\C-cd" 'ms-pack-ansify-string
)