Commit | Line | Data |
---|---|---|
c694af80 MD |
1 | ;;; multistring.el --- editing multiline strings. |
2 | ||
6e7d5622 | 3 | ;; Copyright (C) 2000, 2006 Free Software Foundation, Inc. |
c694af80 | 4 | |
53befeb7 NJ |
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. | |
9 | ;;;; | |
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. | |
14 | ;;;; | |
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 | |
18 | ;;;; 02111-1307 USA | |
c694af80 MD |
19 | |
20 | ;;; Author: Mikael Djurfeldt <djurfeldt@nada.kth.se> | |
21 | ||
22 | ;;; Commentary: | |
23 | ||
24 | ;; Commands for editing multiline ANSI C compatible string literals. | |
25 | ||
26 | ;;; Code: | |
27 | ||
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. | |
32 | ||
33 | ANSI C doesn't allow a string literal to span multiple lines. This | |
34 | function makes editing of multiline strings easier. | |
35 | ||
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: | |
39 | ||
40 | \"A string consisting of | |
41 | multiple | |
42 | lines.\" | |
43 | ||
44 | is converted into | |
45 | ||
46 | \"A string consisting of\n\" | |
47 | \"multiple\n\" | |
48 | \"lines\"" | |
49 | (interactive "*P") | |
50 | (save-excursion | |
51 | (let (beg end) | |
52 | (save-restriction | |
53 | (ms-narrow-canonicalize-ansi-c-string) | |
54 | (if (not arg) | |
55 | (ms-break-string)) | |
56 | (setq beg (point-min)) | |
57 | (setq end (point-max))) | |
58 | (if (not arg) | |
59 | (c-indent-region beg end))))) | |
60 | ||
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. | |
65 | ||
66 | When doing the reverse conversion, \\[fill-region] is used to break up | |
67 | the text into multiple lines." | |
68 | (interactive "*r\nP") | |
69 | (save-excursion | |
70 | (save-restriction | |
71 | (narrow-to-region from to) | |
72 | (if (not unpack-flag) | |
73 | (ms-pack-buffer) | |
74 | (ms-unpack-buffer))))) | |
75 | ||
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. | |
80 | ||
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." | |
87 | (interactive "*P") | |
88 | (save-excursion | |
89 | (let (beg end) | |
90 | (save-restriction | |
91 | (ms-narrow-canonicalize-ansi-c-string) | |
92 | (if (not arg) | |
93 | (ms-break-string " " "" "\\n") | |
94 | (ms-unpack-buffer)) | |
95 | (setq beg (point-min)) | |
96 | (setq end (point-max))) | |
97 | (if (not arg) | |
98 | (c-indent-region beg end))))) | |
99 | ||
100 | (defun ms-pack-buffer () | |
101 | "Pack paragraphs into single lines and remove one newline after paragraphs." | |
102 | (interactive "*") | |
103 | (goto-char (point-min)) | |
104 | (skip-chars-forward "^\n") | |
105 | (while (not (eobp)) | |
106 | (delete-char 1) | |
107 | (skip-chars-forward "\n") | |
108 | (skip-chars-forward "^\n"))) | |
109 | ||
110 | (defun ms-unpack-buffer () | |
111 | "Break single lines into paragraphs and add an extra newline between each." | |
112 | (interactive "*") | |
113 | (goto-char (point-min)) | |
114 | (skip-chars-forward "^\n") | |
115 | (while (not (eobp)) | |
116 | (insert ?\n) | |
117 | (skip-chars-forward "\n") | |
118 | (skip-chars-forward "^\n")) | |
119 | (fill-region (point-min) (point-max) nil t)) | |
120 | ||
121 | (defconst ms-whitespace " \t\n") | |
122 | (defconst ms-string-beginning "\"") | |
123 | (defconst ms-string-end "\\(\\\\*\\)\"") | |
124 | (defconst ms-quoted-newline "\\(\\\\*\\)\\(\\\\n\\)") | |
125 | ||
126 | (defun ms-in-string-p () | |
127 | (eq (c-in-literal) 'string)) | |
128 | ||
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."))) | |
134 | (set-mark (point)) | |
135 | ;; Find beginning | |
136 | (ms-beginning-of-string) | |
137 | (let ((beg (point))) | |
138 | ;; Extend string backwards | |
139 | (while (ms-extend-backwards) | |
140 | (setq beg (point))) | |
141 | (goto-char (mark)) | |
142 | ;; Find end | |
143 | (ms-end-of-string) | |
144 | (let ((end (point))) | |
145 | ;; Extend string forwards | |
146 | (while (ms-extend-forwards) | |
147 | (setq end (point))) | |
148 | ;; Narrow | |
149 | (narrow-to-region beg end) | |
150 | ;; Convert \n into explicit newlines | |
151 | (ms-convert-quoted-newlines)))) | |
152 | ||
153 | (defun ms-beginning-of-string () | |
154 | (let ((pos (search-backward ms-string-beginning nil t))) | |
155 | (while (and pos | |
156 | (char-before) | |
157 | (eq (char-before) ?\\)) | |
158 | (setq pos (search-backward ms-string-beginning nil t))) | |
159 | (if pos | |
160 | (progn | |
161 | (forward-char) | |
162 | (1+ pos))))) | |
163 | ||
164 | (defun ms-extend-backwards () | |
165 | (let ((end (point))) | |
166 | (backward-char) | |
167 | (skip-chars-backward ms-whitespace) | |
168 | (if (eq (char-before) ?\") | |
169 | (progn | |
170 | (backward-char) | |
171 | (delete-region (point) end) | |
172 | (ms-beginning-of-string))))) | |
173 | ||
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))) | |
178 | (if pos | |
179 | (progn | |
180 | (backward-char) | |
181 | (match-end 1))))) | |
182 | ||
183 | (defun ms-extend-forwards () | |
184 | (let ((start (point))) | |
185 | (forward-char) | |
186 | (skip-chars-forward ms-whitespace) | |
187 | (if (eq (char-after) ?\") | |
188 | (progn | |
189 | (forward-char) | |
190 | (delete-region start (point)) | |
191 | (ms-end-of-string))))) | |
192 | ||
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)))) | |
198 | ||
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") | |
205 | (while (not (eobp)) | |
206 | (delete-char 1) | |
207 | (if (not (eq (char-after) ?\n)) | |
208 | (insert single-term) | |
209 | (insert multi-term-1) | |
210 | (while (eq (char-after) ?\n) | |
211 | (delete-char 1) | |
212 | (insert multi-term-n))) | |
213 | (insert "\"\n\"") | |
214 | (skip-chars-forward "^\n")))) | |
215 | ||
216 | (eval-after-load "cc-mode" | |
217 | (progn | |
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) | |
220 | )) |