-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngn-1.6.el
146 lines (119 loc) · 4.19 KB
/
ngn-1.6.el
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
;;; ngn.el --- Quickly insert a newsgroup name.
;; Copyright 2000-2017 by Dave Pearson <davep@davep.org>
;; Author: Dave Pearson <davep@davep.org>
;; Version: 1.6
;; Keywords: convenience, news, gnus, newsrc
;; URL: https://github.com/davep/ngn.el
;; Package-Requires: ((cl-lib "0.5"))
;; ngn.el is free software distributed under the terms of the GNU General
;; Public Licence, version 2 or (at your option) any later version. For
;; details see the file COPYING.
;;; Commentary:
;;
;; ngn.el provides commands for quickly inserting a newsgroup name into a
;; buffer, using complention, building the list from your newsgroup name
;; sources.
;;
;; The latest ngn.el is always available from:
;;
;; <URL:https://github.com/davep/ngn.el>
;;; TODO:
;;
;; o Do away with `ngn-newsrc' and `ngn-merge-with-gnus-newsrc' and,
;; instead, provide a flexible method of defining multiple sources for group
;; names.
;;; Code:
;; Things we need:
(eval-when-compile
(require 'cl-lib))
;; Customize options.
(defgroup ngn nil
"Newgroup name lookup and recall."
:group 'convenience
:prefix "ngn-")
(defcustom ngn-newsrc "~/.newsrc"
"Name of your newsrc file."
:type 'file
:group 'ngn)
(defcustom ngn-cache-p t
"Should we cache the newsgroup names?"
:type 'boolean
:group 'ngn)
(defcustom ngn-must-exist t
"Must the input group name be a known group?"
:type 'boolean
:group 'ngn)
(defcustom ngn-url-format-function #'(lambda (group)
(format "<URL:news:%s>" group))
"Function to format a newsgroup name as an URL."
:type 'function
:group 'ngn)
;; Non customising variables.
(defvar ngn-history nil
"History list for `ngn-reader'.")
(defvar ngn-cache nil
"Newsgroup name cache.")
;; Main code:
(defun ngn-merge-with-gnus-newsrc (groups)
"Merge the existing group list GROUPS with the Gnus group list (if available)."
(if (boundp 'gnus-newsrc-alist)
(append (cl-loop for group in (mapcar #'car (symbol-value 'gnus-newsrc-alist))
unless (assoc group groups)
collect (list group))
groups)
groups))
(defun ngn-load-group-names ()
"Load the newsgroup list for use with `completing-read'."
(if (and ngn-cache ngn-cache-p)
ngn-cache
(setq ngn-cache
(ngn-merge-with-gnus-newsrc
(when (file-readable-p ngn-newsrc)
(with-temp-buffer
(save-excursion
(insert-file-contents-literally ngn-newsrc)
(while (re-search-forward "[:!].*$" nil t)
(replace-match "")))
(cl-loop until (eobp)
collect (list (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
do (forward-line))))))))
(defun ngn-reader ()
"Prompt the user for a newsgroup name, using completion."
(let ((default (car ngn-history)))
(completing-read (format "Newsgroup%s: " (if default
(format " (default %s)" default)
""))
(ngn-load-group-names)
nil
ngn-must-exist
""
'ngn-history
default)))
;;;###autoload
(defun ngn-clear-cache ()
"Clear the newsgroup name cache."
(interactive)
(setq ngn-cache nil))
;;;###autoload
(defun ngn-insert-name (groupname)
"Insert GROUPNAME into the current buffer."
(interactive (list (ngn-reader)))
(insert groupname))
;;;###autoload
(defun ngn-insert-url (groupname)
"Insert GROUPNAME, as an URL, into the current buffer."
(interactive (list (ngn-reader)))
(insert (funcall ngn-url-format-function groupname)))
;;;###autoload
(defun ngn-insert (no-format)
"Insert a newsgroup name, formatting the name depending on the prefix.
If NO-FORMAT is 't' (or the command is invoked with
\\[universal-argument]) then `ngn-insert-url' is used to insert
the group name. If a prefix is provided then `ngn-insert-name' is
used."
(interactive "P")
(call-interactively (if no-format #'ngn-insert-name #'ngn-insert-url)))
(provide 'ngn)
;;; ngn.el ends here