From f64d2922ed44dcacd3a0fd06eb494e94bd128bbe Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 6 Jan 2015 23:02:59 -0200 Subject: Define sx-format-replacements Use FORMAT-STRING to format the values in ALIST. --- sx.el | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'sx.el') diff --git a/sx.el b/sx.el index e080271..6112510 100644 --- a/sx.el +++ b/sx.el @@ -337,6 +337,30 @@ removed from the display name before it is returned." (format "[%s]" (car kar)) (cdr kar) string))) string)) +(defun sx-format-replacements (format alist) + "Use FORMAT-STRING to format the values in ALIST. +ALIST is a list with elements of the form (CHAR . STRING). +The value is a copy of FORMAT-STRING, but with certain constructs +replaced by text as given by ALIST. + +The construct is a `%' character followed by any other character. +The replacement is the STRING corresponding to CHAR in ALIST. + +The %% construct is special, it is replaced with a single %, even +if ALIST contains a different string at the ?% entry." + (let ((alist (cons '(?% . "%") alist))) + (with-temp-buffer + (insert format) + (goto-char (point-min)) + (while (search-forward "%" nil 'noerror) + (delete-char -1) + (unless (eobp) + (insert + (or (cdr (assq (char-after) alist)) + (error "ALIST has no value for `%c'" (char-after)))) + (delete-char 1))) + (buffer-string)))) + (defcustom sx-init-hook nil "Hook run when SX initializes. -- cgit v1.2.3 From 2fed944ba8c3bbf85b4c9eb225ff8a785597521b Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 12:45:36 -0200 Subject: Improve sx-format-replacements to take a property-alist --- sx.el | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 6112510..f01a11d 100644 --- a/sx.el +++ b/sx.el @@ -337,14 +337,17 @@ removed from the display name before it is returned." (format "[%s]" (car kar)) (cdr kar) string))) string)) -(defun sx-format-replacements (format alist) +(defun sx-format-replacements (format alist &optional property-alist) "Use FORMAT-STRING to format the values in ALIST. ALIST is a list with elements of the form (CHAR . STRING). The value is a copy of FORMAT-STRING, but with certain constructs replaced by text as given by ALIST. The construct is a `%' character followed by any other character. -The replacement is the STRING corresponding to CHAR in ALIST. +The replacement is the STRING corresponding to CHAR in ALIST. In +addition, if CHAR is also the car of an element in +PROPERTY-ALIST, the cdr of that element should be a list of text +properties which will be applied on the replacement. The %% construct is special, it is replaced with a single %, even if ALIST contains a different string at the ?% entry." @@ -352,12 +355,22 @@ if ALIST contains a different string at the ?% entry." (with-temp-buffer (insert format) (goto-char (point-min)) - (while (search-forward "%" nil 'noerror) - (delete-char -1) - (unless (eobp) - (insert - (or (cdr (assq (char-after) alist)) - (error "ALIST has no value for `%c'" (char-after)))) + (while (search-forward-regexp + (rx "%" (group-n 1 (* (any "-+ #0-9.")))) nil 'noerror) + (let* ((char (char-after)) + ;; Understand flags + (flag (match-string 1)) + (val (cdr-safe (assq char alist)))) + (unless val + (error "Invalid format character: `%%%c'" char)) + ;; Insert first, to preserve text properties. + (insert-and-inherit + (apply #'propertize + (format (concat "%" flag "s") val) + (cdr-safe (assq char property-alist)))) + ;; Delete the specifier body. + (replace-match "") + ;; Delete `char-after'. (delete-char 1))) (buffer-string)))) -- cgit v1.2.3 From c46dd1cde6d8428a73be0f2e6918b701af541004 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 15:46:13 -0200 Subject: Fix sx-format-replacements replace-match moves point --- sx.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index f01a11d..4159933 100644 --- a/sx.el +++ b/sx.el @@ -369,7 +369,8 @@ if ALIST contains a different string at the ?% entry." (format (concat "%" flag "s") val) (cdr-safe (assq char property-alist)))) ;; Delete the specifier body. - (replace-match "") + (delete-region (match-beginning 0) + (match-end 0)) ;; Delete `char-after'. (delete-char 1))) (buffer-string)))) -- cgit v1.2.3 From dc98b2bd2a7b805e5d3c8a410bcaf137cd4cf78a Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 15:54:53 -0200 Subject: sx-format-replacements: Fix propertizing --- sx.el | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 4159933..d67771b 100644 --- a/sx.el +++ b/sx.el @@ -364,10 +364,10 @@ if ALIST contains a different string at the ?% entry." (unless val (error "Invalid format character: `%%%c'" char)) ;; Insert first, to preserve text properties. - (insert-and-inherit - (apply #'propertize - (format (concat "%" flag "s") val) - (cdr-safe (assq char property-alist)))) + (insert-and-inherit (format (concat "%" flag "s") val)) + (when property-alist + (add-text-properties (match-end 0) (point) + (cdr-safe (assq char property-alist)))) ;; Delete the specifier body. (delete-region (match-beginning 0) (match-end 0)) -- cgit v1.2.3 From 5e29ea61e86c399cb966fc2299566d43694c2cfe Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Wed, 7 Jan 2015 16:29:09 -0200 Subject: Move @name to sx-user --- sx-user.el | 42 ++++++++++++++++++++++++++++++++++++++++++ sx.el | 33 --------------------------------- 2 files changed, 42 insertions(+), 33 deletions(-) (limited to 'sx.el') diff --git a/sx-user.el b/sx-user.el index fe620ca..2e8c89a 100644 --- a/sx-user.el +++ b/sx-user.el @@ -148,6 +148,48 @@ the `sx-button-user' category." :type 'sx-button-user) text)))) + +;;; @name conversion +(defconst sx-user--ascii-replacement-list + '(("[:space:]" . "") + ("àåáâäãåą" . "a") + ("èéêëę" . "e") + ("ìíîïı" . "i") + ("òóôõöøőð" . "o") + ("ùúûüŭů" . "u") + ("çćčĉ" . "c") + ("żźž" . "z") + ("śşšŝ" . "s") + ("ñń" . "n") + ("ýÿ" . "y") + ("ğĝ" . "g") + ("ř" . "r") + ("ł" . "l") + ("đ" . "d") + ("ß" . "ss") + ("Þ" . "th") + ("ĥ" . "h") + ("ĵ" . "j") + ("^[:ascii:]" . "")) + "List of replacements to use for non-ascii characters. +Used to convert user names into @mentions.") + +(defun sx-user--@name (user) + "Get the `display_name' of USER prepended with @. +In order to correctly @mention the user, all whitespace is +removed from the display name and a series of unicode conversions +are performed before it is returned +See `sx-user--ascii-replacement-list'. + +If all you need is the @name, this is very slightly faster than +using `sx-user--format'." + (sx-assoc-let user + (if (stringp .display_name) + (concat "@" (sx--recursive-replace + sx-user--ascii-replacement-list .display_name)) + ;; "" + ))) + (provide 'sx-user) ;;; sx-user.el ends here diff --git a/sx.el b/sx.el index d67771b..36ecfca 100644 --- a/sx.el +++ b/sx.el @@ -294,39 +294,6 @@ Return the result of BODY." (push ov sx--overlays)) result)) -(defconst sx--ascii-replacement-list - '(("[:space:]" . "") - ("àåáâäãåą" . "a") - ("èéêëę" . "e") - ("ìíîïı" . "i") - ("òóôõöøőð" . "o") - ("ùúûüŭů" . "u") - ("çćčĉ" . "c") - ("żźž" . "z") - ("śşšŝ" . "s") - ("ñń" . "n") - ("ýÿ" . "y") - ("ğĝ" . "g") - ("ř" . "r") - ("ł" . "l") - ("đ" . "d") - ("ß" . "ss") - ("Þ" . "th") - ("ĥ" . "h") - ("ĵ" . "j") - ("^[:ascii:]" . "")) - "List of replacements to use for non-ascii characters. -Used to convert user names into @mentions.") - -(defun sx--user-@name (user) - "Get the `display_name' of USER prepended with @. -In order to correctly @mention the user, all whitespace is -removed from the display name before it is returned." - (sx-assoc-let user - (when (stringp .display_name) - (concat "@" (sx--recursive-replace - sx--ascii-replacement-list .display_name))))) - (defun sx--recursive-replace (alist string) "Replace each car of ALIST with its cdr in STRING." (if alist -- cgit v1.2.3