-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcyphejor.el
181 lines (153 loc) · 6.26 KB
/
cyphejor.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
;;; cyphejor.el --- Shorten major mode names using user-defined rules -*- lexical-binding: t; -*-
;;
;; Copyright © 2015–present Mark Karpov <[email protected]>
;;
;; Author: Mark Karpov <[email protected]>
;; URL: https://github.com/mrkkrp/cyphejor
;; Version: 0.1.2
;; Package-Requires: ((emacs "24.4"))
;; Keywords: convenience
;;
;; This file is not part of GNU Emacs.
;;
;; This program 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 of the License, 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.
;;
;; You should have received a copy of the GNU General Public License along
;; with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package shortens major mode names by using a set of user-defined rules.
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(defgroup cyphejor nil
"Shorten major mode names using user-defined rules."
:group 'convenience
:tag "Cyphejor"
:prefix "cyphejor-"
:link '(url-link :tag "GitHub" "https://github.com/mrkkrp/cyphejor"))
(defcustom cyphejor-rules nil
"Rules used to convert names of major modes.
Every element of the list must be of the following form:
(STRING REPLACEMENT &rest PARAMETERS)
where STRING is a word in the major mode's symbol name,
REPLACEMENT is a string to be used instead of that word,
PARAMETERS is a list that may contain the following keywords:
:prefix—put it at the beginning of result string
:postfix—put it at the end of result string
The following keywords influence the algorithm in general:
:downcase—replace words that are not matched explicitly with
their first letter downcased
:upcase—replace words that are not matched explicitly with
their first letter upcased
:capitalize—replace words that are not matched explicitly with
the same words but capitalizing their first letters.
If nothing is specified, a word will be used unchanged, separated
from other words with spaces if necessary."
:tag "Active rules"
:type '(repeat
(choice
(const :tag "use first downcased letter" :downcase)
(const :tag "use first upcased letter" :upcase)
(const :tag "capitalize the first letter" :capitalize)
(list string string)
(list string string
(choice (const :tag "put it in the beginning" :prefix)
(const :tag "put it in the end" :postfix))))))
(defun cyphejor--cypher (old-name rules)
"Convert the OLD-NAME into its shorter form following RULES.
The format of RULES is described in the doc-string of
`cyphejor-rules'.
OLD-NAME must be a string where words are separated with
punctuation characters."
(let ((words (split-string (downcase old-name) "[\b\\-]+" t))
(downcase (cl-find :downcase rules))
(upcase (cl-find :upcase rules))
(capitalize (cl-find :capitalize rules))
prefix-words
postfix-words
conversion-table
prefix-result
result
postfix-result)
(dolist (rule (cl-remove-if-not #'listp rules))
(let ((before (car rule))
(after (cadr rule))
(where (cl-caddr rule)))
(push (cons before after) conversion-table)
(cl-case where
(:prefix (push before prefix-words))
(:postfix (push before postfix-words)))))
(dolist (word words)
(let ((translated
(or (cdr (assoc word conversion-table))
(cond (downcase (cl-subseq word 0 1))
(upcase (upcase (cl-subseq word 0 1)))
(capitalize (capitalize word))
(t (format " %s " word))))))
(cond ((member word prefix-words)
(push translated prefix-result))
((member word postfix-words)
(push translated postfix-result))
(t
(push translated result)))))
(string-trim
(apply #'concat
(mapcar (lambda (x) (apply #'concat (reverse x)))
(list prefix-result
result
postfix-result))))))
(defun cyphejor--hook ()
"Set `mode-name' according to the symbol name in `major-mode'.
This uses `cyphejor--cypher' and `cyphejor-rules' to generate new
mode name."
(setq mode-name
(cyphejor--cypher
(symbol-name major-mode)
cyphejor-rules)))
(defun cyphejor--fundamental-mode-advice (buffer &optional _inhibit-buffer-hooks)
"Set `mode-name' of BUFFER according to the symbol name in `major-mode'.
Only do so when the buffer is in fundamental mode.
INHIBIT-BUFFER-HOOKS is accepted for compatibility reasons and
has no effect."
(with-current-buffer buffer
(when (eq major-mode 'fundamental-mode)
(save-match-data
(cyphejor--hook)))))
;;;###autoload
(define-minor-mode cyphejor-mode
"Toggle the `cyphejor-mode' minor mode.
With a prefix argument ARG, enable `cyphejor-mode' if ARG is
positive, and disable it otherwise. If called from Lisp, enable
the mode if ARG is omitted or NIL, and toggle it if ARG is
`toggle'.
This global minor mode shortens names of major modes by using a
set of user-defined rules in `cyphejor-rules'. See the
description of the variable for more information."
:init-value nil
:lighter ""
:keymap nil
:global t
(funcall (if cyphejor-mode #'add-hook #'remove-hook)
'after-change-major-mode-hook
#'cyphejor--hook)
(if cyphejor-mode
(progn
(advice-add 'wdired-change-to-dired-mode :after #'cyphejor--hook)
(advice-add 'get-buffer-create :after #'cyphejor--fundamental-mode-advice))
(advice-remove 'wdired-change-to-dired-mode #'cyphejor--hook)
(advice-remove 'get-buffer-create #'cyphejor--fundamental-mode-advice))
(when cyphejor-mode
(mapc (lambda (buffer)
(with-current-buffer buffer
(cyphejor--hook)))
(buffer-list))))
(provide 'cyphejor)
;;; cyphejor.el ends here