erc-compat.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ;;; erc-compat.el --- ERC compatibility code for XEmacs
  2. ;; Copyright (C) 2002-2003, 2005-2017 Free Software Foundation, Inc.
  3. ;; Author: Alex Schroeder <alex@gnu.org>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; URL: http://www.emacswiki.org/cgi-bin/wiki/ERC
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs 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 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This mostly defines stuff that cannot be worked around easily.
  19. ;;; Code:
  20. (require 'format-spec)
  21. ;;;###autoload (autoload 'erc-define-minor-mode "erc-compat")
  22. (defalias 'erc-define-minor-mode 'define-minor-mode)
  23. (put 'erc-define-minor-mode 'edebug-form-spec 'define-minor-mode)
  24. (defun erc-decode-coding-string (s coding-system)
  25. "Decode S using CODING-SYSTEM."
  26. (decode-coding-string s coding-system t))
  27. (defun erc-encode-coding-string (s coding-system)
  28. "Encode S using CODING-SYSTEM.
  29. Return the same string, if the encoding operation is trivial.
  30. See `erc-encoding-coding-alist'."
  31. (encode-coding-string s coding-system t))
  32. (defalias 'erc-propertize 'propertize)
  33. (defalias 'erc-view-mode-enter 'view-mode-enter)
  34. (autoload 'help-function-arglist "help-fns")
  35. (defalias 'erc-function-arglist 'help-function-arglist)
  36. (defalias 'erc-delete-dups 'delete-dups)
  37. (defalias 'erc-replace-regexp-in-string 'replace-regexp-in-string)
  38. (defun erc-set-write-file-functions (new-val)
  39. (set (make-local-variable 'write-file-functions) new-val))
  40. (defvar erc-emacs-build-time
  41. (if (or (stringp emacs-build-time) (not emacs-build-time))
  42. emacs-build-time
  43. (format-time-string "%Y-%m-%d" emacs-build-time))
  44. "Time at which Emacs was dumped out, or nil if not available.")
  45. ;; Emacs 21 and XEmacs do not have user-emacs-directory, but XEmacs
  46. ;; has user-init-directory.
  47. (defvar erc-user-emacs-directory
  48. (cond ((boundp 'user-emacs-directory)
  49. user-emacs-directory)
  50. ((boundp 'user-init-directory)
  51. user-init-directory)
  52. (t "~/.emacs.d/"))
  53. "Directory beneath which additional per-user Emacs-specific files
  54. are placed.
  55. Note that this should end with a directory separator.")
  56. ;; XEmacs's `replace-match' does not replace matching subexpressions in strings.
  57. (defun erc-replace-match-subexpression-in-string
  58. (newtext string match subexp start &optional fixedcase literal)
  59. "Replace the subexpression SUBEXP of the last match in STRING with NEWTEXT.
  60. MATCH is the text which matched the subexpression (see `match-string').
  61. START is the beginning position of the last match (see `match-beginning').
  62. See `replace-match' for explanations of FIXEDCASE and LITERAL."
  63. (cond ((featurep 'xemacs)
  64. (string-match match string start)
  65. (replace-match newtext fixedcase literal string))
  66. (t (replace-match newtext fixedcase literal string subexp))))
  67. (defalias 'erc-with-selected-window 'with-selected-window)
  68. (defalias 'erc-cancel-timer 'cancel-timer)
  69. (defalias 'erc-make-obsolete 'make-obsolete)
  70. (defalias 'erc-make-obsolete-variable 'make-obsolete-variable)
  71. ;; Provide a simpler replacement for `member-if'
  72. (defun erc-member-if (predicate list)
  73. "Find the first item satisfying PREDICATE in LIST.
  74. Return the sublist of LIST whose car matches."
  75. (let ((ptr list))
  76. (catch 'found
  77. (while ptr
  78. (when (funcall predicate (car ptr))
  79. (throw 'found ptr))
  80. (setq ptr (cdr ptr))))))
  81. ;; Provide a simpler replacement for `delete-if'
  82. (defun erc-delete-if (predicate seq)
  83. "Remove all items satisfying PREDICATE in SEQ.
  84. This is a destructive function: it reuses the storage of SEQ
  85. whenever possible."
  86. ;; remove from car
  87. (while (when (funcall predicate (car seq))
  88. (setq seq (cdr seq))))
  89. ;; remove from cdr
  90. (let ((ptr seq)
  91. (next (cdr seq)))
  92. (while next
  93. (when (funcall predicate (car next))
  94. (setcdr ptr (if (consp next)
  95. (cdr next)
  96. nil)))
  97. (setq ptr (cdr ptr))
  98. (setq next (cdr ptr))))
  99. seq)
  100. ;; Provide a simpler replacement for `remove-if-not'
  101. (defun erc-remove-if-not (predicate seq)
  102. "Remove all items not satisfying PREDICATE in SEQ.
  103. This is a non-destructive function; it makes a copy of SEQ to
  104. avoid corrupting the original SEQ."
  105. (let (newseq)
  106. (dolist (el seq)
  107. (when (funcall predicate el)
  108. (setq newseq (cons el newseq))))
  109. (nreverse newseq)))
  110. ;; Copied from cl-extra.el
  111. (defun erc-subseq (seq start &optional end)
  112. "Return the subsequence of SEQ from START to END.
  113. If END is omitted, it defaults to the length of the sequence.
  114. If START or END is negative, it counts from the end."
  115. (if (stringp seq) (substring seq start end)
  116. (let (len)
  117. (and end (< end 0) (setq end (+ end (setq len (length seq)))))
  118. (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
  119. (cond ((listp seq)
  120. (if (> start 0) (setq seq (nthcdr start seq)))
  121. (if end
  122. (let ((res nil))
  123. (while (>= (setq end (1- end)) start)
  124. (push (pop seq) res))
  125. (nreverse res))
  126. (copy-sequence seq)))
  127. (t
  128. (or end (setq end (or len (length seq))))
  129. (let ((res (make-vector (max (- end start) 0) nil))
  130. (i 0))
  131. (while (< start end)
  132. (aset res i (aref seq start))
  133. (setq i (1+ i) start (1+ start)))
  134. res))))))
  135. (provide 'erc-compat)
  136. ;;; erc-compat.el ends here
  137. ;;
  138. ;; Local Variables:
  139. ;; indent-tabs-mode: t
  140. ;; tab-width: 8
  141. ;; End: