erc-compat.el 5.6 KB

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