unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* displaying subscripts and superscripts
@ 2010-04-18 22:31 Mirko
  2010-04-19  3:19 ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Mirko @ 2010-04-18 22:31 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-04-21 18:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-18 22:31 displaying subscripts and superscripts Mirko
2010-04-19  3:19 ` Stefan Monnier
2010-04-19 23:29   ` Glenn Morris
2010-04-21 18:18     ` Stefan Monnier
2010-04-20  0:14   ` Mirko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).