summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrea Crotti <andrea.crotti.0@gmail.com>2016-08-08 14:29:52 +0100
committerGitHub <noreply@github.com>2016-08-08 14:29:52 +0100
commitc6304aa86e6a9c409d0e78672ef247d1abcf8f55 (patch)
treedb2aa767d1c8e6582abfb236ebb1c9b8167adb70
parentf2dab022d8f10a9b09fcdfaafbcb53aeca8cc4a2 (diff)
parent00530f9dd5c174dc85a690edd234c648cc87bf5a (diff)
Merge pull request #135 from sh-ow/dev
Add two generic comment snippets for prog-mode
-rw-r--r--prog-mode/.yas-setup.el35
-rw-r--r--prog-mode/comment6
-rw-r--r--prog-mode/commentblock36
-rw-r--r--prog-mode/commentline13
4 files changed, 90 insertions, 0 deletions
diff --git a/prog-mode/.yas-setup.el b/prog-mode/.yas-setup.el
index 07040d5..22a8168 100644
--- a/prog-mode/.yas-setup.el
+++ b/prog-mode/.yas-setup.el
@@ -2,3 +2,38 @@
(defun yas-with-comment (str)
(format "%s%s%s" comment-start str comment-end))
+
+;; whitespace removing functions from Magnar Sveen ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+(defun yas-s-trim-left (s)
+ "Remove whitespace at the beginning of S."
+ (if (string-match "\\`[ \t\n\r]+" s)
+ (replace-match "" t t s)
+ s))
+
+(defun yas-s-trim-right (s)
+ "Remove whitespace at the end of S."
+ (if (string-match "[ \t\n\r]+\\'" s)
+ (replace-match "" t t s)
+ s))
+
+(defun yas-s-trim (s)
+ "Remove whitespace at the beginning and end of S."
+ (yas-s-trim-left (yas-s-trim-right s)))
+
+
+(defun yas-string-reverse (str)
+ "Reverse a string STR manually to be compatible with emacs versions < 25."
+ (apply #'string
+ (reverse
+ (string-to-list str))))
+
+(defun yas-trimmed-comment-start ()
+ "This function returns `comment-start' trimmed by whitespaces."
+ (yas-s-trim comment-start))
+
+(defun yas-trimmed-comment-end ()
+ "This function returns `comment-end' trimmed by whitespaces if `comment-end' is not empty.
+Otherwise the reversed output of function `yas-trimmed-comment-start' is returned."
+ (if (eq (length comment-end) 0)
+ (yas-string-reverse (yas-trimmed-comment-start))
+ (yas-s-trim comment-end)))
diff --git a/prog-mode/comment b/prog-mode/comment
new file mode 100644
index 0000000..edd53e9
--- /dev/null
+++ b/prog-mode/comment
@@ -0,0 +1,6 @@
+# -*- mode: snippet -*-
+# contributor: sh-ow <sh-ow@users.noreply.github.com>
+# name: comment
+# key: co
+# --
+${1:$(yas-trimmed-comment-start)} ${1:comment}${1:$(unless (eq (length comment-end) 0) (concat " " (yas-trimmed-comment-end)))}$0 \ No newline at end of file
diff --git a/prog-mode/commentblock b/prog-mode/commentblock
new file mode 100644
index 0000000..ba61f61
--- /dev/null
+++ b/prog-mode/commentblock
@@ -0,0 +1,36 @@
+# -*- mode: snippet -*-
+# contributor: sh-ow <sh-ow@users.noreply.github.com>
+# name: commentblock
+# key: cob
+# --
+${1:$(let* ((col (current-column))
+ (str "")
+ (lastcom (substring (yas-trimmed-comment-start) -1))
+ (start (yas-trimmed-comment-start))
+ (end (yas-trimmed-comment-end))
+ (over (- (+ (string-width yas-text) (length start) (length end) col) 77)))
+ (while (< (length str) (+ (- 79 (length start) (length end) col) (if (> over 0) over 0)))
+ (setq str (concat str lastcom)))
+ (concat start str end))}
+${1:$(let* ((col (current-column))
+ (str "")
+ (start (yas-trimmed-comment-start))
+ (end (yas-trimmed-comment-end)))
+ (while (< (length str) (ffloor (/ (- 78.0 (+ col (length start) (string-width yas-text) (length end))) 2.0)))
+ (setq str (concat str " ")))
+ (concat start str))} ${1:comment} ${1:$(let* ((col (current-column))
+ (str "")
+ (start (yas-trimmed-comment-start))
+ (end (yas-trimmed-comment-end)))
+ (while (< (length str) (- 79.0 (if (eq (mod (string-width yas-text) 2) 1) (- col 1) col) (length end)))
+ (setq str (concat str " ")))
+ (concat str end))}
+${1:$(let* ((col (current-column))
+ (str "")
+ (lastcom (substring (yas-trimmed-comment-start) -1))
+ (start (yas-trimmed-comment-start))
+ (end (yas-trimmed-comment-end))
+ (over (- (+ (string-width yas-text) (length start) (length end) col) 77)))
+ (while (< (length str) (+ (- 79 (length start) (length end) col) (if (> over 0) over 0)))
+ (setq str (concat str lastcom)))
+ (concat start str end))}$0 \ No newline at end of file
diff --git a/prog-mode/commentline b/prog-mode/commentline
new file mode 100644
index 0000000..0b0edff
--- /dev/null
+++ b/prog-mode/commentline
@@ -0,0 +1,13 @@
+# -*- mode: snippet -*-
+# contributor: sh-ow <sh-ow@users.noreply.github.com>
+# name: commentline
+# key: col
+# --
+${1:$(yas-trimmed-comment-start)} ${1:comment} ${1:$(let* ((str "")
+ (curr (current-column))
+ (start (yas-trimmed-comment-start))
+ (lastcom (substring start -1))
+ (end (yas-trimmed-comment-end)))
+ (while (< (length str) (- 79 (+ curr (length end))))
+ (setq str (concat str lastcom)))
+ (concat str end))}$0 \ No newline at end of file