Skip to content

Commit 5e4752b

Browse files
Script does a good job
1 parent 79f9907 commit 5e4752b

Some content is hidden

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

82 files changed

+754
-21
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ Original idea from [Tommy Bennett](https://github.com/tommybennett). It's a port
66

77
## Usage ##
88

9-
FIXME: explanation
9+
Build it with Leiningen:
1010

11-
$ java -jar algorithm-mnemonics-emacs-0.1.0-standalone.jar [args]
11+
lein uberjar
12+
13+
Create the snippets
14+
15+
$ java -jar algorithm-mnemonics-emacs-0.1.0-standalone.jar -p <path_to_write_snippets>
1216

1317
## Bugs ##
1418

project.clj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
(defproject algorithm-mnemonics-emacs "0.1.0-SNAPSHOT"
2-
:description "FIXME: write description"
3-
:url "http://example.com/FIXME"
1+
(defproject algorithm-mnemonics-emacs "0.1.0"
2+
:description "Algorithm Mnemonics: Increase Productivity with STL Algorithms."
3+
:url "https://github.com/ludwigpacifici/algorithm-mnemonics-emacs"
44
:license {:name "GNU General Public License Version 3, 29 June 2007"
55
:url "https://www.gnu.org/licenses/gpl-3.0.en.html"}
6-
:dependencies [[org.clojure/clojure "1.8.0"]]
6+
:dependencies [[org.clojure/clojure "1.8.0"]
7+
[org.clojure/tools.cli "0.3.5"]]
78
:main ^:skip-aot algorithm-mnemonics-emacs.core
89
:target-path "target/%s"
910
:profiles {:uberjar {:aot :all}})
Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
(ns algorithm-mnemonics-emacs.core
22
(:require [clojure.xml :as xml]
3-
[clojure.string :as str])
3+
[clojure.string :as str]
4+
[clojure.tools.cli :as cli])
45
(:gen-class))
56

6-
(def url "https://raw.githubusercontent.com/tommybennett/algorithm-mnemonics/master/algorithm_mnemonics.xml")
7+
(def cli-options
8+
[["-p" "--path PATH" "Path to write the snippets"
9+
:default "./"]
10+
["-f" "--file FILE" "XML file to read. Can be a local file or an URL."
11+
:default "https://raw.githubusercontent.com/tommybennett/algorithm-mnemonics/master/algorithm_mnemonics.xml"]
12+
["-h" "--help"]])
713

8-
(def directory "c++-algorithm/")
9-
10-
(defn -parse [s]
14+
(defn- parse [s]
1115
(xml/parse
1216
(java.io.ByteArrayInputStream. (.getBytes s))))
1317

@@ -19,25 +23,65 @@
1923
"C++ code of the mnemonic"
2024
(get-in mnemonic [:content 0 :content 0]))
2125

22-
(defn- snippet-write [filename snippet]
23-
"Write the snippet <str> in a file <filenamed>"
24-
(spit (str directory filename) snippet))
26+
(defn- snippet-write [filename snippet directory]
27+
"Write the snippet <str> in a file <filename>"
28+
(let [path (str directory filename)]
29+
(println "Writting snippet in " path)
30+
(spit path snippet)))
2531

2632
(defn- snippet-data [name code]
2733
(str "# -*- mode: snippet -*-\n"
2834
"# name: " name "\n"
2935
"# key: " name "\n"
36+
"# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>\n"
3037
"# --"
31-
code))
38+
code
39+
"$0\n"))
40+
41+
(defn- sanitize [code]
42+
(-> code
43+
(str/replace "\t" "")
44+
(str/replace #"(\(|\{|\[) +" "$1")
45+
(str/replace #" +(\)|\}|\])" "$1")))
3246

33-
(defn- snippet [mnemonic]
47+
(defn- convert-placeholders [code]
48+
(-> code
49+
(str/replace-first "%\\m C%" "${1:container}")
50+
(str/replace "%\\m C%" "$1")
51+
(str/replace-first "%\\c" "$2")
52+
(str/replace-first "%\\c" "$3")
53+
(str/replace-first "%\\c" "$4")
54+
(str/replace-first "%\\c" "$5")
55+
(str/replace-first "%\\c" "$6")
56+
(str/replace-first "%\\c" "$7")
57+
(str/replace-first "%\\c" "$8")
58+
(str/replace-first "%\\c" "$9")
59+
(str/replace-first "%\\c" "$10")))
60+
61+
(defn- snippet [mnemonic directory]
3462
(let [name (snippet-name mnemonic)
35-
code (str/replace (snippet-code mnemonic) "\t" "")]
36-
(snippet-write name (snippet-data name code))))
63+
code (-> (snippet-code mnemonic)
64+
sanitize
65+
convert-placeholders)]
66+
(snippet-write name (snippet-data name code) directory)))
67+
68+
(defn- do-it [path file]
69+
(let [raw-mnemonics (parse (slurp file))
70+
mnemonics (get-in raw-mnemonics [:content])
71+
directory (-> (str path "/")
72+
str/trim
73+
(str/replace "//" "/"))]
74+
(.mkdir (java.io.File. directory))
75+
(doseq [m mnemonics] (snippet m directory))
76+
(println (count mnemonics) "generated snippets")))
3777

3878
(defn -main
3979
"Convert the algorithm mnemonic XML file to YASnippet files"
4080
[& args]
41-
(let [mnemonics (parse (slurp url))]
42-
(.mkdir (java.io.File. "c++-algorithm"))
43-
(map snippet (get-in mnemonics [:content]))))
81+
(let [options (cli/parse-opts args cli-options)
82+
help (get-in options [:options :help])
83+
file (get-in options [:options :file])
84+
path (get-in options [:options :path])]
85+
(if help
86+
(println (:summary options))
87+
(do-it path file))))

test/acl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: snippet -*-
2+
# name: acl
3+
# key: acl
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto sum = std::accumulate(begin(${1:container}), end($1), 0, [](int total, $2) {
7+
$3
8+
});
9+
$0

test/acm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: acm
3+
# key: acm
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto sum = std::accumulate(begin(${1:container}), end($1), 0);
7+
$0

test/ajf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- mode: snippet -*-
2+
# name: ajf
3+
# key: ajf
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto pos = std::adjacent_find(begin(${1:container}), end($1));
7+
if (pos != end($1)) {
8+
$2
9+
}
10+
$0

test/alo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- mode: snippet -*-
2+
# name: alo
3+
# key: alo
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
if (std::all_of(begin(${1:container}), end($1), []($2) {
7+
$3
8+
})) {
9+
$4
10+
}
11+
$0

test/ano

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- mode: snippet -*-
2+
# name: ano
3+
# key: ano
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
if (std::any_of(begin(${1:container}), end($1), []($2) {
7+
$3
8+
})) {
9+
$4
10+
}
11+
$0

test/cni

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: snippet -*-
2+
# name: cni
3+
# key: cni
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto n = std::count_if(begin(${1:container}), end($1), []($2) {
7+
$3
8+
});
9+
$0

test/cnt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: cnt
3+
# key: cnt
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto n = std::count(begin(${1:container}), end($1), $2);
7+
$0

test/cpb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: cpb
3+
# key: cpb
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
std::copy_backward(begin(${1:container}), end($1), end($1));
7+
$0

test/cpi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- mode: snippet -*-
2+
# name: cpi
3+
# key: cpi
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
std::copy_if(begin(${1:container}), end($1), begin($2),
7+
[]($3) {
8+
$4
9+
});
10+
$0

test/cpn

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: cpn
3+
# key: cpn
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
std::copy_n(begin(${1:container}), $2, end($1));
7+
$0

test/cpy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: cpy
3+
# key: cpy
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
std::copy(begin(${1:container}), end($1), begin($2));
7+
$0

test/eql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: snippet -*-
2+
# name: eql
3+
# key: eql
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
if (std::equal(begin(${1:container}), end($1), begin($2))) {
7+
$3
8+
}
9+
$0

test/erm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: erm
3+
# key: erm
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
${1:container}.erase(std::remove(begin($1), end($1), $2), end($1));
7+
$0

test/ffo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- mode: snippet -*-
2+
# name: ffo
3+
# key: ffo
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto pos = std::find_first_of(begin(${1:container}), end($1),
7+
begin($2), end($3));
8+
if (pos != end($1)) {
9+
$4
10+
}
11+
$0

test/fil

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: fil
3+
# key: fil
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
std::fill(begin(${1:container}), end($1), $2);
7+
$0

test/fin

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- mode: snippet -*-
2+
# name: fin
3+
# key: fin
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto pos = std::find_if_not(begin(${1:container}), end($1),[]($2) {
7+
$3
8+
});
9+
if (pos != end($1)) {
10+
$4
11+
}
12+
$0

test/fln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- mode: snippet -*-
2+
# name: fln
3+
# key: fln
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
std::fill_n(begin(${1:container}), $2, $3);
7+
$0

test/fnd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- mode: snippet -*-
2+
# name: fnd
3+
# key: fnd
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto pos = std::find(begin(${1:container}), end($1), $2);
7+
if (pos != end($1)) {
8+
$3
9+
}
10+
$0

test/fne

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- mode: snippet -*-
2+
# name: fne
3+
# key: fne
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto pos = std::find_end(begin(${1:container}), end($1),
7+
begin($2), end($3));
8+
if (pos != end($1)) {
9+
$4
10+
}
11+
$0

test/fni

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- mode: snippet -*-
2+
# name: fni
3+
# key: fni
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto pos = std::find_if(begin(${1:container}), end($1), []($2) {
7+
$3
8+
});
9+
if (pos != end($1)) {
10+
$4
11+
}
12+
$0

test/fre

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: snippet -*-
2+
# name: fre
3+
# key: fre
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
std::for_each(begin(${1:container}), end($1), []($2) {
7+
$3
8+
});
9+
$0

test/gnn

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: snippet -*-
2+
# name: gnn
3+
# key: gnn
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
std::generate_n(begin(${1:container}), $2, []($3) {
7+
$4
8+
});
9+
$0

test/gnr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: snippet -*-
2+
# name: gnr
3+
# key: gnr
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
std::generate(begin(${1:container}), end($1), []($2) {
7+
$3
8+
});
9+
$0

test/ihp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: snippet -*-
2+
# name: ihp
3+
# key: ihp
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
if (std::is_heap(begin(${1:container}), end($1))) {
7+
$2
8+
}
9+
$0

test/ihu

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- mode: snippet -*-
2+
# name: ihu
3+
# key: ihu
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
auto pos = std::is_heap_until(begin(${1:container}), end($1));
7+
if (pos != end($1)) {
8+
$2
9+
}
10+
$0

test/ipr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- mode: snippet -*-
2+
# name: ipr
3+
# key: ipr
4+
# contributor: Tommy BENNETT and Ludwig PACIFICI <[email protected]>
5+
# --
6+
if (std::is_permutation(begin(${1:container}), end($1), begin($2))) {
7+
$3
8+
}
9+
$0

0 commit comments

Comments
 (0)