diff options
author | sh-ow <sh-ow@users.noreply.github.com> | 2016-04-14 00:59:31 +0200 |
---|---|---|
committer | sh-ow <sh-ow@users.noreply.github.com> | 2016-04-14 00:59:31 +0200 |
commit | e2409dd612e22497f50677aad6cda4e50e1aa7ea (patch) | |
tree | 33b9392daee8b68ec89754963d27a9593c60ad7c | |
parent | 9b30fe4123b1b2b14f4234a5c4bf2ef5301904e0 (diff) |
prog-mode: rename yas-get-comment-xxx functions, different approach for whitespace trim
-rw-r--r-- | prog-mode/.yas-setup.el | 40 | ||||
-rw-r--r-- | prog-mode/comment | 2 | ||||
-rw-r--r-- | prog-mode/commentblock | 20 | ||||
-rw-r--r-- | prog-mode/commentline | 16 |
4 files changed, 46 insertions, 32 deletions
diff --git a/prog-mode/.yas-setup.el b/prog-mode/.yas-setup.el index d70faf0..fa548ab 100644 --- a/prog-mode/.yas-setup.el +++ b/prog-mode/.yas-setup.el @@ -1,23 +1,37 @@ (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-get-comment-start () - (substring comment-start 0 - (dotimes (i (length comment-start)) - (unless (or (eq (aref (yas-string-reverse comment-start) i) nil) - (eq (aref (yas-string-reverse comment-start) i) 32)) - (return (- (length comment-start) i)))))) +(defun yas-trimmed-comment-start () + "This function returns `comment-start' trimmed by whitespaces." + (yas-s-trim comment-start)) -(defun yas-get-comment-end () +(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-get-comment-start)) - (substring comment-end - (dotimes (i (length comment-end)) - (unless (or (eq (aref comment-end i) nil) - (eq (aref comment-end i) 32)) - (return i)))))) + (yas-string-reverse (yas-trimmed-comment-start)) + (yas-s-trim comment-end))) diff --git a/prog-mode/comment b/prog-mode/comment index 3a0f6c5..edd53e9 100644 --- a/prog-mode/comment +++ b/prog-mode/comment @@ -3,4 +3,4 @@ # name: comment # key: co # -- -${1:$(yas-get-comment-start)} ${1:comment}${1:$(unless (eq (length comment-end) 0) (concat " " (yas-get-comment-end)))}$0
\ No newline at end of file +${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 index 09bcddb..ba61f61 100644 --- a/prog-mode/commentblock +++ b/prog-mode/commentblock @@ -5,31 +5,31 @@ # -- ${1:$(let* ((col (current-column)) (str "") - (lastcom (substring (yas-get-comment-start) -1)) - (start (yas-get-comment-start)) - (end (yas-get-comment-end)) + (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-get-comment-start)) - (end (yas-get-comment-end))) + (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-get-comment-start)) - (end (yas-get-comment-end))) + (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-get-comment-start) -1)) - (start (yas-get-comment-start)) - (end (yas-get-comment-end)) + (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))) diff --git a/prog-mode/commentline b/prog-mode/commentline index e8a5a8c..0b0edff 100644 --- a/prog-mode/commentline +++ b/prog-mode/commentline @@ -3,11 +3,11 @@ # name: commentline # key: col # -- -${1:$(yas-get-comment-start)} ${1:comment} ${1:$(let* ((str "") - (curr (current-column)) - (start (yas-get-comment-start)) - (lastcom (substring start -1)) - (end (yas-get-comment-end))) - (while (< (length str) (- 79 (+ curr (length end)))) - (setq str (concat str lastcom))) - (concat str end))}$0
\ No newline at end of file +${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 |