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

+18
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

+5
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

+7
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

+8
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

+9
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

+9
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

+2-1
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

+2-2
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

+44
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

+5
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));

c++-mode/copy_backward

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: copy_backward
3+
# key: cpb
4+
# --
5+
std::copy_backward(std::begin(${1:container}), std::end($1), std::end($1));

c++-mode/copy_if

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- mode: snippet -*-
2+
# name: copy_if
3+
# key: cpi
4+
# --
5+
std::copy_if(std::begin(${1:container}), std::end($1), std::begin($2),
6+
[]($3) {
7+
$4
8+
});

c++-mode/copy_n

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: copy_n
3+
# key: cpn
4+
# --
5+
std::copy_n(std::begin(${1:container}), $2, std::end($1));

c++-mode/copy_ostream_iter

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: copy
3+
# key: oit
4+
# --
5+
std::copy(std::begin(${1:container}), std::end($1), std::ostream_iterator<$2>{
6+
%\istd::cout, "$3"
7+
});

c++-mode/count

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: count
3+
# key: cnt
4+
# --
5+
auto n = std::count(std::begin(${1:container}), std::end($1), $2);

c++-mode/count_if

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: count_if
3+
# key: count_if
4+
# --
5+
auto n = std::count_if(std::begin(${1:container}), std::end($1), []($2) {
6+
$3
7+
});

c++-mode/cout

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- mode: snippet -*-
22
# name: cout
3+
# key: out
4+
# uuid: out
35
# --
4-
`(setq --cpp-ns (if (search "using namespace std;" (buffer-string)) "" "std::"))
5-
--cpp-ns`cout << ${1:string} << `--cpp-ns`endl;`(progn (makunbound '--cpp-ns) "")`
6+
`(setq --cpp-ns (if (doom-snippets-c++-using-std-p) "" "std::"))
7+
--cpp-ns`cout << `%`$1 << `--cpp-ns`endl;`(progn (makunbound '--cpp-ns) "")`

c++-mode/d_operator_istream

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: d_operator>>
3+
# key: >>
4+
# --
5+
friend std::istream& operator>>(std::istream&, const ${1:Name}&);

c++-mode/d_operator_ostream

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: d_operator<<
3+
# key: <<
4+
# --
5+
friend std::ostream& operator<<(std::ostream&, const ${1:Name}&);

c++-mode/equal

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: equal
3+
# key: eql
4+
# --
5+
if (std::equal(std::begin(${1:container}), std::end($1), std::begin($2))) {
6+
`%`$3
7+
}

c++-mode/erase

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: remove
3+
# key: erm
4+
# --
5+
${1:container}.erase(std::remove(std::begin($1), std::end($1), $2), std::end($1));

c++-mode/erase_find_last_not_of

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: generate_n
3+
# key: erf
4+
# --
5+
${1:container}.erase($1.find_last_not_of(" \t\n\r") + 1);

c++-mode/fill

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: fill
3+
# key: fil
4+
# --
5+
std::fill(std::begin(${1:container}), std::end($1), $2);

c++-mode/fill_n

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: fill_n
3+
# key: fln
4+
# --
5+
std::fill_n(std::begin(${1:container}), $2, $3);

c++-mode/fin

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- mode: snippet -*-
2+
# name: find_if_not
3+
# key: fin
4+
# --
5+
auto pos = std::find_if_not(std::begin(${1:container}), std::end($1),[]($2) {
6+
$3
7+
});
8+
if (pos != std::end($1)) {
9+
$4
10+
}
11+
$0

c++-mode/find

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- mode: snippet -*-
2+
# name: find
3+
# key: fnd
4+
# --
5+
auto pos = std::find(std::begin(${1:container}), std::end($1), $2);
6+
if (pos != std::end($1)) {
7+
`%`$3
8+
}

c++-mode/find_end

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- mode: snippet -*-
2+
# name: find_end
3+
# key: fne
4+
# --
5+
auto pos = std::find_std::end(
6+
std::begin(${1:container}), std::end($1),
7+
std::begin($2), std::end($3)
8+
);
9+
if (pos != std::end($1)) {
10+
`%`$4
11+
}

c++-mode/find_first_of

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- mode: snippet -*-
2+
# name: find_first_of
3+
# key: ffo
4+
# --
5+
auto pos = std::find_first_of(
6+
std::begin(${1:container}), std::end($1),
7+
std::begin($2), std::end($3)
8+
);
9+
if (pos != std::end($1)) {
10+
`%`$4
11+
}

c++-mode/find_if

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- mode: snippet -*-
2+
# name: find_if
3+
# key: fni
4+
# --
5+
auto pos = std::find_if(std::begin(${1:container}), std::end($1), []($2) {
6+
`%`$3
7+
});
8+
if (pos != std::end($1)) {
9+
$4
10+
}

c++-mode/for_each

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: for_each
3+
# key: fre
4+
# --
5+
std::for_each(std::begin(${1:container}), std::end($1), []($2) {
6+
`%`$3
7+
});

c++-mode/fori renamed to c++-mode/for_iter

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- mode: snippet -*-
2-
# name: fori
2+
# name: for_iter
33
# key: fori
44
# uuid: fori
55
# --

c++-mode/generate

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: generate
3+
# key: gnr
4+
# --
5+
std::generate(std::begin(${1:container}), std::end($1), []($2) {
6+
`%`$3
7+
});

c++-mode/generate_n

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: generate_n
3+
# key: gnn
4+
# --
5+
std::generate_n(std::begin(${1:container}), $2, []($3) {
6+
`%`$4
7+
});

c++-mode/inch

-4
This file was deleted.

c++-mode/include

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: #include <lib>
3+
# key: inc
4+
# --
5+
#include <`%`${1:lib}>

c++-mode/include_header

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- mode: snippet -*-
2+
# name: #include "{self}.h"
3+
# key: inch
4+
# --
5+
#include "`(file-name-nondirectory (file-name-sans-extension (or (doom-snippets-text) (buffer-file-name) )))`.h"

c++-mode/include_iostream

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- mode: snippet -*-
2+
# name: #include <iostream>
3+
# key: iio
4+
# uuid: iio
5+
# --
6+
#include <iostream>

c++-mode/include_sstream

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- mode: snippet -*-
2+
# name: #include <sstream>
3+
# key: iss
4+
# uuid: iss
5+
# --
6+
#include <sstream>

c++-mode/include_string

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- mode: snippet -*-
2+
# name: #include <string>
3+
# key: istr
4+
# uuid: istr
5+
# --
6+
#include <string>

c++-mode/io

-6
This file was deleted.

0 commit comments

Comments
 (0)