-
Notifications
You must be signed in to change notification settings - Fork 3
/
sniem-mark-jump.el
197 lines (166 loc) · 6.35 KB
/
sniem-mark-jump.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
;;; sniem-mark-jump.el --- Hands-eased united editing method -*- lexical-binding: t -*-
;; Author: SpringHan
;; Maintainer: SpringHan
;; This file is not part of GNU Emacs
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Hands-eased united editing method.
;;; Code:
(require 'sniem-common)
(require 'sniem-operation)
(declare-function sniem-change-mode "sniem")
(defcustom sniem-mark-jump-items
'("TODO" "BUG" "NOTE")
"The faces for `sniem-mark-jump'."
:type 'list
:group 'sniem)
(defcustom sniem-mark-jump-author-name nil
"The default name for mark."
:type 'string
:group 'sniem)
(defcustom sniem-mark-jump-author-name-enable nil
"If enable the author name."
:type 'boolean
:group 'sniem)
(defcustom sniem-mark-jump-regexp nil
"The regexp for mark."
:type 'string
:group 'sniem)
(defun sniem-mark-jump-lisp-mode-p ()
"Check if the current major mode belongs to Lisp mode."
(string-match-p "\\(?:.*\\)lisp\\(?:.*\\)" (symbol-name major-mode)))
(defun sniem-mark-jump-insert (type &optional name)
"Insert the mark with TYPE.
Optional argument NAME means enable the name."
(interactive (list (completing-read "Enter the type: "
sniem-mark-jump-items)))
(unless (eq (face-at-point) 'font-lock-comment-face)
(if (and (eolp)
(not (looking-back "^[\s\t]*" (line-beginning-position) t)))
(insert (if (not (memq (char-before) '(?\t ?\s)))
" "
"")
comment-start)
(unless (= (line-beginning-position) (line-end-position))
(sniem-open-line-previous))
(insert (if (sniem-mark-jump-lisp-mode-p)
(concat comment-start comment-start)
comment-start))))
(insert (if (string= " " (substring comment-start -1))
""
" ")
type
(format "%s: " (if (or name sniem-mark-jump-author-name-enable)
(sniem-mark-jump--get-author-name)
"")))
(when comment-end
(save-mark-and-excursion
(insert comment-end)))
(sniem-change-mode 'insert))
(defun sniem-mark-jump-insert-with-name (&optional name)
"`sniem-mark-jump-insert' with NAME."
(interactive "P")
(if name
(funcall #'sniem-mark-jump-insert
(completing-read "Enter the type: " sniem-mark-jump-items) t)
(call-interactively #'sniem-mark-jump-insert)))
(sniem-define-motion sniem-mark-jump-next (&optional type)
"Jump next."
(interactive "P")
(when type
(setq type (sniem-mark-jump--get-type type)))
(sniem-mark-jump--jump t type))
(sniem-define-motion sniem-mark-jump-prev (&optional type)
"Jump previous."
(interactive "P")
(when type
(setq type (sniem-mark-jump--get-type type)))
(sniem-mark-jump--jump nil type))
(defun sniem-mark-jump--get-type (num)
"Get type by NUM."
(nth (1- num) sniem-mark-jump-items))
(defun sniem-mark-jump--jump (next &optional type)
"Jump to NEXT/previous item.
Optional argument TYPE is the type of comment mark."
(let ((search-command (if next
're-search-forward
're-search-backward))
(point (point))
(case-fold-search nil)
(tmp t))
(when (sniem-mark-jump--comment-face-p)
(sniem-mark-jump--escape-comment (when next t)))
(catch 'stop
(while tmp
(setq tmp (funcall search-command
(concat "\\(" comment-start "*\\)"
(if type
(concat "\\(?:.*\\)" type "\\(?:.*\\)")
sniem-mark-jump-regexp))
nil t))
(when (and tmp (numberp tmp))
(goto-char (comment-beginning))
(throw 'stop t)))
(message "[Sniem]: The mark can not be found.")
(goto-char point))))
(defun sniem-mark-jump--escape-comment (forward)
"Escape current comment.
Argument FORWARD means search forward."
(let ((motion (if forward
'forward-char
'backward-char)))
(while (sniem-mark-jump--comment-face-p)
(funcall motion))))
(defun sniem-mark-jump--comment-face-p ()
"Check if the content at point has the comment face."
(let ((face-list (get-text-property (point) 'face)))
(when face-list
(or (and (symbolp face-list)
(or (eq face-list 'font-lock-comment-face)
(eq face-list 'font-lock-comment-delimiter-face)))
(and (listp face-list) (memq 'font-lock-comment-face face-list))))))
(defun sniem-mark-jump--get-author-name ()
"Get the author's name."
(if sniem-mark-jump-author-name
sniem-mark-jump-author-name
(unless sniem-mark-jump-author-name-enable
(setq-local sniem-mark-jump-author-name-enable t))
(let ((tmp (read-string "Enter your name: ")))
(if (string-equal tmp "")
;; When the name string is empty, disabling to insert author name.
(progn
(setq-local sniem-mark-jump-author-name-enable nil)
"")
tmp))))
(defun sniem-mark-jump-reset-regexp ()
"Reset the regexp."
(setq sniem-mark-jump-regexp
(concat "\\(?:.*\\)\\("
(mapconcat #'sniem-mark-jump--self sniem-mark-jump-items "\\|")
"\\)\\(?:.*\\)")))
(defun sniem-mark-jump--self (arg)
"Return ARG itself."
arg)
(sniem-mark-jump-reset-regexp)
;;; For user
(defun sniem-mark-jump-set-items (way item)
"Set `sniem-mark-jump-items'.
The WAY includes:
:add - Add a new ITEM.
:delete - Delete a item."
(pcase way
(:add (unless (sniem--mems item sniem-mark-jump-items)
(setq sniem-mark-jump-items (append sniem-mark-jump-items (list item)))))
(:delete (setq sniem-mark-jump-items (delete item sniem-mark-jump-items))))
(sniem-mark-jump-reset-regexp))
(provide 'sniem-mark-jump)
;;; sniem-mark-jump.el ends here