-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccent.el
142 lines (122 loc) · 5.23 KB
/
accent.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
;;; accent.el --- Popup for accented characters (diacritics) -*- lexical-binding: t; -*-
;; Copyright (c) 2022 Elia Scotto
;; Author: Elia Scotto <[email protected]>
;; Maintainer: Elia Scotto <[email protected]>
;; URL: https://github.com/elias94/accent
;; Keywords: i18n
;; Version: 1.4
;; Package-Requires: ((emacs "24.3") (popup "0.5.8"))
;; 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:
;; accent.el enable a visual popup for using accented characters in Emacs.
;;
;; Is recommended to bind a global keybinding to use when typing such
;; as `C-x C-a`; add the following to your configuration:
;;
;; (global-set-key (kbd "C-x C-a") 'accent-menu)
;;
;; See README.md for more information.
;; https://github.com/elias94/accent/blob/main/README.md
;;; Code:
(require 'popup)
(defconst accent-version "1.4"
"Version of accent.el.")
(defgroup accent nil
"Shows popup with accented letters while pressing C-x C-a on an
accented character."
:group 'convenience)
(defcustom accent-position 'before
"If set to 'before (default) it takes the character before the cursor.
If set to 'after it takes the caracter after the cursor. Set it to 'after
if you have the `cursor-type` set to 'block and want to apply an accent to
the character under the cursor."
:group 'accent
:type 'symbol)
(defcustom accent-custom '()
"Used to append custom accented characters to the default one.
It uses a list of characters associated to a single letter,
e.g. '(a (ằ)) ."
:group 'accent
:type '(alist :value-type (character (alist :value-type character))))
(defvar accent-diacritics '((a (à á â ä æ ã å ā))
(c (ç ć č))
(e (è é ê ë ē ė ę))
(i (î ï í ī į ì))
(l (ł))
(n (ñ ń))
(o (ô ö ò ó œ ø ō õ))
(s (ß ś š))
(u (û ü ù ú ū))
(y (ÿ))
(z (ž ź ż))
(A (À Á Â Ä Æ Ã Å Ā))
(C (Ç Ć Č))
(E (È É Ê Ë Ē Ė Ę))
(I (Î Ï Í Ī Į Ì))
(L (Ł))
(N (Ñ Ń))
(O (Ô Ö Ò Ó Œ Ø Ō Õ))
(S (Ś Š))
(U (Û Ü Ù Ú Ū))
(Y (Ÿ))
(Z (Ž Ź Ż)))
"List of diacritics available.
For each character, includes a list
of available options to be displayed in the popup.")
(defun accent-lst ()
"Merge `accent-custom` with default accenter characters."
(cl-labels ((merge-custom (accent)
(let ((custom-acc (cl-find (car accent) accent-custom :key #'car)))
(if custom-acc
(list (car accent)
(cl-concatenate 'list
(cadr accent)
(cadr custom-acc)))
accent))))
(mapcar #'merge-custom accent-diacritics)))
;;;###autoload
(defun accent-menu ()
"Display a popup menu with available accents if current character is matching."
(interactive)
(let* ((after? (eq accent-position 'after))
(char (if after? (char-after) (char-before)))
(curr (intern (string char)))
(diac (assoc curr (accent-lst))))
(if diac
(let ((opt (popup-menu* (cadr diac))))
(when opt
(progn
(delete-char (if after? 1 -1))
(insert (symbol-name opt)))))
(message "No accented characters available"))))
(declare-function company-begin-backend "company")
;;;###autoload
(defun accent-company (command &rest _ignored)
"Backend for `company' to complete accents.
See `company-backends' for the description of COMMAND."
(interactive (list 'interactive))
(let* ((after? (eq accent-position 'after))
(char (if after? (char-after) (char-before)))
(curr (intern (string char)))
(diac (assoc curr (accent-lst))))
(cl-case command
(interactive (company-begin-backend 'accent-company))
(prefix (when diac
(string char)))
(candidates (mapcar (lambda (d) (if after?
(format "%s%s" (string (char-before)) d)
(format "%s" d)))
(cadr diac)))
(post-completion (when after?
(delete-char 1))))))
(provide 'accent)
;;; accent.el ends here