1 ;;; multistring.el --- editing multiline strings.
3 ;; Copyright (C) 2000, 2006 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Author: Mikael Djurfeldt <djurfeldt@nada.kth.se>
26 ;; Commands for editing multiline ANSI C compatible string literals.
30 (defun ms-ansify-string (arg)
31 "Convert a string literal spanning multiple lines into multiple literals.
32 With no argument, convert the single string at point to multiple strings.
33 With an argument, convert multiple strings to a single one.
35 ANSI C doesn't allow a string literal to span multiple lines. This
36 function makes editing of multiline strings easier.
38 The programmer can edit a string spanning multiple lines and then use
39 this function to convert it into multiple literals representing the
40 original string through the ANSI C string concatenation feature:
42 \"A string consisting of
48 \"A string consisting of\n\"
55 (ms-narrow-canonicalize-ansi-c-string)
58 (setq beg (point-min))
59 (setq end (point-max)))
61 (c-indent-region beg end)))))
63 (defun ms-pack-region (from to &optional unpack-flag)
64 "Pack paragraphs into single lines and remove one newline after paragraphs.
65 With no argument, do the conversion.
66 With an argument, do the reverse.
68 When doing the reverse conversion, \\[fill-region] is used to break up
69 the text into multiple lines."
73 (narrow-to-region from to)
76 (ms-unpack-buffer)))))
78 (defun ms-pack-ansify-string (arg)
79 "Pack text in a string literal and convert into multiple literals.
80 With no argument, do the conversion.
81 With an argument, do the reverse.
83 This command has the combined effect of \\[ms-pack-region] and
84 \\[ms-ansify-string]. It is typically used if you want to store
85 entire paragraphs without newlines in an ANSI C literal, but want to
86 split it into multiple literals in order for the program text to look
87 sensible. Using the reverse command, you can \"unpack\" the text,
88 edit it and repack the text using the forward conversion."
93 (ms-narrow-canonicalize-ansi-c-string)
95 (ms-break-string " " "" "\\n")
97 (setq beg (point-min))
98 (setq end (point-max)))
100 (c-indent-region beg end)))))
102 (defun ms-pack-buffer ()
103 "Pack paragraphs into single lines and remove one newline after paragraphs."
105 (goto-char (point-min))
106 (skip-chars-forward "^\n")
109 (skip-chars-forward "\n")
110 (skip-chars-forward "^\n")))
112 (defun ms-unpack-buffer ()
113 "Break single lines into paragraphs and add an extra newline between each."
115 (goto-char (point-min))
116 (skip-chars-forward "^\n")
119 (skip-chars-forward "\n")
120 (skip-chars-forward "^\n"))
121 (fill-region (point-min) (point-max) nil t))
123 (defconst ms-whitespace " \t\n")
124 (defconst ms-string-beginning "\"")
125 (defconst ms-string-end "\\(\\\\*\\)\"")
126 (defconst ms-quoted-newline "\\(\\\\*\\)\\(\\\\n\\)")
128 (defun ms-in-string-p ()
129 (eq (c-in-literal) 'string))
131 (defun ms-narrow-canonicalize-ansi-c-string ()
132 ;; Find and check reference point
133 (cond ((ms-in-string-p))
134 ((eq (char-after) ?\") (forward-char))
135 (t (error "Not in string.")))
138 (ms-beginning-of-string)
140 ;; Extend string backwards
141 (while (ms-extend-backwards)
147 ;; Extend string forwards
148 (while (ms-extend-forwards)
151 (narrow-to-region beg end)
152 ;; Convert \n into explicit newlines
153 (ms-convert-quoted-newlines))))
155 (defun ms-beginning-of-string ()
156 (let ((pos (search-backward ms-string-beginning nil t)))
159 (eq (char-before) ?\\))
160 (setq pos (search-backward ms-string-beginning nil t)))
166 (defun ms-extend-backwards ()
169 (skip-chars-backward ms-whitespace)
170 (if (eq (char-before) ?\")
173 (delete-region (point) end)
174 (ms-beginning-of-string)))))
176 (defun ms-end-of-string ()
177 (let ((pos (search-forward-regexp ms-string-end nil t)))
178 (while (and pos (= (logand (- (match-end 1) (match-beginning 1)) 1) 1))
179 (setq pos (search-forward-regexp ms-string-end nil t)))
185 (defun ms-extend-forwards ()
186 (let ((start (point)))
188 (skip-chars-forward ms-whitespace)
189 (if (eq (char-after) ?\")
192 (delete-region start (point))
193 (ms-end-of-string)))))
195 (defun ms-convert-quoted-newlines ()
196 (goto-char (point-min))
197 (while (search-forward-regexp ms-quoted-newline nil t)
198 (if (= (logand (- (match-end 1) (match-beginning 1)) 1) 0)
199 (replace-match "\n" nil t nil 2))))
201 (defun ms-break-string (&optional single-term multi-term-1 multi-term-n)
202 (let ((single-term (or single-term "\\n"))
203 (multi-term-1 (or multi-term-1 "\\n"))
204 (multi-term-n (or multi-term-n "\\n")))
205 (goto-char (point-min))
206 (skip-chars-forward "^\n")
209 (if (not (eq (char-after) ?\n))
211 (insert multi-term-1)
212 (while (eq (char-after) ?\n)
214 (insert multi-term-n)))
216 (skip-chars-forward "^\n"))))
218 (eval-after-load "cc-mode"
220 (define-key c-mode-base-map "\C-ca" 'ms-ansify-string)
221 (define-key c-mode-base-map "\C-cd" 'ms-pack-ansify-string)