gnus-util.el 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  1. ;;; gnus-util.el --- utility functions for Gnus
  2. ;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: news
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Nothing in this file depends on any other parts of Gnus -- all
  18. ;; functions and macros in this file are utility functions that are
  19. ;; used by Gnus and may be used by any other package without loading
  20. ;; Gnus first.
  21. ;; [Unfortunately, it does depend on other parts of Gnus, e.g. the
  22. ;; autoloads and defvars below...]
  23. ;;; Code:
  24. ;; For Emacs <22.2 and XEmacs.
  25. (eval-and-compile
  26. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  27. (eval-when-compile
  28. (require 'cl))
  29. (require 'time-date)
  30. (defcustom gnus-completing-read-function 'gnus-emacs-completing-read
  31. "Function use to do completing read."
  32. :version "24.1"
  33. :group 'gnus-meta
  34. :type `(radio (function-item
  35. :doc "Use Emacs standard `completing-read' function."
  36. gnus-emacs-completing-read)
  37. ;; iswitchb.el is very old and ido.el is unavailable
  38. ;; in XEmacs, so we exclude those function items.
  39. ,@(unless (featurep 'xemacs)
  40. '((function-item
  41. :doc "Use `ido-completing-read' function."
  42. gnus-ido-completing-read)
  43. (function-item
  44. :doc "Use iswitchb based completing-read function."
  45. gnus-iswitchb-completing-read)))))
  46. (defcustom gnus-completion-styles
  47. (if (and (boundp 'completion-styles-alist)
  48. (boundp 'completion-styles))
  49. (append (when (and (assq 'substring completion-styles-alist)
  50. (not (memq 'substring completion-styles)))
  51. (list 'substring))
  52. completion-styles)
  53. nil)
  54. "Value of `completion-styles' to use when completing."
  55. :version "24.1"
  56. :group 'gnus-meta
  57. :type 'list)
  58. ;; Fixme: this should be a gnus variable, not nnmail-.
  59. (defvar nnmail-pathname-coding-system)
  60. (defvar nnmail-active-file-coding-system)
  61. ;; Inappropriate references to other parts of Gnus.
  62. (defvar gnus-emphasize-whitespace-regexp)
  63. (defvar gnus-original-article-buffer)
  64. (defvar gnus-user-agent)
  65. (autoload 'gnus-get-buffer-window "gnus-win")
  66. (autoload 'nnheader-narrow-to-headers "nnheader")
  67. (autoload 'nnheader-replace-chars-in-string "nnheader")
  68. (autoload 'mail-header-remove-comments "mail-parse")
  69. (eval-and-compile
  70. (cond
  71. ;; Prefer `replace-regexp-in-string' (present in Emacs, XEmacs 21.5,
  72. ;; SXEmacs 22.1.4) over `replace-in-string'. The latter leads to inf-loops
  73. ;; on empty matches:
  74. ;; (replace-in-string "foo" "/*$" "/")
  75. ;; (replace-in-string "xe" "\\(x\\)?" "")
  76. ((fboundp 'replace-regexp-in-string)
  77. (defun gnus-replace-in-string (string regexp newtext &optional literal)
  78. "Replace all matches for REGEXP with NEWTEXT in STRING.
  79. If LITERAL is non-nil, insert NEWTEXT literally. Return a new
  80. string containing the replacements.
  81. This is a compatibility function for different Emacsen."
  82. (replace-regexp-in-string regexp newtext string nil literal)))
  83. ((fboundp 'replace-in-string)
  84. (defalias 'gnus-replace-in-string 'replace-in-string))))
  85. (defun gnus-boundp (variable)
  86. "Return non-nil if VARIABLE is bound and non-nil."
  87. (and (boundp variable)
  88. (symbol-value variable)))
  89. (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
  90. "Pop to BUFFER, evaluate FORMS, and then return to the original window."
  91. (let ((tempvar (make-symbol "GnusStartBufferWindow"))
  92. (w (make-symbol "w"))
  93. (buf (make-symbol "buf")))
  94. `(let* ((,tempvar (selected-window))
  95. (,buf ,buffer)
  96. (,w (gnus-get-buffer-window ,buf 'visible)))
  97. (unwind-protect
  98. (progn
  99. (if ,w
  100. (progn
  101. (select-window ,w)
  102. (set-buffer (window-buffer ,w)))
  103. (pop-to-buffer ,buf))
  104. ,@forms)
  105. (select-window ,tempvar)))))
  106. (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
  107. (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
  108. (defmacro gnus-intern-safe (string hashtable)
  109. "Get hash value. Arguments are STRING and HASHTABLE."
  110. `(let ((symbol (intern ,string ,hashtable)))
  111. (or (boundp symbol)
  112. (set symbol nil))
  113. symbol))
  114. (defsubst gnus-goto-char (point)
  115. (and point (goto-char point)))
  116. (defmacro gnus-buffer-exists-p (buffer)
  117. `(let ((buffer ,buffer))
  118. (when buffer
  119. (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
  120. buffer))))
  121. ;; The LOCAL arg to `add-hook' is interpreted differently in Emacs and
  122. ;; XEmacs. In Emacs we don't need to call `make-local-hook' first.
  123. ;; It's harmless, though, so the main purpose of this alias is to shut
  124. ;; up the byte compiler.
  125. (defalias 'gnus-make-local-hook (if (featurep 'xemacs)
  126. 'make-local-hook
  127. 'ignore))
  128. (defun gnus-delete-first (elt list)
  129. "Delete by side effect the first occurrence of ELT as a member of LIST."
  130. (if (equal (car list) elt)
  131. (cdr list)
  132. (let ((total list))
  133. (while (and (cdr list)
  134. (not (equal (cadr list) elt)))
  135. (setq list (cdr list)))
  136. (when (cdr list)
  137. (setcdr list (cddr list)))
  138. total)))
  139. ;; Delete the current line (and the next N lines).
  140. (defmacro gnus-delete-line (&optional n)
  141. `(delete-region (point-at-bol)
  142. (progn (forward-line ,(or n 1)) (point))))
  143. (defun gnus-byte-code (func)
  144. "Return a form that can be `eval'ed based on FUNC."
  145. (let ((fval (indirect-function func)))
  146. (if (byte-code-function-p fval)
  147. (let ((flist (append fval nil)))
  148. (setcar flist 'byte-code)
  149. flist)
  150. (cons 'progn (cddr fval)))))
  151. (defun gnus-extract-address-components (from)
  152. "Extract address components from a From header.
  153. Given an RFC-822 address FROM, extract full name and canonical address.
  154. Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). Much more simple
  155. solution than `mail-extract-address-components', which works much better, but
  156. is slower."
  157. (let (name address)
  158. ;; First find the address - the thing with the @ in it. This may
  159. ;; not be accurate in mail addresses, but does the trick most of
  160. ;; the time in news messages.
  161. (cond (;; Check ``<foo@bar>'' first in order to handle the quite common
  162. ;; form ``"abc@xyz" <foo@bar>'' (i.e. ``@'' as part of a comment)
  163. ;; correctly.
  164. (string-match "<\\([^@ \t<>]+[!@][^@ \t<>]+\\)>" from)
  165. (setq address (substring from (match-beginning 1) (match-end 1))))
  166. ((string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
  167. (setq address (substring from (match-beginning 0) (match-end 0)))))
  168. ;; Then we check whether the "name <address>" format is used.
  169. (and address
  170. ;; Linear white space is not required.
  171. (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
  172. (and (setq name (substring from 0 (match-beginning 0)))
  173. ;; Strip any quotes from the name.
  174. (string-match "^\".*\"$" name)
  175. (setq name (substring name 1 (1- (match-end 0))))))
  176. ;; If not, then "address (name)" is used.
  177. (or name
  178. (and (string-match "(.+)" from)
  179. (setq name (substring from (1+ (match-beginning 0))
  180. (1- (match-end 0)))))
  181. (and (string-match "()" from)
  182. (setq name address))
  183. ;; XOVER might not support folded From headers.
  184. (and (string-match "(.*" from)
  185. (setq name (substring from (1+ (match-beginning 0))
  186. (match-end 0)))))
  187. (list (if (string= name "") nil name) (or address from))))
  188. (defun gnus-extract-address-component-name (from)
  189. "Extract name from a From header.
  190. Uses `gnus-extract-address-components'."
  191. (nth 0 (gnus-extract-address-components from)))
  192. (defun gnus-extract-address-component-email (from)
  193. "Extract e-mail address from a From header.
  194. Uses `gnus-extract-address-components'."
  195. (nth 1 (gnus-extract-address-components from)))
  196. (declare-function message-fetch-field "message" (header &optional not-all))
  197. (defun gnus-fetch-field (field)
  198. "Return the value of the header FIELD of current article."
  199. (require 'message)
  200. (save-excursion
  201. (save-restriction
  202. (let ((inhibit-point-motion-hooks t))
  203. (nnheader-narrow-to-headers)
  204. (message-fetch-field field)))))
  205. (defun gnus-fetch-original-field (field)
  206. "Fetch FIELD from the original version of the current article."
  207. (with-current-buffer gnus-original-article-buffer
  208. (gnus-fetch-field field)))
  209. (defun gnus-goto-colon ()
  210. (beginning-of-line)
  211. (let ((eol (point-at-eol)))
  212. (goto-char (or (text-property-any (point) eol 'gnus-position t)
  213. (search-forward ":" eol t)
  214. (point)))))
  215. (declare-function gnus-find-method-for-group "gnus" (group &optional info))
  216. (declare-function gnus-group-name-decode "gnus-group" (string charset))
  217. (declare-function gnus-group-name-charset "gnus-group" (method group))
  218. ;; gnus-group requires gnus-int which requires message.
  219. (declare-function message-tokenize-header "message"
  220. (header &optional separator))
  221. (defun gnus-decode-newsgroups (newsgroups group &optional method)
  222. (require 'gnus-group)
  223. (let ((method (or method (gnus-find-method-for-group group))))
  224. (mapconcat (lambda (group)
  225. (gnus-group-name-decode group (gnus-group-name-charset
  226. method group)))
  227. (message-tokenize-header newsgroups)
  228. ",")))
  229. (defun gnus-remove-text-with-property (prop)
  230. "Delete all text in the current buffer with text property PROP."
  231. (let ((start (point-min))
  232. end)
  233. (unless (get-text-property start prop)
  234. (setq start (next-single-property-change start prop)))
  235. (while start
  236. (setq end (text-property-any start (point-max) prop nil))
  237. (delete-region start (or end (point-max)))
  238. (setq start (when end
  239. (next-single-property-change start prop))))))
  240. (defun gnus-find-text-property-region (start end prop)
  241. "Return a list of text property regions that has property PROP."
  242. (let (regions value)
  243. (unless (get-text-property start prop)
  244. (setq start (next-single-property-change start prop)))
  245. (while start
  246. (setq value (get-text-property start prop)
  247. end (text-property-not-all start (point-max) prop value))
  248. (if (not end)
  249. (setq start nil)
  250. (when value
  251. (push (list (set-marker (make-marker) start)
  252. (set-marker (make-marker) end)
  253. value)
  254. regions))
  255. (setq start (next-single-property-change start prop))))
  256. (nreverse regions)))
  257. (defun gnus-newsgroup-directory-form (newsgroup)
  258. "Make hierarchical directory name from NEWSGROUP name."
  259. (let* ((newsgroup (gnus-newsgroup-savable-name newsgroup))
  260. (idx (string-match ":" newsgroup)))
  261. (concat
  262. (if idx (substring newsgroup 0 idx))
  263. (if idx "/")
  264. (nnheader-replace-chars-in-string
  265. (if idx (substring newsgroup (1+ idx)) newsgroup)
  266. ?. ?/))))
  267. (defun gnus-newsgroup-savable-name (group)
  268. ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
  269. ;; with dots.
  270. (nnheader-replace-chars-in-string group ?/ ?.))
  271. (defun gnus-string> (s1 s2)
  272. (not (or (string< s1 s2)
  273. (string= s1 s2))))
  274. (defun gnus-string< (s1 s2)
  275. "Return t if first arg string is less than second in lexicographic order.
  276. Case is significant if and only if `case-fold-search' is nil.
  277. Symbols are also allowed; their print names are used instead."
  278. (if case-fold-search
  279. (string-lessp (downcase (if (symbolp s1) (symbol-name s1) s1))
  280. (downcase (if (symbolp s2) (symbol-name s2) s2)))
  281. (string-lessp s1 s2)))
  282. ;;; Time functions.
  283. (defun gnus-file-newer-than (file date)
  284. (let ((fdate (nth 5 (file-attributes file))))
  285. (or (> (car fdate) (car date))
  286. (and (= (car fdate) (car date))
  287. (> (nth 1 fdate) (nth 1 date))))))
  288. ;; Every version of Emacs Gnus supports has built-in float-time.
  289. ;; The featurep test silences an irritating compiler warning.
  290. (eval-and-compile
  291. (if (or (featurep 'emacs)
  292. (fboundp 'float-time))
  293. (defalias 'gnus-float-time 'float-time)
  294. (defun gnus-float-time (&optional time)
  295. "Convert time value TIME to a floating point number.
  296. TIME defaults to the current time."
  297. (time-to-seconds (or time (current-time))))))
  298. ;;; Keymap macros.
  299. (defmacro gnus-local-set-keys (&rest plist)
  300. "Set the keys in PLIST in the current keymap."
  301. `(gnus-define-keys-1 (current-local-map) ',plist))
  302. (defmacro gnus-define-keys (keymap &rest plist)
  303. "Define all keys in PLIST in KEYMAP."
  304. `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
  305. (defmacro gnus-define-keys-safe (keymap &rest plist)
  306. "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
  307. `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t))
  308. (put 'gnus-define-keys 'lisp-indent-function 1)
  309. (put 'gnus-define-keys-safe 'lisp-indent-function 1)
  310. (put 'gnus-local-set-keys 'lisp-indent-function 1)
  311. (defmacro gnus-define-keymap (keymap &rest plist)
  312. "Define all keys in PLIST in KEYMAP."
  313. `(gnus-define-keys-1 ,keymap (quote ,plist)))
  314. (put 'gnus-define-keymap 'lisp-indent-function 1)
  315. (defun gnus-define-keys-1 (keymap plist &optional safe)
  316. (when (null keymap)
  317. (error "Can't set keys in a null keymap"))
  318. (cond ((symbolp keymap)
  319. (setq keymap (symbol-value keymap)))
  320. ((keymapp keymap))
  321. ((listp keymap)
  322. (set (car keymap) nil)
  323. (define-prefix-command (car keymap))
  324. (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
  325. (setq keymap (symbol-value (car keymap)))))
  326. (let (key)
  327. (while plist
  328. (when (symbolp (setq key (pop plist)))
  329. (setq key (symbol-value key)))
  330. (if (or (not safe)
  331. (eq (lookup-key keymap key) 'undefined))
  332. (define-key keymap key (pop plist))
  333. (pop plist)))))
  334. (defun gnus-y-or-n-p (prompt)
  335. (prog1
  336. (y-or-n-p prompt)
  337. (message "")))
  338. (defun gnus-yes-or-no-p (prompt)
  339. (prog1
  340. (yes-or-no-p prompt)
  341. (message "")))
  342. ;; By Frank Schmitt <ich@Frank-Schmitt.net>. Allows to have
  343. ;; age-depending date representations. (e.g. just the time if it's
  344. ;; from today, the day of the week if it's within the last 7 days and
  345. ;; the full date if it's older)
  346. (defun gnus-seconds-today ()
  347. "Return the number of seconds passed today."
  348. (let ((now (decode-time (current-time))))
  349. (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600))))
  350. (defun gnus-seconds-month ()
  351. "Return the number of seconds passed this month."
  352. (let ((now (decode-time (current-time))))
  353. (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)
  354. (* (- (car (nthcdr 3 now)) 1) 3600 24))))
  355. (defun gnus-seconds-year ()
  356. "Return the number of seconds passed this year."
  357. (let ((now (decode-time (current-time)))
  358. (days (format-time-string "%j" (current-time))))
  359. (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)
  360. (* (- (string-to-number days) 1) 3600 24))))
  361. (defmacro gnus-date-get-time (date)
  362. "Convert DATE string to Emacs time.
  363. Cache the result as a text property stored in DATE."
  364. ;; Either return the cached value...
  365. `(let ((d ,date))
  366. (if (equal "" d)
  367. '(0 0)
  368. (or (get-text-property 0 'gnus-time d)
  369. ;; or compute the value...
  370. (let ((time (safe-date-to-time d)))
  371. ;; and store it back in the string.
  372. (put-text-property 0 1 'gnus-time time d)
  373. time)))))
  374. (defun gnus-dd-mmm (messy-date)
  375. "Return a string like DD-MMM from a big messy string."
  376. (condition-case ()
  377. (format-time-string "%d-%b" (gnus-date-get-time messy-date))
  378. (error " - ")))
  379. (defsubst gnus-time-iso8601 (time)
  380. "Return a string of TIME in YYYYMMDDTHHMMSS format."
  381. (format-time-string "%Y%m%dT%H%M%S" time))
  382. (defun gnus-date-iso8601 (date)
  383. "Convert the DATE to YYYYMMDDTHHMMSS."
  384. (condition-case ()
  385. (gnus-time-iso8601 (gnus-date-get-time date))
  386. (error "")))
  387. (defun gnus-mode-string-quote (string)
  388. "Quote all \"%\"'s in STRING."
  389. (gnus-replace-in-string string "%" "%%"))
  390. ;; Make a hash table (default and minimum size is 256).
  391. ;; Optional argument HASHSIZE specifies the table size.
  392. (defun gnus-make-hashtable (&optional hashsize)
  393. (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 256) 256) 0))
  394. ;; Make a number that is suitable for hashing; bigger than MIN and
  395. ;; equal to some 2^x. Many machines (such as sparcs) do not have a
  396. ;; hardware modulo operation, so they implement it in software. On
  397. ;; many sparcs over 50% of the time to intern is spent in the modulo.
  398. ;; Yes, it's slower than actually computing the hash from the string!
  399. ;; So we use powers of 2 so people can optimize the modulo to a mask.
  400. (defun gnus-create-hash-size (min)
  401. (let ((i 1))
  402. (while (< i min)
  403. (setq i (* 2 i)))
  404. i))
  405. (defcustom gnus-verbose 6
  406. "*Integer that says how verbose Gnus should be.
  407. The higher the number, the more messages Gnus will flash to say what
  408. it's doing. At zero, Gnus will be totally mute; at five, Gnus will
  409. display most important messages; and at ten, Gnus will keep on
  410. jabbering all the time."
  411. :version "24.1"
  412. :group 'gnus-start
  413. :type 'integer)
  414. (defcustom gnus-add-timestamp-to-message nil
  415. "Non-nil means add timestamps to messages that Gnus issues.
  416. If it is `log', add timestamps to only the messages that go into the
  417. \"*Messages*\" buffer (in XEmacs, it is the \" *Message-Log*\" buffer).
  418. If it is neither nil nor `log', add timestamps not only to log messages
  419. but also to the ones displayed in the echo area."
  420. :version "23.1" ;; No Gnus
  421. :group 'gnus-various
  422. :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
  423. (const :tag "Logged messages only" log)
  424. (sexp :tag "All messages"
  425. :match (lambda (widget value) value)
  426. :value t)
  427. (const :tag "No timestamp" nil)))
  428. (eval-when-compile
  429. (defmacro gnus-message-with-timestamp-1 (format-string args)
  430. (let ((timestamp '(format-time-string "%Y%m%dT%H%M%S.%3N> " time)))
  431. (if (featurep 'xemacs)
  432. `(let (str time)
  433. (if (or (and (null ,format-string) (null ,args))
  434. (progn
  435. (setq str (apply 'format ,format-string ,args))
  436. (zerop (length str))))
  437. (prog1
  438. (and ,format-string str)
  439. (clear-message nil))
  440. (cond ((eq gnus-add-timestamp-to-message 'log)
  441. (setq time (current-time))
  442. (display-message 'no-log str)
  443. (log-message 'message (concat ,timestamp str)))
  444. (gnus-add-timestamp-to-message
  445. (setq time (current-time))
  446. (display-message 'message (concat ,timestamp str)))
  447. (t
  448. (display-message 'message str))))
  449. str)
  450. `(let (str time)
  451. (cond ((eq gnus-add-timestamp-to-message 'log)
  452. (setq str (let (message-log-max)
  453. (apply 'message ,format-string ,args)))
  454. (when (and message-log-max
  455. (> message-log-max 0)
  456. (/= (length str) 0))
  457. (setq time (current-time))
  458. (with-current-buffer (get-buffer-create "*Messages*")
  459. (goto-char (point-max))
  460. (insert ,timestamp str "\n")
  461. (forward-line (- message-log-max))
  462. (delete-region (point-min) (point))
  463. (goto-char (point-max))))
  464. str)
  465. (gnus-add-timestamp-to-message
  466. (if (or (and (null ,format-string) (null ,args))
  467. (progn
  468. (setq str (apply 'format ,format-string ,args))
  469. (zerop (length str))))
  470. (prog1
  471. (and ,format-string str)
  472. (message nil))
  473. (setq time (current-time))
  474. (message "%s" (concat ,timestamp str))
  475. str))
  476. (t
  477. (apply 'message ,format-string ,args))))))))
  478. (defvar gnus-action-message-log nil)
  479. (defun gnus-message-with-timestamp (format-string &rest args)
  480. "Display message with timestamp. Arguments are the same as `message'.
  481. The `gnus-add-timestamp-to-message' variable controls how to add
  482. timestamp to message."
  483. (gnus-message-with-timestamp-1 format-string args))
  484. (defun gnus-message (level &rest args)
  485. "If LEVEL is lower than `gnus-verbose' print ARGS using `message'.
  486. Guideline for numbers:
  487. 1 - error messages, 3 - non-serious error messages, 5 - messages for things
  488. that take a long time, 7 - not very important messages on stuff, 9 - messages
  489. inside loops."
  490. (if (<= level gnus-verbose)
  491. (let ((message
  492. (if gnus-add-timestamp-to-message
  493. (apply 'gnus-message-with-timestamp args)
  494. (apply 'message args))))
  495. (when (and (consp gnus-action-message-log)
  496. (<= level 3))
  497. (push message gnus-action-message-log))
  498. message)
  499. ;; We have to do this format thingy here even if the result isn't
  500. ;; shown - the return value has to be the same as the return value
  501. ;; from `message'.
  502. (apply 'format args)))
  503. (defun gnus-final-warning ()
  504. (when (and (consp gnus-action-message-log)
  505. (setq gnus-action-message-log
  506. (delete nil gnus-action-message-log)))
  507. (message "Warning: %s"
  508. (mapconcat #'identity gnus-action-message-log "; "))))
  509. (defun gnus-error (level &rest args)
  510. "Beep an error if LEVEL is equal to or less than `gnus-verbose'.
  511. ARGS are passed to `message'."
  512. (when (<= (floor level) gnus-verbose)
  513. (apply 'message args)
  514. (ding)
  515. (let (duration)
  516. (when (and (floatp level)
  517. (not (zerop (setq duration (* 10 (- level (floor level)))))))
  518. (sit-for duration))))
  519. nil)
  520. (defun gnus-split-references (references)
  521. "Return a list of Message-IDs in REFERENCES."
  522. (let ((beg 0)
  523. (references (mail-header-remove-comments (or references "")))
  524. ids)
  525. (while (string-match "<[^<]+[^< \t]" references beg)
  526. (push (substring references (match-beginning 0) (setq beg (match-end 0)))
  527. ids))
  528. (nreverse ids)))
  529. (defun gnus-extract-references (references)
  530. "Return a list of Message-IDs in REFERENCES (in In-Reply-To
  531. format), trimmed to only contain the Message-IDs."
  532. (let ((ids (gnus-split-references references))
  533. refs)
  534. (dolist (id ids)
  535. (when (string-match "<[^<>]+>" id)
  536. (push (match-string 0 id) refs)))
  537. refs))
  538. (defsubst gnus-parent-id (references &optional n)
  539. "Return the last Message-ID in REFERENCES.
  540. If N, return the Nth ancestor instead."
  541. (when (and references
  542. (not (zerop (length references))))
  543. (if n
  544. (let ((ids (inline (gnus-split-references references))))
  545. (while (nthcdr n ids)
  546. (setq ids (cdr ids)))
  547. (car ids))
  548. (let ((references (mail-header-remove-comments references)))
  549. (when (string-match "\\(<[^<]+>\\)[ \t]*\\'" references)
  550. (match-string 1 references))))))
  551. (defsubst gnus-buffer-live-p (buffer)
  552. "Say whether BUFFER is alive or not."
  553. (and buffer (buffer-live-p (get-buffer buffer))))
  554. (defun gnus-horizontal-recenter ()
  555. "Recenter the current buffer horizontally."
  556. (if (< (current-column) (/ (window-width) 2))
  557. (set-window-hscroll (gnus-get-buffer-window (current-buffer) t) 0)
  558. (let* ((orig (point))
  559. (end (window-end (gnus-get-buffer-window (current-buffer) t)))
  560. (max 0))
  561. (when end
  562. ;; Find the longest line currently displayed in the window.
  563. (goto-char (window-start))
  564. (while (and (not (eobp))
  565. (< (point) end))
  566. (end-of-line)
  567. (setq max (max max (current-column)))
  568. (forward-line 1))
  569. (goto-char orig)
  570. ;; Scroll horizontally to center (sort of) the point.
  571. (if (> max (window-width))
  572. (set-window-hscroll
  573. (gnus-get-buffer-window (current-buffer) t)
  574. (min (- (current-column) (/ (window-width) 3))
  575. (+ 2 (- max (window-width)))))
  576. (set-window-hscroll (gnus-get-buffer-window (current-buffer) t) 0))
  577. max))))
  578. (defun gnus-read-event-char (&optional prompt)
  579. "Get the next event."
  580. (let ((event (read-event prompt)))
  581. ;; should be gnus-characterp, but this can't be called in XEmacs anyway
  582. (cons (and (numberp event) event) event)))
  583. (defun gnus-sortable-date (date)
  584. "Make string suitable for sorting from DATE."
  585. (gnus-time-iso8601 (date-to-time date)))
  586. (defun gnus-copy-file (file &optional to)
  587. "Copy FILE to TO."
  588. (interactive
  589. (list (read-file-name "Copy file: " default-directory)
  590. (read-file-name "Copy file to: " default-directory)))
  591. (unless to
  592. (setq to (read-file-name "Copy file to: " default-directory)))
  593. (when (file-directory-p to)
  594. (setq to (concat (file-name-as-directory to)
  595. (file-name-nondirectory file))))
  596. (copy-file file to))
  597. (defvar gnus-work-buffer " *gnus work*")
  598. (declare-function gnus-get-buffer-create "gnus" (name))
  599. ;; gnus.el requires mm-util.
  600. (declare-function mm-enable-multibyte "mm-util")
  601. (defun gnus-set-work-buffer ()
  602. "Put point in the empty Gnus work buffer."
  603. (if (get-buffer gnus-work-buffer)
  604. (progn
  605. (set-buffer gnus-work-buffer)
  606. (erase-buffer))
  607. (set-buffer (gnus-get-buffer-create gnus-work-buffer))
  608. (kill-all-local-variables)
  609. (mm-enable-multibyte)))
  610. (defmacro gnus-group-real-name (group)
  611. "Find the real name of a foreign newsgroup."
  612. `(let ((gname ,group))
  613. (if (string-match "^[^:]+:" gname)
  614. (substring gname (match-end 0))
  615. gname)))
  616. (defmacro gnus-group-server (group)
  617. "Find the server name of a foreign newsgroup.
  618. For example, (gnus-group-server \"nnimap+yxa:INBOX.foo\") would
  619. yield \"nnimap:yxa\"."
  620. `(let ((gname ,group))
  621. (if (string-match "^\\([^:+]+\\)\\(?:\\+\\([^:]*\\)\\)?:" gname)
  622. (format "%s:%s" (match-string 1 gname) (or
  623. (match-string 2 gname)
  624. ""))
  625. (format "%s:%s" (car gnus-select-method) (cadr gnus-select-method)))))
  626. (defun gnus-make-sort-function (funs)
  627. "Return a composite sort condition based on the functions in FUNS."
  628. (cond
  629. ;; Just a simple function.
  630. ((functionp funs) funs)
  631. ;; No functions at all.
  632. ((null funs) funs)
  633. ;; A list of functions.
  634. ((or (cdr funs)
  635. (listp (car funs)))
  636. (gnus-byte-compile
  637. `(lambda (t1 t2)
  638. ,(gnus-make-sort-function-1 (reverse funs)))))
  639. ;; A list containing just one function.
  640. (t
  641. (car funs))))
  642. (defun gnus-make-sort-function-1 (funs)
  643. "Return a composite sort condition based on the functions in FUNS."
  644. (let ((function (car funs))
  645. (first 't1)
  646. (last 't2))
  647. (when (consp function)
  648. (cond
  649. ;; Reversed spec.
  650. ((eq (car function) 'not)
  651. (setq function (cadr function)
  652. first 't2
  653. last 't1))
  654. ((functionp function)
  655. ;; Do nothing.
  656. )
  657. (t
  658. (error "Invalid sort spec: %s" function))))
  659. (if (cdr funs)
  660. `(or (,function ,first ,last)
  661. (and (not (,function ,last ,first))
  662. ,(gnus-make-sort-function-1 (cdr funs))))
  663. `(,function ,first ,last))))
  664. (defun gnus-turn-off-edit-menu (type)
  665. "Turn off edit menu in `gnus-TYPE-mode-map'."
  666. (define-key (symbol-value (intern (format "gnus-%s-mode-map" type)))
  667. [menu-bar edit] 'undefined))
  668. (defmacro gnus-bind-print-variables (&rest forms)
  669. "Bind print-* variables and evaluate FORMS.
  670. This macro is used with `prin1', `pp', etc. in order to ensure printed
  671. Lisp objects are loadable. Bind `print-quoted' and `print-readably'
  672. to t, and `print-escape-multibyte', `print-escape-newlines',
  673. `print-escape-nonascii', `print-length', `print-level' and
  674. `print-string-length' to nil."
  675. `(let ((print-quoted t)
  676. (print-readably t)
  677. ;;print-circle
  678. ;;print-continuous-numbering
  679. print-escape-multibyte
  680. print-escape-newlines
  681. print-escape-nonascii
  682. ;;print-gensym
  683. print-length
  684. print-level
  685. print-string-length)
  686. ,@forms))
  687. (defun gnus-prin1 (form)
  688. "Use `prin1' on FORM in the current buffer.
  689. Bind `print-quoted' and `print-readably' to t, and `print-length' and
  690. `print-level' to nil. See also `gnus-bind-print-variables'."
  691. (gnus-bind-print-variables (prin1 form (current-buffer))))
  692. (defun gnus-prin1-to-string (form)
  693. "The same as `prin1'.
  694. Bind `print-quoted' and `print-readably' to t, and `print-length' and
  695. `print-level' to nil. See also `gnus-bind-print-variables'."
  696. (gnus-bind-print-variables (prin1-to-string form)))
  697. (defun gnus-pp (form &optional stream)
  698. "Use `pp' on FORM in the current buffer.
  699. Bind `print-quoted' and `print-readably' to t, and `print-length' and
  700. `print-level' to nil. See also `gnus-bind-print-variables'."
  701. (gnus-bind-print-variables (pp form (or stream (current-buffer)))))
  702. (defun gnus-pp-to-string (form)
  703. "The same as `pp-to-string'.
  704. Bind `print-quoted' and `print-readably' to t, and `print-length' and
  705. `print-level' to nil. See also `gnus-bind-print-variables'."
  706. (gnus-bind-print-variables (pp-to-string form)))
  707. (defun gnus-make-directory (directory)
  708. "Make DIRECTORY (and all its parents) if it doesn't exist."
  709. (require 'nnmail)
  710. (let ((file-name-coding-system nnmail-pathname-coding-system))
  711. (when (and directory
  712. (not (file-exists-p directory)))
  713. (make-directory directory t)))
  714. t)
  715. (defun gnus-write-buffer (file)
  716. "Write the current buffer's contents to FILE."
  717. (require 'nnmail)
  718. (let ((file-name-coding-system nnmail-pathname-coding-system))
  719. ;; Make sure the directory exists.
  720. (gnus-make-directory (file-name-directory file))
  721. ;; Write the buffer.
  722. (write-region (point-min) (point-max) file nil 'quietly)))
  723. (defun gnus-delete-file (file)
  724. "Delete FILE if it exists."
  725. (when (file-exists-p file)
  726. (delete-file file)))
  727. (defun gnus-delete-duplicates (list)
  728. "Remove duplicate entries from LIST."
  729. (let ((result nil))
  730. (while list
  731. (unless (member (car list) result)
  732. (push (car list) result))
  733. (pop list))
  734. (nreverse result)))
  735. (defun gnus-delete-directory (directory)
  736. "Delete files in DIRECTORY. Subdirectories remain.
  737. If there's no subdirectory, delete DIRECTORY as well."
  738. (when (file-directory-p directory)
  739. (let ((files (directory-files
  740. directory t "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
  741. file dir)
  742. (while files
  743. (setq file (pop files))
  744. (if (eq t (car (file-attributes file)))
  745. ;; `file' is a subdirectory.
  746. (setq dir t)
  747. ;; `file' is a file or a symlink.
  748. (delete-file file)))
  749. (unless dir
  750. (delete-directory directory)))))
  751. ;; The following two functions are used in gnus-registry.
  752. ;; They were contributed by Andreas Fuchs <asf@void.at>.
  753. (defun gnus-alist-to-hashtable (alist)
  754. "Build a hashtable from the values in ALIST."
  755. (let ((ht (make-hash-table
  756. :size 4096
  757. :test 'equal)))
  758. (mapc
  759. (lambda (kv-pair)
  760. (puthash (car kv-pair) (cdr kv-pair) ht))
  761. alist)
  762. ht))
  763. (defun gnus-hashtable-to-alist (hash)
  764. "Build an alist from the values in HASH."
  765. (let ((list nil))
  766. (maphash
  767. (lambda (key value)
  768. (setq list (cons (cons key value) list)))
  769. hash)
  770. list))
  771. (defun gnus-strip-whitespace (string)
  772. "Return STRING stripped of all whitespace."
  773. (while (string-match "[\r\n\t ]+" string)
  774. (setq string (replace-match "" t t string)))
  775. string)
  776. (declare-function gnus-put-text-property "gnus"
  777. (start end property value &optional object))
  778. (defsubst gnus-put-text-property-excluding-newlines (beg end prop val)
  779. "The same as `put-text-property', but don't put this prop on any newlines in the region."
  780. (save-match-data
  781. (save-excursion
  782. (save-restriction
  783. (goto-char beg)
  784. (while (re-search-forward gnus-emphasize-whitespace-regexp end 'move)
  785. (gnus-put-text-property beg (match-beginning 0) prop val)
  786. (setq beg (point)))
  787. (gnus-put-text-property beg (point) prop val)))))
  788. (declare-function gnus-overlay-put "gnus" (overlay prop value))
  789. (declare-function gnus-make-overlay "gnus"
  790. (beg end &optional buffer front-advance rear-advance))
  791. (defsubst gnus-put-overlay-excluding-newlines (beg end prop val)
  792. "The same as `put-text-property', but don't put this prop on any newlines in the region."
  793. (save-match-data
  794. (save-excursion
  795. (save-restriction
  796. (goto-char beg)
  797. (while (re-search-forward gnus-emphasize-whitespace-regexp end 'move)
  798. (gnus-overlay-put
  799. (gnus-make-overlay beg (match-beginning 0))
  800. prop val)
  801. (setq beg (point)))
  802. (gnus-overlay-put (gnus-make-overlay beg (point)) prop val)))))
  803. (defun gnus-put-text-property-excluding-characters-with-faces (beg end
  804. prop val)
  805. "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
  806. (let ((b beg))
  807. (while (/= b end)
  808. (when (get-text-property b 'gnus-face)
  809. (setq b (next-single-property-change b 'gnus-face nil end)))
  810. (when (/= b end)
  811. (inline
  812. (gnus-put-text-property
  813. b (setq b (next-single-property-change b 'gnus-face nil end))
  814. prop val))))))
  815. (defmacro gnus-faces-at (position)
  816. "Return a list of faces at POSITION."
  817. (if (featurep 'xemacs)
  818. `(let ((pos ,position))
  819. (mapcar-extents 'extent-face
  820. nil (current-buffer) pos pos nil 'face))
  821. `(let ((pos ,position))
  822. (delq nil (cons (get-text-property pos 'face)
  823. (mapcar
  824. (lambda (overlay)
  825. (overlay-get overlay 'face))
  826. (overlays-at pos)))))))
  827. (if (fboundp 'invisible-p)
  828. (defalias 'gnus-invisible-p 'invisible-p)
  829. ;; for Emacs < 22.2, and XEmacs.
  830. (defun gnus-invisible-p (pos)
  831. "Return non-nil if the character after POS is currently invisible."
  832. (let ((prop (get-char-property pos 'invisible)))
  833. (if (eq buffer-invisibility-spec t)
  834. prop
  835. (or (memq prop buffer-invisibility-spec)
  836. (assq prop buffer-invisibility-spec))))))
  837. ;; Note: the optional 2nd argument has a different meaning between
  838. ;; Emacs and XEmacs.
  839. ;; (next-char-property-change POSITION &optional LIMIT)
  840. ;; (next-extent-change POS &optional OBJECT)
  841. (defalias 'gnus-next-char-property-change
  842. (if (fboundp 'next-extent-change)
  843. 'next-extent-change 'next-char-property-change))
  844. (defalias 'gnus-previous-char-property-change
  845. (if (fboundp 'previous-extent-change)
  846. 'previous-extent-change 'previous-char-property-change))
  847. ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
  848. ;; The primary idea here is to try to protect internal datastructures
  849. ;; from becoming corrupted when the user hits C-g, or if a hook or
  850. ;; similar blows up. Often in Gnus multiple tables/lists need to be
  851. ;; updated at the same time, or information can be lost.
  852. (defvar gnus-atomic-be-safe t
  853. "If t, certain operations will be protected from interruption by C-g.")
  854. (defmacro gnus-atomic-progn (&rest forms)
  855. "Evaluate FORMS atomically, which means to protect the evaluation
  856. from being interrupted by the user. An error from the forms themselves
  857. will return without finishing the operation. Since interrupts from
  858. the user are disabled, it is recommended that only the most minimal
  859. operations are performed by FORMS. If you wish to assign many
  860. complicated values atomically, compute the results into temporary
  861. variables and then do only the assignment atomically."
  862. `(let ((inhibit-quit gnus-atomic-be-safe))
  863. ,@forms))
  864. (put 'gnus-atomic-progn 'lisp-indent-function 0)
  865. (defmacro gnus-atomic-progn-assign (protect &rest forms)
  866. "Evaluate FORMS, but ensure that the variables listed in PROTECT
  867. are not changed if anything in FORMS signals an error or otherwise
  868. non-locally exits. The variables listed in PROTECT are updated atomically.
  869. It is safe to use gnus-atomic-progn-assign with long computations.
  870. Note that if any of the symbols in PROTECT were unbound, they will be
  871. set to nil on a successful assignment. In case of an error or other
  872. non-local exit, it will still be unbound."
  873. (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
  874. (concat (symbol-name x)
  875. "-tmp"))
  876. x))
  877. protect))
  878. (sym-temp-map (mapcar (lambda (x) (list (cadr x) (car x)))
  879. temp-sym-map))
  880. (temp-sym-let (mapcar (lambda (x) (list (car x)
  881. `(and (boundp ',(cadr x))
  882. ,(cadr x))))
  883. temp-sym-map))
  884. (sym-temp-let sym-temp-map)
  885. (temp-sym-assign (apply 'append temp-sym-map))
  886. (sym-temp-assign (apply 'append sym-temp-map))
  887. (result (make-symbol "result-tmp")))
  888. `(let (,@temp-sym-let
  889. ,result)
  890. (let ,sym-temp-let
  891. (setq ,result (progn ,@forms))
  892. (setq ,@temp-sym-assign))
  893. (let ((inhibit-quit gnus-atomic-be-safe))
  894. (setq ,@sym-temp-assign))
  895. ,result)))
  896. (put 'gnus-atomic-progn-assign 'lisp-indent-function 1)
  897. ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
  898. (defmacro gnus-atomic-setq (&rest pairs)
  899. "Similar to setq, except that the real symbols are only assigned when
  900. there are no errors. And when the real symbols are assigned, they are
  901. done so atomically. If other variables might be changed via side-effect,
  902. see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
  903. with potentially long computations."
  904. (let ((tpairs pairs)
  905. syms)
  906. (while tpairs
  907. (push (car tpairs) syms)
  908. (setq tpairs (cddr tpairs)))
  909. `(gnus-atomic-progn-assign ,syms
  910. (setq ,@pairs))))
  911. ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
  912. ;;; Functions for saving to babyl/mail files.
  913. (eval-when-compile
  914. (if (featurep 'xemacs)
  915. ;; Don't load tm and apel XEmacs packages that provide some
  916. ;; Emacs emulating functions and variables.
  917. (let ((features features))
  918. (provide 'tm-view)
  919. (unless (fboundp 'set-alist) (defalias 'set-alist 'ignore))
  920. (require 'rmail)) ;; It requires tm-view that loads apel.
  921. (require 'rmail))
  922. (autoload 'rmail-update-summary "rmailsum"))
  923. (defvar mm-text-coding-system)
  924. (declare-function mm-append-to-file "mm-util"
  925. (start end filename &optional codesys inhibit))
  926. (defun gnus-output-to-rmail (filename &optional ask)
  927. "Append the current article to an Rmail file named FILENAME.
  928. In Emacs 22 this writes Babyl format; in Emacs 23 it writes mbox unless
  929. FILENAME exists and is Babyl format."
  930. (require 'rmail)
  931. (require 'mm-util)
  932. (require 'nnmail)
  933. ;; Some of this codes is borrowed from rmailout.el.
  934. (setq filename (expand-file-name filename))
  935. ;; FIXME should we really be messing with this defcustom?
  936. ;; It is not needed for the operation of this function.
  937. (if (boundp 'rmail-default-rmail-file)
  938. (setq rmail-default-rmail-file filename) ; 22
  939. (setq rmail-default-file filename)) ; 23
  940. (let ((artbuf (current-buffer))
  941. (tmpbuf (get-buffer-create " *Gnus-output*"))
  942. ;; Babyl rmail.el defines this, mbox does not.
  943. (babyl (fboundp 'rmail-insert-rmail-file-header)))
  944. (save-excursion
  945. ;; Note that we ignore the possibility of visiting a Babyl
  946. ;; format buffer in Emacs 23, since Rmail no longer supports that.
  947. (or (get-file-buffer filename)
  948. (progn
  949. ;; In case someone wants to write to a Babyl file from Emacs 23.
  950. (when (file-exists-p filename)
  951. (setq babyl (mail-file-babyl-p filename))
  952. t))
  953. (if (or (not ask)
  954. (gnus-yes-or-no-p
  955. (concat "\"" filename "\" does not exist, create it? ")))
  956. (let ((file-buffer (create-file-buffer filename)))
  957. (with-current-buffer file-buffer
  958. (if (fboundp 'rmail-insert-rmail-file-header)
  959. (rmail-insert-rmail-file-header))
  960. (let ((require-final-newline nil)
  961. (coding-system-for-write mm-text-coding-system))
  962. (gnus-write-buffer filename)))
  963. (kill-buffer file-buffer))
  964. (error "Output file does not exist")))
  965. (set-buffer tmpbuf)
  966. (erase-buffer)
  967. (insert-buffer-substring artbuf)
  968. (if babyl
  969. (gnus-convert-article-to-rmail)
  970. ;; Non-Babyl case copied from gnus-output-to-mail.
  971. (goto-char (point-min))
  972. (if (looking-at "From ")
  973. (forward-line 1)
  974. (insert "From nobody " (current-time-string) "\n"))
  975. (let (case-fold-search)
  976. (while (re-search-forward "^From " nil t)
  977. (beginning-of-line)
  978. (insert ">"))))
  979. ;; Decide whether to append to a file or to an Emacs buffer.
  980. (let ((outbuf (get-file-buffer filename)))
  981. (if (not outbuf)
  982. (progn
  983. (unless babyl ; from gnus-output-to-mail
  984. (let ((buffer-read-only nil))
  985. (goto-char (point-max))
  986. (forward-char -2)
  987. (unless (looking-at "\n\n")
  988. (goto-char (point-max))
  989. (unless (bolp)
  990. (insert "\n"))
  991. (insert "\n"))))
  992. (let ((file-name-coding-system nnmail-pathname-coding-system))
  993. (mm-append-to-file (point-min) (point-max) filename)))
  994. ;; File has been visited, in buffer OUTBUF.
  995. (set-buffer outbuf)
  996. (let ((buffer-read-only nil)
  997. (msg (and (boundp 'rmail-current-message)
  998. (symbol-value 'rmail-current-message))))
  999. ;; If MSG is non-nil, buffer is in RMAIL mode.
  1000. ;; Compare this with rmail-output-to-rmail-buffer in Emacs 23.
  1001. (when msg
  1002. (unless babyl
  1003. (rmail-swap-buffers-maybe)
  1004. (rmail-maybe-set-message-counters))
  1005. (widen)
  1006. (narrow-to-region (point-max) (point-max)))
  1007. (insert-buffer-substring tmpbuf)
  1008. (when msg
  1009. (when babyl
  1010. (goto-char (point-min))
  1011. (widen)
  1012. (search-backward "\n\^_")
  1013. (narrow-to-region (point) (point-max)))
  1014. (rmail-count-new-messages t)
  1015. (when (rmail-summary-exists)
  1016. (rmail-select-summary
  1017. (rmail-update-summary)))
  1018. (rmail-show-message msg))
  1019. (save-buffer)))))
  1020. (kill-buffer tmpbuf)))
  1021. (defun gnus-output-to-mail (filename &optional ask)
  1022. "Append the current article to a mail file named FILENAME."
  1023. (require 'nnmail)
  1024. (setq filename (expand-file-name filename))
  1025. (let ((artbuf (current-buffer))
  1026. (tmpbuf (get-buffer-create " *Gnus-output*")))
  1027. (save-excursion
  1028. ;; Create the file, if it doesn't exist.
  1029. (when (and (not (get-file-buffer filename))
  1030. (not (file-exists-p filename)))
  1031. (if (or (not ask)
  1032. (gnus-y-or-n-p
  1033. (concat "\"" filename "\" does not exist, create it? ")))
  1034. (let ((file-buffer (create-file-buffer filename)))
  1035. (with-current-buffer file-buffer
  1036. (let ((require-final-newline nil)
  1037. (coding-system-for-write mm-text-coding-system))
  1038. (gnus-write-buffer filename)))
  1039. (kill-buffer file-buffer))
  1040. (error "Output file does not exist")))
  1041. (set-buffer tmpbuf)
  1042. (erase-buffer)
  1043. (insert-buffer-substring artbuf)
  1044. (goto-char (point-min))
  1045. (if (looking-at "From ")
  1046. (forward-line 1)
  1047. (insert "From nobody " (current-time-string) "\n"))
  1048. (let (case-fold-search)
  1049. (while (re-search-forward "^From " nil t)
  1050. (beginning-of-line)
  1051. (insert ">")))
  1052. ;; Decide whether to append to a file or to an Emacs buffer.
  1053. (let ((outbuf (get-file-buffer filename)))
  1054. (if (not outbuf)
  1055. (let ((buffer-read-only nil))
  1056. (save-excursion
  1057. (goto-char (point-max))
  1058. (forward-char -2)
  1059. (unless (looking-at "\n\n")
  1060. (goto-char (point-max))
  1061. (unless (bolp)
  1062. (insert "\n"))
  1063. (insert "\n"))
  1064. (goto-char (point-max))
  1065. (let ((file-name-coding-system nnmail-pathname-coding-system))
  1066. (mm-append-to-file (point-min) (point-max) filename))))
  1067. ;; File has been visited, in buffer OUTBUF.
  1068. (set-buffer outbuf)
  1069. (let ((buffer-read-only nil))
  1070. (goto-char (point-max))
  1071. (unless (eobp)
  1072. (insert "\n"))
  1073. (insert "\n")
  1074. (insert-buffer-substring tmpbuf)))))
  1075. (kill-buffer tmpbuf)))
  1076. (defun gnus-convert-article-to-rmail ()
  1077. "Convert article in current buffer to Rmail message format."
  1078. (let ((buffer-read-only nil))
  1079. ;; Convert article directly into Babyl format.
  1080. (goto-char (point-min))
  1081. (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
  1082. (while (search-forward "\n\^_" nil t) ;single char
  1083. (replace-match "\n^_" t t)) ;2 chars: "^" and "_"
  1084. (goto-char (point-max))
  1085. (insert "\^_")))
  1086. (defun gnus-map-function (funs arg)
  1087. "Apply the result of the first function in FUNS to the second, and so on.
  1088. ARG is passed to the first function."
  1089. (while funs
  1090. (setq arg (funcall (pop funs) arg)))
  1091. arg)
  1092. (defun gnus-run-hooks (&rest funcs)
  1093. "Does the same as `run-hooks', but saves the current buffer."
  1094. (save-current-buffer
  1095. (apply 'run-hooks funcs)))
  1096. (defun gnus-run-hook-with-args (hook &rest args)
  1097. "Does the same as `run-hook-with-args', but saves the current buffer."
  1098. (save-current-buffer
  1099. (apply 'run-hook-with-args hook args)))
  1100. (defun gnus-run-mode-hooks (&rest funcs)
  1101. "Run `run-mode-hooks' if it is available, otherwise `run-hooks'.
  1102. This function saves the current buffer."
  1103. (if (fboundp 'run-mode-hooks)
  1104. (save-current-buffer (apply 'run-mode-hooks funcs))
  1105. (save-current-buffer (apply 'run-hooks funcs))))
  1106. ;;; Various
  1107. (defvar gnus-group-buffer) ; Compiler directive
  1108. (defun gnus-alive-p ()
  1109. "Say whether Gnus is running or not."
  1110. (and (boundp 'gnus-group-buffer)
  1111. (get-buffer gnus-group-buffer)
  1112. (with-current-buffer gnus-group-buffer
  1113. (eq major-mode 'gnus-group-mode))))
  1114. (defun gnus-process-live-p (process)
  1115. "Returns non-nil if PROCESS is alive.
  1116. A process is considered alive if its status is `run', `open',
  1117. `listen', `connect' or `stop'."
  1118. (memq (process-status process)
  1119. '(run open listen connect stop)))
  1120. (defun gnus-remove-if (predicate sequence &optional hash-table-p)
  1121. "Return a copy of SEQUENCE with all items satisfying PREDICATE removed.
  1122. SEQUENCE should be a list, a vector, or a string. Returns always a list.
  1123. If HASH-TABLE-P is non-nil, regards SEQUENCE as a hash table."
  1124. (let (out)
  1125. (if hash-table-p
  1126. (mapatoms (lambda (symbol)
  1127. (unless (funcall predicate symbol)
  1128. (push symbol out)))
  1129. sequence)
  1130. (unless (listp sequence)
  1131. (setq sequence (append sequence nil)))
  1132. (while sequence
  1133. (unless (funcall predicate (car sequence))
  1134. (push (car sequence) out))
  1135. (setq sequence (cdr sequence))))
  1136. (nreverse out)))
  1137. (defun gnus-remove-if-not (predicate sequence &optional hash-table-p)
  1138. "Return a copy of SEQUENCE with all items not satisfying PREDICATE removed.
  1139. SEQUENCE should be a list, a vector, or a string. Returns always a list.
  1140. If HASH-TABLE-P is non-nil, regards SEQUENCE as a hash table."
  1141. (let (out)
  1142. (if hash-table-p
  1143. (mapatoms (lambda (symbol)
  1144. (when (funcall predicate symbol)
  1145. (push symbol out)))
  1146. sequence)
  1147. (unless (listp sequence)
  1148. (setq sequence (append sequence nil)))
  1149. (while sequence
  1150. (when (funcall predicate (car sequence))
  1151. (push (car sequence) out))
  1152. (setq sequence (cdr sequence))))
  1153. (nreverse out)))
  1154. (if (fboundp 'assq-delete-all)
  1155. (defalias 'gnus-delete-alist 'assq-delete-all)
  1156. (defun gnus-delete-alist (key alist)
  1157. "Delete from ALIST all elements whose car is KEY.
  1158. Return the modified alist."
  1159. (let (entry)
  1160. (while (setq entry (assq key alist))
  1161. (setq alist (delq entry alist)))
  1162. alist)))
  1163. (defun gnus-grep-in-list (word list)
  1164. "Find if a WORD matches any regular expression in the given LIST."
  1165. (when (and word list)
  1166. (catch 'found
  1167. (dolist (r list)
  1168. (when (string-match r word)
  1169. (throw 'found r))))))
  1170. (defmacro gnus-alist-pull (key alist &optional assoc-p)
  1171. "Modify ALIST to be without KEY."
  1172. (unless (symbolp alist)
  1173. (error "Not a symbol: %s" alist))
  1174. (let ((fun (if assoc-p 'assoc 'assq)))
  1175. `(setq ,alist (delq (,fun ,key ,alist) ,alist))))
  1176. (defun gnus-globalify-regexp (re)
  1177. "Return a regexp that matches a whole line, if RE matches a part of it."
  1178. (concat (unless (string-match "^\\^" re) "^.*")
  1179. re
  1180. (unless (string-match "\\$$" re) ".*$")))
  1181. (defun gnus-set-window-start (&optional point)
  1182. "Set the window start to POINT, or (point) if nil."
  1183. (let ((win (gnus-get-buffer-window (current-buffer) t)))
  1184. (when win
  1185. (set-window-start win (or point (point))))))
  1186. (defun gnus-annotation-in-region-p (b e)
  1187. (if (= b e)
  1188. (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
  1189. (text-property-any b e 'gnus-undeletable t)))
  1190. (defun gnus-or (&rest elems)
  1191. "Return non-nil if any of the elements are non-nil."
  1192. (catch 'found
  1193. (while elems
  1194. (when (pop elems)
  1195. (throw 'found t)))))
  1196. (defun gnus-and (&rest elems)
  1197. "Return non-nil if all of the elements are non-nil."
  1198. (catch 'found
  1199. (while elems
  1200. (unless (pop elems)
  1201. (throw 'found nil)))
  1202. t))
  1203. ;; gnus.el requires mm-util.
  1204. (declare-function mm-disable-multibyte "mm-util")
  1205. (defun gnus-write-active-file (file hashtb &optional full-names)
  1206. ;; `coding-system-for-write' should be `raw-text' or equivalent.
  1207. (let ((coding-system-for-write nnmail-active-file-coding-system))
  1208. (with-temp-file file
  1209. ;; The buffer should be in the unibyte mode because group names
  1210. ;; are ASCII text or encoded non-ASCII text (i.e., unibyte).
  1211. (mm-disable-multibyte)
  1212. (mapatoms
  1213. (lambda (sym)
  1214. (when (and sym
  1215. (boundp sym)
  1216. (symbol-value sym))
  1217. (insert (format "%S %d %d y\n"
  1218. (if full-names
  1219. sym
  1220. (intern (gnus-group-real-name (symbol-name sym))))
  1221. (or (cdr (symbol-value sym))
  1222. (car (symbol-value sym)))
  1223. (car (symbol-value sym))))))
  1224. hashtb)
  1225. (goto-char (point-max))
  1226. (while (search-backward "\\." nil t)
  1227. (delete-char 1)))))
  1228. ;; Fixme: Why not use `with-output-to-temp-buffer'?
  1229. (defmacro gnus-with-output-to-file (file &rest body)
  1230. (let ((buffer (make-symbol "output-buffer"))
  1231. (size (make-symbol "output-buffer-size"))
  1232. (leng (make-symbol "output-buffer-length"))
  1233. (append (make-symbol "output-buffer-append")))
  1234. `(let* ((,size 131072)
  1235. (,buffer (make-string ,size 0))
  1236. (,leng 0)
  1237. (,append nil)
  1238. (standard-output
  1239. (lambda (c)
  1240. (aset ,buffer ,leng c)
  1241. (if (= ,size (setq ,leng (1+ ,leng)))
  1242. (progn (write-region ,buffer nil ,file ,append 'no-msg)
  1243. (setq ,leng 0
  1244. ,append t))))))
  1245. ,@body
  1246. (when (> ,leng 0)
  1247. (let ((coding-system-for-write 'no-conversion))
  1248. (write-region (substring ,buffer 0 ,leng) nil ,file
  1249. ,append 'no-msg))))))
  1250. (put 'gnus-with-output-to-file 'lisp-indent-function 1)
  1251. (put 'gnus-with-output-to-file 'edebug-form-spec '(form body))
  1252. (if (fboundp 'union)
  1253. (defalias 'gnus-union 'union)
  1254. (defun gnus-union (l1 l2)
  1255. "Set union of lists L1 and L2."
  1256. (cond ((null l1) l2)
  1257. ((null l2) l1)
  1258. ((equal l1 l2) l1)
  1259. (t
  1260. (or (>= (length l1) (length l2))
  1261. (setq l1 (prog1 l2 (setq l2 l1))))
  1262. (while l2
  1263. (or (member (car l2) l1)
  1264. (push (car l2) l1))
  1265. (pop l2))
  1266. l1))))
  1267. (declare-function gnus-add-text-properties "gnus"
  1268. (start end properties &optional object))
  1269. (defun gnus-add-text-properties-when
  1270. (property value start end properties &optional object)
  1271. "Like `gnus-add-text-properties', only applied on where PROPERTY is VALUE."
  1272. (let (point)
  1273. (while (and start
  1274. (< start end) ;; XEmacs will loop for every when start=end.
  1275. (setq point (text-property-not-all start end property value)))
  1276. (gnus-add-text-properties start point properties object)
  1277. (setq start (text-property-any point end property value)))
  1278. (if start
  1279. (gnus-add-text-properties start end properties object))))
  1280. (defun gnus-remove-text-properties-when
  1281. (property value start end properties &optional object)
  1282. "Like `remove-text-properties', only applied on where PROPERTY is VALUE."
  1283. (let (point)
  1284. (while (and start
  1285. (< start end)
  1286. (setq point (text-property-not-all start end property value)))
  1287. (remove-text-properties start point properties object)
  1288. (setq start (text-property-any point end property value)))
  1289. (if start
  1290. (remove-text-properties start end properties object))
  1291. t))
  1292. (defun gnus-string-remove-all-properties (string)
  1293. (condition-case ()
  1294. (let ((s string))
  1295. (set-text-properties 0 (length string) nil string)
  1296. s)
  1297. (error string)))
  1298. ;; This might use `compare-strings' to reduce consing in the
  1299. ;; case-insensitive case, but it has to cope with null args.
  1300. ;; (`string-equal' uses symbol print names.)
  1301. (defun gnus-string-equal (x y)
  1302. "Like `string-equal', except it compares case-insensitively."
  1303. (and (= (length x) (length y))
  1304. (or (string-equal x y)
  1305. (string-equal (downcase x) (downcase y)))))
  1306. (defcustom gnus-use-byte-compile t
  1307. "If non-nil, byte-compile crucial run-time code.
  1308. Setting it to nil has no effect after the first time `gnus-byte-compile'
  1309. is run."
  1310. :type 'boolean
  1311. :version "22.1"
  1312. :group 'gnus-various)
  1313. (defun gnus-byte-compile (form)
  1314. "Byte-compile FORM if `gnus-use-byte-compile' is non-nil."
  1315. (if gnus-use-byte-compile
  1316. (progn
  1317. (condition-case nil
  1318. ;; Work around a bug in XEmacs 21.4
  1319. (require 'byte-optimize)
  1320. (error))
  1321. (require 'bytecomp)
  1322. (defalias 'gnus-byte-compile
  1323. (lambda (form)
  1324. (let ((byte-compile-warnings '(unresolved callargs redefine)))
  1325. (byte-compile form))))
  1326. (gnus-byte-compile form))
  1327. form))
  1328. (defun gnus-remassoc (key alist)
  1329. "Delete by side effect any elements of LIST whose car is `equal' to KEY.
  1330. The modified LIST is returned. If the first member
  1331. of LIST has a car that is `equal' to KEY, there is no way to remove it
  1332. by side effect; therefore, write `(setq foo (gnus-remassoc key foo))' to be
  1333. sure of changing the value of `foo'."
  1334. (when alist
  1335. (if (equal key (caar alist))
  1336. (cdr alist)
  1337. (setcdr alist (gnus-remassoc key (cdr alist)))
  1338. alist)))
  1339. (defun gnus-update-alist-soft (key value alist)
  1340. (if value
  1341. (cons (cons key value) (gnus-remassoc key alist))
  1342. (gnus-remassoc key alist)))
  1343. (defun gnus-create-info-command (node)
  1344. "Create a command that will go to info NODE."
  1345. `(lambda ()
  1346. (interactive)
  1347. ,(concat "Enter the info system at node " node)
  1348. (Info-goto-node ,node)
  1349. (setq gnus-info-buffer (current-buffer))
  1350. (gnus-configure-windows 'info)))
  1351. (defun gnus-not-ignore (&rest args)
  1352. t)
  1353. (defvar gnus-directory-sep-char-regexp "/"
  1354. "The regexp of directory separator character.
  1355. If you find some problem with the directory separator character, try
  1356. \"[/\\\\\]\" for some systems.")
  1357. (defun gnus-url-unhex (x)
  1358. (if (> x ?9)
  1359. (if (>= x ?a)
  1360. (+ 10 (- x ?a))
  1361. (+ 10 (- x ?A)))
  1362. (- x ?0)))
  1363. ;; Fixme: Do it like QP.
  1364. (defun gnus-url-unhex-string (str &optional allow-newlines)
  1365. "Remove %XX, embedded spaces, etc in a url.
  1366. If optional second argument ALLOW-NEWLINES is non-nil, then allow the
  1367. decoding of carriage returns and line feeds in the string, which is normally
  1368. forbidden in URL encoding."
  1369. (let ((tmp "")
  1370. (case-fold-search t))
  1371. (while (string-match "%[0-9a-f][0-9a-f]" str)
  1372. (let* ((start (match-beginning 0))
  1373. (ch1 (gnus-url-unhex (elt str (+ start 1))))
  1374. (code (+ (* 16 ch1)
  1375. (gnus-url-unhex (elt str (+ start 2))))))
  1376. (setq tmp (concat
  1377. tmp (substring str 0 start)
  1378. (cond
  1379. (allow-newlines
  1380. (char-to-string code))
  1381. ((or (= code ?\n) (= code ?\r))
  1382. " ")
  1383. (t (char-to-string code))))
  1384. str (substring str (match-end 0)))))
  1385. (setq tmp (concat tmp str))
  1386. tmp))
  1387. (defun gnus-make-predicate (spec)
  1388. "Transform SPEC into a function that can be called.
  1389. SPEC is a predicate specifier that contains stuff like `or', `and',
  1390. `not', lists and functions. The functions all take one parameter."
  1391. `(lambda (elem) ,(gnus-make-predicate-1 spec)))
  1392. (defun gnus-make-predicate-1 (spec)
  1393. (cond
  1394. ((symbolp spec)
  1395. `(,spec elem))
  1396. ((listp spec)
  1397. (if (memq (car spec) '(or and not))
  1398. `(,(car spec) ,@(mapcar 'gnus-make-predicate-1 (cdr spec)))
  1399. (error "Invalid predicate specifier: %s" spec)))))
  1400. (defun gnus-completing-read (prompt collection &optional require-match
  1401. initial-input history def)
  1402. "Call `gnus-completing-read-function'."
  1403. (funcall gnus-completing-read-function
  1404. (concat prompt (when def
  1405. (concat " (default " def ")"))
  1406. ": ")
  1407. collection require-match initial-input history def))
  1408. (defun gnus-emacs-completing-read (prompt collection &optional require-match
  1409. initial-input history def)
  1410. "Call standard `completing-read-function'."
  1411. (let ((completion-styles gnus-completion-styles))
  1412. (completing-read prompt
  1413. ;; Old XEmacs (at least 21.4) expect an alist for
  1414. ;; collection.
  1415. (mapcar 'list collection)
  1416. nil require-match initial-input history def)))
  1417. (autoload 'ido-completing-read "ido")
  1418. (defun gnus-ido-completing-read (prompt collection &optional require-match
  1419. initial-input history def)
  1420. "Call `ido-completing-read-function'."
  1421. (ido-completing-read prompt collection nil require-match
  1422. initial-input history def))
  1423. (declare-function iswitchb-read-buffer "iswitchb"
  1424. (prompt &optional default require-match start matches-set))
  1425. (defvar iswitchb-temp-buflist)
  1426. (defun gnus-iswitchb-completing-read (prompt collection &optional require-match
  1427. initial-input history def)
  1428. "`iswitchb' based completing-read function."
  1429. ;; Make sure iswitchb is loaded before we let-bind its variables.
  1430. ;; If it is loaded inside the let, variables can become unbound afterwards.
  1431. (require 'iswitchb)
  1432. (let ((iswitchb-make-buflist-hook
  1433. (lambda ()
  1434. (setq iswitchb-temp-buflist
  1435. (let ((choices (append
  1436. (when initial-input (list initial-input))
  1437. (symbol-value history) collection))
  1438. filtered-choices)
  1439. (dolist (x choices)
  1440. (setq filtered-choices (adjoin x filtered-choices)))
  1441. (nreverse filtered-choices))))))
  1442. (unwind-protect
  1443. (progn
  1444. (or iswitchb-mode
  1445. (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup))
  1446. (iswitchb-read-buffer prompt def require-match))
  1447. (or iswitchb-mode
  1448. (remove-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)))))
  1449. (defun gnus-graphic-display-p ()
  1450. (if (featurep 'xemacs)
  1451. (device-on-window-system-p)
  1452. (display-graphic-p)))
  1453. (put 'gnus-parse-without-error 'lisp-indent-function 0)
  1454. (put 'gnus-parse-without-error 'edebug-form-spec '(body))
  1455. (defmacro gnus-parse-without-error (&rest body)
  1456. "Allow continuing onto the next line even if an error occurs."
  1457. `(while (not (eobp))
  1458. (condition-case ()
  1459. (progn
  1460. ,@body
  1461. (goto-char (point-max)))
  1462. (error
  1463. (gnus-error 4 "Invalid data on line %d"
  1464. (count-lines (point-min) (point)))
  1465. (forward-line 1)))))
  1466. (defun gnus-cache-file-contents (file variable function)
  1467. "Cache the contents of FILE in VARIABLE. The contents come from FUNCTION."
  1468. (let ((time (nth 5 (file-attributes file)))
  1469. contents value)
  1470. (if (or (null (setq value (symbol-value variable)))
  1471. (not (equal (car value) file))
  1472. (not (equal (nth 1 value) time)))
  1473. (progn
  1474. (setq contents (funcall function file))
  1475. (set variable (list file time contents))
  1476. contents)
  1477. (nth 2 value))))
  1478. (defun gnus-multiple-choice (prompt choice &optional idx)
  1479. "Ask user a multiple choice question.
  1480. CHOICE is a list of the choice char and help message at IDX."
  1481. (let (tchar buf)
  1482. (save-window-excursion
  1483. (save-excursion
  1484. (while (not tchar)
  1485. (message "%s (%s): "
  1486. prompt
  1487. (concat
  1488. (mapconcat (lambda (s) (char-to-string (car s)))
  1489. choice ", ") ", ?"))
  1490. (setq tchar (read-char))
  1491. (when (not (assq tchar choice))
  1492. (setq tchar nil)
  1493. (setq buf (get-buffer-create "*Gnus Help*"))
  1494. (pop-to-buffer buf)
  1495. (fundamental-mode) ; for Emacs 20.4+
  1496. (buffer-disable-undo)
  1497. (erase-buffer)
  1498. (insert prompt ":\n\n")
  1499. (let ((max -1)
  1500. (list choice)
  1501. (alist choice)
  1502. (idx (or idx 1))
  1503. (i 0)
  1504. n width pad format)
  1505. ;; find the longest string to display
  1506. (while list
  1507. (setq n (length (nth idx (car list))))
  1508. (unless (> max n)
  1509. (setq max n))
  1510. (setq list (cdr list)))
  1511. (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
  1512. (setq n (/ (1- (window-width)) max)) ; items per line
  1513. (setq width (/ (1- (window-width)) n)) ; width of each item
  1514. ;; insert `n' items, each in a field of width `width'
  1515. (while alist
  1516. (if (< i n)
  1517. ()
  1518. (setq i 0)
  1519. (delete-char -1) ; the `\n' takes a char
  1520. (insert "\n"))
  1521. (setq pad (- width 3))
  1522. (setq format (concat "%c: %-" (int-to-string pad) "s"))
  1523. (insert (format format (caar alist) (nth idx (car alist))))
  1524. (setq alist (cdr alist))
  1525. (setq i (1+ i))))))))
  1526. (if (buffer-live-p buf)
  1527. (kill-buffer buf))
  1528. tchar))
  1529. (if (featurep 'emacs)
  1530. (defalias 'gnus-select-frame-set-input-focus 'select-frame-set-input-focus)
  1531. (if (fboundp 'select-frame-set-input-focus)
  1532. (defalias 'gnus-select-frame-set-input-focus 'select-frame-set-input-focus)
  1533. ;; XEmacs 21.4, SXEmacs
  1534. (defun gnus-select-frame-set-input-focus (frame)
  1535. "Select FRAME, raise it, and set input focus, if possible."
  1536. (raise-frame frame)
  1537. (select-frame frame)
  1538. (focus-frame frame))))
  1539. (defun gnus-frame-or-window-display-name (object)
  1540. "Given a frame or window, return the associated display name.
  1541. Return nil otherwise."
  1542. (if (featurep 'xemacs)
  1543. (device-connection (dfw-device object))
  1544. (if (or (framep object)
  1545. (and (windowp object)
  1546. (setq object (window-frame object))))
  1547. (let ((display (frame-parameter object 'display)))
  1548. (if (and (stringp display)
  1549. ;; Exclude invalid display names.
  1550. (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'"
  1551. display))
  1552. display)))))
  1553. (defvar tool-bar-mode)
  1554. (defun gnus-tool-bar-update (&rest ignore)
  1555. "Update the tool bar."
  1556. (when (and (boundp 'tool-bar-mode)
  1557. tool-bar-mode)
  1558. (let* ((args nil)
  1559. (func (cond ((featurep 'xemacs)
  1560. 'ignore)
  1561. ((fboundp 'tool-bar-update)
  1562. 'tool-bar-update)
  1563. ((fboundp 'force-window-update)
  1564. 'force-window-update)
  1565. ((fboundp 'redraw-frame)
  1566. (setq args (list (selected-frame)))
  1567. 'redraw-frame)
  1568. (t 'ignore))))
  1569. (apply func args))))
  1570. ;; Fixme: This has only one use (in gnus-agent), which isn't worthwhile.
  1571. (defmacro gnus-mapcar (function seq1 &rest seqs2_n)
  1572. "Apply FUNCTION to each element of the sequences, and make a list of the results.
  1573. If there are several sequences, FUNCTION is called with that many arguments,
  1574. and mapping stops as soon as the shortest sequence runs out. With just one
  1575. sequence, this is like `mapcar'. With several, it is like the Common Lisp
  1576. `mapcar' function extended to arbitrary sequence types."
  1577. (if seqs2_n
  1578. (let* ((seqs (cons seq1 seqs2_n))
  1579. (cnt 0)
  1580. (heads (mapcar (lambda (seq)
  1581. (make-symbol (concat "head"
  1582. (int-to-string
  1583. (setq cnt (1+ cnt))))))
  1584. seqs))
  1585. (result (make-symbol "result"))
  1586. (result-tail (make-symbol "result-tail")))
  1587. `(let* ,(let* ((bindings (cons nil nil))
  1588. (heads heads))
  1589. (nconc bindings (list (list result '(cons nil nil))))
  1590. (nconc bindings (list (list result-tail result)))
  1591. (while heads
  1592. (nconc bindings (list (list (pop heads) (pop seqs)))))
  1593. (cdr bindings))
  1594. (while (and ,@heads)
  1595. (setcdr ,result-tail (cons (funcall ,function
  1596. ,@(mapcar (lambda (h) (list 'car h))
  1597. heads))
  1598. nil))
  1599. (setq ,result-tail (cdr ,result-tail)
  1600. ,@(apply 'nconc (mapcar (lambda (h) (list h (list 'cdr h))) heads))))
  1601. (cdr ,result)))
  1602. `(mapcar ,function ,seq1)))
  1603. (if (fboundp 'merge)
  1604. (defalias 'gnus-merge 'merge)
  1605. ;; Adapted from cl-seq.el
  1606. (defun gnus-merge (type list1 list2 pred)
  1607. "Destructively merge lists LIST1 and LIST2 to produce a new list.
  1608. Argument TYPE is for compatibility and ignored.
  1609. Ordering of the elements is preserved according to PRED, a `less-than'
  1610. predicate on the elements."
  1611. (let ((res nil))
  1612. (while (and list1 list2)
  1613. (if (funcall pred (car list2) (car list1))
  1614. (push (pop list2) res)
  1615. (push (pop list1) res)))
  1616. (nconc (nreverse res) list1 list2))))
  1617. (defvar xemacs-codename)
  1618. (defvar sxemacs-codename)
  1619. (defvar emacs-program-version)
  1620. (defun gnus-emacs-version ()
  1621. "Stringified Emacs version."
  1622. (let* ((lst (if (listp gnus-user-agent)
  1623. gnus-user-agent
  1624. '(gnus emacs type)))
  1625. (system-v (cond ((memq 'config lst)
  1626. system-configuration)
  1627. ((memq 'type lst)
  1628. (symbol-name system-type))
  1629. (t nil)))
  1630. codename emacsname)
  1631. (cond ((featurep 'sxemacs)
  1632. (setq emacsname "SXEmacs"
  1633. codename sxemacs-codename))
  1634. ((featurep 'xemacs)
  1635. (setq emacsname "XEmacs"
  1636. codename xemacs-codename))
  1637. (t
  1638. (setq emacsname "Emacs")))
  1639. (cond
  1640. ((not (memq 'emacs lst))
  1641. nil)
  1642. ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
  1643. ;; Emacs:
  1644. (concat "Emacs/" (match-string 1 emacs-version)
  1645. (if system-v
  1646. (concat " (" system-v ")")
  1647. "")))
  1648. ((or (featurep 'sxemacs) (featurep 'xemacs))
  1649. ;; XEmacs or SXEmacs:
  1650. (concat emacsname "/" emacs-program-version
  1651. (let (plst)
  1652. (when (memq 'codename lst)
  1653. (push codename plst))
  1654. (when system-v
  1655. (push system-v plst))
  1656. (unless (featurep 'mule)
  1657. (push "no MULE" plst))
  1658. (when (> (length plst) 0)
  1659. (concat
  1660. " (" (mapconcat 'identity (reverse plst) ", ") ")")))))
  1661. (t emacs-version))))
  1662. (defun gnus-rename-file (old-path new-path &optional trim)
  1663. "Rename OLD-PATH as NEW-PATH. If TRIM, recursively delete
  1664. empty directories from OLD-PATH."
  1665. (when (file-exists-p old-path)
  1666. (let* ((old-dir (file-name-directory old-path))
  1667. (old-name (file-name-nondirectory old-path))
  1668. (new-dir (file-name-directory new-path))
  1669. (new-name (file-name-nondirectory new-path))
  1670. temp)
  1671. (gnus-make-directory new-dir)
  1672. (rename-file old-path new-path t)
  1673. (when trim
  1674. (while (progn (setq temp (directory-files old-dir))
  1675. (while (member (car temp) '("." ".."))
  1676. (setq temp (cdr temp)))
  1677. (= (length temp) 0))
  1678. (delete-directory old-dir)
  1679. (setq old-dir (file-name-as-directory
  1680. (file-truename
  1681. (concat old-dir "..")))))))))
  1682. (defun gnus-set-file-modes (filename mode)
  1683. "Wrapper for set-file-modes."
  1684. (ignore-errors
  1685. (set-file-modes filename mode)))
  1686. (if (fboundp 'set-process-query-on-exit-flag)
  1687. (defalias 'gnus-set-process-query-on-exit-flag
  1688. 'set-process-query-on-exit-flag)
  1689. (defalias 'gnus-set-process-query-on-exit-flag
  1690. 'process-kill-without-query))
  1691. (defalias 'gnus-read-shell-command
  1692. (if (fboundp 'read-shell-command) 'read-shell-command 'read-string))
  1693. (defmacro gnus-put-display-table (range value display-table)
  1694. "Set the value for char RANGE to VALUE in DISPLAY-TABLE. "
  1695. (if (featurep 'xemacs)
  1696. (progn
  1697. `(if (fboundp 'put-display-table)
  1698. (put-display-table ,range ,value ,display-table)
  1699. (if (sequencep ,display-table)
  1700. (aset ,display-table ,range ,value)
  1701. (put-char-table ,range ,value ,display-table))))
  1702. `(aset ,display-table ,range ,value)))
  1703. (defmacro gnus-get-display-table (character display-table)
  1704. "Find value for CHARACTER in DISPLAY-TABLE. "
  1705. (if (featurep 'xemacs)
  1706. `(if (fboundp 'get-display-table)
  1707. (get-display-table ,character ,display-table)
  1708. (if (sequencep ,display-table)
  1709. (aref ,display-table ,character)
  1710. (get-char-table ,character ,display-table)))
  1711. `(aref ,display-table ,character)))
  1712. (defun gnus-rescale-image (image size)
  1713. "Rescale IMAGE to SIZE if possible.
  1714. SIZE is in format (WIDTH . HEIGHT). Return a new image.
  1715. Sizes are in pixels."
  1716. (if (or (not (fboundp 'imagemagick-types))
  1717. (not (get-buffer-window (current-buffer))))
  1718. image
  1719. (let ((new-width (car size))
  1720. (new-height (cdr size)))
  1721. (when (> (cdr (image-size image t)) new-height)
  1722. (setq image (or (create-image (plist-get (cdr image) :data) 'imagemagick t
  1723. :height new-height)
  1724. image)))
  1725. (when (> (car (image-size image t)) new-width)
  1726. (setq image (or
  1727. (create-image (plist-get (cdr image) :data) 'imagemagick t
  1728. :width new-width)
  1729. image)))
  1730. image)))
  1731. (defun gnus-list-memq-of-list (elements list)
  1732. "Return non-nil if any of the members of ELEMENTS are in LIST."
  1733. (let ((found nil))
  1734. (dolist (elem elements)
  1735. (setq found (or found
  1736. (memq elem list))))
  1737. found))
  1738. (eval-and-compile
  1739. (cond
  1740. ((fboundp 'match-substitute-replacement)
  1741. (defalias 'gnus-match-substitute-replacement 'match-substitute-replacement))
  1742. (t
  1743. (defun gnus-match-substitute-replacement (replacement &optional fixedcase literal string subexp)
  1744. "Return REPLACEMENT as it will be inserted by `replace-match'.
  1745. In other words, all back-references in the form `\\&' and `\\N'
  1746. are substituted with actual strings matched by the last search.
  1747. Optional FIXEDCASE, LITERAL, STRING and SUBEXP have the same
  1748. meaning as for `replace-match'.
  1749. This is the definition of match-substitute-replacement in subr.el from GNU Emacs."
  1750. (let ((match (match-string 0 string)))
  1751. (save-match-data
  1752. (set-match-data (mapcar (lambda (x)
  1753. (if (numberp x)
  1754. (- x (match-beginning 0))
  1755. x))
  1756. (match-data t)))
  1757. (replace-match replacement fixedcase literal match subexp)))))))
  1758. (if (fboundp 'string-match-p)
  1759. (defalias 'gnus-string-match-p 'string-match-p)
  1760. (defsubst gnus-string-match-p (regexp string &optional start)
  1761. "\
  1762. Same as `string-match' except this function does not change the match data."
  1763. (save-match-data
  1764. (string-match regexp string start))))
  1765. (eval-and-compile
  1766. (if (fboundp 'macroexpand-all)
  1767. (defalias 'gnus-macroexpand-all 'macroexpand-all)
  1768. (defun gnus-macroexpand-all (form &optional environment)
  1769. "Return result of expanding macros at all levels in FORM.
  1770. If no macros are expanded, FORM is returned unchanged.
  1771. The second optional arg ENVIRONMENT specifies an environment of macro
  1772. definitions to shadow the loaded ones for use in file byte-compilation."
  1773. (if (consp form)
  1774. (let ((idx 1)
  1775. (len (length (setq form (copy-sequence form))))
  1776. expanded)
  1777. (while (< idx len)
  1778. (setcar (nthcdr idx form) (gnus-macroexpand-all (nth idx form)
  1779. environment))
  1780. (setq idx (1+ idx)))
  1781. (if (eq (setq expanded (macroexpand form environment)) form)
  1782. form
  1783. (gnus-macroexpand-all expanded environment)))
  1784. form))))
  1785. ;; Simple check: can be a macro but this way, although slow, it's really clear.
  1786. ;; We don't use `bound-and-true-p' because it's not in XEmacs.
  1787. (defun gnus-bound-and-true-p (sym)
  1788. (and (boundp sym) (symbol-value sym)))
  1789. (provide 'gnus-util)
  1790. ;;; gnus-util.el ends here