all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 4f2559b6fc154dd757a7facb61e0ad98468556b6 13992 bytes (raw)
name: contrib/lisp/org-mew.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
 
;;; org-mew.el --- Support for links to Mew messages from within Org-mode

;; Copyright (C) 2008-2016 Free Software Foundation, Inc.

;; Author: Tokuya Kameshima <kames at fa2 dot so-net dot ne dot jp>
;; Keywords: outlines, hypermedia, calendar, wp
;; Homepage: http://orgmode.org

;; This file is not part of GNU Emacs.

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:

;; This file implements links to Mew messages from within Org-mode.
;; Org-mode loads this module by default - if this is not what you want,
;; configure the variable `org-modules'.
;;
;; Here is an example of workflow:

;; In your ~/.mew.el configuration file:
;;
;; (define-key mew-summary-mode-map "'" 'org-mew-search)
;; (eval-after-load "mew-summary"
;;   '(define-key mew-summary-mode-map "\C-o" 'org-mew-capture))

;; 1. In the Mew's inbox folder, take a glance at new messages to find
;;    a message that requires any action.

;; 2. If the message is a reply from somebody and associated with the
;;    existing orgmode entry, type M-x `org-mew-search' RET (or press
;;    the ' key simply) to find the entry.  If you can find the entry
;;    successfully and think you should start the task right now,
;;    start the task by M-x `org-agenda-clock-in' RET.

;; 3. If the message is a new message, type M-x `org-mew-capture' RET,
;;    enter the refile folder, and the buffer to capture the message
;;    is shown up (without selecting the template by hand).  Then you
;;    can fill the template and type C-c C-c to complete the capture.
;;    Note that you can configure `org-capture-templates' so that the
;;    captured entry has a link to the message.

;;; Code:

(require 'org)

(defgroup org-mew nil
  "Options concerning the Mew link."
  :tag "Org Startup"
  :group 'org-link)

(defcustom org-mew-link-to-refile-destination t
  "Create a link to the refile destination if the message is marked as refile."
  :group 'org-mew
  :type 'boolean)

(defcustom org-mew-inbox-folder nil
  "The folder where new messages are incorporated.
If `org-mew-inbox-folder' is non-nil, `org-mew-open' locates the message
in this inbox folder as well as the folder specified by the link."
  :group 'org-mew
  :type 'string)

(defcustom org-mew-use-id-db t
  "Use ID database to locate the message if id.db is created."
  :group 'org-mew
  :type 'boolean)

(defcustom org-mew-subject-alist
  (list (cons (concat "^\\(?:\\(?:re\\|fwd?\\): *\\)*"
		      "\\(?:[[(][a-z0-9._-]+[:,]? [0-9]+[])]\\)? *"
		      "\\(?:\\(?:re\\|fwd?\\): *\\)*"
		      "\\(.*\\)[ \t]*")
	      1))
  "Alist of subject regular expression and matched group number for search."
  :group 'org-mew
  :type '(repeat (cons (regexp) (integer))))

(defcustom org-mew-capture-inbox-folders nil
  "List of inbox folders whose messages need refile marked before capture.
`org-mew-capture' will ask you to put the refile mark on the
message if the message's folder is any of these folders and the
message is not marked.  Nil means `org-mew-capture' never ask you
destination folders before capture."
  :group 'org-mew
  :type '(repeat string))

(defcustom org-mew-capture-guess-alist nil
  "Alist of the regular expression of the folder name and the capture
template selection keys.

For example,
    '((\"^%emacs-orgmode$\" . \"o\")
      (\"\" . \"t\"))
the messages in \"%emacs-orgmode\" folder will be captured with
the capture template associated with \"o\" key, and any other
messages will be captured with the capture template associated
with \"t\" key."
  :group 'org-mew
  :type '(repeat (cons regexp string)))

;; Declare external functions and variables
(declare-function mew-cache-hit "ext:mew-cache" (fld msg &optional must-hit))
(declare-function mew-case-folder "ext:mew-func" (case folder))
(declare-function mew-folder-path-to-folder
		  "ext:mew-func" (path &optional has-proto))
(declare-function mew-idstr-to-id-list "ext:mew-header" (idstr &optional rev))
(declare-function mew-folder-remotep "ext:mew-func" (folder))
(declare-function mew-folder-virtualp "ext:mew-func" (folder))
(declare-function mew-header-get-value "ext:mew-header"
		  (field &optional as-list))
(declare-function mew-init "ext:mew" ())
(declare-function mew-refile-get "ext:mew-refile" (msg))
(declare-function mew-sinfo-get-case "ext:mew-summary" ())
(declare-function mew-summary-diag-global "ext:mew-thread" (id opt who))
(declare-function mew-summary-display "ext:mew-summary2" (&optional redisplay))
(declare-function mew-summary-folder-name "ext:mew-syntax" (&optional ext))
(declare-function mew-summary-get-mark "ext:mew-mark" ())
(declare-function mew-summary-message-number2 "ext:mew-syntax" ())
(declare-function mew-summary-pick-with-mewl "ext:mew-pick"
		  (pattern folder src-msgs))
(declare-function mew-summary-refile "ext:mew-refile" (&optional report))
(declare-function mew-summary-search-msg "ext:mew-const" (msg))
(declare-function mew-summary-set-message-buffer "ext:mew-summary3" (fld msg))
(declare-function mew-summary-visit-folder "ext:mew-summary4"
		  (folder &optional goend no-ls))
(declare-function mew-window-push "ext:mew" ())
(declare-function mew-expand-folder "ext:mew-func" (folder))
(declare-function mew-case:folder-folder "ext:mew-func" (case:folder))
(declare-function mew "ext:mew" (&optional arg))
(declare-function mew-message-goto-summary "ext:mew-message" ())
(declare-function mew-summary-mode "ext:mew-summary" ())

(defvar mew-init-p)
(defvar mew-mark-afterstep-spec)
(defvar mew-summary-goto-line-then-display)

;; Install the link type
(org-add-link-type "mew" 'org-mew-open)
(add-hook 'org-store-link-functions 'org-mew-store-link)

;; Implementation
(defun org-mew-store-link ()
  "Store a link to a Mew folder or message."
  (save-window-excursion
    (if (eq major-mode 'mew-message-mode)
	(mew-message-goto-summary))
    (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
      (let ((msgnum (mew-summary-message-number2))
	    (folder-name (org-mew-folder-name)))
	(if (fboundp 'mew-summary-set-message-buffer)
	    (mew-summary-set-message-buffer folder-name msgnum)
	  (set-buffer (mew-cache-hit folder-name msgnum t)))
	(let* ((message-id (mew-header-get-value "Message-Id:"))
	       (from (mew-header-get-value "From:"))
	       (to (mew-header-get-value "To:"))
	       (date (mew-header-get-value "Date:"))
	       (subject (mew-header-get-value "Subject:"))
	       desc link)
	  (org-store-link-props :type "mew" :from from :to to :date date
				:subject subject :message-id message-id)
	  (setq message-id (org-remove-angle-brackets message-id))
	  (setq desc (org-email-link-description))
	  (setq link (concat "mew:" folder-name "#" message-id))
	  (org-add-link-props :link link :description desc)
	  link)))))

(defun org-mew-folder-name ()
  "Return the folder name of the current message."
  (save-window-excursion
    (if (eq major-mode 'mew-message-mode)
	(mew-message-goto-summary))
    (let* ((msgnum (mew-summary-message-number2))
	   (mark-info (mew-summary-get-mark)))
      (if (and org-mew-link-to-refile-destination
	       (eq mark-info ?o))	; marked as refile
	  (mew-case-folder (mew-sinfo-get-case)
			   (nth 1 (mew-refile-get msgnum)))
	(let ((folder-or-path (mew-summary-folder-name)))
	  (mew-folder-path-to-folder folder-or-path t))))))

(defun org-mew-open (path)
  "Follow the Mew message link specified by PATH."
  (let (folder message-id)
    (cond ((string-match "\\`\\(+.*\\)+\\+\\([0-9]+\\)\\'" path) ; for Bastien's
	   (setq folder (match-string 1 path))
	   (setq message-id (match-string 2 path)))
	  ((string-match "\\`\\(\\(%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path)
	   (setq folder (match-string 1 path))
	   (setq message-id (match-string 4 path)))
	  ((and org-mew-use-id-db (string-match "\\`#\\(.+\\)" path))
	   (setq folder nil)
	   (setq message-id (match-string 1 path)))
	  (t (error "Error in Mew link")))
    (require 'mew)
    (mew-window-push)
    (unless mew-init-p (mew-init))
    (if (null folder)
	(progn
	  (mew t)
	  (org-mew-open-by-message-id message-id))
      (or (org-mew-follow-link folder message-id)
	  (and org-mew-inbox-folder (not (string= org-mew-inbox-folder folder))
	       (org-mew-follow-link org-mew-inbox-folder message-id))
	  (and org-mew-use-id-db
	       (org-mew-open-by-message-id message-id))
	  (error "Message not found")))))

(defun org-mew-follow-link (folder message-id)
  (unless (org-mew-folder-exists-p folder)
    (error "No such folder or wrong folder %s" folder))
  (mew-summary-visit-folder folder)
  (when message-id
    (let ((msgnum (org-mew-get-msgnum folder message-id)))
      (when (mew-summary-search-msg msgnum)
	(if mew-summary-goto-line-then-display
	    (mew-summary-display))
	t))))

(defun org-mew-folder-exists-p (folder)
  (let ((dir (mew-expand-folder folder)))
    (cond
     ((mew-folder-virtualp folder) (get-buffer folder))
     ((null dir) nil)
     ((mew-folder-remotep (mew-case:folder-folder folder)) t)
     (t (file-directory-p dir)))))

(defun org-mew-get-msgnum (folder message-id)
  (if (string-match "\\`[0-9]+\\'" message-id)
      message-id
    (let* ((pattern (concat "message-id=" message-id))
	   (msgs (mew-summary-pick-with-mewl pattern folder nil)))
      (car msgs))))

(defun org-mew-open-by-message-id (message-id)
  "Open message using ID database."
  (let ((result (mew-summary-diag-global (format "<%s>" message-id)
					 "-p" "Message")))
    (unless (eq result t)
      (error "Message not found"))))

;; In ~/.mew.el, add the following line:
;;   (define-key mew-summary-mode-map "'" 'org-mew-search)
(defun org-mew-search (&optional arg)
  "Show all entries related to the message using `org-search-view'.

It shows entries which contains the message ID, the reference
IDs, or the subject of the message.

With C-u prefix, search for the entries that contains the message
ID or any of the reference IDs.  With C-u C-u prefix, search for
the message ID or the last reference ID.

The search phase for the subject is extracted with
`org-mew-subject-alist', which defines the regular expression of
the subject and the group number to extract.  You can get rid of
\"Re:\" and some other prefix from the subject text."
  (interactive "P")
  (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
    (let ((last-reference-only (equal arg '(16)))
	  (by-subject (null arg))
	  (msgnum (mew-summary-message-number2))
	  (folder-name (mew-summary-folder-name))
	  subject message-id references id-list)
      (save-window-excursion
	(if (fboundp 'mew-summary-set-message-buffer)
	    (mew-summary-set-message-buffer folder-name msgnum)
	  (set-buffer (mew-cache-hit folder-name msgnum t)))
	(setq subject (mew-header-get-value "Subject:"))
	(setq message-id (mew-header-get-value "Message-Id:"))
	(setq references (mew-header-get-value "References:")))
      (setq id-list (mapcar (lambda (id) (org-remove-angle-brackets id))
			    (mew-idstr-to-id-list references)))
      (if last-reference-only
	  (setq id-list (last id-list))
	(if message-id
	    (setq id-list (cons (org-remove-angle-brackets message-id)
				id-list))))
      (when (and by-subject (stringp subject))
	(catch 'matched
	  (mapc (lambda (elem)
		  (let ((regexp (car elem))
			(num (cdr elem)))
		    (when (string-match regexp subject)
		      (setq subject (match-string num subject))
		      (throw 'matched t))))
		org-mew-subject-alist))
	(setq id-list (cons subject id-list)))
      (cond ((null id-list)
	     (error "No message ID to search"))
	    ((equal (length id-list) 1)
	     (org-search-view nil (car id-list)))
	    (t
	     (org-search-view nil (format "{\\(%s\\)}"
					  (mapconcat 'regexp-quote
						     id-list "\\|"))))))
    (delete-other-windows)))

(defun org-mew-capture (arg)
  "Guess the capture template from the folder name and invoke `org-capture'.

This selects a capture template in `org-capture-templates' by
searching for capture template selection keys defined in
`org-mew-capture-guess-alist' which are associated with the
regular expression that matches the message's folder name, and
then invokes `org-capture'.

If the message's folder is a inbox folder, you are prompted to
put the refile mark on the message and the capture template is
guessed from the refile destination folder.  You can customize
the inbox folders by `org-mew-capture-inbox-folders'.

If ARG is non-nil, this does not guess the capture template but
asks you to select the capture template."
  (interactive "P")
  (or (not (member (org-mew-folder-name)
		   org-mew-capture-inbox-folders))
      (eq (mew-summary-get-mark) ?o)
      (save-window-excursion
	(if (eq major-mode 'mew-message-mode)
	    (mew-message-goto-summary))
	(let ((mew-mark-afterstep-spec '((?o 0 0 0 0 0 0 0))))
	  (mew-summary-refile)))
      (error "No refile folder selected"))
  (let* ((org-mew-link-to-refile-destination t)
	 (folder-name (org-mew-folder-name))
	 (keys (if arg
		   nil
		 (org-mew-capture-guess-selection-keys folder-name))))
    (org-capture nil keys)))

(defun org-mew-capture-guess-selection-keys (folder-name)
  (catch 'found
    (let ((alist org-mew-capture-guess-alist))
      (while alist
	(let ((elem (car alist)))
	  (if (string-match (car elem) folder-name)
	      (throw 'found (cdr elem))))
	(setq alist (cdr alist))))))

(provide 'org-mew)

;;; org-mew.el ends here

debug log:

solving 4f2559b ...
found 4f2559b in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.