help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

displaying subscripts and superscripts


From: Mirko
Subject: displaying subscripts and superscripts
Date: Tue, 04 May 2010 15:45:14 -0000
User-agent: G2/1.0

Hello,

I am trying to display text following an underscore as a subscript and
following a caret as a superscript.  By looking at various pieces of
code (tex-mode.el  and pretty-greek.el from Pascal Bourgignon), I came
up with the following (which, of course does not work):

(defvar suscript-height-minimum 0)
(defvar suscript-height-ratio 0.8)

(defun suscript-height (height)
  "Return the integer height of subscript/superscript font in 1/10
points.
Not smaller than the value set by `tex-suscript-height-minimum'."
  (ceiling (max (if (integerp suscript-height-minimum)
                    suscript-height-minimum
                    ;; For bootstrapping.
                    (condition-case nil
                        (* suscript-height-minimum
                           (face-attribute 'default :height))
                      (error 0)))
                ;; NB assumes height is integer.
                (* height suscript-height-ratio))))

(defface superscript
  '((t :height suscript-height))
  "Face used for superscripts.")

(defface subscript
  '((t :height suscript-height))
  "Face used for subscripts.")

(defun suscript-match-regexp (escape-char)
  "regexp for suscripted text match in common-lisp code"
  (let ((terminating-string (format "_^         )$-")))
    (format "%s\\(.*?\\)[%s]" escape-char
            terminating-string terminating-string)))

(defvar subscript-match (suscript-match-regexp "_")
  "Matches _whatever[_|^|SPACE|TAB|)")
(defvar superscript-match (suscript-match-regexp "\\^")
  "Matches ^whatever[_|^|SPACE|TAB|)")

(defun pretty-suscript ()
  "Load super&subscript keywords into `suscript-flk' and
load that into `font-lock-keywords'"
  (interactive)
  (setq suscript-flk
        `((,subscript-match
           (1 (face subscript display (raise 0.2))))
          (,superscript-match
           (1 (face superscript display (raise -0.2))))))
  (font-lock-add-keywords nil suscript-flk))

(defun cancel-pretty-suscript ()
  "Remove `suscript-flk' keywords from `font-lock-keywords'"
  (interactive)
  (font-lock-remove-keywords nil suscript-flk))

The contents `font-lock-keywords' are as follows:

(t
 (("_\\(.*?\\)[_^       )$-]"
   (1
    (face subscript display
          (raise 0.2))))
  ("\\^\\(.*?\\)[_^     )$-]"
   (1
    (face superscript display
          (raise -0.2))))
 ...)

To my untrained eyes, it looks OK, when compared to entries produced
by pretty-greek.el

So, where am I going wrong?  (Hopefully the list is finite).

Thank you,

Mirko


reply via email to

[Prev in Thread] Current Thread [Next in Thread]