Klimi's new dotfiles with stow.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

220 Zeilen
10 KiB

vor 4 Jahren
  1. ;;; haskell-string.el --- Haskell related string utilities -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2013 Herbert Valerio Riedel
  3. ;; Author: Herbert Valerio Riedel <hvr@gnu.org>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This file is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation; either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This file is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Todo:
  17. ;; - write ERT tests
  18. ;;; Code:
  19. (require 'cl-lib)
  20. (defun haskell-string-trim (string)
  21. "Remove whitespace around STRING.
  22. A Whitespace character is defined in the Haskell Report as follows
  23. whitechar -> newline | vertab | space | tab | uniWhite
  24. newline -> return linefeed | return | linefeed | formfeed
  25. uniWhite -> any Unicode character defined as whitespace
  26. Note: The implementation currently only supports ASCII
  27. white-space characters, i.e. the implemention doesn't
  28. consider uniWhite."
  29. (let ((s1 (if (string-match "[\t\n\v\f\r ]+\\'" string) (replace-match "" t t string) string)))
  30. (if (string-match "\\`[\t\n\v\f\r ]+" s1) (replace-match "" t t s1) s1)))
  31. (defun haskell-string-only-spaces-p (string)
  32. "Return t if STRING contains only whitespace (or is empty)."
  33. (string= "" (haskell-string-trim string)))
  34. (defun haskell-string-take (string n)
  35. "Return (up to) N character length prefix of STRING."
  36. (substring string 0 (min (length string) n)))
  37. (defconst haskell-string-literal-encode-ascii-array
  38. [ "\\NUL" "\\SOH" "\\STX" "\\ETX" "\\EOT" "\\ENQ" "\\ACK" "\\a" "\\b" "\\t" "\\n" "\\v" "\\f" "\\r" "\\SO" "\\SI" "\\DLE" "\\DC1" "\\DC2" "\\DC3" "\\DC4" "\\NAK" "\\SYN" "\\ETB" "\\CAN" "\\EM" "\\SUB" "\\ESC" "\\FS" "\\GS" "\\RS" "\\US" " " "!" "\\\"" "#" "$" "%" "&" "'" "(" ")" "*" "+" "," "-" "." "/" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" ":" ";" "<" "=" ">" "?" "@" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "[" "\\\\" "]" "^" "_" "`" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "{" "|" "}" "~" "\\DEL" ]
  39. "Array of encodings for 7-bit ASCII character points indexed by ASCII value.")
  40. (defun haskell-string-literal-encode (str &optional no-quotes)
  41. "Encode STR according Haskell escape rules using 7-bit ASCII representation.
  42. The serialization has been implemented to closely match the
  43. behaviour of GHC's Show instance for Strings.
  44. If NO-QUOTES is non-nil, omit wrapping result in quotes.
  45. This is the dual operation to `haskell-string-literal-decode'."
  46. (let ((lastc -1))
  47. (let ((encode (lambda (c)
  48. (let ((lc lastc))
  49. (setq lastc c)
  50. (if (>= c 128) ;; if non-ASCII code point
  51. (format "\\%d" c)
  52. ;; else, for ASCII code points
  53. (if (or (and (= lc 14) (= c ?H)) ;; "\SO\&H"
  54. (and (>= lc 128) (>= c ?0) (<= c ?9))) ;; "\123\&4"
  55. (concat "\\&" (aref haskell-string-literal-encode-ascii-array c))
  56. (aref haskell-string-literal-encode-ascii-array c)
  57. ))))))
  58. (if no-quotes
  59. (mapconcat encode str "")
  60. (concat "\"" (mapconcat encode str "") "\"")))))
  61. (defconst haskell-string-literal-escapes-regexp
  62. (concat "[\\]\\(?:"
  63. (regexp-opt (append
  64. (mapcar (lambda (c) (format "%c" c))
  65. "abfnrtv\\\"'&") ;; "charesc" escape sequences
  66. (mapcar (lambda (c) (format "^%c" c))
  67. "ABCDEFGHIJKLMNOPQRSTUVWXYZ@[\\]^_") ;; "cntrl" escape sequences
  68. (mapcar (lambda (s) (format "%s" s))
  69. (split-string "NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR
  70. SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC
  71. FS GS RS US SP DEL")))) ;; "ascii" (w\o "cntrl") escape sequences
  72. "\\|" "[\t\n\v\f\r ]+[\\]" ;; whitespace gaps
  73. "\\|" "[0-9]+" ;; decimal escape sequence
  74. "\\|" "o[0-7]+" ;; octal escape sequence
  75. "\\|" "x[0-9a-f]+" ;; hex escape sequence
  76. "\\)?") ;; everything else is an invalid escape sequence
  77. "Regexp for matching escape codes in string literals.
  78. See Haskell Report Sect 2.6,
  79. URL `http://www.haskell.org/onlinereport/haskell2010/haskellch2.html#x7-200002.6',
  80. for more details.")
  81. (defconst haskell-string-literal-decode1-table
  82. (let ((h (make-hash-table :test 'equal)))
  83. (mapc (lambda (c) (puthash (concat "\\" (car c)) (cdr c) h))
  84. '(;; ascii-escapes
  85. ("NUL" . "\x00") ("SOH" . "\x01") ("STX" . "\x02") ("ETX" . "\x03") ("EOT" . "\x04") ("ENQ" . "\x05")
  86. ("ACK" . "\x06") ("BEL" . "\x07") ("BS" . "\x08") ("HT" . "\x09") ("LF" . "\x0a") ("VT" . "\x0b")
  87. ("FF" . "\x0c") ("CR" . "\x0d") ("SO" . "\x0e") ("SI" . "\x0f") ("DLE" . "\x10") ("DC1" . "\x11")
  88. ("DC2" . "\x12") ("DC3" . "\x13") ("DC4" . "\x14") ("NAK" . "\x15") ("SYN" . "\x16") ("ETB" . "\x17")
  89. ("CAN" . "\x18") ("EM" . "\x19") ("SUB" . "\x1a") ("ESC" . "\x1b") ("FS" . "\x1c") ("GS" . "\x1d")
  90. ("RS" . "\x1e") ("US" . "\x1f") ("SP" . "\x20") ("DEL" . "\x7f" )
  91. ;; C-compatible single-char escape sequences
  92. ("a" . "\x07") ("b" . "\x08") ("f" . "\x0c") ("n" . "\x0a") ("r" . "\x0d") ("t" . "\x09") ("v" . "\x0b")
  93. ;; trivial escapes
  94. ("\\" . "\\") ("\"" . "\"") ("'" . "'")
  95. ;; "empty" escape
  96. ("&" . "")))
  97. h)
  98. "Hash table containing irregular escape sequences and their decoded strings.
  99. Used by `haskell-string-literal-decode1'.")
  100. (defun haskell-string-literal-decode1 (l)
  101. "Decode a single string literal escape sequence.
  102. L must contain exactly one escape sequence.
  103. This is an internal function used by `haskell-string-literal-decode'."
  104. (let ((case-fold-search nil))
  105. (cond
  106. ((gethash l haskell-string-literal-decode1-table))
  107. ((string-match "\\`[\\][0-9]+\\'" l) (char-to-string (string-to-number (substring l 1) 10)))
  108. ((string-match "\\`[\\]x[[:xdigit:]]+\\'" l) (char-to-string (string-to-number (substring l 2) 16)))
  109. ((string-match "\\`[\\]o[0-7]+\\'" l) (char-to-string (string-to-number (substring l 2) 8)))
  110. ((string-match "\\`[\\]\\^[@-_]\\'" l) (char-to-string (- (aref l 2) ?@))) ;; "cntrl" escapes
  111. ((string-match "\\`[\\][\t\n\v\f\r ]+[\\]\\'" l) "") ;; whitespace gap
  112. (t (error "Invalid escape sequence")))))
  113. (defun haskell-string-literal-decode (estr &optional no-quotes)
  114. "Decode a Haskell string-literal.
  115. If NO-QUOTES is nil, ESTR must be surrounded by quotes.
  116. This is the dual operation to `haskell-string-literal-encode'."
  117. (if (and (not no-quotes)
  118. (string-match-p "\\`\"[^\\\"[:cntrl:]]*\"\\'" estr))
  119. (substring estr 1 -1) ;; optimized fast-path for trivial strings
  120. (let ((s (if no-quotes ;; else: do general decoding
  121. estr
  122. (if (string-match-p "\\`\".*\"\\'" estr)
  123. (substring estr 1 -1)
  124. (error "String literal must be delimited by quotes"))))
  125. (case-fold-search nil))
  126. (replace-regexp-in-string haskell-string-literal-escapes-regexp #'haskell-string-literal-decode1 s t t))))
  127. (defun haskell-string-ellipsize (string n)
  128. "Return STRING truncated to (at most) N characters.
  129. If truncation occurred, last character in string is replaced by `'.
  130. See also `haskell-string-take'."
  131. (cond
  132. ((<= (length string) n) string) ;; no truncation needed
  133. ((< n 1) "")
  134. (t (concat (substring string 0 (1- n)) ""))))
  135. (defun haskell-string-chomp (str)
  136. "Chomp leading and tailing whitespace from STR."
  137. (while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'"
  138. str)
  139. (setq str (replace-match "" t t str)))
  140. str)
  141. (defun haskell-string-split-to-lines (str)
  142. "Split STR to lines and return a list of strings with preceeding and
  143. succeding space removed."
  144. (when (stringp str)
  145. (cl-mapcar #'haskell-string-chomp (split-string str "\n"))))
  146. (defun haskell-string-trim-prefix (prefix str)
  147. "If PREFIX is prefix of STR, the string is trimmed."
  148. (when (and (stringp prefix)
  149. (stringp str))
  150. (if (string-prefix-p prefix str)
  151. (substring str (length prefix)))))
  152. (defun haskell-string-trim-suffix (suffix str)
  153. "If SUFFIX is suffix of STR, the string is trimmed."
  154. (when (and (stringp suffix)
  155. (stringp str))
  156. (if (string-suffix-p suffix str)
  157. (substring str 0 (* -1 (length suffix))))))
  158. (defun haskell-string-drop-qualifier (ident)
  159. "Drop qualifier from given identifier IDENT.
  160. If the identifier is not qualified return it unchanged."
  161. (or (and (string-match "^\\([^.]*\\.\\)*\\(?1:[^.]+\\)$" ident)
  162. (match-string 1 ident))
  163. ident))
  164. (defun haskell-mode-message-line (str)
  165. "Echo STR in mini-buffer.
  166. Given string is shrinken to single line, multiple lines just
  167. disturbs the programmer."
  168. (unless (active-minibuffer-window)
  169. (message "%s" (haskell-mode-one-line str (frame-width)))))
  170. (defun haskell-mode-one-line (str &optional width)
  171. "Try to fit STR as much as possible on one line according to given WIDTH."
  172. (unless width
  173. (setq width (length str)))
  174. (let* ((long-line (replace-regexp-in-string "\n" " " str))
  175. (condensed (replace-regexp-in-string
  176. " +" " " (haskell-string-trim long-line))))
  177. (truncate-string-to-width condensed width nil nil "")))
  178. (provide 'haskell-string)
  179. ;;; haskell-string.el ends here