enhanced toggle function: toggle list easier to maintain (reverse entries are now generated), also now case will be preserved when toggling

This commit is contained in:
Thomas von Dein
2017-06-27 20:49:52 +02:00
parent d126019252
commit 212839ce05

View File

@@ -121,14 +121,10 @@
(defcustom config-general-toggle-values (defcustom config-general-toggle-values
'(("true" . "false") '(("true" . "false")
("false" . "true")
("on" . "off") ("on" . "off")
("off" . "on")
("yes" . "no") ("yes" . "no")
("no" . "yes") ("enable" . "disable"))
("enable" . "disable") "Values which can be toggled with <C-c C-t>. Only pairs are supported."
("disable" . "enable"))
"Values which can be toggled, for simplicity's sake, add both values."
:group 'config-general :group 'config-general
:type 'list) :type 'list)
@@ -244,19 +240,30 @@ completion based on (point) position."
(back-to-indentation)))) (back-to-indentation))))
(defun config-general-toggle-flag () (defun config-general-toggle-flag ()
"Toggle a value of the list `config-general-toggle-values'." "Toggle a value from the list `config-general-toggle-values' to its reverse.
Case will be preserved, the toggle list can be modified on the fly."
(interactive) (interactive)
(let* ((thing (downcase (thing-at-point 'word t))) (let* ((thing (downcase (thing-at-point 'word t)))
(flag (assoc thing config-general-toggle-values)) (flag (assoc thing (config-general--toggle-list)))
(A (car (bounds-of-thing-at-point 'word))) (A (car (bounds-of-thing-at-point 'word)))
(B (cdr (bounds-of-thing-at-point 'word)))) (B (cdr (bounds-of-thing-at-point 'word))))
(when (and thing flag) (when (and thing flag)
(goto-char B) ;; idea from: https://emacs.stackexchange.com/questions/24601
(backward-kill-word 1) ;; /replace-word-at-point-preserving-the-case-pattern/24617
(insert (cdr flag))))) (set-match-data (list A B))
(replace-match (cdr flag)))))
;;;; Internal Functions ;;;; Internal Functions
(defun config-general--toggle-list ()
"Add each entry of `config-general-toggle-values' in its reverse form
and return a new list of both forms."
(let ((N config-general-toggle-values))
(dolist (E config-general-toggle-values)
(add-to-list 'N `(,(cdr E) . ,(car E)) t)
)
N))
(defun config-general--fl-beg-eof (limit) (defun config-general--fl-beg-eof (limit)
(re-search-forward "<<\\([A-Z0-9]+\\)\n" limit t)) (re-search-forward "<<\\([A-Z0-9]+\\)\n" limit t))