4949
5050; ; Third-party.
5151
52+ (require 'magit-section )
53+
5254; ; This package.
5355(require 'ement-api )
5456(require 'ement-macros )
5557(require 'ement-structs )
5658(require 'ement-room )
5759(require 'ement-notify )
5860
61+ ; ;;;; Compilation
62+
63+ ; ; To avoid compilation warnings.
64+
65+ (eval-when-compile
66+ (require 'taxy-magit-section ))
67+
5968; ;;; Variables
6069
6170(defvar ement-sessions nil
@@ -273,7 +282,7 @@ in them won't work."
273282 " Switch to a buffer showing ROOM on SESSION.
274283Calls `pop-to-buffer-same-window' . Interactively, with prefix,
275284call `pop-to-buffer' ."
276- (interactive (ement-complete-room (ement-complete-session) nil ))
285+ (interactive (ement-complete-room :session (ement-complete-session) :suggest nil ))
277286 (pcase-let* (((cl-struct ement-room (local (map buffer))) room))
278287 (unless (buffer-live-p buffer)
279288 (setf buffer (ement-room--buffer session room
@@ -419,7 +428,7 @@ new one automatically if necessary."
419428 (interactive
420429 (let* ((session (ement-complete-session))
421430 (user-id (ement-complete-user-id))
422- (room (car (ement-complete-room session))))
431+ (room (car (ement-complete-room :session session))))
423432 (list user-id room session)))
424433 (pcase-let* ((endpoint (format " rooms/%s /invite "
425434 (url-hexify-string (ement-room-id room))))
@@ -546,12 +555,17 @@ If no URI is found, prompt the user for the hostname."
546555 (alist-get selected-id ement-sessions nil nil #'equal )))
547556 (otherwise (user-error " No active sessions. Call `ement-connect' to log in" ))))
548557
549- (cl-defun ement-complete-room (&optional session (suggest t ))
558+ (cl-defun ement-complete-room (&key session predicate
559+ (prompt " Room: " ) (suggest t ))
550560 " Return a (room session) list selected from SESSION with completion.
551561If SESSION is nil, select from rooms in all of `ement-sessions' .
552- When SUGGEST, suggest current buffer's room as initial
553- input (i.e. it should be set to nil when switching from one room
554- buffer to another)."
562+ When SUGGEST, suggest current buffer's room (or a room at point
563+ in a room list buffer) as initial input (i.e. it should be set to
564+ nil when switching from one room buffer to another). PROMPT may
565+ override the default prompt. PREDICATE may be a function to
566+ select which rooms are offered; it is also applied to the
567+ suggested room."
568+ (declare (indent defun ))
555569 (pcase-let* ((sessions (if session
556570 (list session)
557571 (mapcar #'cdr ement-sessions)))
@@ -561,12 +575,28 @@ buffer to another)."
561575 collect (cons (ement--format-room room)
562576 (list room session)))))
563577 (names (mapcar #'car name-to-room-session))
564- (selected-name (completing-read " Room: " names nil t
565- (when (and suggest (equal major-mode 'ement-room-mode ))
566- ; ; Suggest current buffer's room.
567- (ement--format-room ement-room)))))
578+ (selected-name (completing-read
579+ prompt names nil t
580+ (when suggest
581+ (when-let ((suggestion (ement--room-at-point)))
582+ (when (or (not predicate)
583+ (funcall predicate suggestion))
584+ suggestion))))))
568585 (alist-get selected-name name-to-room-session nil nil #'string= )))
569586
587+ (defun ement--room-at-point ()
588+ " Return room at point.
589+ Works in major-modes `ement-room-mode' , `ement-room-list-mode' ,
590+ and `ement-taxy-mode' ."
591+ (pcase major-mode
592+ ('ement-room-mode (ement--format-room ement-room))
593+ ('ement-room-list-mode (ement--format-room (tabulated-list-get-id )))
594+ ('ement-taxy-mode
595+ (cl-typecase (oref (magit-current-section) value)
596+ (taxy-magit-section nil )
597+ (t (pcase (oref (magit-current-section) value)
598+ (`[, room , _session ] (ement--format-room room))))))))
599+
570600(defun ement--format-room (room )
571601 " Return ROOM formatted with name, alias, ID, and topic.
572602Suitable for use in completion, etc."
0 commit comments