home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / epoch / tek-epoch-stuff / tek-style-utils.el < prev    next >
Encoding:
Text File  |  1991-11-20  |  18.2 KB  |  508 lines

  1. ;*****************************************************************************
  2. ;
  3. ; Filename:    tek-style-utils.el
  4. ;
  5. ; Copyright (C) 1991  Ken Wood
  6. ;
  7. ; This program is free software; you can redistribute it and/or modify
  8. ; it under the terms of the GNU General Public License as published by
  9. ; the Free Software Foundation; either version 1, or (at your option)
  10. ; any later version.
  11. ;
  12. ; This program is distributed in the hope that it will be useful,
  13. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ; GNU General Public License for more details.
  16. ;
  17. ; You should have received a copy of the GNU General Public License
  18. ; along with this program; if not, write to the Free Software
  19. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. ;
  21. ; Author:        Ken Wood, <kwood@austek.oz.au>
  22. ; Organisation:        Austek Microsystems Pty Ltd, Australia.
  23. ; Released with permission from Austek Microsystems.
  24. ;
  25. ; Description:    Set up some default fonts for highlighting etc, and
  26. ;        define a function for building styles from default
  27. ;        option values and X11 resource defaults.
  28. ;
  29. ; IMPORTANT:    Check that the variable tek-highlight-use-attributes is
  30. ;        set correctly (see below) before attempting to use this
  31. ;        package.
  32. ;
  33. ; When loaded, this package attempts to define 3 non-proportional fonts
  34. ; which match the default minibuffer font in with and height: a non-bold
  35. ; italic font, a bold italic font and a bold upright font.
  36. ; A function for defining and modifying attribute styles based on X
  37. ; resources is also provided for use by the other packages. You may
  38. ; modify the these styles by setting X11 resources (usually in your
  39. ; .Xdefaults file). Up to 9 options may be specified for each style.
  40. ; Sensible defaults are used for any options that you do not specify.
  41. ; The options are represented by the following X11 resources:
  42. ;     Emacs.STYLE-NAME.style-font: Font.
  43. ;     Emacs.STYLE-NAME.style-foreground: Foreground color.
  44. ;     Emacs.STYLE-NAME.style-background: Background color.
  45. ;     Emacs.STYLE-NAME.style-cursor-foreground: Cursor foreground color.
  46. ;     Emacs.STYLE-NAME.style-cursor-background: Cursor background color.
  47. ;     Emacs.STYLE-NAME.style-underline: Underline color.
  48. ;     Emacs.STYLE-NAME.style-stipple: Stipple pattern.
  49. ;     Emacs.STYLE-NAME.style-cursor-stipple: Stipple pattern.
  50. ;     Emacs.STYLE-NAME.style-background-stipple: Stipple pattern.
  51. ; The values of STYLE-NAME currently used are:
  52. ;     motion: Mouse drag regions.
  53. ;     src-comment: Comments in source code.
  54. ;     info: Info browser buttons.
  55. ;     VM-from: From: lines in VM.
  56. ;     VM-subject: Subject: lines in VM.
  57. ;     gnus-from: From: lines in GNUS.
  58. ;     gnus-subject: Subject: lines in GNUS.
  59. ;     mh-e-from: From: lines in mh-e.
  60. ;     mh-e-subject: Subject: lines in mh-e.
  61. ;     manual-seealso: "See Also" sections in man pages.
  62. ;     manual-usersupplied: User supplied options in man pages.
  63. ;     manual-heading: Fixed options in man pages. These may be shown
  64. ;     in the default font depending on your implementation of the
  65. ;     "man" program.
  66. ; The above X11 resources must be loaded before you start up epoch, in
  67. ; order for them to take effect.
  68. ;
  69. ; Note: the stipple options have not been tested and I don't really understand
  70. ; stipple patterns, so there may be some problems here.
  71. ;*****************************************************************************
  72.  
  73. ; $Id: tek-style-utils.el,v 1.6 1991/11/21 03:20:58 kwood Exp $
  74.  
  75. (provide 'tek-style-utils)
  76.  
  77. (defvar tek-highlight-use-attributes t
  78.   "\
  79. If non-nil, then do highlighting as for Epoch version 3 - using
  80. attributes with the style attached to the attribute. Otherwise do it
  81. as for Epoch version 4 - using the style directly.")
  82.  
  83. (defvar tek-default-font nil
  84.   "\
  85. Default font used in the minibuffer. This font is the basis for a
  86. number of special fonts used for highlighting in various places.")
  87.  
  88. (defvar tek-italic-fixed-font nil
  89.   "\
  90. A non-bold italic or oblique fixed-width font, as similar as possible
  91. to the default minibuffer font, or nil if there are no such fonts.")
  92.  
  93. (defvar tek-italic-bold-fixed-font nil
  94.   "\
  95. A bold italic or oblique fixed-width font, as similar as possible to
  96. the default minibuffer font, or nil if there is no such font.")
  97.  
  98. (defvar tek-bold-fixed-font nil
  99.   "\
  100. A bold roman (upright) fixed-width font, as similar as possible to the
  101. default minibuffer font, or nil if there is no such font.")
  102.  
  103. ;
  104. ; Now, search for & load some commonly needed fonts.
  105. ;
  106. (let (
  107.       ; Variables to hold default font information
  108.       default-font-info
  109.       default-font-name
  110.       default-font-height
  111.       default-font-width
  112.       ; String specifying the required font width.
  113.       font-width
  114.       ;
  115.       ; Variables to hold the possible values of style attributes to try
  116.       ;
  117.       height-try-list
  118.       ; List of font weights to try for bold fonts.
  119.       (bold-weight-try-list (list "-bold" "-demibold"))
  120.       ; List of slant options to try for italic fonts.
  121.       (slant-try-list (list "-i" "-o"))
  122.       ; List of spacing options to try.
  123.       (spacing-try-list (list "-m" "-c"))
  124.       )
  125.   ; Get details of current default font for the minibuffer
  126.   (setq default-font-info (font nil (minibuf-screen)))
  127.   (setq default-font-name (car default-font-info))
  128.   (setq default-font-width (cadr default-font-info))
  129.   (setq default-font-height (cadr (cdr default-font-info)))
  130.   ; Convert the font-width to a string, since it will be used
  131.   ; directly. Width must be multiplied by 10 first.
  132.   (setq font-width
  133.     (concat "-" (int-to-string (* default-font-width 10))))
  134.   ; Generate a list of font height strings to try for fixed-width fonts
  135.   ; from the default font height.
  136.   (setq height-try-list
  137.     (list (concat "-" (int-to-string default-font-height))
  138.           (concat "-" (int-to-string (1+ default-font-height)))
  139.           (concat "-" (int-to-string (1- default-font-height)))))
  140.   ;
  141.   ; First of all, record the default font.
  142.   (setq tek-default-font default-font-name)
  143.   ;
  144.   ;
  145.   ; Now, try to find an italic font similar to the minibuffer font.
  146.   ; From most preferred to least preferred, we'd like the
  147.   ; following options:
  148.   ;    1) monospaced (m), then character spacing (c).
  149.   ;     2) italic (i), then oblique (o).
  150.   ;    3) same height, then 1 pixel taller, then 1 pixel shorter.
  151.   (setq tek-italic-fixed-font
  152.     (let (
  153.           ; Variable to hold the font name currently being tried
  154.           font-try-name
  155.           ; Font finally selected, nil if none
  156.           (font-try-val nil)
  157.           ; Loop variables
  158.           (current-height-list height-try-list)
  159.           current-slant-list
  160.           current-spacing-list
  161.           )
  162.       ; Loop through the height options
  163.       (while (and (not font-try-val) current-height-list)
  164.         ; Set up the list of slant options
  165.         (setq current-slant-list slant-try-list)
  166.         ; Loop through the slant options
  167.         (while (and (not font-try-val) current-slant-list)
  168.           ; Set up the list of spacing options
  169.           (setq current-spacing-list spacing-try-list)
  170.           ; Loop through the spacing options
  171.           (while (and (not font-try-val) current-spacing-list)
  172.         (setq font-try-name
  173.               (concat "-*-*-medium"
  174.                   (car current-slant-list)
  175.                   "-*-*"
  176.                   (car current-height-list)
  177.                   "-*-*-*"
  178.                   (car current-spacing-list)
  179.                   font-width
  180.                   "-*-*"))
  181.         (setq font-try-val (get-font font-try-name))
  182.         ; Remove the first element from the spacing list
  183.         (setq current-spacing-list (cdr current-spacing-list))
  184.         ) ; end spacing loop
  185.           ; Remove the first element from the slant list
  186.           (setq current-slant-list (cdr current-slant-list))
  187.           ) ; end slant loop
  188.         ; Remove the first element from the height list
  189.         (setq current-height-list (cdr current-height-list))
  190.         ) ; end height try list
  191.       ; Check to see if we found a suitable font and return it or nil.
  192.       (if font-try-val
  193.           font-try-name
  194.         nil)))
  195.   ;
  196.   ; Now, try to find a bold italic font similar to the minibuffer font.
  197.   ; From most preferred to least preferred, we'd like the following
  198.   ; options:
  199.   ;    1) monospaced (m), then character spacing (c).
  200.   ;    2) italic (i), then oblique (o).
  201.   ;    3) bold, then demibold.
  202.   ;    4) same height, then 1 pixel taller, then 1 pixel shorter.
  203.   (setq tek-italic-bold-fixed-font
  204.     (let (
  205.           ; Variable to hold the font name currently being tried
  206.           font-try-name
  207.           ; Font finally selected, nil if none
  208.           (font-try-val nil)
  209.           ; Loop variables
  210.           (current-height-list height-try-list)
  211.           current-weight-list
  212.           current-slant-list
  213.           current-spacing-list
  214.           )
  215.       ; Loop through the height options
  216.       (while (and (not font-try-val) current-height-list)
  217.         ; Set up the list of weight options
  218.         (setq current-weight-list bold-weight-try-list)
  219.         ; Loop through the weight options
  220.         (while (and (not font-try-val) current-weight-list)
  221.           ; Set up the list of slant options
  222.           (setq current-slant-list slant-try-list)
  223.           ; Loop through the slant options
  224.           (while (and (not font-try-val) current-slant-list)
  225.         ; Set up the list of spacing options
  226.         (setq current-spacing-list spacing-try-list)
  227.         ; Loop through the spacing options
  228.         (while (and (not font-try-val) current-spacing-list)
  229.           (setq font-try-name
  230.             (concat "-*-*"
  231.                 (car current-weight-list)
  232.                 (car current-slant-list)
  233.                 "-*-*"
  234.                 (car current-height-list)
  235.                 "-*-*-*"
  236.                 (car current-spacing-list)
  237.                 font-width
  238.                 "-*-*"))
  239.           (setq font-try-val (get-font font-try-name))
  240.           ; Remove the first element from the spacing list
  241.           (setq current-spacing-list (cdr current-spacing-list))
  242.           ) ; end spacing loop
  243.         (setq font-try-val (get-font font-try-name))
  244.         ; Remove the first element from the slant list
  245.         (setq current-slant-list (cdr current-slant-list))
  246.         ) ; end slant loop
  247.           ; Remove the first element from the weight list
  248.           (setq current-weight-list (cdr current-weight-list))
  249.           ) ; end weight loop
  250.         ; Remove the first element from the height list
  251.         (setq current-height-list (cdr current-height-list))
  252.         ) ; end height try list
  253.       ; Check to see if we found a suitable font and return it or nil.
  254.       (if font-try-val
  255.           font-try-name
  256.         nil)))
  257.   ; Now, try to find a bold fixed font similar to the minibuffer font.
  258.   ; From most preferred to least preferred, we'd like the following
  259.   ; options:
  260.   ;    1) monospaced (m), then character spacing (c).
  261.   ;    2) bold, then demibold.
  262.   ;    3) same height, then 1 pixel taller, then 1 pixel shorter.
  263.   (setq tek-bold-fixed-font
  264.     (let (
  265.           ; Variable to hold the font name currently being tried
  266.           font-try-name
  267.           ; Font finally selected, nil if none
  268.           (font-try-val nil)
  269.           ; Loop variables
  270.           (current-height-list height-try-list)
  271.           current-weight-list
  272.           current-spacing-list
  273.           )
  274.       ; First of all, just try appending "bold" to the current font
  275.       ; name. This will work with some aliases, such as "6x13".
  276.       (setq font-try-name (concat default-font-name "bold"))
  277.       (setq font-try-val (get-font font-try-name))
  278.       ; If the above fails, loop through the normal options as usual.
  279.       ; Loop through the height options
  280.       (while (and (not font-try-val) current-height-list)
  281.         ; Set up the list of weight options
  282.         (setq current-weight-list bold-weight-try-list)
  283.         ; Loop through the weight options
  284.         (while (and (not font-try-val) current-weight-list)
  285.           ; Set up the list of spacing options
  286.           (setq current-spacing-list spacing-try-list)
  287.           ; Loop through the spacing options
  288.           (while (and (not font-try-val) current-spacing-list)
  289.         (setq font-try-name
  290.               (concat "-*-*"
  291.                   (car current-weight-list)
  292.                   "-r-*-*"
  293.                   (car current-height-list)
  294.                   "-*-*-*"
  295.                   (car current-spacing-list)
  296.                   font-width
  297.                   "-*-*"))
  298.         (setq font-try-val (get-font font-try-name))
  299.         ; Remove the first element from the spacing list
  300.         (setq current-spacing-list (cdr current-spacing-list))
  301.         ) ; end spacing loop
  302.           ; Remove the first element from the weight list
  303.           (setq current-weight-list (cdr current-weight-list))
  304.           ) ; end weight loop
  305.         ; Remove the first element from the height list
  306.         (setq current-height-list (cdr current-height-list))
  307.         ) ; end height try list
  308.       ; Check to see if we found a suitable font and return it or nil.
  309.       (if font-try-val
  310.           font-try-name
  311.         nil)))
  312.   )
  313.  
  314.  
  315. ; Define the prefix to be use when looking for X resources
  316. (defvar tek-resource-prefix "Emacs"
  317.   "\
  318. String to be prepended to resource names prior to looking up their
  319. value.")
  320.  
  321.  
  322. ; Define a function to initialise a style
  323. (defun tek-build-style (style-name
  324.             &optional package-style
  325.             &optional default-style-font
  326.             &optional default-style-foreground
  327.             &optional default-style-background
  328.             &optional default-style-cursor-foreground
  329.             &optional default-style-cursor-background
  330.             &optional default-style-underline
  331.             &optional default-style-stipple
  332.             &optional default-style-cursor-stipple
  333.             &optional default-style-background-stipple)
  334.   "\
  335. Return a style object, with values set as specified by the X11
  336. resources for STYLE-NAME, a string. If optional PACKAGE-STYLE is not
  337. given or nil, then create a new style.
  338.  
  339. There are 9 style fields which may be specified for the package: font,
  340. foreground, background, cursor-foreground, cursor-background,
  341. underline, stipple, cursor-stipple and background-stipple. The value
  342. of `tek-resource-prefix' is prepended to the package name,
  343. before looking up the value of the resource. Thus, the possible
  344. resources that may be specified are:
  345.  
  346. RESOURCE.STYLE-NAME.style-font
  347. RESOURCE.STYLE-NAME.style-foreground
  348. RESOURCE.STYLE-NAME.style-background
  349. RESOURCE.STYLE-NAME.style-cursor-foreground
  350. RESOURCE.STYLE-NAME.style-cursor-background
  351. RESOURCE.STYLE-NAME.style-underline
  352. RESOURCE.STYLE-NAME.style-stipple
  353. RESOURCE.STYLE-NAME.style-cursor-stipple
  354. RESOURCE.STYLE-NAME.style-background-stipple
  355.  
  356. See make-style and the style-* and set-style-* functions for details
  357. of style options.
  358.  
  359. This function also takes 9 optional arguments: FONT, FOREGROUND,
  360. BACKGROUND, CURSOR-FOREGROUND, CURSOR-BACKGROUND, UNDERLINE, STIPPLE,
  361. CURSOR-STIPPLE and BACKGROUND-STIPPLE. These are the default values to
  362. use no X11 resources for the package are defined. If any X11 resources
  363. are not defined, the default value will be used to specify that style
  364. option.
  365.  
  366. If any style option is not specified by either X11 resource and the
  367. corresponding default value is not specified or is nil, then that
  368. style field will not be set."
  369.   (let (
  370.     ; Use a local variable for the package style.
  371.     (new-package-style package-style)
  372.     ; Names of the specified X11 resources.
  373.     (font-resource-name
  374.      (concat tek-resource-prefix "."
  375.          style-name ".style-font"))
  376.     (foreground-resource-name
  377.      (concat tek-resource-prefix "."
  378.          style-name ".style-foreground"))
  379.     (background-resource-name
  380.      (concat tek-resource-prefix "."
  381.          style-name ".style-background"))
  382.     (cursor-foreground-resource-name
  383.      (concat tek-resource-prefix "."
  384.          style-name ".style-cursor-foreground"))
  385.     (cursor-background-resource-name
  386.      (concat tek-resource-prefix "."
  387.          style-name ".style-cursor-background"))
  388.     (underline-resource-name
  389.      (concat tek-resource-prefix "."
  390.          style-name ".style-underline"))
  391.     (stipple-resource-name
  392.      (concat tek-resource-prefix "."
  393.          style-name ".style-stipple"))
  394.     (cursor-stipple-resource-name
  395.      (concat tek-resource-prefix "."
  396.          style-name ".style-cursor-stipple"))
  397.     (background-stipple-resource-name
  398.      (concat tek-resource-prefix "."
  399.          style-name ".style-backgound-stipple"))
  400.     ; Calculated style field values.
  401.     (package-font nil)
  402.     (package-foreground nil)
  403.     (package-background nil)
  404.     (package-cursor-foreground nil)
  405.     (package-cursor-background nil)
  406.     (package-underline nil)
  407.     (package-stipple nil)
  408.     (package-cursor-stipple nil)
  409.     (package-background-stipple nil)
  410.     )
  411.     ; Attempt to find the required settings in the X11 resources.
  412.     (setq package-font
  413.       (get-default font-resource-name))
  414.     (setq package-foreground
  415.       (get-default foreground-resource-name))
  416.     (setq package-background
  417.       (get-default background-resource-name))
  418.     (setq package-cursor-foreground
  419.       (get-default cursor-foreground-resource-name))
  420.     (setq package-cursor-background
  421.       (get-default cursor-background-resource-name))
  422.     (setq package-underline
  423.       (get-default underline-resource-name))
  424.     (setq package-stipple
  425.       (get-default stipple-resource-name))
  426.     (setq package-cursor-stipple
  427.       (get-default cursor-stipple-resource-name))
  428.     (setq package-background-stipple
  429.       (get-default background-stipple-resource-name))
  430.     ; If any of the X11 resources were not defined, use the defaults.
  431.     (if (not package-font)
  432.     (setq package-font default-style-font))
  433.     (if (not package-foreground)
  434.     (setq package-foreground default-style-foreground))
  435.     (if (not package-background)
  436.     (setq package-background default-style-background))
  437.     (if (not package-cursor-foreground)
  438.     (setq package-cursor-foreground default-style-cursor-foreground))
  439.     (if (not package-cursor-background)
  440.     (setq package-cursor-background default-style-cursor-background))
  441.     (if (not package-underline)
  442.     (setq package-underline default-style-underline))
  443.     (if (not package-stipple)
  444.     (setq package-stipple default-style-stipple))
  445.     (if (not package-cursor-stipple)
  446.     (setq package-cursor-stipple default-style-cursor-stipple))
  447.     (if (not package-background-stipple)
  448.     (setq package-background-stipple default-style-background-stipple))
  449.     ; Create a new style if necessary.
  450.     (if (not new-package-style)
  451.     (setq new-package-style (make-style)))
  452.     ; Set each of the style fields if a value is defined for it.
  453.     (if package-font
  454.     (set-style-font new-package-style
  455.             package-font))
  456.     (if package-foreground
  457.     (set-style-foreground new-package-style
  458.                   package-foreground))
  459.     (if package-background
  460.     (set-style-background new-package-style
  461.                   package-background))
  462.     (if package-cursor-foreground
  463.     (set-style-cursor-foreground new-package-style
  464.                      package-cursor-foreground))
  465.     (if package-background
  466.     (set-style-background new-package-style
  467.                   package-background))
  468.     (if package-underline
  469.     (set-style-underline new-package-style
  470.                  package-underline))
  471.     (if package-stipple
  472.     (set-style-stipple new-package-style
  473.                package-stipple))
  474.     (if package-cursor-stipple
  475.     (set-style-cursor-stipple new-package-style
  476.                   package-cursor-stipple))
  477.     (if package-background-stipple
  478.     (set-style-background-stipple new-package-style
  479.                       package-background-stipple))
  480.     ; return the style
  481.     new-package-style
  482.     ))
  483.