case-commands.sl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % Case-Commands.SL - NMODE Case Conversion commands
  4. %
  5. % Author: Alan Snyder
  6. % Hewlett-Packard/CRC
  7. % Date: 6 October 1982
  8. %
  9. % The original code was contributed by Jeff Soreff.
  10. %
  11. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  12. (CompileTime (load fast-int fast-vectors fast-strings))
  13. (fluid '(
  14. nmode-command-argument
  15. nmode-current-buffer
  16. ))
  17. % Global variables:
  18. (fluid '(shifted-digits-association-list))
  19. (setf shifted-digits-association-list NIL)
  20. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  21. % Case Conversion Commands:
  22. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  23. (de uppercase-word-command ()
  24. (transform-region-with-next-word-or-fragment #'string-upcase))
  25. (de lowercase-word-command ()
  26. (transform-region-with-next-word-or-fragment #'string-downcase))
  27. (de uppercase-initial-command ()
  28. (transform-region-with-next-word-or-fragment #'string-capitalize))
  29. (de uppercase-region-command ()
  30. (transform-marked-region #'string-upcase))
  31. (de lowercase-region-command ()
  32. (transform-marked-region #'string-downcase))
  33. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  34. % Upcase Digit Command:
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36. (de upcase-digit-command ()
  37. % Convert the previous digit to the corresponding "shifted character"
  38. % on the keyboard. Search only within the current line or the previous
  39. % line. Ding if no digit found.
  40. (let ((point (buffer-get-position))
  41. (limit-line-pos (- (current-line-pos) 1))
  42. (ok NIL)
  43. )
  44. (while (and (>= (current-line-pos) limit-line-pos)
  45. (not (at-buffer-start?))
  46. (not (setf ok (digitp (previous-character))))
  47. )
  48. (move-backward)
  49. )
  50. (cond ((and ok (set-up-shifted-digits-association-list))
  51. (let* ((old (previous-character))
  52. (new (cdr (assoc old shifted-digits-association-list)))
  53. )
  54. (delete-previous-character)
  55. (insert-character new)
  56. ))
  57. (t (Ding))
  58. )
  59. (buffer-set-position point)
  60. ))
  61. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  62. % General Transformation Functions:
  63. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  64. (de transform-region (string-conversion-function bp1 bp2)
  65. % Transform the region in the current buffer between the positions
  66. % BP1 and BP2 by applying the specified function to each partial or
  67. % complete line. The function should accept a single string argument
  68. % and return the transformed string. Return 1 if BP2 > BP1;
  69. % return -1 if BP2 < BP1. The buffer pointer is left at the "end"
  70. % of the transformed region (the greater of BP1 and BP2).
  71. (let* ((modified-flag (=> nmode-current-buffer modified?))
  72. (extracted-pair (extract-region t bp1 bp2))
  73. (newregion (cdr extracted-pair))
  74. (oldregion (if (not modified-flag) (copyvector newregion)))
  75. )
  76. (for (from index 0 (vector-upper-bound newregion) 1)
  77. (do (vector-store newregion index
  78. (apply string-conversion-function
  79. (list (vector-fetch newregion index))))))
  80. (insert-text newregion)
  81. (if (and (not modified-flag) (text-equal newregion oldregion))
  82. (=> nmode-current-buffer set-modified? nil)
  83. )
  84. (car extracted-pair)
  85. ))
  86. (de transform-region-with-next-word-or-fragment (string-conversion-function)
  87. % Transform the region consisting of the following N words, where N is
  88. % the command argument. N may be negative, meaning previous words.
  89. (let ((start (buffer-get-position)))
  90. (move-over-words nmode-command-argument)
  91. (transform-region string-conversion-function start (buffer-get-position))
  92. ))
  93. (de transform-marked-region (string-conversion-function)
  94. % Transform the region defined by point and mark.
  95. (let ((point (buffer-get-position))
  96. (mark (current-mark))
  97. )
  98. (when (= (transform-region string-conversion-function point mark) 1)
  99. % The mark was at the end of the region. If the transformation changed
  100. % the length of the region, the mark may need to be updated.
  101. (previous-mark) % pop off old mark
  102. (set-mark-from-point) % set the mark to the end of the transformed region
  103. (buffer-set-position point)
  104. )))
  105. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  106. % Auxiliary Function:
  107. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  108. (de set-up-shifted-digits-association-list ()
  109. % Ensure that the "shifted digits association list" is set up properly.
  110. % If necessary, ask the user for the required information. Returns the
  111. % association list if properly set up, NIL if an error occurred.
  112. (if (not shifted-digits-association-list)
  113. (let ((shifted-digits
  114. (prompt-for-string
  115. "Type the digits 1, 2, ... 9, 0, holding down Shift:" nil)))
  116. (cond ((= (string-length shifted-digits) 10)
  117. (setq shifted-digits-association-list
  118. (pair
  119. (string-to-list "1234567890")
  120. (string-to-list shifted-digits))))
  121. ((> (string-length shifted-digits) 10)
  122. (nmode-error "Typed too many shifted digits!"))
  123. (t
  124. (nmode-error "Typed too few shifted digits!"))
  125. )))
  126. shifted-digits-association-list
  127. )