Emacs: Replace Straight Quotes to Curly π
Here's a command to convert straight quote to curly quotes.
(defun xah-ascii-to-math-symbol (&optional Begin End) "Replace ASCII less-or-equal etc to unicode chars, on text block or region. Example: <= β β€ >= β β₯ ---> β βΆ --> β βΆ -- β β ~= β β Each of these on the left must have a space around them. URL `http://xahlee.info/emacs/emacs/elisp_straight_curly_quotes.html' Version: 2019-07-25 2021-08-17 2023-11-26" (interactive) (let (xp1 xp2) (if (and Begin End) (setq xp1 Begin xp2 End) (let ((xbds (xah-get-bounds-of-block-or-region))) (setq xp1 (car xbds) xp2 (cdr xbds)))) (let ((case-fold-search nil)) (save-excursion (save-restriction (narrow-to-region xp1 xp2) (xah-replace-pairs-region (point-min) (point-max) [ [" <= " " β€ "] [" >= " " β₯ "] [" ---> " " βΆ "] [" --> " " βΆ "] ["--" " β "] ["~=" "β"] ] t t) ;; ))))) (defun xah-prettify-punctuations (&optional Begin End) "Replace ASCII punctuations etc to Unicode symbols. Example: -- β β & β οΌ ... β β¦ :) β π URL `http://xahlee.info/emacs/emacs/elisp_straight_curly_quotes.html' Version: 2019-07-25 2021-08-17 2023-11-26" (interactive) (let (xp1 xp2) (if (and Begin End) (setq xp1 Begin xp2 End) (let ((xbds (xah-get-bounds-of-block-or-region))) (setq xp1 (car xbds) xp2 (cdr xbds)))) (let ((case-fold-search nil)) (save-excursion (save-restriction (narrow-to-region xp1 xp2) (xah-replace-pairs-region (point-min) (point-max) [ [" β " " β "] ; rid of extra space in em-dash ["..." "β¦"] [" & " " οΌ "] [" :)" " π"] [" :(" " βΉ"] [" ;)" " π"] [" , " ", "] ["β" " β "] ] t t) ;; ))))) (defun xah-fix-curly-single-quote-to-apostrophe (&optional Begin End) "Replace RIGHT SINGLE QUOTATION MARK to APOSTROPHE. Example: donβt β don't iβve β i've itβs β it's URL `http://xahlee.info/emacs/emacs/elisp_straight_curly_quotes.html' Version: 2019-07-25 2021-08-17 2023-01-04 2023-11-26" (interactive) (let (xp1 xp2) (if (and Begin End) (setq xp1 Begin xp2 End) (let ((xbds (xah-get-bounds-of-block-or-region))) (setq xp1 (car xbds) xp2 (cdr xbds)))) (let ((case-fold-search nil)) (save-excursion (save-restriction (narrow-to-region xp1 xp2) ;; (xah-replace-pairs-region ;; (point-min) (point-max) ;; [ ;; [">\'" ">β"] ;; [" \'" " β"] ;; ["(\'" "(β"] ;; ["\' " "β "] ;; ["\'," "β,"] ;; [".\'" ".β"] ;; ["!\'" "!β"] ;; ["?\'" "?β"] ;; ["\')" "β)"] ;; ["\']" "β]"] ;; ] t t) (xah-replace-regexp-pairs-region (point-min) (point-max) [ ["\\bcanβt\\b" "can't"] ["\\bdonβt\\b" "don't"] ["\\bdoesnβt\\b" "doesn't"] ["\\bwonβt\\b" "won't"] ["\\bisnβt\\b" "isn't"] ["\\barenβt\\b" "aren't"] ["\\bainβt\\b" "ain't"] ["\\bdidnβt\\b" "didn't"] ["\\barenβt\\b" "aren't"] ["\\bwasnβt\\b" "wasn't"] ["\\bwerenβt\\b" "weren't"] ["\\bcouldnβt\\b" "couldn't"] ["\\bshouldnβt\\b" "shouldn't"] ["\\bβve\\b" "'ve"] ["\\bβre\\b" "'re"] ["\\bβem\\b" "'em"] ["\\bβll\\b" "'ll"] ["\\bβm\\b" "'m"] ["\\bβd\\b" "'d"] ["\\bβs\\b" "'s"] ["sβ " "s' "] ["sβ\n" "s'\n"] ] t t :hilight)))))) (defun xah-fix-double-quote-to-curly (&optional Begin End) "Change straight double quotes to curly. This is a heuristic based algo, result can be wrong in some places. Version: 2023-01-04 2023-06-05 2023-11-26" (interactive) (let (xp1 xp2) (if (and Begin End) (setq xp1 Begin xp2 End) (let ((xbds (xah-get-bounds-of-block-or-region))) (setq xp1 (car xbds) xp2 (cdr xbds)))) (let ((case-fold-search nil)) (save-excursion (save-restriction (narrow-to-region xp1 xp2) (xah-replace-regexp-pairs-region (point-min) (point-max) [ ["^\"" "β"] ["\"\\'" "β"] ] t t) (xah-replace-pairs-region (point-min) (point-max) [ ["\n\"" "\nβ"] [">\"" ">β"] ["(\"" "(β"] [" \"" " β"] ["\" " "β "] ["\"," "β,"] ["\",\n" "β,\n"] ["\". " "β. "] ["\".\n" "β.\n"] ["\"." "β."] ["\"?" "β?"] ["\";" "β;"] ["\":" "β:"] ["\")" "β)"] ["\"]" "β]"] ;; ["\"[" "\β["] [".\"" ".β"] [",\"" ",β"] ["!\"" "!β"] ["?\"" "?β"] ["\"<" "β<"] ;; ["\"\n" "β\n"] ] t t)))))) (defun xah-fix-double-quote-to-curly (&optional Begin End) "Change straight double quotes to curly. This is a heuristic based algo, result can be wrong in some places. Version: 2023-01-04 2023-06-05 2023-11-26" (interactive) (let (xp1 xp2) (if (and Begin End) (setq xp1 Begin xp2 End) (let ((xbds (xah-get-bounds-of-block-or-region))) (setq xp1 (car xbds) xp2 (cdr xbds)))) (let ((case-fold-search nil)) (save-excursion (save-restriction (narrow-to-region xp1 xp2) (xah-replace-regexp-pairs-region (point-min) (point-max) [ ["^\"" "β"] ["\"\\'" "β"] ] t t) (xah-replace-pairs-region (point-min) (point-max) [ ["\n\"" "\nβ"] [">\"" ">β"] ["(\"" "(β"] [" \"" " β"] ["\" " "β "] ["\"," "β,"] ["\",\n" "β,\n"] ["\". " "β. "] ["\".\n" "β.\n"] ["\"." "β."] ["\"?" "β?"] ["\";" "β;"] ["\":" "β:"] ["\")" "β)"] ["\"]" "β]"] ;; ["\"[" "\β["] [".\"" ".β"] [",\"" ",β"] ["!\"" "!β"] ["?\"" "?β"] ["\"<" "β<"] ;; ["\"\n" "β\n"] ] t t)))))) (defun xah-replace-straight-quotes (&optional Begin End) "Replace straight double quotes to curly ones, and others. Works on current text block or selection. Examples of changes: γ\"β¦\"γ β γββ¦βγ γ...γ β γβ¦γ γIβmγ β γI'mγ γ--γ β γβγ γ~=γ β γβγ When called in lisp code, Begin and End are region begin/end positions. WARNING: this command does not guarantee 100% correct conversion of quotes, because it impossible. You should double check highlighted places after. URL `http://xahlee.info/emacs/emacs/elisp_straight_curly_quotes.html' Version: 2015-04-29 2021-08-17" ;; some examples for debug ;; do "βem all -- done..." ;; Iβam not ;; said "canβt have it, canβt, just canβt" ;; βIβve canβtβ (interactive) (let (xp1 xp2) (if (and Begin End) (setq xp1 Begin xp2 End) (let ((xbds (xah-get-bounds-of-block-or-region))) (setq xp1 (car xbds) xp2 (cdr xbds)))) (let ((case-fold-search nil)) (save-excursion (save-restriction (narrow-to-region xp1 xp2) (xah-prettify-punctuations (point-min) (point-max)) (xah-ascii-to-math-symbol (point-min) (point-max)) (xah-fix-double-quote-to-curly (point-min) (point-max)) (xah-fix-curly-single-quote-to-apostrophe (point-min) (point-max)))))))
requires package Emacs: xah-get-thing.el and Emacs: xah-replace-pairs.el