multistring.el 6.6 KB

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