Skip to content

Commit b0611c0

Browse files
committed
c++-mode: more snippets
1 parent 7102c6e commit b0611c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+660
-41
lines changed

c++-mode/.yas-setup.el

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
;; -*- mode: emacs-lisp-mode; -*-
2+
3+
(defun doom-snippets-c++-using-std-p ()
4+
"Return non-nil if 'using namespace std' is found at the top of this file."
5+
(save-excursion
6+
(goto-char (point-max))
7+
(or (search-forward "using namespace std;" 512 t)
8+
(search-forward "std::" 1024 t))))
9+
10+
(defun doom-snippets-c++-class-name (str)
11+
"Search for a class name like `DerivedClass' in STR
12+
(which may look like `DerivedClass : ParentClass1, ParentClass2, ...')
13+
If found, the class name is returned, otherwise STR is returned"
14+
(yas-substr str "[^: ]*"))
15+
16+
(defun doom-snippets-c++-class-method-decl-choice ()
17+
"Choose and return the end of a C++11 class method declaration"
18+
(yas-choose-value '(";" " = default;" " = delete;")))

c++-mode/accumulate

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: accumulate
3+
# key: acm
4+
# --
5+
auto sum = std::accumulate(std::begin(${1:container}), std::end($1), 0);

c++-mode/accumulate-with-closure

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: accumulate w/ closure
3+
# key: acl
4+
# --
5+
auto sum = std::accumulate(std::begin(${1:container}), std::end($1), 0, [](int total, $2) {
6+
`%`$3
7+
});

c++-mode/adjacent_find

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- mode: snippet -*-
2+
# name: adjacent_find
3+
# key: ajf
4+
# --
5+
auto pos = std::adjacent_find(std::begin(${1:container}), std::end($1));
6+
if (pos != std::end($1)) {
7+
$2
8+
}

c++-mode/all_of

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: snippet -*-
2+
# name: all_of
3+
# key: alo
4+
# --
5+
if (std::all_of(std::begin(${1:container}), std::end($1), []($2) {
6+
`%`$3
7+
})) {
8+
$4
9+
}

c++-mode/any_of

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: snippet -*-
2+
# name: any_of
3+
# key: ano
4+
# --
5+
if (std::any_of(std::begin(${1:container}), std::end($1), []($2) {
6+
`%`$3
7+
})) {
8+
$4
9+
}

c++-mode/cerr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
# key: err
44
# uuid: err
55
# --
6-
cerr << $0;
6+
`(setq --cpp-ns (if (doom-snippets-c++-using-std-p) "" "std::"))
7+
cerr` << `%`$1 << `--cpp-ns`endl;`(progn (makunbound '--cpp-ns) "")`

c++-mode/cin

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- mode: snippet -*-
22
# name: cin
33
# --
4-
`(setq --cpp-ns (if (search "using namespace std;" (buffer-string)) "" "std::"))
5-
--cpp-ns`cin >> ${1:string};`(progn (makunbound '--cpp-ns) "")`
4+
`(setq --cpp-ns (if (doom-snippets-c++-using-std-p) "" "std::"))
5+
--cpp-ns`cin >> ${1:string};

c++-mode/class11

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- mode: snippet -*-
2+
# name: class11
3+
# key: cls11
4+
# group: c++11
5+
# uuid: d7c41f87-9b8a-479d-bb12-89f4cbdd46a7
6+
# contributor: Ved Vyas
7+
# desc: Snippet for C++11 classes based on c++-mode/class. Allows for Rule of
8+
# [0, All]. A choice between ";", " = default;", and " = delete;" is presented
9+
# for each method. The methods and some of the optional keywords/specifiers are
10+
# exposed as fields that users can easily skip-and-clear.
11+
# Hackish query-replace-regexp to renumber non-mirror fields in the region
12+
# between public and protected (can use N as a field number in the snippet):
13+
# \${[0-9N]*:\([^\$]\) -> ${\,(+ 2 \#):\1
14+
# References:
15+
# 1. http://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_five
16+
# 2. https://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29#Example_in_C.2B.2B
17+
# 3. http://stackoverflow.com/a/4782927
18+
# --
19+
class ${1:Name}
20+
{
21+
public:
22+
${2: ${3://! Default constructor
23+
}${1:$(doom-snippets-c++-class-name yas-text)}()${4:;$(doom-snippets-c++-class-method-decl-choice)}
24+
25+
}${5: ${6://! Copy constructor
26+
}${1:$(doom-snippets-c++-class-name yas-text)}(const ${1:$(doom-snippets-c++-class-name yas-text)} &other)${7:;$(doom-snippets-c++-class-method-decl-choice)}
27+
28+
}${8: ${9://! Move constructor
29+
}${1:$(doom-snippets-c++-class-name yas-text)}(${1:$(doom-snippets-c++-class-name yas-text)} &&other)${10: noexcept}${11:;$(doom-snippets-c++-class-method-decl-choice)}
30+
31+
}${12: ${13://! Destructor
32+
}${14:virtual }~${1:$(doom-snippets-c++-class-name yas-text)}()${15: noexcept}${16:;$(doom-snippets-c++-class-method-decl-choice)}
33+
34+
}${17: ${18://! Copy assignment operator
35+
}${1:$(doom-snippets-c++-class-name yas-text)}& operator=(const ${1:$(doom-snippets-c++-class-name yas-text)} &other)${19:;$(doom-snippets-c++-class-method-decl-choice)}
36+
37+
}${20: ${21://! Move assignment operator
38+
}${1:$(doom-snippets-c++-class-name yas-text)}& operator=(${1:$(doom-snippets-c++-class-name yas-text)} &&other)${22: noexcept}${23:;$(doom-snippets-c++-class-method-decl-choice)}
39+
40+
}$0
41+
42+
protected:
43+
private:
44+
};

c++-mode/copy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: copy
3+
# key: cpy
4+
# --
5+
std::copy(std::begin(${1:container}), std::end($1), std::begin($2));

0 commit comments

Comments
 (0)