Re: autoload cookies

Andrey Slusar <[email protected]> Fri, 22 Oct 2004 21:59:13 +0300
Newsgroups gmane.emacs.xemacs.user.russian
Organization XEmacs User
Message-ID <[email protected]>
Fri, 22 Oct 2004 19:51:26 +0400, Nickolay Pakoulin wrote:

> Файлы с autoload'ами строятся при сборке пакетов.  Из makefile вызывается
> xemacs, который выполняет функцию batch-update-autoloads.

> Функция читает файлы из каталога и по magic-cookie ###autoload скидывает формы
> в файл с именем auto-autoloads.

 Тогда непонятно вот что - я запостил патч на semantic, который
добавляет функции, нужные для работы semantic на xemacs 21.4, но они
есть в xemacs 21.5
 Вот:
================================================================
in semantic package function "semanticdb-cache-filename" require
"subst-char-in-string" and "replace-regexp-in-string" functions. I am
ported this functions from XEmacs 21.5.17

--8<---------------cut here---------------start------------->8---
diff -ru semantic.orig/semanticdb.el semantic/semanticdb.el
--- semantic.orig/semanticdb.el	Sat Oct 16 00:52:39 2004
+++ semantic/semanticdb.el	Sat Oct 16 01:39:25 2004
@@ -314,6 +314,78 @@
       nil)
     ))
 
+;; `subst-char-in-string' undefined in XEmacs <= 21.4
+(unless (fboundp 'subst-char-in-string)
+  (defun subst-char-in-string (fromchar tochar string &optional inplace)
+    "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
+Unless optional argument INPLACE is non-nil, return a new string."
+    (let ((i (length string))
+	  (newstr (if inplace string (copy-sequence string))))
+      (while (> i 0)
+	(setq i (1- i))
+	(if (eq (aref newstr i) fromchar)
+	    (aset newstr i tochar)))
+      newstr)))
+
+;; `replace-regexp-in-string' undefined in XEmacs <= 21.4
+(unless (fboundp 'replace-regexp-in-string)
+  (defun replace-regexp-in-string (regexp rep string &optional
+					  fixedcase literal subexp start)
+    "Replace all matches for REGEXP with REP in STRING.
+
+Return a new string containing the replacements.
+
+Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
+arguments with the same names of function `replace-match'.  If START
+is non-nil, start replacements at that index in STRING.
+
+REP is either a string used as the NEWTEXT arg of `replace-match' or a
+function.  If it is a function it is applied to each match to generate
+the replacement passed to `replace-match'; the match-data at this
+point are such that match 0 is the function's argument.
+
+To replace only the first match (if any), make REGEXP match up to \\'
+and replace a sub-expression, e.g.
+  (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1)
+    => \" bar foo\"
+"
+
+    ;; To avoid excessive consing from multiple matches in long strings,
+    ;; don't just call `replace-match' continually.  Walk down the
+    ;; string looking for matches of REGEXP and building up a (reversed)
+    ;; list MATCHES.  This comprises segments of STRING which weren't
+    ;; matched interspersed with replacements for segments that were.
+    ;; [For a `large' number of replacments it's more efficient to
+    ;; operate in a temporary buffer; we can't tell from the function's
+    ;; args whether to choose the buffer-based implementation, though it
+    ;; might be reasonable to do so for long enough STRING.]
+    (let ((l (length string))
+	  (start (or start 0))
+	  matches str mb me)
+      (save-match-data
+	(while (and (< start l) (string-match regexp string start))
+	  (setq mb (match-beginning 0)
+		me (match-end 0))
+	  ;; If we matched the empty string, make sure we advance by one char
+	  (when (= me mb) (setq me (min l (1+ mb))))
+	  ;; Generate a replacement for the matched substring.
+	  ;; Operate only on the substring to minimize string consing.
+	  ;; Set up match data for the substring for replacement;
+	  ;; presumably this is likely to be faster than munging the
+	  ;; match data directly in Lisp.
+	  (string-match regexp (setq str (substring string mb me)))
+	  (setq matches
+		(cons (replace-match (if (stringp rep)
+					 rep
+				       (funcall rep (match-string 0 str)))
+				     fixedcase literal str subexp)
+		      (cons (substring string start mb) ; unmatched prefix
+			    matches)))
+	  (setq start me))
+	;; Reconstruct a string from the pieces.
+	(setq matches (cons (substring string start l) matches)) ; leftover
+	(apply #'concat (nreverse matches))))))
+
 ;;; Filename manipulation
 ;;
 (defun semanticdb-cache-filename (path)
--8<---------------cut here---------------end--------------->8---
===================================================================

На что мне Stephen ответил:
====================================================================
    Andrey> in semantic package function "semanticdb-cache-filename"
    Andrey> require "subst-char-in-string" and
    Andrey> "replace-regexp-in-string" functions. I am ported this
    Andrey> functions from XEmacs 21.5.17

Nice catch!  But this is a bad idea, IMO:

    Andrey> diff -ru semantic.orig/semanticdb.el semantic/semanticdb.el

How about putting the conditional code in a separate file with
;;;###autoload cookies or some such?  If that doesn't work yet, we
should find a way to make it work and start the "future" package.

("(require 'future)" isn't as cool as Python's "from future import
...", but it ain't bad.  ;-)
===================================================================

 Зачем в этом случае ';;;###autoload'?

---- 
 Андрей Слюсар.
 JID: [email protected]