domo141 / nottoomuch

Misc material I use with notmuch mail indexer
31 stars 3 forks source link

feature request (?) : add single email address to addresses.active #5

Open pseudomyne opened 7 years ago

pseudomyne commented 7 years ago

hi ! Thank you for your script, that i use daily. I'm not a lisper, I just pick code here and there. For now, I'm looking for a way to add single mail addresses to my database (addresses.active). I've found this piece of code https://github.com/dimitri/emacs.d/blob/master/dim-mailrc.el Works well. I thought I could adapt it to the situation, but it looks beyond my skills. What I'd like is emacs to return email address at point, then prompt for an alias, and append them to addresses.active. Would you have a moment to give a hand ? Thanks again !

domo141 commented 7 years ago

that should be pretty easy: in temp buffer, loading the file, appending content there and save it should only be a few lines of code (four ;). I'll look this interesting thing in soon...

there is another problem: (iirc) --update will overwrite addresses.active and these new addresses would be lost. perhaps a new file: 'addresses.priority' could be added, and contents of that file are prepended to addresses.active whenever --update is used -- that would make that addition in emacs a bit more complex (but notmuch!)

domo141 commented 7 years ago

I just committed version of nottoomuch-addresses.sh (to the dogfood branch) which reads 'addresses.top' and puts the contents of it to the top of 'addresses.active' list (whenever --update is done). I'll be doing something more there in near future and will experiment with that lisp stuff...

domo141 commented 7 years ago

gist or something could be there, but...

(require 'thingatpt)
(require 'mail-parse)

;; based on https://github.com/dimitri/emacs.d/blob/master/dim-mailrc.el

(defun thing-at-point-bounds-of-email-address ()
  "return a cons of begin and end position of email address at point, including full name"
  (save-excursion
    (let* ((search-point (point))
       (start (re-search-backward "[:,]" (line-beginning-position) 'move))
       (dummy (goto-char search-point))
       (end   (re-search-forward  "[:,]" (line-end-position) t)))
      (setq start (if start (+ 1 start)
            (line-beginning-position)))
      (unless end (setq end (line-end-position)))
      (cons start end))))

(defun thing-at-point-email-address ()
  "return full email address at point"
  (let* ((bounds (thing-at-point-bounds-of-email-address))
     (email-address-text
      (when bounds (buffer-substring-no-properties (car bounds) (cdr bounds)))))
    (mail-header-parse-address email-address-text)))

(put 'email-address 'thing-at-point 'thing-at-point-email-address)

(defun user:add-nottoomuch-email-address ()
  "read email address at point and add it to ~/.config/nottoomuch/addresses.top"
  (interactive)
  (let* ((afile "~/.config/nottoomuch/addresses.top")
     (acons (thing-at-point 'email-address))
     (address (format "%s <%s>" (cdr acons) (car acons)))
     (buffer (find-file-noselect afile)))
    (when address
      (with-current-buffer buffer
    ;; don't append email addrss if it is already there
    (save-excursion
      (goto-char (point-min))
      (if (search-forward address nil t)
          (error "Address %s is already present in %s" address afile)))
    (if (y-or-n-p (format "Add %s to %s" address afile))
        (save-current-buffer
          (save-excursion
        (goto-char (point-max))
        (insert address))))))))

(global-set-key (kbd "C-c C-@") 'user:add-nottoomuch-email-address)
pseudomyne commented 7 years ago

Hi Domo ! Glad to see you back on that. Been trying your script lately with partial success. When I try to add a mail address from the head of a thread in some notmuch show, it sometimes returns the following error : forward-sexp: Scan error: "Unbalanced parentheses", 53, 63 Note that if I kill and yank the address in another buffer and run the script on it again, it works. So it seems to be related to the notmuch «header».

2° In some cases, I’d like to add a mail address from a single xxx@xmail.com form, wishing to add the name for it manually (getting a name instead of , or in place of the proposed name). So I guess it would be nice to include the option to add a name by hand, within a second prompt (it could guess for the name by default).

3° Some nice feat to my taste would also be that addresses.sh «updates» itself without having to run it by hand ; at least, if this appears tricky, that the "update" does not browse for all the new mail addresses. Indeed, there are lots of junk mail addresses I don’t want to be added in my address list.