modes.sl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % MODES.SL - NMODE Mode Manipulation Functions
  4. %
  5. % Author: Alan Snyder
  6. % Hewlett-Packard/CRC
  7. % Date: 14 September 1982
  8. %
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. (CompileTime (load objects extended-char))
  11. % Global variables:
  12. (fluid '(nmode-default-mode
  13. nmode-minor-modes % list of active minor modes (don't modify inplace!)
  14. ))
  15. % Internal static variables:
  16. (fluid '(nmode-defined-modes
  17. nmode-file-modes
  18. ))
  19. (setf nmode-default-mode NIL)
  20. (setf nmode-defined-modes ())
  21. (setf nmode-file-modes ())
  22. (setf nmode-minor-modes ())
  23. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24. %
  25. % Mode Definition:
  26. %
  27. % The following function is used to define a mode (either major or minor):
  28. %
  29. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  30. (de nmode-define-mode (name establish-expressions)
  31. (let* ((mode (make-instance 'mode
  32. 'name name
  33. 'establish-expressions establish-expressions
  34. ))
  35. (pair (Ass
  36. (function string-equal)
  37. name
  38. nmode-defined-modes
  39. )))
  40. (if pair
  41. (rplacd pair mode)
  42. (setf nmode-defined-modes
  43. (cons (cons name mode) nmode-defined-modes)
  44. ))
  45. mode
  46. ))
  47. (defflavor mode (
  48. name
  49. establish-expressions
  50. )
  51. ()
  52. gettable-instance-variables
  53. initable-instance-variables
  54. )
  55. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  56. %
  57. % File Modes
  58. %
  59. % The following functions associate a default mode with certain
  60. % filename extensions.
  61. %
  62. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  63. (de nmode-declare-file-mode (file-type mode)
  64. (let ((pair (Ass
  65. (function string-equal)
  66. file-type
  67. nmode-file-modes
  68. )))
  69. (if pair
  70. (rplacd pair mode)
  71. (setf nmode-file-modes
  72. (cons (cons file-type mode) nmode-file-modes)
  73. ))
  74. ))
  75. (de pathname-default-mode (pn)
  76. (setf pn (pathname pn))
  77. (let ((pair (Ass
  78. (function string-equal)
  79. (pathname-type pn)
  80. nmode-file-modes
  81. )))
  82. (if pair (cdr pair) nmode-default-mode)
  83. ))
  84. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  85. %
  86. % Minor Modes
  87. %
  88. % A minor mode is a mode that can be turned on or off independently of the
  89. % current buffer or the current major mode.
  90. %
  91. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  92. (de minor-mode-active? (m)
  93. % M is a mode object. Return T if it is an active minor mode.
  94. (memq m nmode-minor-modes)
  95. )
  96. (de activate-minor-mode (m)
  97. % M is a mode object. Make it active (if it isn't already).
  98. (when (not (minor-mode-active? m))
  99. (setf nmode-minor-modes (cons m nmode-minor-modes))
  100. (nmode-establish-current-mode)
  101. ))
  102. (de deactivate-minor-mode (m)
  103. % M is a mode object. If it is active, deactivate it.
  104. (when (minor-mode-active? m)
  105. (setf nmode-minor-modes (delq m nmode-minor-modes))
  106. (nmode-establish-current-mode)
  107. ))
  108. (de toggle-minor-mode (m)
  109. % M is a mode object. If it is active, deactivate it and return T;
  110. % otherwise, activate it and return NIL.
  111. (let ((is-active? (minor-mode-active? m)))
  112. (if is-active?
  113. (deactivate-minor-mode m)
  114. (activate-minor-mode m)
  115. )
  116. is-active?
  117. ))
  118. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  119. %
  120. % Manipulating mode lists:
  121. %
  122. % The following functions are provided for use in user init files. They are
  123. % not used in NMODE. See the file -CUSTOMIZING.TXT for information on how to
  124. % customize NMODE.
  125. %
  126. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  127. (de add-to-command-list (listname command func)
  128. (let* ((old-list (eval listname))
  129. (old-binding (assoc command old-list))
  130. (binding (cons command func))
  131. )
  132. (cond
  133. % If the binding isn't already in the a-list.
  134. ((null old-binding)
  135. % Add the new binding
  136. (set listname (aconc old-list binding)))
  137. % Otherwise, replace the old operation in the binding.
  138. (T
  139. (setf (cdr old-binding) func)))
  140. NIL
  141. ))
  142. (de remove-from-command-list (listname command)
  143. (let* ((old-list (eval listname))
  144. (old-binding (assoc command old-list))
  145. )
  146. (cond (old-binding
  147. (set listname (DelQ old-binding old-list))
  148. NIL
  149. ))))
  150. (de set-text-command (command func)
  151. % This function is a shorthand for modifying text mode. The arguments are as
  152. % for ADD-TO-COMMAND-LIST. The change takes effect immediately.
  153. (add-to-command-list 'Text-Command-List command func)
  154. (nmode-establish-current-mode))