Skip to content

Commit 11d6abf

Browse files
authored
Merge pull request #1540 from haskell/haskell-string-utils
haskell-string-utils
2 parents d533226 + 8492d35 commit 11d6abf

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

haskell-interactive-mode.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
(require 'haskell-font-lock)
3636
(require 'haskell-presentation-mode)
3737
(require 'haskell-utils)
38-
38+
(require 'haskell-string)
3939
(require 'ansi-color)
4040
(require 'cl-lib)
4141
(require 'etags)

haskell-load.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
(require 'haskell-modules)
2828
(require 'haskell-commands)
2929
(require 'haskell-session)
30+
(require 'haskell-string)
3031

3132
(defun haskell-process-look-config-changes (session)
3233
"Check whether a cabal configuration file has changed.

haskell-mode.el

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,19 +1198,6 @@ generated."
11981198
(let ((tags-file-name dir))
11991199
(xref-find-definitions and-then-find-this-tag))))))
12001200

1201-
(defun haskell-mode-message-line (str)
1202-
"Echo STR in mini-buffer.
1203-
Given string is shrinken to single line, multiple lines just
1204-
disturbs the programmer."
1205-
(message "%s" (haskell-mode-one-line str (frame-width))))
1206-
1207-
(defun haskell-mode-one-line (str width)
1208-
"Try to fit STR as much as possible on one line according to given WIDTH."
1209-
(let* ((long-line (replace-regexp-in-string "\n" " " str))
1210-
(condensed (replace-regexp-in-string
1211-
" +" " " (haskell-string-trim long-line))))
1212-
(truncate-string-to-width condensed width nil nil "")))
1213-
12141201
;; Provide ourselves:
12151202
(provide 'haskell-mode)
12161203
;;; haskell-mode.el ends here

haskell-string.el

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
;;; Code:
2929

30+
(require 'cl-lib)
31+
3032
(defun haskell-string-trim (string)
3133
"Remove whitespace around STRING.
3234
@@ -162,6 +164,33 @@ See also `haskell-string-take'."
162164
((< n 1) "")
163165
(t (concat (substring string 0 (1- n)) ""))))
164166

167+
(defun haskell-string-chomp (str)
168+
"Chomp leading and tailing whitespace from STR."
169+
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'"
170+
str)
171+
(setq str (replace-match "" t t str)))
172+
str)
173+
174+
(defun haskell-string-split-to-lines (str)
175+
"Split STR to lines and return a list of strings with preceeding and
176+
succeding space removed."
177+
(when (stringp str)
178+
(cl-mapcar #'haskell-string-chomp (split-string str "\n"))))
179+
180+
(defun haskell-string-trim-prefix (prefix str)
181+
"If PREFIX is prefix of STR, the string is trimmed."
182+
(when (and (stringp prefix)
183+
(stringp str))
184+
(if (string-prefix-p prefix str)
185+
(substring str (length prefix)))))
186+
187+
(defun haskell-string-trim-suffix (suffix str)
188+
"If SUFFIX is suffix of STR, the string is trimmed."
189+
(when (and (stringp suffix)
190+
(stringp str))
191+
(if (string-suffix-p suffix str)
192+
(substring str 0 (* -1 (length suffix))))))
193+
165194
(defun haskell-string-drop-qualifier (ident)
166195
"Drop qualifier from given identifier IDENT.
167196
@@ -170,6 +199,21 @@ If the identifier is not qualified return it unchanged."
170199
(match-string 1 ident))
171200
ident))
172201

202+
(defun haskell-mode-message-line (str)
203+
"Echo STR in mini-buffer.
204+
Given string is shrinken to single line, multiple lines just
205+
disturbs the programmer."
206+
(message "%s" (haskell-mode-one-line str (frame-width))))
207+
208+
(defun haskell-mode-one-line (str &optional width)
209+
"Try to fit STR as much as possible on one line according to given WIDTH."
210+
(unless width
211+
(setq width (length str)))
212+
(let* ((long-line (replace-regexp-in-string "\n" " " str))
213+
(condensed (replace-regexp-in-string
214+
" +" " " (haskell-string-trim long-line))))
215+
(truncate-string-to-width condensed width nil nil "")))
216+
173217
(provide 'haskell-string)
174218

175219
;;; haskell-string.el ends here

0 commit comments

Comments
 (0)