case-table.el 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ;;; case-table.el --- code to extend the character set and support case tables -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1988, 1994, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: Howard Gayle
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: i18n
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs 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. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Written by:
  20. ;; TN/ETX/TX/UMG Howard Gayle UUCP : seismo!enea!erix!howard
  21. ;; Telefonaktiebolaget L M Ericsson Phone: +46 8 719 55 65
  22. ;; Ericsson Telecom Telex: 14910 ERIC S
  23. ;; S-126 25 Stockholm FAX : +46 8 719 64 82
  24. ;; Sweden
  25. ;;; Code:
  26. (defun describe-buffer-case-table ()
  27. "Describe the case table of the current buffer."
  28. (interactive)
  29. (let ((description (make-char-table 'case-table)))
  30. (map-char-table
  31. (function (lambda (key value)
  32. (if (not (natnump value))
  33. (if (consp key)
  34. (set-char-table-range description key "case-invariant")
  35. (aset description key "case-invariant"))
  36. (let (from to)
  37. (if (consp key)
  38. (setq from (car key) to (cdr key))
  39. (setq from (setq to key)))
  40. (while (<= from to)
  41. (aset
  42. description from
  43. (cond ((/= from (downcase from))
  44. (concat "uppercase, matches "
  45. (char-to-string (downcase from))))
  46. ((/= from (upcase from))
  47. (concat "lowercase, matches "
  48. (char-to-string (upcase from))))
  49. (t "case-invariant")))
  50. (setq from (1+ from)))))))
  51. (current-case-table))
  52. (save-excursion
  53. (with-output-to-temp-buffer "*Help*"
  54. (set-buffer standard-output)
  55. (describe-vector description)
  56. (help-mode)))))
  57. (defun case-table-get-table (case-table table)
  58. "Return the TABLE of CASE-TABLE.
  59. TABLE can be `down', `up', `eqv' or `canon'."
  60. (let ((slot-nb (cdr (assq table '((up . 0) (canon . 1) (eqv . 2))))))
  61. (or (if (eq table 'down) case-table)
  62. (char-table-extra-slot case-table slot-nb)
  63. ;; Setup all extra slots of CASE-TABLE by temporarily selecting
  64. ;; it as the standard case table.
  65. (let ((old (standard-case-table)))
  66. (unwind-protect
  67. (progn
  68. (set-standard-case-table case-table)
  69. (char-table-extra-slot case-table slot-nb))
  70. (or (eq case-table old)
  71. (set-standard-case-table old)))))))
  72. (defun get-upcase-table (case-table)
  73. "Return the upcase table of CASE-TABLE."
  74. (case-table-get-table case-table 'up))
  75. (make-obsolete 'get-upcase-table 'case-table-get-table "24.4")
  76. (defun copy-case-table (case-table)
  77. (let ((copy (copy-sequence case-table))
  78. (up (char-table-extra-slot case-table 0)))
  79. ;; Clear out the extra slots (except for upcase table) so that
  80. ;; they will be recomputed from the main (downcase) table.
  81. (if up
  82. (set-char-table-extra-slot copy 0 (copy-sequence up)))
  83. (set-char-table-extra-slot copy 1 nil)
  84. (set-char-table-extra-slot copy 2 nil)
  85. copy))
  86. (defun set-case-syntax-delims (l r table)
  87. "Make characters L and R a matching pair of non-case-converting delimiters.
  88. This sets the entries for L and R in TABLE, which is a string
  89. that will be used as the downcase part of a case table.
  90. It also modifies `standard-syntax-table' to
  91. indicate left and right delimiters."
  92. (aset table l l)
  93. (aset table r r)
  94. (let ((up (case-table-get-table table 'up)))
  95. (aset up l l)
  96. (aset up r r))
  97. ;; Clear out the extra slots so that they will be
  98. ;; recomputed from the main (downcase) table and upcase table.
  99. (set-char-table-extra-slot table 1 nil)
  100. (set-char-table-extra-slot table 2 nil)
  101. (modify-syntax-entry l (concat "(" (char-to-string r) " ")
  102. (standard-syntax-table))
  103. (modify-syntax-entry r (concat ")" (char-to-string l) " ")
  104. (standard-syntax-table)))
  105. (defun set-case-syntax-pair (uc lc table)
  106. "Make characters UC and LC a pair of inter-case-converting letters.
  107. This sets the entries for characters UC and LC in TABLE, which is a string
  108. that will be used as the downcase part of a case table.
  109. It also modifies `standard-syntax-table' to give them the syntax of
  110. word constituents."
  111. (aset table uc lc)
  112. (aset table lc lc)
  113. (let ((up (case-table-get-table table 'up)))
  114. (aset up uc uc)
  115. (aset up lc uc))
  116. ;; Clear out the extra slots so that they will be
  117. ;; recomputed from the main (downcase) table and upcase table.
  118. (set-char-table-extra-slot table 1 nil)
  119. (set-char-table-extra-slot table 2 nil)
  120. (modify-syntax-entry lc "w " (standard-syntax-table))
  121. (modify-syntax-entry uc "w " (standard-syntax-table)))
  122. (defun set-upcase-syntax (uc lc table)
  123. "Make character UC an upcase of character LC.
  124. It also modifies `standard-syntax-table' to give them the syntax of
  125. word constituents."
  126. (aset table lc lc)
  127. (let ((up (case-table-get-table table 'up)))
  128. (aset up uc uc)
  129. (aset up lc uc))
  130. ;; Clear out the extra slots so that they will be
  131. ;; recomputed from the main (downcase) table and upcase table.
  132. (set-char-table-extra-slot table 1 nil)
  133. (set-char-table-extra-slot table 2 nil)
  134. (modify-syntax-entry lc "w " (standard-syntax-table))
  135. (modify-syntax-entry uc "w " (standard-syntax-table)))
  136. (defun set-downcase-syntax (uc lc table)
  137. "Make character LC a downcase of character UC.
  138. It also modifies `standard-syntax-table' to give them the syntax of
  139. word constituents."
  140. (aset table uc lc)
  141. (aset table lc lc)
  142. (let ((up (case-table-get-table table 'up)))
  143. (aset up uc uc))
  144. ;; Clear out the extra slots so that they will be
  145. ;; recomputed from the main (downcase) table and upcase table.
  146. (set-char-table-extra-slot table 1 nil)
  147. (set-char-table-extra-slot table 2 nil)
  148. (modify-syntax-entry lc "w " (standard-syntax-table))
  149. (modify-syntax-entry uc "w " (standard-syntax-table)))
  150. (defun set-case-syntax (c syntax table)
  151. "Make character C case-invariant with syntax SYNTAX.
  152. This sets the entry for character C in TABLE, which is a string
  153. that will be used as the downcase part of a case table.
  154. It also modifies `standard-syntax-table'.
  155. SYNTAX should be \" \", \"w\", \".\" or \"_\"."
  156. (aset table c c)
  157. (let ((up (case-table-get-table table 'up)))
  158. (aset up c c))
  159. ;; Clear out the extra slots so that they will be
  160. ;; recomputed from the main (downcase) table and upcase table.
  161. (set-char-table-extra-slot table 1 nil)
  162. (set-char-table-extra-slot table 2 nil)
  163. (modify-syntax-entry c syntax (standard-syntax-table)))
  164. (provide 'case-table)
  165. ;;; case-table.el ends here