1 ;;; xetla-core.el --- Core of xetla
3 ;; Copyright (C) 2003-2004 by Stefan Reichoer (GPL)
4 ;; Copyright (C) 2004 Steve Youngs (BSD)
6 ;; Author: Steve Youngs <steve@eicq.org>
7 ;; Maintainer: Steve Youngs <steve@eicq.org>
9 ;; Keywords: arch archive tla
11 ;; Based on xtla-core.el by: Stefan Reichoer, <stefan@xsteve.at>
13 ;; This file is part of XEtla.
15 ;; Redistribution and use in source and binary forms, with or without
16 ;; modification, are permitted provided that the following conditions
19 ;; 1. Redistributions of source code must retain the above copyright
20 ;; notice, this list of conditions and the following disclaimer.
22 ;; 2. Redistributions in binary form must reproduce the above copyright
23 ;; notice, this list of conditions and the following disclaimer in the
24 ;; documentation and/or other materials provided with the distribution.
26 ;; 3. Neither the name of the author nor the names of any contributors
27 ;; may be used to endorse or promote products derived from this
28 ;; software without specific prior written permission.
30 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
31 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 ;; DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 ;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
37 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
39 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
40 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 ;; Contributions from:
45 ;; Matthieu Moy <Matthieu.Moy@imag.fr>
46 ;; Masatake YAMATO <jet@gyve.org>
47 ;; Milan Zamazal <pdm@zamazal.org>
48 ;; Martin Pool <mbp@sourcefrog.net>
49 ;; Robert Widhopf-Fenk <hack@robf.de>
50 ;; Mark Triggs <mst@dishevelled.net>
53 ;; This file provides the low-level functions used by xetla.el
61 ;; --------------------------------------
62 ;; Compatibility stuff
63 ;; --------------------------------------
66 (autoload 'read-passwd "passwd"))
70 (defvar xetla-buffer-type-alist
71 '((changes "*xetla-changes*" root)
72 (inventory "*xetla-inventory*" path)
73 (missing "*xetla-missing*" single)
74 (cat-log "*xetla-cat-log(%s)*" string)
75 (file-diff "*xetla-file-diff*" path)
76 (changelog "*xetla-changelog*" root)
77 (tree-lint "*xetla-tree-lint*" root)
78 (logs "*xetla-logs*" root)
79 (errors "*xetla-error*" multiple)
80 (generic "*xetla-process*" multiple)
81 (browse "*xetla-browse*" single)
82 (changeset "*xetla-changeset(%s)*" string))
83 "List of (type name mode) used to generate a name for a buffer.
85 TYPE is the type of buffer to create, passed as the first argument to
86 `xetla-get-buffer-create'.
88 NAME is a string, used as a name for the returned buffer.
90 MODE is a symbol defining the way to manage (value of
91 `default-directory' in the created buffer) paths for this type of
92 buffers. It can have the following values:
94 * 'root: `default-directory' will be the tree-root of the specified
97 * 'path: `default-directory' will be the path specified.
99 For 'root and 'path, `xetla-get-buffer-create' will return the existing
100 buffer for this type and this path if it exists, or create a new one
103 * 'single: There is only one buffer of this type for each Emacs
104 instance. If a path is provided, `default-directory' is set to that
105 path. Otherwise, the path is left unchanged when a buffer is
106 reused, and set to the current directory on buffer creation.
108 * 'multiple: `default-directory' is set to the path specified. A new
109 buffer is returned anyway. (No buffer reuse).
111 * 'string: The path specified is actually a string. It won't be used
112 to set `default-directory'. The name of the created buffer will be
113 (format name string).")
115 (defvar xetla-buffers-tree nil
116 "Tree containing all xetla buffers.
119 ((type1 (\"path1\" buffer \"original name of buffer\")
120 (\"path2\" buffer2 \"original name of buffer2\"))
121 (type2 (\"path1\" buffer3 \"original name of buffer3\")
122 (\"path3\" buffer4 \"original name of buffer4\")))
123 Used to keep track of all the xetla related buffers.")
125 (defun xetla-buffers-tree-remove (buffer)
126 "Remove BUFFER from the buffers tree."
127 (dolist (type-cons xetla-buffers-tree)
128 (dolist (path-buffer (cdr type-cons))
129 (when (eq (cadr path-buffer) buffer)
130 (setcdr type-cons (delete path-buffer (cdr type-cons)))))))
132 (defun xetla-buffers-tree-add (type path buffer)
133 "Add a buffer of TYPE visiting PATH to the buffers tree.
134 BUFFER should be the buffer to add."
135 (let ((current-assoc (assoc type xetla-buffers-tree)))
137 (setcdr current-assoc
138 (cons (list path buffer (buffer-name buffer))
139 (cdr current-assoc)))
140 (setq xetla-buffers-tree
141 (cons `(,type (,path ,buffer ,(buffer-name buffer))) xetla-buffers-tree)))))
143 (defvar xetla-temp-directory (temp-directory)
144 "Temporary directory for some xetla operations.")
146 (defun xetla-make-temp-name (file)
147 "Generate a temporary file name based on FILE.
148 The path for the file name can be set via `xetla-temp-directory'."
149 (make-temp-name (concat (xetla-uniquify-file-name xetla-temp-directory) file)))
151 (defun xetla-uniquify-file-name (path)
152 "Return a unique string designating PATH.
153 If PATH is a directory,the returned contains one and exactly one trailing
154 slash. If PATH is nil, then nil is returned."
156 (let ((expanded (expand-file-name
157 (if (file-directory-p path)
158 (file-name-as-directory path)
160 (if (featurep 'xemacs)
161 (replace-regexp-in-string "/+$" "/" expanded)
164 (defun xetla-config-file-full-path (file &optional create-config-dir)
165 "Return the full path for the config file FILE.
166 FILE will be stored in the `xetla-config-directory'.
167 If CREATE-CONFIG-DIR is non nil, ensure that the `xetla-config-directory'
169 (let ((full-name (xetla-uniquify-file-name
170 (concat xetla-config-directory file))))
171 (unless (file-exists-p xetla-config-directory)
172 (when create-config-dir
173 (make-directory xetla-config-directory t)
174 (message "The config files of Xetla will be stored in %s!"
175 xetla-config-directory)
177 ;; TODO remove migration code as some time in the future
178 (unless (file-exists-p (expand-file-name xetla-bookmarks-file-name
179 xetla-config-directory))
180 (let ((old-ones (list (list (expand-file-name xetla-bookmarks-file-name
181 xetla-config-directory)
182 "~/.xetla-bookmarks.el"
183 "~/.xetla/.xetla-bookmarks.el")))
186 (setq olds (car old-ones) old-ones (cdr old-ones))
187 (if olds (setq n (car olds) olds (cdr olds)))
189 (setq o (expand-file-name (car olds)) olds (cdr olds))
190 (if (file-exists-p o)
191 (if (yes-or-no-p (format "Migrate %s to %s? " o n))
193 (if (yes-or-no-p (format "Delete %s? " o))
194 (delete-file o))))))))
198 (defun xetla-stringise (uberstring &rest args)
199 "Create a string from UBERSTRING.
201 - a string which evals to itself
202 - a variable whose value is a string
203 - a function which is called and should eval to a string
204 - a form which is evaluated and should return a string.
206 All results are run against `xetla-stringise' again
207 until finally a string is produced."
208 (cond ((stringp uberstring)
210 ((and (symbolp uberstring)
212 (xetla-stringise (symbol-value uberstring)))
213 ((and (symbolp uberstring)
214 (fboundp uberstring))
215 (xetla-stringise (apply uberstring args)))
216 ((ignore-errors (xetla-stringise (eval uberstring))))
217 ((ignore-errors (xetla-stringise (eval (cons 'progn uberstring)))))
220 ;; this is useful since i can now have something like:
222 ;; (set-alist 'xetla-buffer-type-alist
224 ;; '((format "*xetla-inventory%s*"
225 ;; (let ((dir (replace-regexp-in-string
226 ;; "^.+/\\([^/]+\\).?$"
228 ;; (expand-file-name path))))
229 ;; (if dir (format " (%s)" dir)
232 ;; to always have the path in the buffer name of the inventory
234 (defun xetla-get-buffer-type (type)
235 "Get entry in `xetla-buffer-type-alist' for given TYPE."
236 (assoc type xetla-buffer-type-alist))
238 (defun xetla-get-buffer-name (type)
239 "Get name part of `xetla-buffer-type-alist' for given TYPE."
240 (let ((name (cadr (xetla-get-buffer-type type))))
241 (xetla-stringise name)))
243 (defun xetla-get-buffer-mode (type)
244 "Get name part of `xetla-buffer-type-alist' for given TYPE."
245 (let ((mode (caddr (xetla-get-buffer-type type))))
249 (autoload 'xetla-tree-root "xetla"))
251 (defun xetla-get-buffer-create (type &optional path)
252 "Get a buffer of type TYPE for the path PATH.
254 Maybe reuse one if it exists, according to the value of
255 `xetla-buffer-type-alist' (see its docstring), or, call
256 `create-file-buffer' to create the buffer.
258 See also `xetla-get-buffer'"
259 ;; Inspired from `cvs-get-buffer-create'
260 (let* ((path (or path default-directory))
261 (elem (assoc type xetla-buffer-type-alist))
263 (or (xetla-get-buffer type path mode)
264 ;; Buffer couldn't be reused. Create one
265 (let ((path (case mode
266 (root (xetla-uniquify-file-name
267 (xetla-tree-root path)))
269 (t (xetla-uniquify-file-name path))))
270 (name (xetla-get-buffer-name type)))
272 (if (eq mode 'string)
273 (get-buffer-create (format name path))
274 (let ((default-directory (or path default-directory)))
275 (create-file-buffer (or name "*xetla-buffer*"))))))
276 (with-current-buffer buffer
277 (xetla-buffers-tree-add type path buffer)
280 (add-hook 'kill-buffer-hook 'xetla-kill-buffer-function)
282 (defun xetla-kill-buffer-function ()
283 "Function run when a buffer is killed."
284 (xetla-buffers-tree-remove (current-buffer))
285 (xetla-kill-process-maybe (current-buffer)))
287 (defun xetla-get-buffer (type &optional path mode)
288 "Get a buffer of type TYPE for the path PATH.
290 Maybe reuse one if it exists, depending on the value of MODE (see
291 `xetla-buffer-type-alist' 's third element), otherwise, return nil. See
292 also `xetla-get-buffer-create'."
293 (let ((mode (or mode (xetla-get-buffer-mode type)))
294 (path (or path default-directory)))
295 (if (eq mode 'single)
296 ;; nothing to do about PATH. Reuse anyway
297 (let* ((list-path (cdr (assoc type xetla-buffers-tree)))
298 (first-elem (car list-path)))
300 (if (string= (buffer-name (cadr first-elem))
303 (setcdr (assoc type xetla-buffers-tree) nil)
306 (let ((path (and path
308 (root (xetla-uniquify-file-name
309 (xetla-tree-root path)))
311 (t (xetla-uniquify-file-name path))))))
312 (if (eq mode 'multiple)
313 ;; no need to search an existing buffer
315 (let* ((list-path (assoc type xetla-buffers-tree))
316 (elem (assoc path (cdr list-path)))
317 (buffer (cadr elem)))
319 (if (and (buffer-live-p buffer)
320 ;; the buffer has not been renamed
321 (string= (buffer-name buffer)
324 ;; remove the buffer and try again
326 (delq (assoc path (cdr list-path))
328 (xetla-get-buffer type path mode)))))))))
330 (defun xetla-add-buffer-type (type name)
331 "Define a new TYPE of buffer whose buffer will be named NAME."
332 (unless (assoc type xetla-buffer-type-alist)
333 (push (list type name) xetla-buffer-type-alist)))
335 (defun xetla-position (item seq)
336 "Position of ITEM in list, or nil if not found.
337 Return 0 if ITEM is the first element of SEQ"
341 (not (eq (car seq-int) item)))
342 (setq seq-int (cdr seq-int))
347 (defun xetla-last-visited-inventory-buffer ()
348 "Return the last visited xetla's inventory buffer."
349 (let ((inventories (remove nil (mapcar
351 (when (buffer-live-p (cadr elt))
353 (cdr (assoc 'inventory xetla-buffers-tree)))))
355 (cadr (car (sort inventories (lambda (a b)
356 (let ((aindex (xetla-position (cadr a) bl))
357 (bindex (xetla-position (cadr b) bl)))
358 (< aindex bindex))))))))
360 (defun xetla-show-inventory-buffer ()
361 "Switch to the last visited inventory buffer."
363 (xetla-switch-to-buffer (xetla-last-visited-inventory-buffer)))
365 ;; --------------------------------------
367 ;; --------------------------------------
369 (defcustom xetla-process-buffer " *xetla-process*"
370 "*Name of the process buffer."
372 :group 'xetla-internal)
374 (defcustom xetla-error-buffer " *xetla-errors*"
375 "*Name of the buffer to which xetla's stderr is redirected."
377 :group 'xetla-internal)
379 (defcustom xetla-number-of-dead-process-buffer 20
380 "*Number of process buffers to keep after process termination.
381 When the number of process buffers exceeds this number, the most ancient
382 is killed. This includes both the process buffer and the error
383 buffer (to which stderr is redirected).
385 A nil value here means \"Never kill any process buffer\". Useful for
386 debugging, but this will eat the memory of your computer ;-)"
388 :group 'xetla-internal)
390 (defcustom xetla-show-internal-buffers-on-menu nil
391 "Toggle display of dead process buffers in the buffer menu."
393 :group 'xetla-internal)
395 (defvar xetla-dead-process-buffer-queue nil
396 "List of process buffers belonging to terminated processes.
397 When the list is greater than `xetla-number-of-dead-process-buffer', the last
400 (defun xetla-kill-process-buffer (buffer)
401 "Don't actually kill BUFFER, but add it to `xetla-dead-process-buffer-queue'.
402 It will eventually be killed when the number of buffers in
403 `xetla-dead-process-buffer-queue'exceeds `xetla-number-of-dead-process-buffer'."
404 (add-to-list 'xetla-dead-process-buffer-queue buffer t)
405 (setq xetla-dead-process-buffer-queue
406 (remove-if-not #'buffer-live-p xetla-dead-process-buffer-queue))
407 (when xetla-number-of-dead-process-buffer
408 (while (> (length xetla-dead-process-buffer-queue)
409 (max 2 xetla-number-of-dead-process-buffer))
410 (let ((currbuf (car xetla-dead-process-buffer-queue)))
411 ;; just to be completely certain
412 (when (buffer-live-p currbuf)
413 (kill-buffer currbuf))
414 ;; now remove that killed buffer
415 (setq xetla-dead-process-buffer-queue
416 (cdr xetla-dead-process-buffer-queue))))))
418 (defvar xetla-last-process-buffer nil
419 "The last created process buffer.")
421 (defvar xetla-last-error-buffer nil
422 "The last created process buffer.")
424 (defun xetla-new-process-buffer (to-be-deleted)
425 "Create a new process buffer.
426 If TO-BE-DELETED is non-nil, make this buffer a candidate for eventually
428 (let ((buffer (create-file-buffer xetla-process-buffer)))
429 (setq xetla-last-process-buffer buffer)
430 (when to-be-deleted (xetla-kill-process-buffer buffer))
433 (defun xetla-new-error-buffer (to-be-deleted)
434 "Create a new error buffer.
435 If TO-BE-DELETED is non-nil, make this buffer a candidate for eventually
437 (let ((buffer (create-file-buffer xetla-error-buffer)))
438 (setq xetla-last-error-buffer buffer)
439 (when to-be-deleted (xetla-kill-process-buffer buffer))
442 ;; --------------------------------------
443 ;; Process management
444 ;; --------------------------------------
446 ;; Candidates for process handlers
447 (defun xetla-default-error-function (output error status arguments)
448 "Default function called when a xetla process ends with a non-zero status.
449 OUTPUT is the buffer containing process standard output.
450 ERROR is the buffer containing process error output.
451 STATUS indicates the return status of the program.
452 ARGUMENTS is a list of the arguments that the process was called with."
453 (if (> (with-current-buffer error (point-max)) 1)
454 (xetla-show-error-buffer error)
455 (if (> (with-current-buffer output (point-max)) 1)
456 (xetla-show-error-buffer output)
457 (error "`tla %s' failed with code %d and no output!"
458 (mapconcat 'identity arguments " ")
460 (error "`tla %s' failed with code %d"
461 (mapconcat 'identity arguments " ")
464 (defvar xetla-default-killed-function-noerror 0
465 "The number of killed processes we will ignore until throwing an error.
466 If the value is 0, `xetla-default-killed-function' will throw an error.
467 See `xetla-default-killed-function'.")
469 (defun xetla-default-killed-function (output error status arguments)
470 "Default function called when a xetla process is killed.
471 OUTPUT is the buffer containing process standard output.
472 ERROR is the buffer containing process error output.
473 STATUS indicates the return status of the program.
474 ARGUMENTS is a list of the arguments that the process was called with."
475 (if (> xetla-default-killed-function-noerror 0)
476 (setq xetla-default-killed-function-noerror
477 (- xetla-default-killed-function-noerror 1))
478 (xetla-switch-to-buffer error)
479 (error "`tla %s' process killed !"
480 (mapconcat 'identity arguments " "))))
482 (defun xetla-null-handler (output error status arguments)
483 "Handle a finished process without doing anything.
484 Candidate as an argument for one of the keywords :finished, :error or :killed
485 in `xetla-run-tla-sync' or `xetla-run-tla-async'.
486 OUTPUT is the buffer containing process standard output.
487 ERROR is the buffer containing process error output.
488 STATUS indicates the return status of the program.
489 ARGUMENTS is a list of the arguments that the process was called with."
492 (defun xetla-status-handler (output error status arguments)
493 "Return an integer value that reflects the process status.
494 Candidate as an argument for one of the keywords :finished, :error or :killed
495 in `xetla-run-tla-sync' or `xetla-run-tla-async'.
496 OUTPUT is the buffer containing process standard output.
497 ERROR is the buffer containing process error output.
498 STATUS indicates the return status of the program.
499 ARGUMENTS is a list of the arguments that the process was called with."
500 (cond ((numberp status) status)
501 ((string-match "^exited abnormally with code \\(.*\\)" status)
502 (string-to-int (match-string 1)))
505 (defun xetla-output-buffer-handler (output error status arguments)
506 "Return the output of a finished process, stripping any trailing newline.
507 OUTPUT is the buffer containing process standard output.
508 ERROR is the buffer containing process error output.
509 STATUS indicates the return status of the program.
510 ARGUMENTS is a list of the arguments that the process was called with."
511 (xetla-buffer-content output))
513 (defun xetla-output-buffer-split-handler (output error status arguments)
514 "Return the output of a finished process as a list of lines.
515 OUTPUT is the buffer containing process standard output.
516 ERROR is the buffer containing process error output.
517 STATUS indicates the return status of the program.
518 ARGUMENTS is a list of the arguments that the process was called with."
519 (split-string (xetla-buffer-content output) "\n"))
521 (defun xetla-default-finish-function (output error status arguments)
522 "Default function called when a xetla process terminates.
523 OUTPUT is the buffer containing process standard output.
524 ERROR is the buffer containing process error output.
525 STATUS indicates the return status of the program.
526 ARGUMENTS is a list of the arguments that the process was called with."
527 (with-current-buffer output
528 (xetla-process-buffer-mode))
529 (xetla-switch-to-buffer output)
530 (message "`tla %s' process finished !"
531 (mapconcat 'identity arguments " "))
534 (defun xetla-finish-function-without-buffer-switch (output error status arguments)
535 "Similar to `xetla-default-finish-function' but no buffer switch.
536 OUTPUT is the buffer containing process standard output.
537 ERROR is the buffer containing process error output.
538 STATUS indicates the return status of the program.
539 ARGUMENTS is a list of the arguments that the process was called with."
540 (message "`tla %s' process finished !"
541 (mapconcat 'identity arguments " "))
544 (defvar xetla-log-cookie nil)
546 (defstruct (xetla-event) output-buffer error-buffer related-buffer
547 command tree event time)
549 (defun xetla-strip-final-newline (string)
550 "Strip the final newline from STRING if there's one."
551 (if (eq (aref string (- (length string) 1)) ?\n)
552 (substring string 0 (- (length string) 1))
555 (defsubst xetla-log-printer-print-buffer (buffer function)
556 "Helper function for `xetla-log-printer'.
557 Print a buffer filed for BUFFER during printing a log event.
558 The printed name of BUFFER is mouse sensitive. If the user
559 clicks it, FUNCTION is invoked."
561 (or (buffer-name buffer)
562 (pp-to-string buffer))
564 (when (buffer-live-p buffer)
565 (let ((map (make-sparse-keymap)))
566 (define-key map [button2] function)
571 (defun xetla-log-printer (elem)
572 "XEtla event printer which prints ELEM."
573 (let ((event (xetla-event-event elem))
576 "Command: " (xetla-event-command elem)
577 "\nDirectory: " (xetla-face-add (xetla-event-tree elem)
578 'xetla-local-directory)
579 "\nDate: " (format-time-string "%c" (xetla-event-time elem))
580 "\nRelated Buffer: " (xetla-log-printer-print-buffer
581 (xetla-event-related-buffer elem)
582 'xetla-switch-to-related-buffer-by-mouse)
583 "\nOutput Buffer: " (xetla-log-printer-print-buffer
584 (xetla-event-output-buffer elem)
585 'xetla-switch-to-output-buffer-by-mouse)
586 "\nError Buffer: " (xetla-log-printer-print-buffer
587 (xetla-event-error-buffer elem)
588 'xetla-switch-to-error-buffer-by-mouse)
589 (if (not (string= event "started"))
590 (concat "\nEvent: " event)
593 ;; Reflect the point to `default-directory'.
594 ;; NOTE: XEmacs doesn't have `point-entered' special text property.
597 'point-entered (lambda (old new)
598 (setq default-directory
601 (ewoc-locate xetla-log-cookie))))))
604 (defvar xetla-process-running nil
605 "List of xetla processes running.
606 A value of nil indicates no processes are running.
608 The list is a list of pairs (process event) where EVENT is the event
609 corresponding to the beginning of the execution of process. It can be
610 used to get more info about the process.")
612 (defmacro xetla-switch-to-buffer-macro (function accessor)
613 "Define a FUNCTION for switching to the buffer associated with some event.
614 ACCESSOR is a function for retrieving the appropriate buffer from a
615 `xetla-event'structure."
617 "In a log buffer, pops to the output or error buffer corresponding to the
620 (let ((buffer (,accessor
621 (ewoc-data (ewoc-locate xetla-log-cookie)))))
622 (cond ((buffer-live-p buffer)
623 (xetla-switch-to-buffer buffer)
624 (unless (member buffer
626 (process-buffer (car p)))
627 xetla-process-running))
628 (xetla-process-buffer-mode)))
629 (t (error "Buffer has been killed"))))))
631 (xetla-switch-to-buffer-macro xetla-switch-to-output-buffer
632 xetla-event-output-buffer)
634 (xetla-switch-to-buffer-macro xetla-switch-to-error-buffer
635 xetla-event-error-buffer)
637 (xetla-switch-to-buffer-macro xetla-switch-to-related-buffer
638 xetla-event-related-buffer)
640 (defmacro xetla-make-bymouse-function (function)
641 "Create a new function by adding mouse interface to FUNCTION.
642 The new function is named FUNCTION-by-mouse; and takes one argument,
644 Thew new function moves the point to the place where mouse is clicked
645 then invoke FUNCTION."
646 `(defun ,(intern (concat (symbol-name function) "-by-mouse")) (event)
647 ,(concat "`" (symbol-name function) "'" " with mouse interface.")
649 (mouse-set-point event)
652 (xetla-make-bymouse-function xetla-switch-to-output-buffer)
653 (xetla-make-bymouse-function xetla-switch-to-error-buffer)
654 (xetla-make-bymouse-function xetla-switch-to-related-buffer)
656 (defun xetla-log-event (output error command tree event)
657 "Log an event in the `xetla-log-buffer' buffer.
658 OUTPUT is the buffer containing process standard output.
659 ERROR is the buffer containing process error output.
660 COMMAND is the command that was executed.
661 TREE is the process's working directory.
662 EVENT is the event that occurred.
664 (unless (and xetla-log-cookie
665 (buffer-live-p (ewoc-buffer xetla-log-cookie)))
666 (with-current-buffer (get-buffer-create xetla-log-buffer)
667 (setq xetla-log-cookie
668 (ewoc-create 'xetla-log-printer))
669 (xetla-log-buffer-mode)))
670 (let ((related-buffer (current-buffer)))
671 (with-current-buffer (ewoc-buffer xetla-log-cookie)
672 (let ((elem (make-xetla-event :output-buffer output
674 :related-buffer related-buffer
678 :time (current-time)))
680 (ewoc-enter-last xetla-log-cookie elem)
681 (ewoc-refresh xetla-log-cookie)
684 (defun xetla-log-next ()
685 "Move to the next log entry."
687 (let ((next (ewoc-next xetla-log-cookie
688 (ewoc-locate xetla-log-cookie))))
689 (when next (goto-char (ewoc-location next)))))
691 (defun xetla-log-prev ()
692 "Move to the previous log entry."
694 (let ((prev (ewoc-prev xetla-log-cookie
695 (ewoc-locate xetla-log-cookie))))
696 (when prev (goto-char (ewoc-location prev)))))
698 (define-derived-mode xetla-log-buffer-mode fundamental-mode "XEtla Log"
699 "Major mode for Xetla's internal log buffer. You can open this buffer
700 with `xetla-open-internal-log-buffer'."
701 (toggle-read-only 1))
703 (define-derived-mode xetla-process-buffer-mode fundamental-mode
705 "Major mode for process buffers. Mainly defines \\[bury-buffer]
707 (toggle-read-only 1))
709 (defmacro xetla-with-keywords (keywords plist &rest body)
710 "Execute a body of code with keywords bound.
711 Each keyword listed in KEYWORDS is bound to its value from PLIST, then
713 (flet ((keyword-to-symbol (keyword)
714 (intern (substring (symbol-name keyword) 1))))
715 (let ((keyword (make-symbol "keyword"))
716 (default (make-symbol "default")))
717 `(let ,(mapcar (lambda (keyword-entry)
718 (keyword-to-symbol (if (consp keyword-entry)
722 (dolist (keyword-entry ',keywords)
723 (let ((,keyword (if (consp keyword-entry)
726 (,default (if (consp keyword-entry)
729 (set (intern (substring (symbol-name ,keyword) 1))
730 (or (cadr (member ,keyword ,plist))
733 (put 'xetla-with-keywords 'lisp-indent-function 1)
735 (defun xetla-build-xetla-command (list-args)
736 "Build a shell command to run tla with args LIST-ARGS."
737 (mapconcat 'shell-quote-argument
738 (cons xetla-tla-executable
739 (delq nil list-args))
743 (defcustom xetla-password-prompt-regexp
744 "[Pp]ass\\(word\\|phrase\\).*:\\s *\\'"
745 "*Regexp matching prompts for passwords in the inferior process.
746 This is used by `eshell-watch-for-password-prompt'."
750 (defun xetla-process-filter (proc string)
751 "Filter PROC's STRING.
752 Prompt for password with `read-passwd' if the output of PROC matches
753 `xetla-password-prompt-regexp'."
754 (with-current-buffer (process-buffer proc)
755 (insert (replace-regexp-in-string "\015" "\n" string))
756 (when (string-match xetla-password-prompt-regexp string)
757 (string-match "^\\([^\n]+\\)\n*\\'" string)
758 (let ((passwd (read-passwd (match-string 1 string))))
759 (process-send-string proc (concat passwd "\n"))))))
761 (defun xetla-run-tla-async (arguments &rest keys)
762 "Run tla asynchronously.
763 ARGUMENTS is a list of arguments. nil values in this list are removed.
764 KEYS is a list of keywords and values. Possible keywords are:
766 :finished ....... Function run when the process finishes. If none
767 specified, `xetla-default-finish-function' is run.
769 :killed ......... Function run when the process is killed. If none
770 specified, `xetla-default-killed-function' is run.
772 :error .......... Function run when the process exits with a non 0
773 status. If none specified,
774 `xetla-default-error-function' is run.
776 All these functions take 4 arguments : output, error, status, and
779 - \"output\" is the output buffer
780 - \"error\" is the buffer where standard error is redirected
781 - \"status\" is the numeric exit-status or the signal number
782 - \"arguments\" is the list of arguments, as a list of strings,
783 like '(\"changes\" \"-diffs\")
785 `xetla-null-handler' can be used here if there's nothing to do.
787 :output-buffer .. Buffer where the output of the process should be
788 redirected. If none specified, a new one is
789 created, and will be entered in
790 `xetla-dead-process-buffer-queue' to be killed
793 :error-buffer ... Buffer where the standard error of the process
794 should be redirected.
796 :related-buffer . Defaults to `current-buffer'. This is the buffer
797 where the result of the process will be used. If
798 this buffer is killed before the end of the
799 execution, the user is prompted if he wants to kill
803 (xetla-run-tla-async `(\"changes\" ,(unless arg \"-diffs\"))
805 (lambda (output error status arguments)
806 (message \"No changes in this working copy\"))
808 (lambda (output error status arguments)
809 (xetla-show-changes-buffer output)))"
811 (:finished :killed :error :output-buffer :error-buffer :related-buffer)
813 (let* ((output-buf (or (and output-buffer (get-buffer-create output-buffer))
814 (xetla-new-process-buffer nil)))
815 (error-buf (or (and error-buffer (get-buffer-create error-buffer))
816 (xetla-new-error-buffer nil)))
817 (error-file (xetla-make-temp-name "arch-errors"))
818 (command (xetla-build-xetla-command arguments))
819 ;; Make the `default-directory' unique. The trailing slash
820 ;; may be necessary in some cases.
821 (default-directory (xetla-uniquify-file-name default-directory))
822 (process (start-process
823 xetla-tla-executable output-buf
826 command error-file)))
829 (xetla-log-event output-buf
832 default-directory "started"))))
833 (with-current-buffer (or related-buffer (current-buffer))
834 (message "running process `%s' in `%s'" command default-directory)
835 (add-to-list 'xetla-process-running process-event)
836 (set-process-filter process 'xetla-process-filter)
837 (set-process-sentinel
839 `(lambda (process event)
840 (let ((default-directory ,default-directory))
841 (xetla-log-event ,output-buf ,error-buf ,command
843 (xetla-strip-final-newline event))
844 (setq xetla-process-running
845 (delq ',process-event xetla-process-running))
846 (when (file-exists-p ,error-file)
847 (with-current-buffer ,error-buf
848 (insert-file-contents ,error-file))
849 (delete-file ,error-file))
850 (let ((state (process-status process))
851 (status (process-exit-status process)))
853 (cond ((and (eq state 'exit) (= status 0))
854 (funcall (or (quote ,finished)
855 'xetla-default-finish-function)
856 ,output-buf ,error-buf
857 status (quote ,arguments)))
859 (funcall (or (quote ,killed)
860 'xetla-default-killed-function)
861 ,output-buf ,error-buf status
863 ((eq state 'exit) ;; status != 0
864 (funcall (or (quote ,error)
865 'xetla-default-error-function)
866 ,output-buf ,error-buf status
867 (quote ,arguments)))))
868 ;; Schedule any buffers we created for killing
869 (unless ,output-buffer (xetla-kill-process-buffer ,output-buf))
870 (unless ,error-buffer (xetla-kill-process-buffer ,error-buf))))))
873 (defun xetla-run-tla-sync (arguments &rest keys)
874 "Run tla synchronously.
875 See `xetla-run-tla-async' for details on possible ARGUMENTS and KEYS."
877 (:finished :killed :error :output-buffer :error-buffer :related-buffer)
879 (let ((output-buf (or (and output-buffer (get-buffer-create output-buffer))
880 (xetla-new-process-buffer t)))
881 (error-buf (or (and error-buffer (get-buffer-create error-buffer))
882 (xetla-new-error-buffer t)))
883 (command (xetla-build-xetla-command arguments))
884 (error-file (xetla-make-temp-name "arch-errors"))
885 ;; Make the `default-directory' unique. The trailing slash
886 ;; may be necessary in some cases.
887 (default-directory (xetla-uniquify-file-name default-directory)))
888 (with-current-buffer (or related-buffer (current-buffer))
889 (xetla-log-event output-buf error-buf command default-directory "started")
890 (let ((status (call-process "sh" nil output-buf nil "-c"
894 (when (file-exists-p error-file)
895 (with-current-buffer error-buf
896 (insert-file-contents error-file))
897 (delete-file error-file))
899 (cond ((stringp status)
900 (when (string= status "Terminated")
901 (funcall (or killed 'xetla-default-killed-function)
902 output-buf error-buf status arguments)))
905 (funcall (or finished 'xetla-default-finish-function)
906 output-buf error-buf status arguments)
907 (funcall (or error 'xetla-default-error-function)
908 output-buf error-buf status arguments)))
909 (t (message "Unknown status - %s" status)))
910 ;; Schedule any buffers we created for killing
911 (unless output-buffer (xetla-kill-process-buffer output-buf))
912 (unless error-buffer (xetla-kill-process-buffer error-buf))))))))
914 (defun xetla-kill-process-maybe (buffer)
915 "Prompts and possibly kill process whose related buffer is BUFFER."
916 (let ((process-list nil))
917 (dolist (process-buffer xetla-process-running)
918 (when (eq (xetla-event-related-buffer (cadr process-buffer))
920 (add-to-list 'process-list (car process-buffer))))
921 (let ((l (length process-list)))
922 (when (and process-list
923 (y-or-n-p (format "%s process%s running in buffer %s. Kill %s? "
924 l (if (> l 1) "es" "")
926 (if (> l 1) "Them" "it"))))
927 (dolist (process process-list)
928 (setq xetla-default-killed-function-noerror
929 (1+ xetla-default-killed-function-noerror))
930 (if (eq (process-status process) 'run)
931 (kill-process process)))))))
934 (add-to-list 'minor-mode-alist
935 '(xetla-process-running
936 (:eval (if (equal (length xetla-process-running) 1)
938 (concat " tla running("
939 (int-to-string (length xetla-process-running))
943 (autoload 'xetla-flash-line "xetla"))
945 (defun xetla-open-internal-log-buffer ()
946 "Switch to the XEtla's internal log buffer.
947 This buffer contains a list of all the tla commands previously executed.
948 The buffer uses the mode `xetla-log-buffer-mode'"
950 (let ((buffer-name (buffer-name)))
951 (xetla-switch-to-buffer xetla-log-buffer)
952 (goto-char (point-max))
953 (when (re-search-backward (concat " Buffer: "
954 (regexp-quote buffer-name)
957 (xetla-flash-line))))
959 (defun xetla-clear-log-buffer ()
960 "Kill the log buffer."
961 (when (bufferp (get-buffer xetla-log-buffer))
962 (kill-buffer xetla-log-buffer)))
964 (defun xetla-buffer-content (buffer)
965 "Return the content of BUFFER as a string.
966 Strips the final newline if there is one."
967 (with-current-buffer buffer
968 (buffer-substring-no-properties
970 (progn (goto-char (point-max))
971 (if (eq (char-before) ?\n)
975 (defun xetla-get-process-output ()
976 "Return the content of the last process buffer.
977 Strips the final newline if there is one."
978 (xetla-buffer-content xetla-last-process-buffer))
980 (defun xetla-get-error-output ()
981 "Return the content of the last error buffer.
982 Strips the final newline if there is one."
983 (xetla-buffer-content xetla-last-error-buffer))
985 (defvar xetla-switched-buffer nil)
986 (defvar xetla-switched-from-buffer nil)
988 (defun xetla-switch-to-buffer (buffer)
989 "Switch to BUFFER using the user's preferred method.
990 See `xetla-switch-to-buffer-mode' for possible settings."
991 (setq xetla-switched-from-buffer (current-buffer))
992 (cond ((eq xetla-switch-to-buffer-mode 'pop-to-buffer)
993 (pop-to-buffer buffer))
994 ((eq xetla-switch-to-buffer-mode 'single-window)
995 (switch-to-buffer buffer))
996 ((eq xetla-switch-to-buffer-mode 'show-in-other-window)
997 (pop-to-buffer buffer)
998 (setq xetla-switched-buffer (current-buffer))
999 (pop-to-buffer xetla-switched-from-buffer))
1000 ;; TODO : dedicated frame.
1002 (error "Switch mode %s not implemented" xetla-switch-to-buffer-mode))))
1004 (defun xetla-post-switch-to-buffer ()
1005 "Executed when showing a changeset.
1007 If `xetla-switched-buffer' is non-nil, show this buffer, but keep
1008 cursor position in previous buffer."
1009 (when xetla-switched-buffer
1010 (pop-to-buffer xetla-switched-buffer)
1011 (setq xetla-switched-buffer nil)
1012 (goto-char (point-min))
1013 (pop-to-buffer xetla-switched-from-buffer)))
1016 (defun xetla-show-process-buffer ()
1017 "Show the process buffer of the last started xetla command."
1019 (xetla-switch-to-buffer xetla-last-process-buffer)
1020 (unless (member xetla-last-process-buffer
1022 (process-buffer (car p)))
1023 xetla-process-running))
1024 (xetla-process-buffer-mode)))
1026 (defun xetla-show-last-process-buffer (&optional type mode path)
1027 "Switch to the last used process buffer in a new buffer of TYPE.
1028 If MODE is specified, it is a function that will be run in the
1029 new buffer. Otherwise, the buffer will remain in fundamental mode, in
1032 If PATH is specified, it will be passed to `xetla-get-buffer-create'."
1033 (when (buffer-live-p xetla-last-process-buffer)
1034 (let ((content (with-current-buffer xetla-last-process-buffer
1036 (xetla-switch-to-buffer (xetla-get-buffer-create
1037 (or type 'generic) path))
1038 (let ((inhibit-read-only t))
1043 (xetla-process-buffer-mode))))
1045 (defun xetla-show-error-buffer (buffer &optional type mode)
1046 "Pops up the error buffer.
1047 Works like `xetla-show-last-process-buffer', but displays BUFFER, of type
1048 'errors if TYPE is not specified.
1049 If MODE is specified, the buffer will use that mode."
1050 (when (buffer-live-p buffer)
1051 (let ((content (with-current-buffer buffer
1053 (xetla-switch-to-buffer (xetla-get-buffer-create
1055 (let ((inhibit-read-only t))
1060 (xetla-process-buffer-mode))))
1062 ;; --------------------------------------
1063 ;; Arch name manipulators
1064 ;; ======================
1066 ;; Normally in XEtla, a name, a revision specifier is represented as a
1069 ;; ("archive" "category" "branch" "version" "revision")
1071 ;; Nil is permitted as the element. However the list length must be 5
1074 ;; (nil "category" "branch" nil nil)
1076 ;; In other hand, in tla command, the name must be represented as a
1079 ;; "archive/category-branch-version-revision"
1081 ;; So we have to convert a name in different representation in many
1084 ;; * xetla-name-split-* is for converting from a string representation
1085 ;; to a list representation. There are semi-qualified version and
1086 ;; fully-qualified version.
1088 ;; - semi-qualified: "category-branch-version-revision".
1089 ;; `xetla-name-split-semi-qualified' expects a name string without
1090 ;; archive component. The archive field of returned list is filled
1093 ;; - fully-qualified: "archive/category-branch-version-revision".
1094 ;; `xetla-name-split' expects a name string including archive.
1096 ;; * xetla-name-construct-* is for converting from a list
1097 ;; representation to a string representation. The functions accept
1098 ;; arguments two ways.
1100 ;; - normal passing: (xetla-name-construct "archive" "category"...)
1101 ;; - packed passing: (xetla-name-construct '("archive" "category"...))
1103 ;; There are semi-qualified version and fully-qualified version.
1104 ;; - semi-qualified: `xetla-name-construct-semi-qualified' connects
1105 ;; arguments with "-".
1106 ;; - fully-qualified: `xetla-name-construct" connects the first argument
1107 ;; and the rest with "/". About the rest,
1108 ;; `xetla-name-construct-semi-qualified' is applied.
1110 ;; * xetla-name-{archive|category|branch|version|revision} is for
1111 ;; extracting a component from a name. The both representations are
1114 ;; * xetla-name-mask is for replace a component in the name list with nil.
1116 ;; --------------------------------------
1119 ;; String representation -> List representation
1121 (defun xetla-name-split-semi-qualified (name &optional archive)
1122 "Split \"--\" connected string NAME into 5 elements list.
1123 The first element is always nil if ARCHIVE is not given.
1124 If ARCHIVE is given, use it as the first.
1125 Even if the elements in name are less than 5, the list is filled by nil
1126 to make the length 5.
1128 ELISP> (xetla-name-split-semi-qualified \"branch--category--version--revision\"
1130 (\"archive\" \"branch\" \"category\" \"version\" \"revision\")
1132 ELISP> (xetla-name-split-semi-qualified
1133 \"branch--category--version--revision\")
1134 (nil \"branch\" \"category\" \"version\" \"revision\")
1136 ELISP> (xetla-name-split-semi-qualified \"branch--category--version\")
1137 (nil \"branch\" \"category\" \"version\" nil)
1139 ELISP> (xetla-name-split-semi-qualified
1140 \"branch--category--version\" \"archive\")
1141 (\"archive\" \"branch\" \"category\" \"version\" nil)
1143 ELISP> (xetla-name-split-semi-qualified \"branch--category\" \"archive\")
1144 (\"archive\" \"branch\" \"category\" nil nil)
1146 ELISP> (xetla-name-split-semi-qualified \"branch--category\"nil)
1147 (nil \"branch\" \"category\" nil nil)
1149 ELISP> (xetla-name-split-semi-qualified \"branch--category--\" nil)
1150 (nil \"branch\" \"category\" \"\" nil)"
1151 (let ((list (xetla-name-split-semi-qualified-internal name)))
1152 (while (> 4 (length list))
1153 (setq list (cons nil list)))
1154 (let ((result (cons archive (nreverse list))))
1155 (when (xetla-is-version-string (caddr result))
1156 (setq result (list (car result)
1163 (defun xetla-is-version-string (string)
1164 "Non-nil if STRING is a candidate for a version name.
1165 That is, if it contains only digits and dots.
1166 The regexp here is less strict than the one of xetla, but must verify
1167 \(xetla-is-version-string string) => string can't be a branch name."
1168 (and string (string-match "^[0-9\.]+$" string)))
1170 (defun xetla-name-split-semi-qualified-internal (name)
1171 "Helper function for `xetla-name-split-semi-qualified'.
1172 Splits a semi-qualified NAME."
1173 (if (string-match "^\\(.+\\)--\\(\\([^-]\\|-[^-]\\)*\\)" name)
1174 (cons (match-string 2 name)
1175 (xetla-name-split-semi-qualified-internal
1176 (match-string 1 name)))
1179 (defun xetla-name-split (name)
1180 "Parse a fully qualified revision NAME, but possibly incomplete.
1181 email@address.com--arch/cat--branch--ver ->
1182 (\"email@address.com--arch\" \"cat\" \"branch\" \"ver\" nil)
1183 email@address.com--arch/cat ->
1184 (\"email@address.com--arch\" \"cat\" nil nil nil)
1185 email@address.com--arch ->
1186 (\"email@address.com-arch\" nil nil nil nil)"
1187 (if (string-match "\\(.*\\)/\\(.*\\)" name)
1188 (xetla-name-split-semi-qualified (match-string 2 name) (match-string 1 name))
1189 (if (string= name "")
1190 (list nil nil nil nil nil)
1191 (list name nil nil nil nil))))
1195 ;; List representation -> string
1197 (defun xetla-name-construct-semi-qualified (&rest comp)
1198 "Concatenate COMP with \"-\".
1199 This function can accept strings or a list which contains strings.
1201 ELISP> (xetla-name-construct-semi-qualified \"a\" \"b\" \"c\")
1203 ELISP> (xetla-name-construct-semi-qualified (list \"a\" \"b\" \"c\"))
1205 (if (consp (car comp)) (setq comp (car comp)))
1206 (if (string= (cadr comp) "")
1208 (concat (car comp) "--"
1209 (mapconcat 'identity (remove nil (cddr comp)) "--"))
1210 (mapconcat 'identity (remove nil comp) "--")))
1212 (defun xetla-name-construct (archive &optional
1217 "Create the revision name ARCHIVE/CATEGORY-BRANCH-VERSION-REVISION.
1218 The arguments may be nil. If ARCHIVE is a revision name list like
1219 (archive category branch version revision), the list element is mapped
1220 to arguments before creating the fully qualified revision name.
1222 If the branch name is the empty string and the version is defined,
1223 then, we have an unnamed branch. The full name is
1224 archive/category-version."
1225 (when (consp archive)
1226 (setq category (xetla-name-category archive)
1227 branch (xetla-name-branch archive)
1228 version (xetla-name-version archive)
1229 revision (xetla-name-revision archive)
1230 ;; archive must be last
1231 archive (xetla-name-archive archive)))
1232 (let ((semi (xetla-name-construct-semi-qualified
1233 category branch version revision)))
1235 (and archive (not (string= archive ""))
1236 (concat archive (when category "/")))
1240 ;; Get a component from a list or string.
1242 (defun xetla-name-archive (target)
1243 "Get archive component from TARGET.
1244 Both representation of TARGET, a string and a list is acceptable."
1245 (when (stringp target)
1246 (setq target (xetla-name-split target)))
1249 (defun xetla-name-category (target)
1250 "Get category component from TARGET.
1251 Both representation of TARGET, a string and a list is acceptable."
1252 (when (stringp target)
1253 (setq target (xetla-name-split target)))
1256 (defun xetla-name-branch (target)
1257 "Get branch component from a TARGET.
1258 Both representation of TARGET, a string and a list is acceptable."
1259 (when (stringp target)
1260 (setq target (xetla-name-split target)))
1263 (defun xetla-name-version (target)
1264 "Get version component from TARGET.
1265 Both representation of TARGET, a string and a list is acceptable."
1266 (when (stringp target)
1267 (setq target (xetla-name-split target)))
1270 (defun xetla-name-revision (target)
1271 "Get revision component from TARGET.
1272 Both representation of TARGET, a string and a list is acceptable."
1273 (when (stringp target)
1274 (setq target (xetla-name-split target)))
1275 (cadddr (cdr target)))
1279 ;; Mask a specified component in the name.
1281 (defun xetla-name-mask (original do-construct-p
1288 "Mask ORIGINAL, a xetla revision name by masks; and return the masked value.
1290 If DO-CONSTRUCT-P is given, the result is converted to a string by
1291 `xetla-name-construct'.
1293 ARCHIVE-MASK, CATEGORY-MASK, BRANCH-MASK, VERSION-MASK and REVISION-MASK should
1294 be either nil or t, and indicate whether that field should be masked.
1296 If a mask value is nil, the associated element in ORIGINAL is set to nil.
1297 Else If a mask value is a string, the associated element in ORIGINAL is set
1299 Else the associated element in ORIGINAL is not changed.
1302 ELISP> (xetla-name-mask '(\"a\" \"c\" \"b\" \"v\" \"r\") nil t t t t nil)
1303 (\"a\" \"c\" \"b\" \"v\" nil)
1305 ELISP> (xetla-name-mask '(\"a\" \"c\" \"b\" \"v\" \"r\") nil t t t nil nil)
1306 (\"a\" \"c\" \"b\" nil nil)
1308 ELISP> (xetla-name-mask '(\"a\" \"c\" \"b\" \"v\" \"r\") t t t t nil nil)
1310 ELISP> (xetla-name-mask '(\"a\" \"c\" \"b\" \"v\" \"r\") t nil nil nil nil t)
1312 ELISP> (xetla-name-mask '(\"a\" \"c\" \"b\" \"v\" \"r\") t nil nil nil t t)
1315 (when (consp original)
1318 (if (stringp archive-mask)
1320 (xetla-name-archive original)))
1322 (if (stringp category-mask)
1324 (xetla-name-category original)))
1326 (if (stringp branch-mask)
1328 (xetla-name-branch original)))
1330 (if (stringp version-mask)
1332 (xetla-name-version original)))
1334 (if (stringp revision-mask)
1336 (xetla-name-revision original))))))
1338 (xetla-name-construct masked)
1341 (defun xetla-name-match (target mask)
1342 "Compare the fully qualified revision list TARGET with a MASK.
1343 Each parameter is a list. The elements of the both lists are compared
1344 via a regexp match. When the mask part of a component is nil, this
1345 comparision is skipped.
1346 Here are some examples:
1348 '(\"xsteve@nit.at-public\" \"xetla\" \"main\" \"0.1\" \"patch-116\")
1349 '(nil \"xt.*\" \"main\" nil nil)) => t
1351 '(\"xsteve@nit.at-public\" \"xetla\" \"main\" \"0.1\" \"patch-116\")
1352 '(nil \"xt.*\" \"devel\" nil nil)) => nil" ;"
1359 (setq t-part (car tl))
1360 (setq m-part (car ml))
1362 (setq matching (string-match m-part t-part)))
1368 (if matching t nil)))
1371 (defun xetla-name-match-from-list (target match-list)
1372 "Match TARGET against a list of possible matches.
1373 Every entry of MATCH-LIST is a list that contains a
1374 match element and a possible result.
1375 The target is matched against the elements in the match-list.
1376 If a match is found return the corresponding result,
1377 otherwise return nil."
1378 (let ((ml match-list)
1382 (while (and (not result) ml)
1383 (setq match (caar ml))
1384 (setq data (cadar ml))
1385 (message "match: %s, data: %s" match data)
1386 (setq result (when (xetla-name-match target match) data))
1390 ;; TODO: Use xetla-archive-tree.
1391 (defun xetla-version-head (archive category branch version)
1392 "Return the newest revision for ARCHIVE/CATEGORY-BRANCH-VERSION."
1393 (xetla-run-tla-sync (list "revisions"
1394 (xetla-name-construct
1399 :finished (lambda (output error status arguments)
1400 (with-current-buffer output
1401 (goto-char (point-max))
1402 (re-search-backward "^.")
1403 (buffer-substring-no-properties
1404 (point) (point-at-eol))))))
1406 ;; --------------------------------------
1407 ;; Archive tree manipulators
1408 ;; --------------------------------------
1410 (defvar xetla-archive-tree nil
1411 "Arch archive/category/branch/version/revision are stored in assoc list:
1413 ((\"xsteve@nit.at-public\" \"http://arch.xsteve.at/2004\")
1415 (\"mbp@sourcefrog.net-2004\"
1416 \"http://sourcefrog.net/arch/mbp@sourcefrog.net-2004\"
1425 (\"mark@dishevelled.net-2003-mst\"
1426 \"http://members.iinet.net.au/~mtriggs/arch/\")
1427 (\"lord@emf.net-2004\"
1428 \"http://regexps.srparish.net/{archives}/lord@emf.net-2004\")
1430 (\"Matthieu.Moy@imag.fr-public\"
1431 \"http://www-verimag.imag.fr/webdav/moy/public\"
1436 \"Merged from Robert (patch8-9), Milan (patch21-22), Stefan (patch5-8)\"
1437 \"Matthieu Moy <Matthieu.Moy@imag.fr>\"
1438 \"2004-06-03 20:13:11 GMT\")
1440 \"Fix default-directory in xetla-run-tla-sync, fix in xetla-changes-ediff\"
1441 \"Matthieu Moy <Matthieu.Moy@imag.fr>\"
1442 \"2004-06-03 15:26:15 GMT\")
1446 \"Matthieu Moy <Matthieu.Moy@imag.fr>\"
1447 \"2004-04-07 22:57:00 GMT\")
1449 \"tag of xsteve@nit.at-public/xetla-main-0.1-patch-5\"
1450 \"Matthieu Moy <Matthieu.Moy@imag.fr>\" \"2004-04-07 22:52:39 GMT\")))))
1454 This list is initially empty, and is built/rebuilt on demand.")
1457 (defun xetla-archive-tree-setcdr (parent value &optional rest)
1458 "In PARENT, update VALUE.
1459 REST are the items that are already present."
1460 (let* ((current (cdr parent))
1461 (list-details (assoc value current)))
1462 (if (or (null current) (null list-details))
1463 ;; rest is '("summary" "creator" "date") when value is "patch-N"
1464 (setcdr parent (cons (cons value rest) current))
1465 (if (and list-details rest)
1466 ;; Field already there. update details.
1467 (setcdr list-details rest)))))
1469 (defun xetla-archive-tree-setcddr (parent value)
1470 "In PARENT, update VALUE."
1471 (let ((current (cddr parent)))
1472 (if (or (null current) (null (assoc value current)))
1473 (setcdr (cdr parent) (cons (cons value nil) current)))))
1476 (defun xetla-archive-tree-add-archive (archive location &optional old)
1477 "Add ARCHIVE at LOCATION to the archive tree.
1478 If OLD is provided, it is an old archive tree from which some
1479 information can be found (this is useful to keep the category/branch/version
1480 info for existing archives)."
1481 (if (xetla-archive-tree-get-archive archive)
1482 (let* ((a (xetla-archive-tree-get-archive archive))
1484 (oldlocation (car val))
1485 (category (cdr val)))
1486 (setcdr a (cons (or location oldlocation) category)))
1487 (let ((oldinfo (xetla-archive-tree-get-archive archive old))
1488 (newinfo (list archive location)))
1490 (setcdr (cdr newinfo) (cddr oldinfo))) ;; list of versions.
1491 (setq xetla-archive-tree (cons newinfo
1492 xetla-archive-tree)))))
1494 (defun xetla-archive-tree-get-archive (archive &optional archive-tree)
1495 "Get the value of ARCHIVE from ARCHIVE-TREE.
1496 If ARCHIVE-TREE is not given, `xetla-archive-tree' is used."
1497 (assoc archive (or archive-tree xetla-archive-tree)))
1500 (defun xetla-archive-tree-add-category (archive category)
1501 "Add a new category to ARCHIVE named CATEGORY."
1502 (xetla-archive-tree-add-archive archive nil)
1503 (xetla-archive-tree-setcddr
1504 (xetla-archive-tree-get-archive archive)
1507 (defun xetla-archive-tree-get-category (archive category)
1508 "From ARCHIVE, get CATEGORY."
1509 (assoc category (cdr (cdr (xetla-archive-tree-get-archive archive)))))
1512 (defun xetla-archive-tree-add-branch (archive category branch)
1513 "Add a new branch to ARCHIVE's CATEGORY named BRANCH."
1514 (xetla-archive-tree-add-category archive category)
1515 (xetla-archive-tree-setcdr
1516 (xetla-archive-tree-get-category archive category)
1519 (defun xetla-archive-tree-get-branch (archive category branch)
1520 "Get a branch from ARCHIVE's CATEGORY named BRANCH."
1521 (assoc branch (cdr (xetla-archive-tree-get-category
1522 archive category))))
1525 (defun xetla-archive-tree-add-version (archive category branch version)
1526 "Add a new version to ARCHIVE CATEGORY BRANCH named VERSION."
1527 (xetla-archive-tree-add-branch archive category branch)
1528 (xetla-archive-tree-setcdr
1529 (xetla-archive-tree-get-branch archive category branch )
1532 (defun xetla-archive-tree-get-version (archive category branch version)
1533 "Get a version from ARCHIVE CATEGORY BRANCH named VERSION."
1534 (assoc version (cdr (xetla-archive-tree-get-branch
1535 archive category branch))))
1538 (defun xetla-archive-tree-add-revision (archive category branch version revision
1539 &optional summary creator date)
1540 "Add a new revision to ARCHIVE CATEGORY BRANCH VERSION named REVISION."
1541 (xetla-archive-tree-add-version archive category branch version)
1542 (xetla-archive-tree-setcdr
1543 (xetla-archive-tree-get-version archive category branch version)
1544 revision (list summary creator date)))
1546 (defun xetla-archive-tree-get-revision (archive category branch version revision)
1547 "Get a revision from ARCHIVE CATEGORY BRANCH VERSION named REVISION."
1548 (assoc revision (cdr (xetla-archive-tree-get-version
1549 archive category branch version))))
1551 ;; Archive tree builders
1552 (defun xetla-archive-tree-build (basename &optional use-cache ignore-error)
1553 "Generic version of xetla-archive-tree-build-*.
1554 BASENAME is used as a base for this tree.
1555 If USE-CACHE is non-nil, load details from the cache where possible.
1556 If IGNORE-ERROR is non-nil, don't throw errors."
1557 (when (stringp basename)
1558 (setq basename (xetla-name-split basename)))
1559 (let ((archive (xetla-name-archive basename))
1560 (category (xetla-name-category basename))
1561 (branch (xetla-name-branch basename))
1562 (version (xetla-name-version basename)))
1565 (xetla-archive-tree-build-revisions archive
1572 (xetla-archive-tree-build-versions archive
1578 (xetla-archive-tree-build-branches archive
1583 (xetla-archive-tree-build-categories archive
1587 (xetla-archive-tree-build-archives use-cache
1590 (defun xetla-archive-tree-build-archives (&optional use-cache ignore-error)
1591 "Builds the list of archives.
1592 If USE-CACHE is non-nil, load details from the cache where possible.
1593 If IGNORE-ERROR is non-nil, don't throw errors."
1594 (when (or (not use-cache)
1595 (not xetla-archive-tree))
1596 (xetla-run-tla-sync '("archives")
1597 :finished 'xetla-null-handler
1601 'xetla-default-error-function))
1602 (let ((old-archive-tree xetla-archive-tree))
1603 (setq xetla-archive-tree nil)
1605 (let ((archive-name)
1607 (set-buffer xetla-last-process-buffer)
1608 (goto-char (point-min))
1609 (while (> (point-at-eol) (point-at-bol))
1610 (setq archive-name (buffer-substring-no-properties
1613 (beginning-of-line-text 2)
1614 (setq archive-location (buffer-substring-no-properties
1615 (point) (point-at-eol)))
1617 (xetla-archive-tree-add-archive archive-name
1619 old-archive-tree)))))))
1621 (defun xetla-archive-tree-build-categories (archive &optional
1624 "Build the list of categories for ARCHIVE in `xetla-archive-tree'.
1625 If USE-CACHE is non-nil, load details from the cache where possible.
1626 If IGNORE-ERROR is non-nil, don't throw errors."
1627 (when (or (not use-cache)
1628 (not (cddr (xetla-archive-tree-get-archive archive))))
1629 (let ((basename archive))
1630 (message "building categories for `%s'..." basename)
1631 (xetla-run-tla-sync (list "categories" "-A" basename)
1632 :finished 'xetla-null-handler
1636 'xetla-default-error-function))
1637 (message "building categories for `%s'...done" basename)
1640 (with-current-buffer xetla-last-process-buffer
1642 (goto-char (point-min))
1643 (while (> (point-at-eol) (point-at-bol))
1644 (setq category (buffer-substring-no-properties
1648 (xetla-archive-tree-add-category archive category)
1651 (defun xetla-archive-tree-build-branches (archive category
1655 "Build the list of branches for ARCHIVE/CATEGORY in `xetla-archive-tree'.
1656 If USE-CACHE is non-nil, load details from the cache where possible.
1657 If IGNORE-ERROR is non-nil, don't throw errors."
1658 (when (or (not use-cache)
1659 (not (cdr (xetla-archive-tree-get-category archive category))))
1660 (let ((basename (xetla-name-construct archive category)))
1661 (message "building branches for `%s'..." basename)
1662 (xetla-run-tla-sync (list "branches" basename)
1663 :finished 'xetla-null-handler
1667 'xetla-default-error-function))
1668 (message "building branches for `%s'...done" basename)
1671 (with-current-buffer xetla-last-process-buffer
1673 (goto-char (point-min))
1674 (while (> (point-at-eol) (point-at-bol))
1675 (setq branch (buffer-substring-no-properties
1678 (xetla-archive-tree-add-branch
1681 (if (looking-at ".*-")
1682 (xetla-name-branch (xetla-name-split-semi-qualified
1686 (forward-line 1))))))
1688 (defun xetla-archive-tree-build-versions (archive category branch
1692 "Build the version list in ARCHIVE/CATEGORY-BRANCH in `xetla-archive-tree'.
1693 If USE-CACHE is non-nil, load details from the cache where possible.
1694 If IGNORE-ERROR is non-nil, don't throw errors."
1695 (when (or (not use-cache)
1696 (not (cdr (xetla-archive-tree-get-branch archive category
1698 (let ((basename (xetla-name-construct archive category branch)))
1699 (message "building versions for `%s'..." basename)
1700 (xetla-run-tla-sync (list "versions" basename)
1701 :finished 'xetla-null-handler
1705 'xetla-default-error-function))
1706 (message "building versions for `%s'...done" basename)
1709 (with-current-buffer xetla-last-process-buffer
1711 (goto-char (point-min))
1712 (while (> (point-at-eol) (point-at-bol))
1713 (setq version (buffer-substring-no-properties
1717 (xetla-archive-tree-add-version
1721 (xetla-name-version (xetla-name-split-semi-qualified version))))))))
1723 (defun xetla-archive-tree-build-revisions (archive category branch version
1728 "Build the revision list in ARCHIVE/CATEGORY-BRANCH-VERSION.
1729 Updates `xetla-archive-tree'.
1730 If USE-CACHE is non-nil, load details from the cache where possible.
1731 If IGNORE-ERROR is non-nil, don't throw errors."
1732 (when (or (not use-cache)
1733 (not (cdr (xetla-archive-tree-get-version archive category branch
1735 (let ((details (or xetla-revisions-shows-summary
1736 xetla-revisions-shows-date
1737 xetla-revisions-shows-creator))
1738 (basename (xetla-name-construct
1739 archive category branch version)))
1740 (message "building revisions for `%s'..." basename)
1743 (xetla-run-tla-sync (list "revisions"
1744 "--summary" "--date" "--creator"
1746 :finished 'xetla-null-handler
1747 :error (if ignore-error
1749 'xetla-default-error-function)))
1751 (xetla-run-tla-sync (list "revisions" basename)
1752 :finished 'xetla-null-handler
1753 :error (if ignore-error
1755 'xetla-default-error-function))))
1756 (message "building revisions for `%s'...done" basename)
1759 (with-current-buffer xetla-last-process-buffer
1760 (let (revision date creator summary)
1761 (goto-char (point-min))
1762 (while (> (point-at-eol) (point-at-bol))
1763 (setq revision (buffer-substring-no-properties
1768 (skip-chars-forward " ")
1769 (setq date (buffer-substring-no-properties (point)
1772 (skip-chars-forward " ")
1773 (setq creator (buffer-substring-no-properties (point)
1776 (skip-chars-forward " ")
1777 (setq summary (buffer-substring-no-properties
1779 (progn (re-search-forward "^\\([^ \t]\\|$\\)")
1784 (xetla-archive-tree-add-revision
1795 (defun xetla-revisions-tree-contains-details
1796 (archive category branch version)
1797 "Whether VERSION has already been listed full details.
1798 Details include summary lines, dates, and creator in the archive tree."
1799 (let ((vtree (xetla-archive-tree-get-version archive category branch
1801 (and (cdr vtree) ;; revision list is here
1802 (cadr (cadr vtree))))) ;; summary line also
1804 ;; --------------------------------------
1805 ;; Revlib tree manipulators
1806 ;; --------------------------------------
1807 (defvar xetla-revlib-tree nil)
1808 (defun xetla-revlib-tree-get-archive (archive &optional archive-tree)
1809 "Get ARCHIVE from ARCHIVE-TREE.
1810 If ARCHIVE-TREE is not given, `xetla-revlib-tree' is used instead."
1811 (assoc archive (or archive-tree xetla-revlib-tree)))
1813 (defun xetla-revlib-tree-build-archives (&optional use-cache ignore-error)
1814 "Build the list of archives in `xetla-revlib-tree'.
1815 If USE-CACHE is non-nil, load from the cache where possible.
1816 If IGNORE-ERROR is non-nil, error is not reported.
1817 Return non-nil if the tree entry for archives are updated."
1818 (when (or (not use-cache)
1819 (not xetla-revlib-tree))
1820 (xetla-run-tla-sync '("library-archives")
1821 :finished 'xetla-null-handler
1825 'xetla-default-error-function))
1826 (let ((old-revlib-tree xetla-revlib-tree) )
1827 (setq xetla-revlib-tree nil)
1829 (let ((archive-name)
1830 (tmp xetla-archive-tree)
1831 (xetla-archive-tree xetla-revlib-tree)
1833 (set-buffer xetla-last-process-buffer)
1834 (goto-char (point-min))
1835 (while (> (point-at-eol) (point-at-bol))
1837 (setq archive-name (buffer-substring-no-properties
1841 (xetla-archive-tree-add-archive archive-name
1844 (setq xetla-revlib-tree xetla-archive-tree
1845 xetla-archive-tree tmp)
1848 (defun xetla-revlib-tree-get-category (archive category)
1849 "Get a category from ARCHIVE named CATEGORY."
1850 (assoc category (cdr (cdr (xetla-revlib-tree-get-archive archive)))))
1852 (defun xetla-revlib-tree-build-categories (archive &optional
1855 "Builds the list of categories for an ARCHIVE in `xetla-revlib-tree'.
1856 If USE-CACHE is non-nil, load from the cache where possible.
1857 If IGNORE-ERROR is non-nil, error is not reported.
1858 Return non-nil if the tree entry for categories are updated."
1859 (when (or (not use-cache)
1860 (not (cddr (xetla-revlib-tree-get-archive archive))))
1861 (xetla-run-tla-sync (list "library-categories" "-A" archive)
1862 :finished 'xetla-null-handler
1866 'xetla-default-error-function))
1867 (with-current-buffer xetla-last-process-buffer
1869 (tmp xetla-archive-tree)
1870 (xetla-archive-tree xetla-revlib-tree)
1872 (goto-char (point-min))
1873 (while (> (point-at-eol) (point-at-bol))
1875 (setq category (buffer-substring-no-properties
1879 (xetla-archive-tree-add-category archive category))
1880 (setq xetla-revlib-tree xetla-archive-tree
1881 xetla-archive-tree tmp)
1884 (defun xetla-revlib-tree-get-branch (archive category branch)
1885 "From ARCHIVE/CATEGORY, get BRANCH."
1886 (assoc branch (cdr (xetla-revlib-tree-get-category
1887 archive category))))
1889 (defun xetla-revlib-tree-build-branches (archive category
1893 "Build the list of branches for ARCHIVE/CATEGORY in `xetla-revlib-tree'.
1894 If USE-CACHE is non-nil, load from the cache where possible.
1895 If IGNORE-ERROR is non-nil, error is not reported.
1896 Return non-nil if the tree entry for branches are updated."
1897 (when (or (not use-cache)
1898 (not (cdr (xetla-revlib-tree-get-category archive category))))
1899 (xetla-run-tla-sync (list "library-branches" "-A" archive category)
1900 :finished 'xetla-null-handler
1904 'xetla-default-error-function))
1905 (with-current-buffer xetla-last-process-buffer
1907 (tmp xetla-archive-tree)
1908 (xetla-archive-tree xetla-revlib-tree)
1910 (goto-char (point-min))
1911 (while (> (point-at-eol) (point-at-bol))
1913 (setq branch (buffer-substring-no-properties
1917 (xetla-archive-tree-add-branch
1920 (xetla-name-branch (xetla-name-split-semi-qualified branch))))
1921 (setq xetla-revlib-tree xetla-archive-tree
1922 xetla-archive-tree tmp)
1925 (defun xetla-revlib-tree-get-version (archive category branch version)
1926 "Get ARCHIVE/CATEGORY-BRANCH-VERSION from the revlib tree."
1927 (assoc version (cdr (xetla-revlib-tree-get-branch
1928 archive category branch))))
1930 (defun xetla-revlib-tree-build-versions (archive category branch
1934 "Build the versions list in ARCHIVE/CATEGORY/BRANCH in `xetla-archive-tree'.
1935 If USE-CACHE is non-nil, load from the cache where possible.
1936 If IGNORE-ERROR is non-nil, error is not reported.
1937 Return non-nil if the tree entry for versions are updated."
1938 (when (or (not use-cache)
1939 (not (cdr (xetla-revlib-tree-get-branch archive category
1941 (xetla-run-tla-sync (list "library-versions"
1942 (xetla-name-construct
1943 archive category branch))
1944 :finished 'xetla-null-handler
1948 'xetla-default-error-function))
1949 (with-current-buffer xetla-last-process-buffer
1951 (tmp xetla-archive-tree)
1952 (xetla-archive-tree xetla-revlib-tree)
1954 (goto-char (point-min))
1955 (while (> (point-at-eol) (point-at-bol))
1957 (setq version (buffer-substring-no-properties
1961 (xetla-archive-tree-add-version
1965 (xetla-name-version (xetla-name-split-semi-qualified version))))
1966 (setq xetla-revlib-tree xetla-archive-tree
1967 xetla-archive-tree tmp)
1970 (defun xetla-revlib-tree-get-revision (archive category branch version revision)
1971 "Get ARCHIVE/CATEGORY-BRANCH-VERSION-REVISION from the revlib tree."
1972 (assoc revision (cdr (xetla-revlib-tree-get-version
1973 archive category branch version))))
1975 (defun xetla-revlib-tree-build-revisions (archive category branch version
1980 "Build the revision list of ARCHIVE/CATEGORY-BRANCH-VERSION.
1981 Updates `xetla-revlib-tree'.
1982 If IGNORE-ERROR is non-nil, error is not reported.
1983 Return non-nil if the tree entry for revisions are updated."
1984 (when (or (not use-cache)
1985 (not (cdr (xetla-revlib-tree-get-version archive category branch
1987 (xetla-run-tla-sync (list "library-revisions"
1988 "--summary" "--date" "--creator"
1989 (xetla-name-construct
1990 archive category branch version))
1991 :finished 'xetla-null-handler
1992 :error (if ignore-error
1994 'xetla-default-error-function))
1995 (with-current-buffer xetla-last-process-buffer
2000 (tmp xetla-archive-tree)
2001 (xetla-archive-tree xetla-revlib-tree)
2004 (goto-char (point-min))
2005 (while (> (point-at-eol) (point-at-bol))
2007 (setq revision (buffer-substring-no-properties
2011 (skip-chars-forward " ")
2012 (setq date (buffer-substring-no-properties (point)
2015 (skip-chars-forward " ")
2016 (setq creator (buffer-substring-no-properties (point)
2019 (skip-chars-forward " ")
2020 (setq summary (buffer-substring-no-properties
2022 (progn (re-search-forward "^\\([^ \t]\\|$\\)")
2027 (xetla-archive-tree-add-revision
2036 (setq xetla-revlib-tree xetla-archive-tree
2037 xetla-archive-tree tmp)
2041 ;; --------------------------------------
2042 ;; Name reading engine
2043 ;; --------------------------------------
2044 ;;Currently only able to read a full revision starting from nothing.
2045 (defun xetla-name-read-refresh-cache ()
2046 "Function to be called from the minibuffer while reading a name."
2048 (xetla-archive-tree-build
2049 (xetla-name-construct
2050 (butlast (delete nil (xetla-name-split (buffer-substring))))))
2051 (setq xetla-archive-tree nil))
2053 (defvar xetla-name-read-arguments "This value should not be refereed."
2054 "Used to suppress warnings from the byte code compiler.
2055 This variable is a just placeholder introduced to suppress the
2056 warnings from byte code compiler. Variable `xetla-name-read-arguments'
2057 should be bound in `let'. Variable `xetla-name-read-arguments' is used
2058 for passing information from `xetla-name-read' to functions called internally
2059 from `xetla-name-read'. Use function `xetla-name-read-arguments' to get the
2062 (defun xetla-name-read-arguments (key)
2063 "Get `xetla-name-read' context information associated to KEY.
2064 `xetla-name-read' calls some functions to read a xetla name.
2065 In the functions, the arguments passed to `xetla-name-read'(context information)
2066 are needed to know. However, `xetla-name-read' cannot pass the context
2067 information directly to the functions because the functions are something to do
2068 with Emacs's completion mechanism; and the mechanism specifies the number
2069 of arguments of the functions. So the context information is passed via
2070 a local variable, `xetla-name-read-arguments', defined in let.
2072 Symbol `archive', `category', `branch', `version', or `revision' are
2074 (cdr (assoc key xetla-name-read-arguments)))
2077 (defun xetla-name-read-complete (string predicate what)
2078 "Completion function for name reading.
2080 Displays STRING and prompts for something satisfying PREDICATE.
2082 This function uses the free variables archive, category, branch,
2083 version, and revision. If one of these variables is non-nil, it means
2084 the corresponding value must be read from keyboard.
2086 REMINDER: this function may be called several times, with different
2089 - nil : The function must return the longest prefix
2090 - t : The function must return the list of completions
2091 - 'lambda : The function must return t if the completion correspond
2092 to an exact match, nil otherwise. (so that Emacs can distinguish
2093 between \"sole completion\" and \"complete, but not unique\"."
2094 (if (and (eq what 'lambda)
2095 (string-match "/\\(.*--\\)?$" string))
2096 ;; The caller just want to know whether this is a full
2097 ;; completion. This can not be the case with such suffix.
2099 (let* ((empty-branch nil)
2100 (use-cache (not current-prefix-arg))
2101 (splited (xetla-name-split string))
2102 (archive-loc (xetla-name-archive splited))
2103 (category-loc (xetla-name-category splited))
2104 (branch-loc (xetla-name-branch splited))
2105 (version-loc (xetla-name-version splited))
2106 (revision-loc (xetla-name-revision splited))
2108 ((and (xetla-name-read-arguments 'category)
2109 (not category-loc) "/"))
2110 ((and (xetla-name-read-arguments 'branch)
2111 (not branch-loc) "--"))
2112 ((and (xetla-name-read-arguments 'version)
2113 (not version-loc) "--"))
2114 ((and (xetla-name-read-arguments 'revision)
2115 (not revision-loc) "--"))
2118 ((eq 'maybe (xetla-name-read-arguments 'category))
2120 ((and (eq 'maybe (xetla-name-read-arguments 'branch))
2121 archive-loc category-loc)
2123 ((and (eq 'maybe (xetla-name-read-arguments 'version))
2124 archive-loc category-loc branch-loc)
2126 ((and (eq 'maybe (xetla-name-read-arguments 'revision))
2127 archive-loc category-loc branch-loc version-loc)
2132 ;; If the user started to write a revision ...
2134 ;; ... and if the user is supposed to be prompted a
2136 (when (xetla-name-read-arguments 'revision)
2137 (let ((xetla-revisions-shows-summary nil)
2138 (xetla-revisions-shows-date nil)
2139 (xetla-revisions-shows-creator nil))
2140 (xetla-archive-tree-build-revisions
2141 archive-loc category-loc branch-loc version-loc use-cache t))
2142 (cdr (xetla-archive-tree-get-version
2143 archive-loc category-loc branch-loc version-loc))))
2145 (when (xetla-name-read-arguments 'version)
2146 (xetla-archive-tree-build-versions
2147 archive-loc category-loc branch-loc use-cache t)
2148 (cdr (xetla-archive-tree-get-branch
2149 archive-loc category-loc branch-loc))))
2150 ;; If the user started a branch ...
2152 ;; And a branch is needed
2153 (when (xetla-name-read-arguments 'branch)
2154 (xetla-archive-tree-build-branches
2155 archive-loc category-loc use-cache t)
2156 (let ((result (cdr (xetla-archive-tree-get-category
2157 archive-loc category-loc))))
2158 (when (and (string= branch-loc "")
2159 (xetla-name-read-arguments 'version)
2160 (let ((empty-br-exists nil))
2162 (cdr (xetla-archive-tree-get-category
2163 archive-loc category-loc)))
2164 (when (string= (car branch) "")
2165 (setq empty-br-exists t)))
2167 (xetla-archive-tree-build-versions
2168 archive-loc category-loc "")
2169 (setq empty-branch (xetla-archive-tree-get-branch
2170 archive-loc category-loc ""))
2172 ;; Remove the "" branch to avoid the --
2177 (when (not (string= (caar tmp) ""))
2178 (setq result (cons (car tmp) result)))
2179 (setq tmp (cdr tmp))))))
2182 (when (xetla-name-read-arguments 'category)
2183 (xetla-archive-tree-build-categories archive-loc use-cache t)
2184 (cddr (xetla-archive-tree-get-archive archive-loc))))
2186 (when (xetla-name-read-arguments 'archive)
2187 (xetla-archive-tree-build-archives use-cache t)
2188 xetla-archive-tree)))))
2189 (let* ((base (mapcar (lambda (x)
2190 (xetla-name-construct
2194 (when category-loc archive-loc)
2195 (when branch-loc category-loc)
2196 (when version-loc branch-loc)
2197 (when revision-loc version-loc)
2201 (and maybep suffix))
2202 (empty-branch-versions
2205 (xetla-name-construct
2206 archive-loc category-loc "" (car x)))
2207 (cdr empty-branch))))
2208 (completions (funcall 'all-completions
2212 (list (concat x suffix)))
2216 (lambda (x) (list x))
2220 (lambda (x) (list x))
2221 empty-branch-versions)))
2225 ;; We just want the list of completions
2227 ((eq (length completions) 1)
2228 ;; There's only one completion
2229 (if (eq what 'lambda)
2230 (string= (car completions) string)
2231 (cond ((string= (car completions) string) t)
2232 (t (car completions)))))
2233 ;; there are several possible completions
2234 (t (if (eq what 'lambda)
2235 ;; complete, but not unique ?
2236 (member string completions)
2237 (try-completion string (mapcar 'list
2239 ;; (xetla-trace "string=%s predicate=%S what=%s ==> result=%S\ncompletions=%S"
2240 ;; string predicate what result completions)
2244 ;; (xetla-name-read "enter category: " "Matthieu.Moy@imag.fr--public" 'prompt)
2245 ;; (xetla-name-read "branch: " "lord@emf.net--2004" 'prompt 'prompt)
2246 ;; (xetla-name-read "revision: " 'prompt 'prompt 'prompt 'prompt 'prompt)
2247 ;; (xetla-name-read "revision or version: " 'prompt 'prompt 'prompt 'prompt 'maybe)
2248 ;; (xetla-name-read "revision or version: " "jet@gyve.org--xetla" "xetla" "jet" 'prompt 'maybe)
2250 (defvar xetla-name-read-history nil) ; TODO: multiple history list?
2251 (defvar xetla-name-read-debug nil
2252 "If non-nil, `condition-case' in `xetla-name-read' is made disabled.")
2253 (defun xetla-name-read (&optional prompt archive category
2254 branch version revision)
2256 To get help on the user interface of `xetla-name-read', please type
2257 M-x xetla-name-read-help RET.
2259 Function reading an archive location from keyboard.
2260 Read name is expressed in a list built by `xetla-name-split'.
2262 First argument PROMPT is the prompt the user will get. Next arguments
2263 ARCHIVE CATEGORY BRANCH VERSION and REVISION are either the default
2264 value, or a request for a value. They can take four values:
2266 - A string means the default value, and will be used as an initial
2269 - The symbol 'prompt means the value will be prompted from the user.
2270 The user will HAVE to give this value.
2272 - The symbol 'maybe means the value will be prompted, but is optional
2275 - nil means the value won't be prompted.
2277 They should appear in the same order as above.
2280 - Read a category in archive \"Matthieu.Moy@imag.fr--public\":
2281 (xetla-name-read \"enter category: \" \"Matthieu.Moy@imag.fr--public\" 'prompt)
2282 - Read a revision, anywhere:
2283 (xetla-name-read \"revision: \" 'prompt 'prompt 'prompt 'prompt 'prompt)
2284 - Read either a revision or a version:
2285 (xetla-name-read \"revision: \" 'prompt 'prompt 'prompt 'prompt 'maybe)
2287 While prompting, a menu \"Xetla\" is added to the menubar. The
2288 following commands are available:
2290 \\{xetla-name-read-minibuf-map}"
2291 (let ((xetla-name-read-arguments `((archive . ,archive)
2292 (category . ,category)
2294 (version . ,version)
2295 (revision . ,revision))))
2296 (if xetla-name-read-debug
2297 (xetla-name-read-internal prompt archive category branch version revision)
2298 (condition-case reason
2299 (xetla-name-read-internal prompt archive category branch version revision)
2301 (run-hooks 'xetla-name-read-error-hook)
2302 (signal (car reason) (cdr reason)))))))
2304 (defun xetla-name-read-internal (prompt archive category branch version revision)
2305 "See `xetla-name-read'."
2306 (run-hooks 'xetla-name-read-init-hook)
2307 (let* ((minibuffer-local-completion-map xetla-name-read-minibuf-map)
2308 (result (xetla-name-construct
2311 (delete 'prompt (list archive category
2314 not-finished too-long last-empty)
2315 ;; Without in some case 'maybe is ignored by xetla-prompt-not-finished
2316 ;; and never the control flow enters the while loop.
2317 ;; We need C language's do-while loop.
2318 (while (or first-try
2323 (unless (eq this-command 'choose-completion)
2325 (message (cond (not-finished "%s%s [incomplete input: %s]")
2326 (too-long "%s%s [too long input for: %s]")
2327 (last-empty (concat "%s%s [empty " last-empty
2330 (concat "case not managed."
2331 " Please submit a bug report"))))
2333 (xetla-name-read-required-input archive
2341 (setq result (completing-read
2342 (or prompt "Location: ")
2343 'xetla-name-read-complete
2345 'xetla-name-read-history)
2347 (setq not-finished (xetla-prompt-not-finished
2348 result archive category branch
2350 (setq too-long (xetla-prompt-too-long
2351 result archive category branch
2353 (setq last-empty (xetla-prompt-last-empty result)))
2356 (setq result (xetla-name-split result)))
2357 (run-hook-with-args 'xetla-name-read-final-hook result)
2360 (defun xetla-prompt-not-finished (result archive category branch
2362 "Check whether user input is complete.
2363 True if RESULT (a string) is not sufficient when the user is
2364 prompted for ARCHIVE CATEGORY BRANCH VERSION REVISION."
2365 (let ((res-split (xetla-name-split result)))
2366 (or (and (eq archive 'prompt) ;; archive required
2367 (not (xetla-name-archive res-split))) ;; but not provided
2368 (and (eq category 'prompt)
2369 (not (xetla-name-category res-split)))
2370 (and (eq branch 'prompt)
2371 (not (xetla-name-branch res-split)))
2372 (and (eq version 'prompt)
2373 (not (xetla-name-version res-split)))
2374 (and (eq revision 'prompt)
2375 (not (xetla-name-revision res-split))))))
2377 (defun xetla-prompt-too-long (result archive category branch
2379 "Check whether the user has entered too many elements.
2380 True if RESULT (a string) contains too many elements when the user
2381 is prompted for ARCHIVE CATEGORY BRANCH VERSION REVISION.
2383 For example, will return true if the user entered
2384 foo@bar-2004/xetla-main while prompted only for a category."
2385 (let ((res-split (xetla-name-split result)))
2386 (or (and (not revision) ;; revision not needed
2387 (xetla-name-revision res-split)) ;; but provided
2389 (xetla-name-version res-split))
2391 (xetla-name-branch res-split))
2393 (xetla-name-category res-split))
2395 (xetla-name-archive res-split)))))
2397 (defun xetla-prompt-last-empty (result)
2398 "Check whether the last field is empty.
2399 Non-nil if RESULT (a string) is terminated by \"--\" or \"/\". This
2400 means the user entered a delimiter but not the element after.
2402 When non-nil, the returned value is a string giving the name of the
2403 item that is currently empty. (eg: archive, category, ...)"
2404 (let ((res-split (xetla-name-split result)))
2405 (cond ((equal (xetla-name-archive res-split) "") "archive" )
2406 ((equal (xetla-name-category res-split) "") "category")
2407 ((and (equal (xetla-name-branch res-split) "")
2408 (not (xetla-name-version res-split))) "branch" )
2409 ((equal (xetla-name-version res-split) "") "version" )
2410 ((equal (xetla-name-revision res-split) "") "revision")
2414 (defun xetla-name-read-required-input (archive
2419 "Return string which represents the elements to be readin `xetla-name-read'.
2420 If ARCHIVE, CATEGORY, BRANCH, VERSION or REVISION are equal to 'maybe, the
2421 corresponding element will be optionally read.
2422 If any of these are non-nil (but not 'maybe), the corresponding element will be
2424 If any of these are nil, the correpsonding element is not required."
2426 (cond ((eq archive 'maybe) "[A]")
2429 (cond ((eq category 'maybe) "[/C]")
2432 (cond ((eq branch 'maybe) "[--B]")
2435 (cond ((eq version 'maybe) "[--V]")
2438 (cond ((eq revision 'maybe) "[--R]")
2444 (defun xetla-location-type (location)
2445 "Return the type of LOCATION."
2447 ((string-match "^ftp://" location) 'ftp)
2448 ((string-match "^sftp://" location) 'sftp)
2449 ((string-match "^http://" location) 'http)
2452 (defun xetla-archive-type (archive)
2453 "Return the type of ARCHIVE."
2455 ((string-match "SOURCE$" archive) 'source)
2456 ;; archive-MIRROR, archive-MIRROR-2 should be treated as mirror
2457 ((string-match ".+-MIRROR" archive) 'mirror)
2460 ;; (xetla-archive-name-source "a")
2461 ;; (xetla-archive-name-source "a-SOURCE")
2462 ;; (xetla-archive-name-source "a-MIRROR")
2463 (defun xetla-archive-name-source (archive &optional existence-check)
2464 "Make source archive name from ARCHIVE.
2465 If EXISTENCE-CHECK is non-nil, check whether the made source archive name
2466 already exists or not; return nil if it doesn't exists.
2468 ELISP> (xetla-archive-name-source \"jet@gyve.org-xetla\")
2469 \"jet@gyve.org-xetla-SOURCE\"
2470 ELISP> (xetla-archive-name-source \"jet@gyve.org-xetla-MIRROR\")
2471 \"jet@gyve.org-xetla\"
2472 ELISP> (xetla-archive-name-source \"jet@gyve.org-xetla-SOURCE\")
2474 (let* ((type (xetla-archive-type archive))
2477 (concat archive "-SOURCE"))
2479 (string-match "\\(.*\\)-MIRROR$" archive)
2480 (match-string 1 archive))
2484 (xetla-archive-tree-build-archives t)
2485 (when (and source (xetla-archive-tree-get-archive source))
2489 ;; (xetla-archive-name-mirror "a")
2490 ;; (xetla-archive-name-mirror "a-SOURCE")
2491 ;; (xetla-archive-name-mirror "a-MIRROR")
2492 (defun xetla-archive-name-mirror (archive &optional existence-check)
2493 "Make mirror archive name from ARCHIVE.
2494 If EXISTENCE-CHECK is non-nil, check whether the made mirror archive name
2495 already exists or not; return nil if it doesn't exists.
2497 ELISP> (xetla-archive-name-mirror \"jet@gyve.org--xetla\")
2498 \"jet@gyve.org--xetla-MIRROR\"
2499 ELISP> (xetla-archive-name-mirror \"jet@gyve.org--xetla-SOURCE\")
2500 \"jet@gyve.org--xetla\"
2501 ELISP> (xetla-archive-name-mirror \"jet@gyve.org--xetla-MIRROR\")
2503 (let* ((type (xetla-archive-type archive))
2506 (concat archive "-MIRROR"))
2508 (string-match "\\(.*\\)-SOURCE" archive)
2509 (match-string 1 archive))
2513 (xetla-archive-tree-build-archives t)
2514 (when (and mirror (xetla-archive-tree-get-archive mirror))
2518 (defun xetla-compute-direct-ancestor (&optional revision)
2519 "Compute the direct ancestor of REVISION.
2520 REVISION must be provided as a list, and a list is returned.
2521 If revision is nil, return the ancestor of the last revision
2524 (list (xetla-name-read "Compute direct ancestor of: "
2525 'prompt 'prompt 'prompt 'prompt 'prompt)))
2527 (xetla-run-tla-sync (list "ancestry-graph" "--immediate"
2529 (xetla-name-construct revision)))
2530 :finished (lambda (output error status arguments)
2532 (xetla-buffer-content
2534 (when (interactive-p)
2535 (message "Ancestor of: %s\n is: %s"
2536 (xetla-name-construct ancestor)
2537 (xetla-name-construct revision)))
2540 ;; Copied from ediff-mouse-event-p. I prefer keeping this duplication
2541 ;; to avoid one more dependancy on ediff.el (whose interface may
2542 ;; change one day ...)
2543 (defsubst xetla-mouse-event-p (event)
2544 "Return true if EVENT is a mouse-related event."
2545 (if (featurep 'xemacs)
2546 (xetla-do-in-xemacs (button-event-p event))
2547 (xetla-do-in-gnu-emacs
2548 (string-match "mouse" (format "%S" (event-basic-type event))))))
2550 ;; --------------------------------------
2551 ;; Face manipulators
2552 ;; --------------------------------------
2553 (defun xetla-face-add (str face &optional keymap menu help)
2554 "Add to string STR the face FACE.
2555 Optionally, also add the text properties KEYMAP, MENU and HELP.
2557 If KEYMAP is a symbol, (symbol-value KEYMAP) is used
2558 as a keymap; and `substitute-command-keys' result
2559 against (format \"\\{%s}\" (symbol-name keymap)) is appended to HELP.
2561 If HELP is nil and if MENU is non nil, the MENU title is used as HELP."
2563 (let* ((strcpy (copy-sequence str))
2564 (key-help (when (symbolp keymap)
2565 (substitute-command-keys (format "\\{%s}" (symbol-name keymap)))))
2566 (prefix-help (if help help (when (and menu (stringp (cadr menu))) (cadr menu))))
2567 (long-help (if key-help
2568 (if prefix-help (concat prefix-help "\n"
2569 ;; Sigh. Font used on tooltips in GNU Emacs with Gtk+
2570 ;; is a proportional.
2571 ;; (make-string (length help) ?=) "\n"
2572 "================" "\n"
2575 (keymap (if (symbolp keymap) (symbol-value keymap) keymap)))
2576 (add-text-properties 0 (length strcpy)
2578 ;;; Even if we define a face in a buffer, it seems that
2579 ;;; font-lock mode just ignore it or remove the face property.
2580 ;;; I don't know the detail but in xetla-inventory buffer,
2581 ;;; I cannot make both font-lock keywords and faces put by tl-face-add
2582 ;;; highlight at once. When font-lock-face is defined, I can do.
2583 ;;; See "Special Properties" subsection in the emacs lisp reference manual.
2584 ;;; `font-lock-face' property is new in Emacs 21.4. However, I guess there is
2585 ;;; no wrong side effect if I define font-lock-face property here.
2586 font-lock-face ,face
2588 `(mouse-face highlight
2590 help-echo ,long-help))
2598 ;; --------------------------------------
2599 ;; Debugging facilities
2600 ;; --------------------------------------
2601 (defvar xetla-debug t)
2603 (defun xetla-trace (&rest msg)
2604 "Display the trace message MSG.
2605 Same as `message' if `xetla-debug' is non-nil.
2606 Does nothing otherwise. Please use it for your debug messages."
2608 (apply 'message (concat "xetla: " (car msg)) (cdr msg))))
2610 (defun xetla-reload (directory)
2611 "Reloads xetla \(usually for debugging purpose\).
2613 With prefix arg, prompts for the DIRECTORY in which xetla should be
2614 loaded. Useful to switch from one branch to the other.
2616 If a Makefile is present in the directory where xetla is to be loaded,
2619 (list (when current-prefix-arg
2620 (let* ((other (read-directory-name
2621 "Load XEtla from: "))
2622 (lispdir (concat (file-name-as-directory other)
2624 (if (file-directory-p lispdir)
2628 (let ((current-path (file-name-directory (locate-library
2631 (cons directory (remove current-path load-path)))))
2632 (let ((default-directory (file-name-directory (locate-library "xetla"))))
2633 (when (file-exists-p
2635 (shell-command "make")))
2636 (when (featurep 'xetla-tips) (unload-feature 'xetla-tips t))
2637 (when (featurep 'xetla-browse) (unload-feature 'xetla-browse t))
2638 (when (featurep 'xetla) (unload-feature 'xetla t))
2639 (when (featurep 'xetla-core) (unload-feature 'xetla-core t))
2640 (when (featurep 'xetla-defs) (unload-feature 'xetla-defs t))
2641 (when (featurep 'xetla-autoloads) (unload-feature 'xetla-autoloads t))
2642 (ignore-errors (require 'xetla-autoloads))
2645 ;; --------------------------------------
2646 ;; Supports spaces in filenames
2647 ;; --------------------------------------
2648 (defvar xetla-supports-spaces-in-filenames nil
2649 "Wether xetla supports spaces in filenames.
2651 Possible values are nil (don't know), 'yes, or 'no. Don't use this
2652 variable directly. Use `xetla-supports-spaces-in-filenames' instead.")
2654 (defun xetla-supports-spaces-in-filenames ()
2655 "Wether xetla supports spaces in filenames.
2659 If `xetla-supports-spaces' is non-nil, use its value. Otherwise, test
2660 if \"escape\" is listed by \"xetla help\", and memorize the result in
2661 `xetla-supports-spaces-in-filenames'"
2664 (cond ((eq xetla-supports-spaces-in-filenames 'yes) t)
2665 ((eq xetla-supports-spaces-in-filenames 'no) nil)
2666 (t (xetla-run-tla-sync
2668 :finished (lambda (output error status arguments)
2669 (with-current-buffer output
2670 (goto-char (point-min))
2671 (search-forward "escape :"
2673 (when (interactive-p)
2674 (message (if answer "Yes" "No")))
2675 (setq xetla-supports-spaces-in-filenames
2676 (if answer 'yes 'no))
2679 (defun xetla-escape (string &optional unescape message)
2680 "Return the pika escaped value of STRING.
2681 If pika escaping is not supported by xetla, return STRING.
2682 If UNESCAPE is non-nil, returns the unescaped version of string.
2683 If MESSAGE is non-nil or if run interactively, also display the value
2685 (interactive "sString to escape: ")
2686 (let ((res (if (and (string-match (if unescape "\\\\"
2687 "[^a-zA-Z._+,{}-]") string)
2688 ;; We need to do the (un)escaping
2689 (xetla-supports-spaces-in-filenames))
2691 (list "escape" (when unescape "--unescaped") string)
2692 :finished (lambda (output error status arguments)
2693 (xetla-buffer-content output)))
2695 (when (or (interactive-p) message)
2699 (defun xetla-unescape (string)
2700 "Run \"tla escape -unescaped\" on STRING.
2702 Return STRING if \"tla escape\" is not available."
2703 (interactive "sString to unescape: ")
2704 (xetla-escape string t (interactive-p)))
2706 (defun xetla-regexp-quote (string)
2707 "Return a regexp string which matches exactly STRING and nothing else.
2708 Special characters are escaped to leave STRING in a suitable form for
2710 (let ((quoted (regexp-quote string)))
2711 (replace-regexp-in-string
2713 (concat "\\\\" ; leading slash
2714 "\\1") ; quoted character
2717 ;; --------------------------------------
2718 ;; Saving and loading state variables
2719 ;; --------------------------------------
2720 (defun xetla-pp-to-string (sexp)
2721 "Return sexp pretty printed by `pp-to-string'."
2722 (let ((print-readably t)
2723 print-level print-length)
2724 (pp-to-string sexp)))
2726 (defun xetla-save-state (&optional vars state-file pp)
2727 "Save variables from VARS list to file STATE-FILE.
2728 The default for VARS is `xetla-state-variables-list'
2729 The default for STATE-FILE is `xetla-state-file-name'.
2730 If PP is non-nil use `xetla-pp-to-string' to format object.
2732 The file will contain a setq setting the vars during loading by
2733 `xetla-load-state'."
2734 (let ((state-file (or state-file
2735 (expand-file-name xetla-state-file-name
2736 xetla-config-directory)))
2737 (vars (or vars xetla-state-variables-list))
2739 (if (not (file-exists-p (file-name-directory state-file)))
2740 (make-directory (file-name-directory state-file) t))
2742 (set-buffer (get-buffer-create " *xetla-state*"))
2744 (insert ";; Generated file. Do not edit!!!\n(setq\n")
2747 (setq v (car vars) vars (cdr vars))
2748 (insert (format "%s\n'%s"
2750 (xetla-pp-to-string (symbol-value v)))))
2752 (setq v (car vars) vars (cdr vars))
2753 (insert (format " %s '%S\n"
2755 (symbol-value v)))))
2757 (write-region (point-min) (point-max) state-file))))
2759 (defun xetla-load-state (&optional state-file)
2760 "Load `xetla-state-file-name`, i.e. evaluate its content."
2761 (let ((state-file (or state-file
2762 (expand-file-name xetla-state-file-name
2763 xetla-config-directory))))
2764 (if (file-exists-p state-file)
2765 (load state-file nil t t))))
2767 ;; (setq xetla-archive-tree nil)
2768 ;; (setq xetla-revlib-tree nil)
2770 (provide 'xetla-core)
2771 ;;; xetla-core.el ends here