Elisp upcase match in replace-regexp-in-string
I’m wanting to extract the content from LaTeX markup, modify the content, and then copy it to the clipboard. Specifically, I want to pull everything inside a \textsc{}
environment and change it to uppercase. This would take something like \textsc{3sg.hum}
and output 3SG.HUM
. The function I’ve written so far will remove the markup, but will not capitalize its contents, so when I run my function on \textsc{3sg.hum}
, it outputs 3sg.hum
.
This is the code I’ve written so far. As of now, I’m just writing the output to the message buffer and I’ll handle the clipboard stuff later.
(defun reformat-latex-gloss (beginning end)
"Strip LaTeX environments, converting small caps to uppercase"
(interactive "r")
(if (use-region-p)
(let* ((gloss (buffer-substring beginning end))
(replacement
(replace-regexp-in-string
"\\\\textsc{\\(.*?\\)}"
(upcase "\\1")
gloss
nil)))
(message "%s" replacement))
""
)
)
When I run this function on something like \textsc{3sg.h}
, it outputs 3sg.h
rather than the expected 3SG.H
. It seems like there’s a problem with the line (upcase "\\1")
; if I replace \\1
with a string like test
, the output will be uppercase TEST
, so it seems like upcase
is not applying to the numbered match as I would have expected.
I haven’t had any luck finding solutions for this problem. Many of the resources like this show how to do this sort of thing in the interactive mode, but I’m wanting to run this non-interactively. I’m still very new to Elisp, so I’m likely overlooking something simple. Thanks for the help!
Read more here: Source link