-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathidee-comments.el
116 lines (100 loc) · 3.97 KB
/
idee-comments.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
;; idee-comments.el --- Comment handling
;; Copyright (C) 2018 Ioannis Canellos
;;
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;
;; Author: Ioannis Canellos
;;; Commentary:
;;; Code:
(require 'idee-vars)
(require 'idee-utils)
;;
;; Functions
;;
(defun idee/comment-style-of-buffer()
"Return the buffer comment style."
(let* ((name (buffer-file-name))
(extension (if name (file-name-extension name) nil)))
(cdr (assoc extension idee/type-comment-styles-alist))))
;;;###autoload
(defun idee/comment (content extension)
"Apply comments to CONTENT for file EXTENSION."
(let ((s (cdr (assoc extension idee/type-comment-styles-alist))))
(if content
(concat (idee/comment-style-custom-block-beginning s)
(mapconcat 'identity (mapcar
(lambda (l) (concat (idee/comment-style-custom-line-prefix s) l "\n"))
(split-string content "\n")) "")
(idee/comment-style-custom-block-ending s))
nil)))
;;;###autoload
(defun idee/comment-remove-at-point (&optional style)
"Remove the comment at the current point."
(interactive)
(save-excursion
(let* ((style (or style (idee/comment-style-of-buffer)))
(block-beginning (idee/comment-style-block-beginning style))
(prefix (idee/comment-style-line-prefix style))
(block-ending (idee/comment-style-block-ending style))
(current (point))
(begin (point-min))
(end (point-min))
(next (point-max)))
(if (and block-beginning block-ending)
;; Detect end and start of comment.
(progn
;; Move back enough characters so that we can read the end of the comment.
(goto-char (- current (length block-ending)))
(setq end (search-forward block-ending nil t))
(setq begin (search-backward block-beginning nil t))
(if end
(progn
(goto-char end)
(setq next (search-forward block-beginning end t))
(if (and (>= current begin) (or (not next) (< end next)))
(delete-region begin end)
(message "no comment detected at point.")))))
(progn
(if (not (equal 1 (line-number-at-pos)))
(while (idee/line-above-commented-or-empty-p) (forward-line -1)))
(while (idee/line-commented-p) (kill-whole-line)))))))
(defun idee/line-commented-p ()
"Check if current line is commented."
(interactive)
(let* ((style (idee/comment-style-of-buffer))
(prefix (idee/comment-style-line-prefix style))
;;(line (thing-at-point 'line t))
(begin (idee/point-beginning-of-line))
(end (idee/point-end-of-line))
(line (buffer-substring begin end)))
(string-match (format "^[[:space:]]*%s" prefix) line)))
(defun idee/line-empty-p ()
"Check if current line is empty."
(save-excursion
(beginning-of-line)
(looking-at "[[:space:]]*$")))
(defun idee/line-commented-or-empty-p ()
"Check if current line is commented."
(interactive)
(or (idee/line-empty-p) (idee/line-commented-p)))
(defun idee/line-above-commented-or-empty-p()
"Check if the line above is commented or empty."
(save-excursion
(if (= (line-number-at-pos) 0)
nil
(progn
(forward-line -1)
(idee/line-commented-or-empty-p)))))
(provide 'idee-comments)
;;; idee-comments.el ends here