diff --git a/src/msg/cli.rs b/src/msg/cli.rs index 1bbea3d..75718f3 100644 --- a/src/msg/cli.rs +++ b/src/msg/cli.rs @@ -302,37 +302,33 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> { } if let Some(matches) = matches.subcommand_matches("template") { - let mut imap_conn = ImapConnector::new(&account)?; if let Some(_) = matches.subcommand_matches("new") { let tpl = Msg::build_new_tpl(&config, &account)?; print(&output_fmt, &tpl)?; } if let Some(matches) = matches.subcommand_matches("reply") { + let mut imap_conn = ImapConnector::new(&account)?; let uid = matches.value_of("uid").unwrap(); - let mbox = matches.value_of("mailbox").unwrap(); - let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?); let tpl = if matches.is_present("reply-all") { msg.build_reply_all_tpl(&config, &account)? } else { msg.build_reply_tpl(&config, &account)? }; - print(&output_fmt, &tpl)?; + imap_conn.logout(); } if let Some(matches) = matches.subcommand_matches("forward") { + let mut imap_conn = ImapConnector::new(&account)?; let uid = matches.value_of("uid").unwrap(); - let mbox = matches.value_of("mailbox").unwrap(); - let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?); let tpl = msg.build_forward_tpl(&config, &account)?; - print(&output_fmt, &tpl)?; - break; + imap_conn.logout(); } - imap_conn.logout(); + break; } diff --git a/vim/autoload/himalaya/mbox.vim b/vim/autoload/himalaya/mbox.vim index 9e003df..bc11ef8 100644 --- a/vim/autoload/himalaya/mbox.vim +++ b/vim/autoload/himalaya/mbox.vim @@ -1,6 +1,4 @@ -let s:print_info = function("himalaya#utils#print_msg") -let s:print_err = function("himalaya#utils#print_err") -let s:cli = function("himalaya#shared#cli") +let s:cli = function("himalaya#shared#cli#call") " Pagination @@ -28,25 +26,23 @@ endfunction function! himalaya#mbox#input() try - call s:print_info("Fetching mailboxes…") - - let mboxes = map(s:cli("mailboxes", []), "v:val.name") - - " if &rtp =~ "fzf.vim" - " call fzf#run({ - " \"source": mboxes, - " \"sink": function("himalaya#mbox#post_input"), - " \"down": "25%", - " \}) - " else + let mboxes = map(s:cli("mailboxes", [], "Fetching mailboxes"), "v:val.name") + if &rtp =~ "fzf.vim" + call fzf#run({ + \"source": mboxes, + \"sink": function("himalaya#mbox#post_input"), + \"down": "25%", + \}) + else let choice = map(copy(mboxes), "printf('%s (%d)', v:val, v:key)") - redraw | echo let choice = input(join(choice, ", ") . ": ") redraw | echo call himalaya#mbox#post_input(mboxes[choice]) - " endif + endif catch - call s:print_err(v:exception) + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif endtry endfunction diff --git a/vim/autoload/himalaya/mbx.vim b/vim/autoload/himalaya/mbx.vim deleted file mode 100644 index 72a3ab4..0000000 --- a/vim/autoload/himalaya/mbx.vim +++ /dev/null @@ -1,107 +0,0 @@ -let s:trim = function("himalaya#utils#trim") -let s:print_err = function("himalaya#utils#print_err") -let s:print_info = function("himalaya#utils#print_msg") - -let s:buff_name = "Himalaya" - -" Exec utils - -function! s:exec(cmd, args) - let cmd = call("printf", [a:cmd] + a:args) - let res = system(cmd) - - try - return eval(res) - catch - throw res - endtry -endfunction - -" Render utils - -function! s:render(type, lines) - let s:max_widths = s:get_max_widths(a:lines, s:config[a:type].columns) - let header = [s:render_line(s:config.labels, s:max_widths, a:type)] - let line = map(copy(a:lines), "s:render_line(v:val, s:max_widths, a:type)") - - return header + line -endfunction - -function! s:render_line(line, max_widths, type) - return "|" . join(map( - \copy(s:config[a:type].columns), - \"s:render_cell(a:line[v:val], a:max_widths[v:key])", - \), "") -endfunction - -function! s:render_cell(cell, max_width) - let cell_width = strdisplaywidth(a:cell[:a:max_width]) - return a:cell[:a:max_width] . repeat(" ", a:max_width - cell_width) . " |" -endfunction - -function! s:get_max_widths(msgs, columns) - let max_widths = map(copy(a:columns), "strlen(s:config.labels[v:val])") - - for msg in a:msgs - let widths = map(copy(a:columns), "strlen(msg[v:val])") - call map(max_widths, "max([widths[v:key], v:val])") - endfor - - return max_widths -endfunction - -" List - -let s:config = { - \"list": { - \"columns": ["delim", "name", "attributes"], - \}, - \"labels": { - \"delim": "DELIM", - \"name": "NAME", - \"attributes": "ATTRIBUTES", - \}, -\} - -function! himalaya#mbx#format_for_list(mbx) - let mbx = copy(a:mbx) - let mbx.attributes = join(mbx.attributes, ", ") - return mbx -endfunction - -function! s:get_focused_mbx() - try - return s:trim(split(getline("."), "|")[1]) - catch - throw "mailbox not found" - endtry -endfunction - -function! himalaya#mbx#list() - try - call s:print_info("Fetching mailboxes…") - - let prev_pos = getpos(".") - let mbxs = s:exec("himalaya --output json mailboxes", []) - let mbxs = map(copy(mbxs), 'himalaya#mbx#format_for_list(v:val)') - - silent! bwipeout "Mailboxes" - silent! edit Mailboxes - - call append(0, s:render("list", mbxs)) - execute "$d" - - call setpos(".", prev_pos) - setlocal filetype=himalaya-mbx-list - let &modified = 0 - - call s:print_info("Done!") - catch - call s:print_err(v:exception) - endtry -endfunction - -function! himalaya#mbx#select() - let mbx = s:get_focused_mbx() - call himalaya#msg#list(mbx) -endfunction diff --git a/vim/autoload/himalaya/msg.vim b/vim/autoload/himalaya/msg.vim index 7510db3..5a06ef2 100644 --- a/vim/autoload/himalaya/msg.vim +++ b/vim/autoload/himalaya/msg.vim @@ -1,7 +1,6 @@ -let s:print_info = function("himalaya#utils#print_msg") -let s:print_err = function("himalaya#utils#print_err") -let s:trim = function("himalaya#utils#trim") -let s:cli = function("himalaya#shared#cli") +let s:log = function("himalaya#shared#log#info") +let s:trim = function("himalaya#shared#utils#trim") +let s:cli = function("himalaya#shared#cli#call") let s:msg_id = 0 let s:draft = "" @@ -11,10 +10,10 @@ let s:draft = "" function! s:format_msg_for_list(msg) let msg = copy(a:msg) - let flag_unseen = index(msg.flags, "Seen") == -1 ? "🟓" : " " - let flag_replied = index(msg.flags, "Answered") == -1 ? " " : "↩" + let flag_new = index(msg.flags, "Seen") == -1 ? "N" : " " let flag_flagged = index(msg.flags, "Flagged") == -1 ? " " : "!" - let msg.flags = printf("%s%s%s", flag_unseen, flag_replied, flag_flagged) + let flag_replied = index(msg.flags, "Answered") == -1 ? " " : "R" + let msg.flags = printf("%s%s%s", flag_new, flag_replied, flag_flagged) return msg endfunction @@ -23,14 +22,10 @@ function! himalaya#msg#list() try let mbox = himalaya#mbox#curr_mbox() let page = himalaya#mbox#curr_page() - - call s:print_info(printf("Fetching %s messages…", tolower(mbox))) - let msgs = s:cli("--mailbox %s list --page %d", [shellescape(mbox), page]) - let msgs = map(copy(msgs), "s:format_msg_for_list(v:val)") - call s:print_info("Done!") - + let msgs = s:cli("--mailbox %s list --page %d", [shellescape(mbox), page], printf("Fetching %s messages", mbox)) + let msgs = map(msgs, "s:format_msg_for_list(v:val)") let buftype = stridx(bufname("%"), "Himalaya messages") == 0 ? "file" : "edit" - execute printf("silent! %s Himalaya messages [%s] [page %d]", buftype, tolower(mbox), page + 1) + execute printf("silent! %s Himalaya messages [%s] [page %d]", buftype, mbox, page + 1) setlocal modifiable execute "%d" call append(0, s:render("list", msgs)) @@ -39,7 +34,9 @@ function! himalaya#msg#list() let &modified = 0 execute 0 catch - call s:print_err(v:exception) + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif endtry endfunction @@ -47,11 +44,7 @@ function! himalaya#msg#read() try let s:msg_id = s:get_focused_msg_id() let mbox = himalaya#mbox#curr_mbox() - - call s:print_info(printf("Fetching message %d…", s:msg_id)) - let msg = s:cli("read %d --mailbox %s", [s:msg_id, shellescape(mbox)]) - call s:print_info("Done!") - + let msg = s:cli("--mailbox %s read %d", [shellescape(mbox), s:msg_id], printf("Fetching message %d", s:msg_id)) let attachment = msg.hasAttachment ? " []" : "" execute printf("silent! edit Himalaya read message [%d]%s", s:msg_id, attachment) setlocal modifiable @@ -62,16 +55,15 @@ function! himalaya#msg#read() let &modified = 0 execute 0 catch - call s:print_err(v:exception) + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif endtry endfunction function! himalaya#msg#write() try - call s:print_info("Fetching new template…") - let msg = s:cli("template new", []) - call s:print_info("Done!") - + let msg = s:cli("template new", [], "Fetching new template") silent! edit Himalaya write call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) execute "$d" @@ -79,7 +71,9 @@ function! himalaya#msg#write() let &modified = 0 execute 0 catch - call s:print_err(v:exception) + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif endtry endfunction @@ -87,11 +81,7 @@ function! himalaya#msg#reply() try let mbox = himalaya#mbox#curr_mbox() let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id - - call s:print_info("Fetching reply template…") - let msg = s:cli("template reply %d --mailbox %s", [msg_id, shellescape(mbox)]) - call s:print_info("Done!") - + let msg = s:cli("--mailbox %s template reply %d", [shellescape(mbox), msg_id], "Fetching reply template") execute printf("silent! edit Himalaya reply [%d]", msg_id) call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) execute "$d" @@ -99,7 +89,9 @@ function! himalaya#msg#reply() let &modified = 0 execute 0 catch - call s:print_err(v:exception) + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif endtry endfunction @@ -107,11 +99,7 @@ function! himalaya#msg#reply_all() try let mbox = himalaya#mbox#curr_mbox() let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id - - call s:print_info("Fetching reply all template…") - let msg = s:cli("template reply %d --mailbox %s --all", [msg_id, shellescape(mbox)]) - call s:print_info("Done!") - + let msg = s:cli("--mailbox %s template reply %d --all", [shellescape(mbox), msg_id], "Fetching reply all template") execute printf("silent! edit Himalaya reply all [%d]", msg_id) call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) execute "$d" @@ -119,7 +107,9 @@ function! himalaya#msg#reply_all() let &modified = 0 execute 0 catch - call s:print_err(v:exception) + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif endtry endfunction @@ -127,11 +117,7 @@ function! himalaya#msg#forward() try let mbox = himalaya#mbox#curr_mbox() let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id - - call s:print_info("Fetching forward template…") - let msg = s:cli("template forward %d --mailbox %s", [msg_id, shellescape(mbox)]) - call s:print_info("Done!") - + let msg = s:cli("--mailbox %s template forward %d", [shellescape(mbox), msg_id], "Fetching forward template") execute printf("silent! edit Himalaya forward [%d]", msg_id) call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) execute "$d" @@ -139,50 +125,51 @@ function! himalaya#msg#forward() let &modified = 0 execute 0 catch - call s:print_err(v:exception) + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif endtry endfunction function! himalaya#msg#draft_save() let s:draft = join(getline(1, "$"), "\r\n") - call s:print_info("Draft saved!") + redraw | call s:log("Save draft [OK]") let &modified = 0 endfunction function! himalaya#msg#draft_handle() - while 1 - let choice = input("(s)end, (d)raft, (q)uit or (c)ancel? ") - let choice = tolower(choice)[0] - redraw | echo + try + while 1 + let choice = input("(s)end, (d)raft, (q)uit or (c)ancel? ") + let choice = tolower(choice)[0] + redraw | echo - if choice == "s" - call s:print_info("Sending message…") - call s:cli("send -- %s", [shellescape(s:draft)]) - call s:print_info("Done!") - return - elseif choice == "d" - call s:print_info("Saving draft…") - call s:cli("save --mailbox Drafts -- %s", [shellescape(s:draft)]) - call s:print_info("Done!") - return - elseif choice == "q" - return - elseif choice == "c" - throw "Action canceled" - endif - endwhile + if choice == "s" + return s:cli("send -- %s", [shellescape(s:draft)], "Sending message") + elseif choice == "d" + return s:cli("--mailbox Drafts save -- %s", [shellescape(s:draft)], "Saving draft") + elseif choice == "q" + return + elseif choice == "c" + throw "Action canceled" + endif + endwhile + catch + " TODO: find a better way to prevent the buffer to close (stop the BufUnload event) + call himalaya#shared#log#err(v:exception) + throw "" + endtry endfunction function! himalaya#msg#attachments() try let mbox = himalaya#mbox#curr_mbox() let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id - - call s:print_info("Downloading attachments…") - let msg = s:cli("attachments %d --mailbox %s", [msg_id, shellescape(mbox)]) - call s:print_info("Done!") + let msg = s:cli("--mailbox %s attachments %d", [shellescape(mbox), msg_id], "Downloading attachments") catch - call s:print_err(v:exception) + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif endtry endfunction diff --git a/vim/autoload/himalaya/shared.vim b/vim/autoload/himalaya/shared.vim deleted file mode 100644 index 78b13d2..0000000 --- a/vim/autoload/himalaya/shared.vim +++ /dev/null @@ -1,28 +0,0 @@ -function! himalaya#shared#define_bindings(bindings) - for [mode, key, name] in a:bindings - let plug = substitute(name, "[#_]", "-", "g") - let plug = printf("(himalaya-%s)", plug) - execute printf("%snoremap %s :call himalaya#%s()", mode, plug, name) - - if !hasmapto(plug, mode) - execute printf("%smap %s %s", mode, key, plug) - endif - endfor -endfunction - -function! himalaya#shared#cli(cmd, args) - let cmd = call("printf", ["himalaya --output json " . a:cmd] + a:args) - let res = system(cmd) - - if !empty(res) - try - return eval(res) - catch - throw res - endtry - endif -endfunction - -function! himalaya#shared#thread_fold(lnum) - return getline(a:lnum)[0] == ">" -endfunction diff --git a/vim/autoload/himalaya/shared/bindings.vim b/vim/autoload/himalaya/shared/bindings.vim new file mode 100644 index 0000000..0cac565 --- /dev/null +++ b/vim/autoload/himalaya/shared/bindings.vim @@ -0,0 +1,11 @@ +function! himalaya#shared#bindings#define(bindings) + for [mode, key, name] in a:bindings + let plug = substitute(name, "[#_]", "-", "g") + let plug = printf("(himalaya-%s)", plug) + execute printf("%snoremap %s :call himalaya#%s()", mode, plug, name) + + if !hasmapto(plug, mode) + execute printf("%smap %s %s", mode, key, plug) + endif + endfor +endfunction diff --git a/vim/autoload/himalaya/shared/cli.vim b/vim/autoload/himalaya/shared/cli.vim new file mode 100644 index 0000000..bdd2f40 --- /dev/null +++ b/vim/autoload/himalaya/shared/cli.vim @@ -0,0 +1,21 @@ +function! himalaya#shared#cli#call(cmd, args, log) + call himalaya#shared#log#info(printf("%s…", a:log)) + let cmd = call("printf", ["himalaya --output json " . a:cmd] + a:args) + let res = system(cmd) + + if empty(res) + redraw | call himalaya#shared#log#info(printf("%s [OK]", a:log)) + else + try + let res = eval(res) + redraw | call himalaya#shared#log#info(printf("%s [OK]", a:log)) + return res + catch + redraw | call himalaya#shared#log#info(printf("%s [ERR]", a:log)) + for line in split(res, "\n") + call himalaya#shared#log#err(line) + endfor + throw "" + endtry + endif +endfunction diff --git a/vim/autoload/himalaya/shared/log.vim b/vim/autoload/himalaya/shared/log.vim new file mode 100644 index 0000000..0d36bb3 --- /dev/null +++ b/vim/autoload/himalaya/shared/log.vim @@ -0,0 +1,10 @@ +function! himalaya#shared#log#info(msg) + echohl None + echomsg a:msg +endfunction + +function! himalaya#shared#log#err(msg) + echohl ErrorMsg + echomsg a:msg + echohl None +endfunction diff --git a/vim/autoload/himalaya/shared/thread.vim b/vim/autoload/himalaya/shared/thread.vim new file mode 100644 index 0000000..583c485 --- /dev/null +++ b/vim/autoload/himalaya/shared/thread.vim @@ -0,0 +1,3 @@ +function! himalaya#shared#thread#fold(lnum) + return getline(a:lnum)[0] == ">" +endfunction diff --git a/vim/autoload/himalaya/shared/utils.vim b/vim/autoload/himalaya/shared/utils.vim new file mode 100644 index 0000000..a942585 --- /dev/null +++ b/vim/autoload/himalaya/shared/utils.vim @@ -0,0 +1,30 @@ +" Compose + +function! himalaya#shared#utils#compose(...) + let funcs = map(reverse(copy(a:000)), 'function(v:val)') + return function('s:compose', [funcs]) +endfunction + +function! s:compose(funcs, arg) + let data = a:arg + + for Func in a:funcs + let data = Func(data) + endfor + + return data +endfunction + +" Trim + +function! himalaya#shared#utils#trim(str) + return himalaya#shared#utils#compose('s:trim_left', 's:trim_right')(a:str) +endfunction + +function! s:trim_left(str) + return substitute(a:str, '^\s*', '', 'g') +endfunction + +function! s:trim_right(str) + return substitute(a:str, '\s*$', '', 'g') +endfunction diff --git a/vim/autoload/himalaya/ui.vim b/vim/autoload/himalaya/ui.vim deleted file mode 100644 index a6e1a87..0000000 --- a/vim/autoload/himalaya/ui.vim +++ /dev/null @@ -1,238 +0,0 @@ -let s:compose = function('himalaya#utils#compose') -let s:trim = function('himalaya#utils#trim') -let s:print_msg = function('himalaya#utils#print_msg') -let s:print_err = function('himalaya#utils#print_err') - -let s:max_widths = [] -let s:buff_name = 'Himalaya' -let s:msgs = [] - -let s:config = { - \'list': { - \'columns': ['uid', 'subject', 'sender', 'date'], - \}, - \'labels': { - \'uid': 'ID', - \'subject': 'SUBJECT', - \'sender': 'SENDER', - \'date': 'DATE', - \}, -\} - -function! himalaya#ui#list() - try - let prev_pos = getpos('.') - let s:msgs = himalaya#msg#list() - let lines = map(copy(s:msgs), 'himalaya#msg#format_for_list(v:val)') - - redir => buf_list | silent! ls | redir END - execute 'silent! edit ' . s:buff_name - - if match(buf_list, '"Himalaya') > -1 - execute '0,$d' - endif - - call append(0, s:render('list', lines)) - execute '$d' - call setpos('.', prev_pos) - setlocal filetype=himalaya-list - let &modified = 0 - echo - catch - call s:print_err(v:exception) - endtry -endfunction - -" Cell management - -function! himalaya#ui#select_next_cell() - normal! f|l - - if col('.') == col('$') - 1 - if line('.') == line('$') - normal! T| - else - normal! j0l - endif - endif -endfunction - -function! himalaya#ui#select_prev_cell() - if col('.') == 2 && line('.') > 2 - normal! k$T| - else - normal! 2T| - endif -endfunction - -function! himalaya#ui#delete_in_cell() - execute printf('normal! %sdt|', col('.') == 1 ? '' : 'T|') -endfunction - -function! himalaya#ui#change_in_cell() - call himalaya#ui#delete_in_cell() - startinsert -endfunction - -function! himalaya#ui#visual_in_cell() - execute printf('normal! %svt|', col('.') == 1 ? '' : 'T|') -endfunction - -" Parse utils - -function! himalaya#ui#parse_buffer() - " try - " let lines = filter(getline(2, "$"), "!empty(s:trim(v:val))") - " let prev_msgs = copy(s:msgs) - " let next_msgs = map(lines, "s:parse_buffer_line(v:key, v:val)") - " let msgs_to_add = filter(copy(next_msgs), "empty(v:val.id)") - " let msgs_to_edit = [] - " let msgs_to_do = [] - " let msgs = [] - - " for prev_msg in prev_msgs - " let next_msg = filter(copy(next_msgs), "v:val.id == prev_msg.id") - - " if empty(next_msg) - " let msgs_to_do += [prev_msg.id] - " elseif prev_msg.desc != next_msg[0].desc || prev_msg.project != next_msg[0].project || prev_msg.due.approx != next_msg[0].due - " let msgs_to_edit += [next_msg[0]] - " endif - " endfor - - " for msg in msgs_to_add | let msgs += [himalaya#msg#add(msg)] | endfor - " for msg in msgs_to_edit | let msgs += [himalaya#msg#edit(msg)] | endfor - " for id in msgs_to_do | let msgs += [himalaya#msg#do(id)] | endfor - - " call himalaya#ui#list() - " let &modified = 0 - " for msg in msgs | call s:print_msg(msg) | endfor - " catch - " call s:print_err(v:exception) - " endtry -endfunction - -function! s:parse_buffer_line(index, line) - if match(a:line, '^|[0-9a-f\-]\{-} *|.* *|.\{-} *|.\{-} *|.\{-} *|$') != -1 - let cells = split(a:line, "|") - let id = s:trim(cells[0]) - let desc = s:trim(join(cells[1:-4], "")) - let project = s:trim(cells[-3]) - let due = s:trim(cells[-1]) - - return { - \"id": id, - \"desc": desc, - \"project": project, - \"due": due, - \} - else - let [desc, project, due] = s:parse_args(s:trim(a:line)) - - return { - \"id": "", - \"desc": desc, - \"project": project, - \"due": due, - \} - endif -endfunction - -function! s:uniq_by_id(a, b) - if a:a.id > a:b.id | return 1 - elseif a:a.id < a:b.id | return -1 - else | return 0 | endif -endfunction - -function! s:parse_args(args) - let args = split(a:args, ' ') - - let idx = 0 - let desc = [] - let project = "" - let due = "" - - while idx < len(args) - let arg = args[idx] - - if arg == "-p" || arg == "--project" - let project = get(args, idx + 1, "") - let idx = idx + 1 - elseif arg == "-d" || arg == "--due" - let due = get(args, idx + 1, "") - let idx = idx + 1 - else - call add(desc, arg) - endif - - let idx = idx + 1 - endwhile - - return [join(desc, ' '), project, due] -endfunction - -" ------------------------------------------------------------------ # Renders # - -function! s:render(type, lines) - let s:max_widths = s:get_max_widths(a:lines, s:config[a:type].columns) - let header = [s:render_line(s:config.labels, s:max_widths, a:type)] - let line = map(copy(a:lines), 's:render_line(v:val, s:max_widths, a:type)') - - return header + line -endfunction - -function! s:render_line(line, max_widths, type) - return '|' . join(map( - \copy(s:config[a:type].columns), - \'s:render_cell(a:line[v:val], a:max_widths[v:key])', - \), '') -endfunction - -function! s:render_cell(cell, max_width) - let cell_width = strdisplaywidth(a:cell[:a:max_width]) - return a:cell[:a:max_width] . repeat(' ', a:max_width - cell_width) . ' |' -endfunction - -" -------------------------------------------------------------------- # Utils # - -function! s:get_max_widths(msgs, columns) - let max_widths = map(copy(a:columns), 'strlen(s:config.labels[v:val])') - - for msg in a:msgs - let widths = map(copy(a:columns), 'strlen(msg[v:val])') - call map(max_widths, 'max([widths[v:key], v:val])') - endfor - - return max_widths -endfunction - -function! s:get_focused_msg_id() - try - return s:trim(split(getline("."), "|")[0]) - catch - throw "msg not found" - endtry -endfunction - -function! s:refresh_buff_name() - let buff_name = 'Himalaya' - - if !g:himalaya_hide_done - let buff_name .= '*' - endif - - if len(g:himalaya_context) > 0 - let tags = map(copy(g:himalaya_context), 'printf(" +%s", v:val)') - let buff_name .= join(tags, '') - endif - - if buff_name != s:buff_name - execute 'silent! enew' - execute 'silent! bwipeout ' . s:buff_name - let s:buff_name = buff_name - endif -endfunction - -function! s:exists_in(list, item) - return index(a:list, a:item) > -1 -endfunction diff --git a/vim/autoload/himalaya/utils.vim b/vim/autoload/himalaya/utils.vim deleted file mode 100644 index b7b7f92..0000000 --- a/vim/autoload/himalaya/utils.vim +++ /dev/null @@ -1,87 +0,0 @@ -" ------------------------------------------------------------------ # Compose # - -function! himalaya#utils#compose(...) - let funcs = map(reverse(copy(a:000)), 'function(v:val)') - return function('s:compose', [funcs]) -endfunction - -function! s:compose(funcs, arg) - let data = a:arg - - for Func in a:funcs - let data = Func(data) - endfor - - return data -endfunction - -" --------------------------------------------------------------------- # Trim # - -function! himalaya#utils#trim(str) - return himalaya#utils#compose('s:trim_left', 's:trim_right')(a:str) -endfunction - -function! s:trim_left(str) - return substitute(a:str, '^\s*', '', 'g') -endfunction - -function! s:trim_right(str) - return substitute(a:str, '\s*$', '', 'g') -endfunction - -" ------------------------------------------------------------------- # Assign # - -function! himalaya#utils#assign(...) - let overrides = copy(a:000) - let base = remove(overrides, 0) - - for override in overrides - for [key, val] in items(override) - let base[key] = val - unlet key val - endfor - endfor - - return base -endfunction - -" ---------------------------------------------------------------------- # Sum # - -function! himalaya#utils#sum(array) - let total = 0 - - for item in a:array - let total += item - endfor - - return total -endfunction - -" ----------------------------------------------------------- # Match one item # - -function! himalaya#utils#match_one(list_src, list_dest) - if empty(a:list_dest) - return 1 - endif - - for item in a:list_src - if index(a:list_dest, item) > -1 | return 1 | endif - endfor - - return 0 -endfunction - - -" --------------------------------------------------------------------- # Logs # - -function! himalaya#utils#print_msg(msg) - echohl None - echom a:msg -endfunction - -function! himalaya#utils#print_err(err) - redraw - echohl ErrorMsg - echom a:err - echohl None -endfunction diff --git a/vim/ftplugin/himalaya-msg-list.vim b/vim/ftplugin/himalaya-msg-list.vim index d4a4f5b..3ffddc4 100644 --- a/vim/ftplugin/himalaya-msg-list.vim +++ b/vim/ftplugin/himalaya-msg-list.vim @@ -8,7 +8,7 @@ nnoremap q :bwipeout nnoremap :bwipeout nnoremap :bwipeout -call himalaya#shared#define_bindings([ +call himalaya#shared#bindings#define([ \["n", "gm" , "mbox#input" ], \["n", "gp" , "mbox#prev_page" ], \["n", "gn" , "mbox#next_page" ], diff --git a/vim/ftplugin/himalaya-msg-read.vim b/vim/ftplugin/himalaya-msg-read.vim index 9dda7f6..b85862a 100644 --- a/vim/ftplugin/himalaya-msg-read.vim +++ b/vim/ftplugin/himalaya-msg-read.vim @@ -1,7 +1,7 @@ setlocal bufhidden=wipe setlocal buftype=nofile setlocal cursorline -setlocal foldexpr=himalaya#shared#thread_fold(v:lnum) +setlocal foldexpr=himalaya#shared#thread#fold(v:lnum) setlocal foldlevel=0 setlocal foldlevelstart=0 setlocal foldmethod=expr @@ -9,7 +9,7 @@ setlocal nomodifiable setlocal nowrap setlocal startofline -call himalaya#shared#define_bindings([ +call himalaya#shared#bindings#define([ \["n", "gw", "msg#write" ], \["n", "gr", "msg#reply" ], \["n", "gR", "msg#reply_all" ], diff --git a/vim/ftplugin/himalaya-msg-write.vim b/vim/ftplugin/himalaya-msg-write.vim index ab4c1ec..fe81ea9 100644 --- a/vim/ftplugin/himalaya-msg-write.vim +++ b/vim/ftplugin/himalaya-msg-write.vim @@ -1,5 +1,5 @@ setlocal cursorline -setlocal foldexpr=himalaya#shared#thread_fold(v:lnum) +setlocal foldexpr=himalaya#shared#thread#fold(v:lnum) setlocal foldlevel=0 setlocal foldlevelstart=0 setlocal foldmethod=expr diff --git a/vim/syntax/himalaya-msg-list.vim b/vim/syntax/himalaya-msg-list.vim index a4ce7dc..4225e2f 100644 --- a/vim/syntax/himalaya-msg-list.vim +++ b/vim/syntax/himalaya-msg-list.vim @@ -9,7 +9,7 @@ syntax match hya_subject /^|.\{-}|.\{-}|.\{-}|/ contains=hya_uid,hya syntax match hya_sender /^|.\{-}|.\{-}|.\{-}|.\{-}|/ contains=hya_uid,hya_flags,hya_subject,hya_sep syntax match hya_date /^|.\{-}|.\{-}|.\{-}|.\{-}|.\{-}|/ contains=hya_uid,hya_flags,hya_subject,hya_sender,hya_sep syntax match hya_head /.*\%1l/ contains=hya_sep -syntax match hya_unseen /^|.\{-}|🟓.*$/ contains=hya_sep +syntax match hya_unseen /^|.\{-}|N.*$/ contains=hya_sep highlight default link hya_sep VertSplit highlight default link hya_uid Identifier