Commit | Line | Data |
---|---|---|
131e4695 | 1 | ;;; subr-x.el --- extra Lisp functions -*- lexical-binding:t -*- |
7b530552 | 2 | |
ba318903 | 3 | ;; Copyright (C) 2013-2014 Free Software Foundation, Inc. |
7b530552 | 4 | |
34dc21db | 5 | ;; Maintainer: emacs-devel@gnu.org |
7b530552 BB |
6 | ;; Keywords: convenience |
7 | ;; Package: emacs | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation, either version 3 of the License, or | |
14 | ;; (at your option) any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
131e4695 BB |
26 | ;; Less commonly used functions that complement basic APIs, often implemented in |
27 | ;; C code (like hash-tables and strings), and are not eligible for inclusion | |
28 | ;; in subr.el. | |
29 | ||
64295f83 GM |
30 | ;; Do not document these functions in the lispref. |
31 | ;; http://lists.gnu.org/archive/html/emacs-devel/2014-01/msg01006.html | |
32 | ||
7b530552 BB |
33 | ;;; Code: |
34 | ||
35 | (defsubst hash-table-keys (hash-table) | |
36 | "Return a list of keys in HASH-TABLE." | |
37 | (let ((keys '())) | |
1d01ad41 | 38 | (maphash (lambda (k _v) (push k keys)) hash-table) |
7b530552 BB |
39 | keys)) |
40 | ||
41 | (defsubst hash-table-values (hash-table) | |
42 | "Return a list of values in HASH-TABLE." | |
43 | (let ((values '())) | |
1d01ad41 | 44 | (maphash (lambda (_k v) (push v values)) hash-table) |
7b530552 BB |
45 | values)) |
46 | ||
015b3b3e BB |
47 | (defsubst string-empty-p (string) |
48 | "Check whether STRING is empty." | |
49 | (string= string "")) | |
50 | ||
4301875e BB |
51 | (defsubst string-join (strings &optional separator) |
52 | "Join all STRINGS using SEPARATOR." | |
53 | (mapconcat 'identity strings separator)) | |
54 | ||
447bdcb8 BB |
55 | (defsubst string-reverse (str) |
56 | "Reverse the string STR." | |
57 | (apply 'string (nreverse (string-to-list str)))) | |
58 | ||
b55aea38 BB |
59 | (defsubst string-trim-left (string) |
60 | "Remove leading whitespace from STRING." | |
61 | (if (string-match "\\`[ \t\n\r]+" string) | |
62 | (replace-match "" t t string) | |
63 | string)) | |
64 | ||
65 | (defsubst string-trim-right (string) | |
66 | "Remove trailing whitespace from STRING." | |
67 | (if (string-match "[ \t\n\r]+\\'" string) | |
68 | (replace-match "" t t string) | |
69 | string)) | |
70 | ||
71 | (defsubst string-trim (string) | |
72 | "Remove leading and trailing whitespace from STRING." | |
73 | (string-trim-left (string-trim-right string))) | |
74 | ||
015b3b3e BB |
75 | (defsubst string-blank-p (string) |
76 | "Check whether STRING is either empty or only whitespace." | |
73c8ceea | 77 | (string-match-p "\\`[ \t\n\r]*\\'" string)) |
015b3b3e | 78 | |
3cbfb935 BB |
79 | (defsubst string-remove-prefix (prefix string) |
80 | "Remove PREFIX from STRING if present." | |
81 | (if (string-prefix-p prefix string) | |
82 | (substring string (length prefix)) | |
83 | string)) | |
84 | ||
85 | (defsubst string-remove-suffix (suffix string) | |
86 | "Remove SUFFIX from STRING if present." | |
87 | (if (string-suffix-p suffix string) | |
88 | (substring string 0 (- (length string) (length suffix))) | |
89 | string)) | |
90 | ||
131e4695 | 91 | (provide 'subr-x) |
7b530552 | 92 | |
131e4695 | 93 | ;;; subr-x.el ends here |