1- ; ;; com-css-sort.el --- Common way of sorting the CSS attributes. -*- lexical-binding : t ; -*-
1+ ; ;; com-css-sort.el --- Common way of sorting the CSS attributes -*- lexical-binding : t ; -*-
22
33; ; Copyright (C) 2018 Shen, Jen-Chieh
44; ; Created date 2018-04-30 14:26:37
77; ; Description: Common way of sorting the CSS attributes.
88; ; Keyword: Common CSS Handy Sort Sorting
99; ; Version: 0.0.5
10- ; ; Package-Requires: ((emacs "24.4 ") (s "1.12.0") (cl-lib "0.6 "))
10+ ; ; Package-Requires: ((emacs "25.1 ") (s "1.12.0"))
1111; ; URL: https://github.com/jcs090218/com-css-sort
1212
1313; ; This file is NOT part of GNU Emacs.
3636(require 's )
3737(require 'subr-x )
3838
39-
4039(defgroup com-css-sort nil
4140 " Sort CSS attributes extension"
4241 :prefix " com-css-sort-"
4342 :group 'editing
4443 :link '(url-link :tag " Repository" " https://github.com/jcs090218/com-css-sort" ))
4544
46-
4745(defcustom com-css-sort-sort-type 'type-sort
4846 " Type of sorting CSS attributes algorithm going to use to sort.
4947'type-sort : Sort by Type Group.
@@ -142,52 +140,55 @@ This wil replace `com-css-sort-default-attributes-order' if it can."
142140 :type 'list
143141 :group 'com-css-sort )
144142
143+ ; ;; Util
145144
146- (defun com-css-sort-get-string-from-file (file-path )
145+ (defun com-css-sort--goto-line (ln )
146+ " Goto LN line number."
147+ (goto-char (point-min )) (forward-line (1- ln)))
148+
149+ (defun com-css-sort--get-string-from-file (file-path )
147150 " Return FILE-PATH's file content."
148151 (with-temp-buffer
149152 (insert-file-contents file-path)
150153 (buffer-string )))
151154
152- (defun com-css-sort-get-ccs-file-list ()
155+ (defun com-css-sort-- get-ccs-file-list ()
153156 " Get the `com-css-sort-sort-file' and turn it into list."
154157 (let ((sort-file-path (concat (cdr (project-current )) com-css-sort-sort-file))
155158 (attr-list '())
156159 (sort-file-content '()))
157160 (when (file-exists-p sort-file-path)
158161 ; ; Get the file content as buffer.
159- (setq sort-file-content (com-css-sort-get-string-from-file sort-file-path))
162+ (setq sort-file-content (com-css-sort-- get-string-from-file sort-file-path))
160163 ; ; Split the file content buffer into list.
161164 (setq attr-list (split-string sort-file-content)))
162165 ; ; Return the sort order list.
163166 attr-list))
164167
165- (defun com-css-sort-back-to-indentation-or-beginning ()
168+ (defun com-css-sort-- back-to-indentation-or-beginning ()
166169 " Toggle between first character and beginning of line."
167- (when (= (point ) (progn (back-to-indentation ) (point )))
168- (beginning-of-line )))
170+ (when (= (point ) (progn (back-to-indentation ) (point ))) (beginning-of-line )))
169171
170- (defun com-css-sort-get-current-line ()
172+ (defun com-css-sort-- get-current-line ()
171173 " Return the current line into string."
172174 (thing-at-point 'line t ))
173175
174- (defun com-css-sort-is-beginning-of-line-p ()
176+ (defun com-css-sort-- is-beginning-of-line-p ()
175177 " Is at the beginning of line?"
176178 (save-excursion
177- (let ((current-point nil )
178- (begin-line-point nil ))
179+ (let ((current-point nil ) (begin-line-point nil ))
179180 (setq current-point (point ))
180181 (beginning-of-line )
181182 (setq begin-line-point (point ))
182183 (= begin-line-point current-point))))
183184
184- (defun com-css-sort-goto-first-char-in-line ()
185+ (defun com-css-sort-- goto-first-char-in-line ()
185186 " Goto beginning of line but ignore 'empty characters'(spaces/tabs)."
186- (com-css-sort-back-to-indentation-or-beginning)
187- (when (com-css-sort-is-beginning-of-line-p)
188- (com-css-sort-back-to-indentation-or-beginning)))
187+ (com-css-sort-- back-to-indentation-or-beginning)
188+ (when (com-css-sort-- is-beginning-of-line-p)
189+ (com-css-sort-- back-to-indentation-or-beginning)))
189190
190- (defun com-css-sort-current-line-empty-p ()
191+ (defun com-css-sort-- current-line-empty-p ()
191192 " Current line empty, but accept spaces/tabs in there. (not absolute)."
192193 (save-excursion
193194 (beginning-of-line )
@@ -202,28 +203,27 @@ This wil replace `com-css-sort-default-attributes-order' if it can."
202203If non-nil, attribute line.
203204If nil, is not attribute line."
204205 ; ; Check attribute line by simply searching key character ':' colon.
205- (string-match-p " :" (com-css-sort-get-current-line)))
206+ (string-match-p " :" (com-css-sort-- get-current-line)))
206207
207208(defun com-css-sort--currnet-line-not-attribute-comment-line ()
208209 " Check if current line is a 'pure' comment line, not an attribute comment line."
209210 (save-excursion
210- (com-css-sort-goto-first-char-in-line)
211- (forward-char 1 )
212- (forward-char 1 )
211+ (com-css-sort--goto-first-char-in-line)
212+ (forward-char 2 )
213213 (and
214214 ; ; First check if is comment line.
215215 (com-css-sort--is-inside-comment-block-p)
216216 ; ; Then check if current line attribute line.
217217 (not (com-css-sort--attribute-line)))))
218218
219- (defun com-css-sort-get-sort-list-until-empty-or-comment-line ()
219+ (defun com-css-sort-- get-sort-list-until-empty-or-comment-line ()
220220 " Get the list we want to sort.
221221Depends on if we meet a empty line or a comment line."
222222 (save-excursion
223223 (let ((line-list '()))
224- (while (and (not (com-css-sort-current-line-empty-p))
224+ (while (and (not (com-css-sort-- current-line-empty-p))
225225 (not (com-css-sort--currnet-line-not-attribute-comment-line)))
226- (let ((current-line (com-css-sort-get-current-line)))
226+ (let ((current-line (com-css-sort-- get-current-line)))
227227 (when (not (string-match " }" current-line))
228228 ; ; Push the line into list.
229229 (push current-line line-list)))
@@ -234,34 +234,32 @@ Depends on if we meet a empty line or a comment line."
234234 line-list)))
235235
236236(defun com-css-sort--next-blank-or-comment-line ()
237- " Move to the next line containing nothing but whitespace or \
238- first character is a comment line."
237+ " Move to the next line containing nothing but whitespace or first character \
238+ is a comment line."
239239 (forward-line 1 )
240- (while (and (not (com-css-sort-current-line-empty-p))
240+ (while (and (not (com-css-sort-- current-line-empty-p))
241241 (not (com-css-sort--currnet-line-not-attribute-comment-line)))
242242 (forward-line 1 )))
243243
244244(defun com-css-sort--next-non-blank-or-comment-line ()
245245 " Move to the next line that is exactly the code.
246246Not the comment or empty line."
247247 (forward-line 1 )
248- (while (and (or (com-css-sort-current-line-empty-p)
248+ (while (and (or (com-css-sort-- current-line-empty-p)
249249 (com-css-sort--currnet-line-not-attribute-comment-line))
250250 (not (= (point ) (point-max ))))
251251 (forward-line 1 )))
252252
253253(defun com-css-sort--beginning-of-attribute-block (start )
254- " Get the beginning of the attribute block.
255- START : current point."
254+ " Get the beginning of the attribute block from current point (START)."
256255 (goto-char start)
257256 (search-backward " {" )
258257 (forward-line 1 )
259258 (beginning-of-line )
260259 (point ))
261260
262261(defun com-css-sort--end-of-attribute-block (start )
263- " Get the end of the attribute block.
264- START : current point."
262+ " Get the end of the attribute block from current point (START)."
265263 (goto-char start)
266264 (re-search-forward " [{}]" )
267265 (forward-line -1 )
@@ -270,9 +268,7 @@ START : current point."
270268
271269(defun com-css-sort--insert-line-list (line-list )
272270 " Insert list of line, LINE-LIST."
273- (save-excursion
274- (dolist (line line-list)
275- (insert line))))
271+ (save-excursion (dolist (line line-list) (insert line))))
276272
277273(defun com-css-sort--swap-list-element (lst index-a index-b )
278274 " Swap the element by using two index.
@@ -282,17 +278,14 @@ INDEX-B : b index of the element."
282278 (cl-rotatef (nth index-a lst) (nth index-b lst)))
283279
284280(defun com-css-sort-sort-line-list-by-type-group (line-list )
285- " Sort line list into type group order.
286- LINE-LIST : list of line."
281+ " Sort LINE-LIST into type group order."
287282 (let (; ; List of index, corresponds to true value. (line)
288283 (index-list '())
289284 ; ; Final return list.
290285 (return-line-list '())
291- ; ; List we are going to actually use it in our algorithm.
292- ; ; This will determine if we use the users file or
293- ; ; use the default file.
294- (real-sort-list (com-css-sort-get-ccs-file-list)))
295-
286+ ; ; List we are going to actually use it in our algorithm. This will determine
287+ ; ; if we use the users file or use the default file.
288+ (real-sort-list (com-css-sort--get-ccs-file-list)))
296289 ; ; If we could not find the user sort order config file.
297290 ; ; We use default list then.
298291 (when (= 0 (length real-sort-list))
@@ -317,55 +310,34 @@ LINE-LIST : list of line."
317310 ; ; Trim the whitespaces or tabs.
318311 (setq first-word-in-line (string-trim first-word-in-line))
319312
320- (setq index (cl-position first-word-in-line
321- real-sort-list
322- :test 'string= ))
313+ (setq index (cl-position first-word-in-line real-sort-list :test 'string= ))
323314
324315 (if (numberp index)
325316 (progn
326- ; ; Add both index and line value to list.
327- ; ; Treat this as a `pair' data structure.
317+ ; ; Add both index and line value to list. Treat this as a `pair'
318+ ; ; data structure.
328319 (push index index-list)
329320 (push in-line return-line-list))
330- (error " You try to sort an CSS attribute that does not in the sort list : %s " first-word-in-line))))
321+ (user- error " [WARNINGS] You try to sort an CSS attribute that does not in the sort list : %s" first-word-in-line))))
331322
332323 ; ; Bubble sort the elements.
333- (let ((index-i 0 )
334- (flag t ))
335- (while (and (< index-i (- (length index-list) 1 ))
336- flag)
337-
338- ; ; Reset flag.
324+ (let ((index-i 0 ) (flag t ))
325+ (while (and (< index-i (- (length index-list) 1 )) flag)
339326 (setq flag nil )
340-
341327 (let ((index-j 0 ))
342328 (while (< index-j (- (- (length index-list) index-i) 1 ))
343-
344- (let ((index-a index-j)
345- (index-b (1+ index-j))
346- (value-a -1 )
347- (value-b -1 ))
348- (setq value-a (nth index-a index-list))
349- (setq value-b (nth index-b index-list))
350-
329+ (let* ((index-a index-j)
330+ (index-b (1+ index-j))
331+ (value-a (nth index-a index-list))
332+ (value-b (nth index-b index-list)))
351333 (when (< value-b value-a)
352334 ; ; Swap index.
353- (com-css-sort--swap-list-element index-list
354- index-b
355- index-a)
335+ (com-css-sort--swap-list-element index-list index-b index-a)
356336 ; ; Swap value with same index.
357- ; ; NOTE: we do this much is all because
358- ; ; of this line of code.
359- (com-css-sort--swap-list-element return-line-list
360- index-b
361- index-a)
362-
363- ; ; Set flag.
337+ ; ; NOTE: we do this much is all because of this line of code.
338+ (com-css-sort--swap-list-element return-line-list index-b index-a)
364339 (setq flag t )))
365-
366- ; ; inc j.
367340 (setq index-j (1+ index-j))))
368- ; ; inc i.
369341 (setq index-i (1+ index-i))))
370342
371343 ; ; Return the sorted list.
@@ -376,7 +348,7 @@ LINE-LIST : list of line."
376348 " Sort CSS attributes in the block.
377349NO-BACK-TO-LINE : Do not go back to the original line."
378350 (interactive )
379- (let ((start-line-num ( string-to- number ( format-mode-line " %l " ) )))
351+ (let ((start-ln ( line- number-at-pos nil t )))
380352 (save-excursion
381353 (save-window-excursion
382354 ; ; Ready to start sorting in the block.
@@ -388,7 +360,7 @@ NO-BACK-TO-LINE : Do not go back to the original line."
388360
389361 (while (< (point ) end)
390362 ; ; Get the empty/comment block of line list for next use.
391- (let ((line-list (com-css-sort-get-sort-list-until-empty-or-comment-line))
363+ (let ((line-list (com-css-sort-- get-sort-list-until-empty-or-comment-line))
392364 (end-region-point nil ))
393365 ; ; Get the current point again.
394366 (setq current (point ))
@@ -399,9 +371,9 @@ NO-BACK-TO-LINE : Do not go back to the original line."
399371
400372 ; ; Find the last point
401373 (let ((record-point (point )))
402- (when (com-css-sort-current-line-empty-p)
374+ (when (com-css-sort-- current-line-empty-p)
403375 (forward-line -1 )
404- (if (string-match " }" (com-css-sort-get-current-line))
376+ (if (string-match " }" (com-css-sort-- get-current-line))
405377 (beginning-of-line )
406378 ; ; Back to point if not true.
407379 (goto-char record-point))))
@@ -410,16 +382,12 @@ NO-BACK-TO-LINE : Do not go back to the original line."
410382 ; ; Delete region.
411383 (delete-region current end-region-point)
412384
413- ; ; NOTE: Design your sort algorithms here
414- ; ; depend on the type.
415- (cond (; ; OPTION: Sort by Type Group.
416- (string= com-css-sort-sort-type 'type-sort )
417- (progn
418- (setq line-list (com-css-sort-sort-line-list-by-type-group line-list))))
419- (; ; OPTION: Sort by Alphabetic Order.
420- (string= com-css-sort-sort-type 'alphabetic-sort )
421- (progn
422- (setq line-list (sort line-list 'string< )))))
385+ ; ; NOTE: Design your sort algorithms here depend on the type.
386+ (cl-case com-css-sort-sort-type
387+ ('type-sort ; OPTION: Sort by Type Group.
388+ (setq line-list (com-css-sort-sort-line-list-by-type-group line-list)))
389+ ('alphabetic-sort ; OPTION: Sort by Alphabetic Order.
390+ (setq line-list (sort line-list 'string< ))))
423391
424392 ; ; Insert the lines.
425393 (com-css-sort--insert-line-list line-list)))
@@ -430,30 +398,23 @@ NO-BACK-TO-LINE : Do not go back to the original line."
430398 ; ; Then goto next code line.
431399 (com-css-sort--next-non-blank-or-comment-line)))))
432400
433- (when (not no-back-to-line)
434- (with-no-warnings
435- (goto-line start-line-num))
401+ (unless no-back-to-line
402+ (com-css-sort--goto-line start-ln)
436403 (end-of-line ))))
437404
438405;;;### autoload
439406(defun com-css-sort-attributes-document ()
440407 " Sort CSS attributes the whole documents."
441408 (interactive )
442- (let ((start-line-num ( string-to- number ( format-mode-line " %l " ) )))
409+ (let ((start-ln ( line- number-at-pos nil t )))
443410 (save-excursion
444411 (save-window-excursion
445412 (goto-char (point-min ))
446-
447- (while (ignore-errors (search-forward " }" ))
448- ; ; Sort once.
449- (com-css-sort-attributes-block t )
450-
451- ; ; Goto next blank line
413+ (while (search-forward " }" nil t )
414+ (com-css-sort-attributes-block t ) ; Sort once.
452415 (com-css-sort--next-blank-or-comment-line))))
453-
454416 ; ; make sure go back to the starting line.
455- (with-no-warnings
456- (goto-line start-line-num))
417+ (com-css-sort--goto-line start-ln)
457418 (end-of-line )))
458419
459420(provide 'com-css-sort )
0 commit comments