multistring.el 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ;;; multistring.el --- editing multiline strings.
  2. ;; Copyright (C) 2000, 2006 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free
  15. ;;;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. ;;;; 02111-1307 USA
  17. ;;; Author: Mikael Djurfeldt <djurfeldt@nada.kth.se>
  18. ;;; Commentary:
  19. ;; Commands for editing multiline ANSI C compatible string literals.
  20. ;;; Code:
  21. (defun ms-ansify-string (arg)
  22. "Convert a string literal spanning multiple lines into multiple literals.
  23. With no argument, convert the single string at point to multiple strings.
  24. With an argument, convert multiple strings to a single one.
  25. ANSI C doesn't allow a string literal to span multiple lines. This
  26. function makes editing of multiline strings easier.
  27. The programmer can edit a string spanning multiple lines and then use
  28. this function to convert it into multiple literals representing the
  29. original string through the ANSI C string concatenation feature:
  30. \"A string consisting of
  31. multiple
  32. lines.\"
  33. is converted into
  34. \"A string consisting of\n\"
  35. \"multiple\n\"
  36. \"lines\""
  37. (interactive "*P")
  38. (save-excursion
  39. (let (beg end)
  40. (save-restriction
  41. (ms-narrow-canonicalize-ansi-c-string)
  42. (if (not arg)
  43. (ms-break-string))
  44. (setq beg (point-min))
  45. (setq end (point-max)))
  46. (if (not arg)
  47. (c-indent-region beg end)))))
  48. (defun ms-pack-region (from to &optional unpack-flag)
  49. "Pack paragraphs into single lines and remove one newline after paragraphs.
  50. With no argument, do the conversion.
  51. With an argument, do the reverse.
  52. When doing the reverse conversion, \\[fill-region] is used to break up
  53. the text into multiple lines."
  54. (interactive "*r\nP")
  55. (save-excursion
  56. (save-restriction
  57. (narrow-to-region from to)
  58. (if (not unpack-flag)
  59. (ms-pack-buffer)
  60. (ms-unpack-buffer)))))
  61. (defun ms-pack-ansify-string (arg)
  62. "Pack text in a string literal and convert into multiple literals.
  63. With no argument, do the conversion.
  64. With an argument, do the reverse.
  65. This command has the combined effect of \\[ms-pack-region] and
  66. \\[ms-ansify-string]. It is typically used if you want to store
  67. entire paragraphs without newlines in an ANSI C literal, but want to
  68. split it into multiple literals in order for the program text to look
  69. sensible. Using the reverse command, you can \"unpack\" the text,
  70. edit it and repack the text using the forward conversion."
  71. (interactive "*P")
  72. (save-excursion
  73. (let (beg end)
  74. (save-restriction
  75. (ms-narrow-canonicalize-ansi-c-string)
  76. (if (not arg)
  77. (ms-break-string " " "" "\\n")
  78. (ms-unpack-buffer))
  79. (setq beg (point-min))
  80. (setq end (point-max)))
  81. (if (not arg)
  82. (c-indent-region beg end)))))
  83. (defun ms-pack-buffer ()
  84. "Pack paragraphs into single lines and remove one newline after paragraphs."
  85. (interactive "*")
  86. (goto-char (point-min))
  87. (skip-chars-forward "^\n")
  88. (while (not (eobp))
  89. (delete-char 1)
  90. (skip-chars-forward "\n")
  91. (skip-chars-forward "^\n")))
  92. (defun ms-unpack-buffer ()
  93. "Break single lines into paragraphs and add an extra newline between each."
  94. (interactive "*")
  95. (goto-char (point-min))
  96. (skip-chars-forward "^\n")
  97. (while (not (eobp))
  98. (insert ?\n)
  99. (skip-chars-forward "\n")
  100. (skip-chars-forward "^\n"))
  101. (fill-region (point-min) (point-max) nil t))
  102. (defconst ms-whitespace " \t\n")
  103. (defconst ms-string-beginning "\"")
  104. (defconst ms-string-end "\\(\\\\*\\)\"")
  105. (defconst ms-quoted-newline "\\(\\\\*\\)\\(\\\\n\\)")
  106. (defun ms-in-string-p ()
  107. (eq (c-in-literal) 'string))
  108. (defun ms-narrow-canonicalize-ansi-c-string ()
  109. ;; Find and check reference point
  110. (cond ((ms-in-string-p))
  111. ((eq (char-after) ?\") (forward-char))
  112. (t (error "Not in string.")))
  113. (set-mark (point))
  114. ;; Find beginning
  115. (ms-beginning-of-string)
  116. (let ((beg (point)))
  117. ;; Extend string backwards
  118. (while (ms-extend-backwards)
  119. (setq beg (point)))
  120. (goto-char (mark))
  121. ;; Find end
  122. (ms-end-of-string)
  123. (let ((end (point)))
  124. ;; Extend string forwards
  125. (while (ms-extend-forwards)
  126. (setq end (point)))
  127. ;; Narrow
  128. (narrow-to-region beg end)
  129. ;; Convert \n into explicit newlines
  130. (ms-convert-quoted-newlines))))
  131. (defun ms-beginning-of-string ()
  132. (let ((pos (search-backward ms-string-beginning nil t)))
  133. (while (and pos
  134. (char-before)
  135. (eq (char-before) ?\\))
  136. (setq pos (search-backward ms-string-beginning nil t)))
  137. (if pos
  138. (progn
  139. (forward-char)
  140. (1+ pos)))))
  141. (defun ms-extend-backwards ()
  142. (let ((end (point)))
  143. (backward-char)
  144. (skip-chars-backward ms-whitespace)
  145. (if (eq (char-before) ?\")
  146. (progn
  147. (backward-char)
  148. (delete-region (point) end)
  149. (ms-beginning-of-string)))))
  150. (defun ms-end-of-string ()
  151. (let ((pos (search-forward-regexp ms-string-end nil t)))
  152. (while (and pos (= (logand (- (match-end 1) (match-beginning 1)) 1) 1))
  153. (setq pos (search-forward-regexp ms-string-end nil t)))
  154. (if pos
  155. (progn
  156. (backward-char)
  157. (match-end 1)))))
  158. (defun ms-extend-forwards ()
  159. (let ((start (point)))
  160. (forward-char)
  161. (skip-chars-forward ms-whitespace)
  162. (if (eq (char-after) ?\")
  163. (progn
  164. (forward-char)
  165. (delete-region start (point))
  166. (ms-end-of-string)))))
  167. (defun ms-convert-quoted-newlines ()
  168. (goto-char (point-min))
  169. (while (search-forward-regexp ms-quoted-newline nil t)
  170. (if (= (logand (- (match-end 1) (match-beginning 1)) 1) 0)
  171. (replace-match "\n" nil t nil 2))))
  172. (defun ms-break-string (&optional single-term multi-term-1 multi-term-n)
  173. (let ((single-term (or single-term "\\n"))
  174. (multi-term-1 (or multi-term-1 "\\n"))
  175. (multi-term-n (or multi-term-n "\\n")))
  176. (goto-char (point-min))
  177. (skip-chars-forward "^\n")
  178. (while (not (eobp))
  179. (delete-char 1)
  180. (if (not (eq (char-after) ?\n))
  181. (insert single-term)
  182. (insert multi-term-1)
  183. (while (eq (char-after) ?\n)
  184. (delete-char 1)
  185. (insert multi-term-n)))
  186. (insert "\"\n\"")
  187. (skip-chars-forward "^\n"))))
  188. (eval-after-load "cc-mode"
  189. (progn
  190. (define-key c-mode-base-map "\C-ca" 'ms-ansify-string)
  191. (define-key c-mode-base-map "\C-cd" 'ms-pack-ansify-string)
  192. ))