(I seem to be winding up fixating on non-asciisms for org-mode; strange)

"Smart" quotes can be annoying when they aren't smart enough.  But when they work you can miss them.  I'm attaching a patch that defines a custom variable org-smart-quotes (nil by default), which when non-nil causes the " and ' characters to display as “smart” quotes, hopefully the right ones.  They're still ' and " in the underlying text, just overlaid with “”.


I started working on parallel patches on the export end (in the -e- files in contrib); it would really help org-mode's standing as a lightweight markup language, imo, but then I saw that's already underway; I had just looked in the wrong exports.


I also note that my post here dated May 9 (http://article.gmane.org/gmane.emacs.orgmode/55756 ) has gone completely without comment, good or bad.  Did I miss something?  Did it not get through (it shows up on gmane)?  At least one of the changes submitted there I would think is pretty uncontroversial.


~mark


diff --git a/lisp/org-entities.el b/lisp/org-entities.el
index 8b5b3f3..ee54abc 100644
--- a/lisp/org-entities.el
+++ b/lisp/org-entities.el
@@ -47,6 +47,14 @@ in backends where the corresponding character is not available."
   :version "24.1"
   :type 'boolean)
 
+(defcustom org-smart-quotes nil
+  "Non-nil means display ' and \" characters as Unicode \"smart\" quotes.
+Org-mode will try to figure out if a quote character is opening or closing.
+
+Note: this does not affect export, only on-screen appearance."
+  :group 'org-entities
+  :type 'boolean)
+
 (defcustom org-entities-user nil
   "User-defined entities used in Org-mode to produce special characters.
 Each entry in this list is a list of strings.  It associates the name
diff --git a/lisp/org.el b/lisp/org.el
index 05f5375..213490e 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5926,6 +5926,7 @@ needs to be inserted at a specific position in the font-lock sequence.")
 		 '(1 'org-archived prepend))
 	   ;; Specials
 	   '(org-do-latex-and-special-faces)
+	   '(org-smartify-quotes)
 	   '(org-fontify-entities)
 	   '(org-raise-scripts)
 	   ;; Code
@@ -5948,6 +5949,33 @@ needs to be inserted at a specific position in the font-lock sequence.")
 		   '(org-font-lock-keywords t nil nil backward-paragraph))
     (kill-local-variable 'font-lock-keywords) nil))
 
+(defconst org-smart-quotes-regex
+  ;; ' is a word character, " is punctuation.
+  "\\(\"\\<\\)\\|\\>\\s.*\\(\"\\)\\|\\(?:\\W\\|^\\)\\('\\)\\|\\w\\s.*\\('\\)")
+
+
+(defun org-smartify-quotes (limit)
+  "Make 'smart quotes' out of straight quotes."
+  (let* (start end subst k)
+    (when org-smart-quotes
+      (catch 'match
+	(while (re-search-forward org-smart-quotes-regex
+		limit t)
+	  (cond ((match-string 1)
+		 (setq k 1 subst "“"))
+		((match-string 2)
+		 (setq k 2 subst "”"))
+		((match-string 3)
+		 (setq k 3 subst "‘"))
+		((match-string 4)
+		 (setq k 4 subst "’")))
+	  (add-text-properties (match-beginning k) (match-end k)
+			       (list 'font-lock-fontified t))
+	  (compose-region (match-beginning k) (match-end k) subst nil)
+	  (backward-char 1)
+	  (throw 'match t))
+	nil))))
+
 (defun org-toggle-pretty-entities ()
   "Toggle the composition display of entities as UTF8 characters."
   (interactive)
2012-05-21  Mark Shoulson  <m...@kli.org>

        * lisp/org.el, lisp/org-entities.el: added org-smart-quotes
        for displaying ' and " characters as "smart quotes."

Reply via email to