about summary refs log tree commit diff stats
path: root/commands/msg
diff options
context:
space:
mode:
authorReto Brunner <reto@labrat.space>2019-12-18 06:34:04 +0100
committerDrew DeVault <sir@cmpwn.com>2019-12-21 09:27:52 -0500
commit7b359be0cd3500e3c7382f110d5cb4ddaf1ce437 (patch)
treec7ee1c584f16bdad79e1d1d829c518d587349ac3 /commands/msg
parentc2b31ed7c96c796eb3fd02da23015029388067d4 (diff)
downloadaerc-7b359be0cd3500e3c7382f110d5cb4ddaf1ce437.tar.gz
delete: adapt to ProvidesMessages
Diffstat (limited to 'commands/msg')
-rw-r--r--commands/msg/delete.go80
1 files changed, 59 insertions, 21 deletions
diff --git a/commands/msg/delete.go b/commands/msg/delete.go
index 8fefbd5..8cd782b 100644
--- a/commands/msg/delete.go
+++ b/commands/msg/delete.go
@@ -6,6 +6,8 @@ import (
 
 	"github.com/gdamore/tcell"
 
+	"git.sr.ht/~sircmpwn/aerc/lib"
+	"git.sr.ht/~sircmpwn/aerc/models"
 	"git.sr.ht/~sircmpwn/aerc/widgets"
 	"git.sr.ht/~sircmpwn/aerc/worker/types"
 )
@@ -29,33 +31,23 @@ func (Delete) Execute(aerc *widgets.Aerc, args []string) error {
 		return errors.New("Usage: :delete")
 	}
 
-	widget := aerc.SelectedTab().(widgets.ProvidesMessage)
-	acct := widget.SelectedAccount()
-	if acct == nil {
-		return errors.New("No account selected")
+	h := newHelper(aerc)
+	store, err := h.store()
+	if err != nil {
+		return err
 	}
-	store := widget.Store()
-	if store == nil {
-		return errors.New("Cannot perform action. Messages still loading")
+	uids, err := h.uids()
+	if err != nil {
+		return err
 	}
-	msg, err := widget.SelectedMessage()
+	acct, err := h.account()
 	if err != nil {
 		return err
 	}
-	_, isMsgView := widget.(*widgets.MessageViewer)
-	mv, _ := aerc.SelectedTab().(*widgets.MessageViewer)
-	store.Next()
-	if isMsgView {
-		nextMsg := store.Selected()
-		if nextMsg == msg || !aerc.Config().Ui.NextMessageOnDelete {
-			aerc.RemoveTab(widget)
-			acct.Messages().Scroll()
-		} else {
-			nextMv := widgets.NewMessageViewer(acct, aerc.Config(), store, nextMsg)
-			aerc.ReplaceTab(mv, nextMv, nextMsg.Envelope.Subject)
-		}
+	if err != nil {
+		return err
 	}
-	store.Delete([]uint32{msg.Uid}, func(msg types.WorkerMessage) {
+	store.Delete(uids, func(msg types.WorkerMessage) {
 		switch msg := msg.(type) {
 		case *types.Done:
 			aerc.PushStatus("Messages deleted.", 10*time.Second)
@@ -64,5 +56,51 @@ func (Delete) Execute(aerc *widgets.Aerc, args []string) error {
 				Color(tcell.ColorDefault, tcell.ColorRed)
 		}
 	})
+
+	//caution, can be nil
+	next := findNextNonDeleted(uids, store)
+
+	mv, isMsgView := h.msgProvider.(*widgets.MessageViewer)
+	if isMsgView {
+		if !aerc.Config().Ui.NextMessageOnDelete {
+			aerc.RemoveTab(h.msgProvider)
+		} else {
+			// no more messages in the list
+			if next == nil {
+				aerc.RemoveTab(h.msgProvider)
+				acct.Messages().Scroll()
+				return nil
+			}
+			nextMv := widgets.NewMessageViewer(acct, aerc.Config(), store, next)
+			aerc.ReplaceTab(mv, nextMv, next.Envelope.Subject)
+		}
+	}
+	acct.Messages().Scroll()
 	return nil
 }
+
+func findNextNonDeleted(deleted []uint32, store *lib.MessageStore) *models.MessageInfo {
+	selected := store.Selected()
+	if !contains(deleted, selected.Uid) {
+		return selected
+	}
+	for {
+		store.Next()
+		next := store.Selected()
+		if next == selected || next == nil {
+			// the last message is in the deleted state or doesn't exist
+			return nil
+		}
+		return next
+	}
+	return nil // Never reached
+}
+
+func contains(uids []uint32, uid uint32) bool {
+	for _, item := range uids {
+		if item == uid {
+			return true
+		}
+	}
+	return false
+}
nt-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
package imap

import (
	"github.com/emersion/go-imap"

	"git.sr.ht/~sircmpwn/aerc/worker/types"
)

func (imapw *IMAPWorker) handleOpenDirectory(msg *types.OpenDirectory) {
	imapw.worker.Logger.Printf("Opening %s", msg.Directory)

	_, err := imapw.client.Select(msg.Directory, false)
	if err != nil {
		imapw.worker.PostMessage(&types.Error{
			Message: types.RespondTo(msg),
			Error:   err,
		}, nil)
	} else {
		imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
		if imapw.idleStop == nil {
			imapw.idleStop = make(chan struct{})
		}
	}
}

func (imapw *IMAPWorker) handleFetchDirectoryContents(
	msg *types.FetchDirectoryContents) {

	imapw.worker.Logger.Printf("Fetching UID list")

	seqSet := &imap.SeqSet{}
	seqSet.AddRange(1, imapw.selected.Messages)
	uids, err := imapw.client.UidSearch(&imap.SearchCriteria{
		SeqNum: seqSet,
	})
	if err != nil {
		imapw.worker.PostMessage(&types.Error{
			Message: types.RespondTo(msg),
			Error:   err,
		}, nil)
	} else {
		imapw.worker.Logger.Printf("Found %d UIDs", len(uids))
		imapw.seqMap = make([]uint32, len(uids))
		imapw.worker.PostMessage(&types.DirectoryContents{
			Message: types.RespondTo(msg),
			Uids:    uids,
		}, nil)
		imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
	}
}