supercite.el 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. ;;; supercite.el --- minor mode for citing mail and news replies
  2. ;; Copyright (C) 1993, 1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: 1993 Barry A. Warsaw <bwarsaw@python.org>
  4. ;; Maintainer: Glenn Morris <rgm@gnu.org>
  5. ;; Created: February 1993
  6. ;; Keywords: mail, news
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;; LCD Archive Entry
  19. ;; supercite|Barry A. Warsaw|supercite-help@python.org
  20. ;; |Mail and news reply citation package
  21. ;; |1993/09/22 18:58:46|3.1|
  22. ;;; Commentary:
  23. ;;; Code:
  24. (require 'regi)
  25. ;; start user configuration variables
  26. ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  27. (defgroup supercite nil
  28. "Supercite package."
  29. :prefix "sc-"
  30. :group 'mail
  31. :group 'news)
  32. (defgroup supercite-frames nil
  33. "Supercite (regi) frames."
  34. :prefix "sc-"
  35. :group 'supercite)
  36. (defgroup supercite-attr nil
  37. "Supercite attributions."
  38. :prefix "sc-"
  39. :group 'supercite)
  40. (defgroup supercite-cite nil
  41. "Supercite citings."
  42. :prefix "sc-"
  43. :group 'supercite)
  44. (defgroup supercite-hooks nil
  45. "Hooking into supercite."
  46. :prefix "sc-"
  47. :group 'supercite)
  48. (defcustom sc-auto-fill-region-p t
  49. "If non-nil, automatically fill each paragraph after it has been cited."
  50. :type 'boolean
  51. :group 'supercite)
  52. (defcustom sc-blank-lines-after-headers 1
  53. "Number of blank lines to leave after mail headers have been nuked.
  54. Set to nil, to use whatever blank lines happen to occur naturally."
  55. :type '(choice (const :tag "leave" nil)
  56. integer)
  57. :group 'supercite)
  58. (defcustom sc-citation-leader " "
  59. "String comprising first part of a citation."
  60. :type 'string
  61. :group 'supercite-cite)
  62. (defcustom sc-citation-delimiter ">"
  63. "String comprising third part of a citation.
  64. This string is used in both nested and non-nested citations."
  65. :type 'string
  66. :group 'supercite-cite)
  67. (defcustom sc-citation-separator " "
  68. "String comprising fourth and last part of a citation."
  69. :type 'string
  70. :group 'supercite-cite)
  71. (defcustom sc-citation-leader-regexp "[ \t]*"
  72. "Regexp describing citation leader for a cited line.
  73. This should NOT have a leading `^' character."
  74. :type 'regexp
  75. :group 'supercite-cite)
  76. ;; Nemacs and Mule users note: please see the texinfo manual for
  77. ;; suggestions on setting these variables.
  78. (defcustom sc-citation-root-regexp "[-._[:alnum:]]*"
  79. "Regexp describing variable root part of a citation for a cited line.
  80. This should NOT have a leading `^' character. See also
  81. `sc-citation-nonnested-root-regexp'."
  82. :type 'regexp
  83. :group 'supercite-cite)
  84. (defcustom sc-citation-nonnested-root-regexp "[-._[:alnum:]]+"
  85. "Regexp describing the variable root part of a nested citation.
  86. This should NOT have a leading `^' character. This variable is
  87. related to `sc-citation-root-regexp' but whereas that variable
  88. describes both nested and non-nested citation roots, this variable
  89. describes only nested citation roots."
  90. :type 'regexp
  91. :group 'supercite-cite)
  92. (defcustom sc-citation-delimiter-regexp "[>]+"
  93. "Regexp describing citation delimiter for a cited line.
  94. This should NOT have a leading `^' character."
  95. :type 'regexp
  96. :group 'supercite-cite)
  97. (defcustom sc-citation-separator-regexp "[ \t]*"
  98. "Regexp describing citation separator for a cited line.
  99. This should NOT have a leading `^' character."
  100. :type 'regexp
  101. :group 'supercite-cite)
  102. (defcustom sc-cite-blank-lines-p nil
  103. "If non-nil, put a citation on blank lines."
  104. :type 'boolean
  105. :group 'supercite-cite)
  106. (defcustom sc-cite-frame-alist '()
  107. "Alist for frame selection during citing.
  108. Each element of this list has the following form:
  109. (INFOKEY ((REGEXP . FRAME)
  110. (REGEXP . FRAME)
  111. (...)))
  112. Where INFOKEY is a key for `sc-mail-field', REGEXP is a regular
  113. expression to match against the INFOKEY's value. FRAME is
  114. a citation frame, or a symbol that represents the name of
  115. a variable whose value is a citation frame."
  116. :type '(repeat (list symbol (repeat (cons regexp
  117. (choice (repeat (repeat sexp))
  118. symbol)))))
  119. :group 'supercite-frames)
  120. (put 'sc-cite-frame-alist 'risky-local-variable t)
  121. (defcustom sc-uncite-frame-alist '()
  122. "Alist for frame selection during unciting.
  123. See the variable `sc-cite-frame-alist' for details."
  124. :type '(repeat (list symbol (repeat (cons regexp
  125. (choice (repeat (repeat sexp))
  126. symbol)))))
  127. :group 'supercite-frames)
  128. (put 'sc-uncite-frame-alist 'risky-local-variable t)
  129. (defcustom sc-recite-frame-alist '()
  130. "Alist for frame selection during reciting.
  131. See the variable `sc-cite-frame-alist' for details."
  132. :type '(repeat (list symbol (repeat (cons regexp
  133. (choice (repeat (repeat sexp))
  134. symbol)))))
  135. :group 'supercite-frames)
  136. (put 'sc-recite-frame-alist 'risky-local-variable t)
  137. (defcustom sc-default-cite-frame
  138. '(;; initialize fill state and temporary variables when entering
  139. ;; frame. this makes things run much faster
  140. (begin (progn
  141. (sc-fill-if-different)
  142. (setq sc-tmp-nested-regexp (sc-cite-regexp "")
  143. sc-tmp-nonnested-regexp (sc-cite-regexp)
  144. sc-tmp-dumb-regexp
  145. (concat "\\("
  146. (sc-cite-regexp "")
  147. "\\)"
  148. (sc-cite-regexp
  149. sc-citation-nonnested-root-regexp)))))
  150. ;; blank lines mean paragraph separators, so fill the last cited
  151. ;; paragraph, unless sc-cite-blank-lines-p is non-nil, in which
  152. ;; case we treat blank lines just like any other line.
  153. ("^[ \t]*$" (if sc-cite-blank-lines-p
  154. (if sc-nested-citation-p
  155. (sc-add-citation-level)
  156. (sc-cite-line))
  157. (sc-fill-if-different "")))
  158. ;; do nothing if looking at a reference tag. make sure that the
  159. ;; tag string isn't the empty string since this will match every
  160. ;; line. it cannot be nil.
  161. (sc-reference-tag-string (if (string= sc-reference-tag-string "")
  162. (list 'continue)
  163. nil))
  164. ;; this regexp catches nested citations in which the author cited
  165. ;; a non-nested citation with a dumb citer.
  166. (sc-tmp-dumb-regexp (sc-cite-coerce-dumb-citer))
  167. ;; if we are looking at a nested citation then add a citation level
  168. (sc-tmp-nested-regexp (sc-add-citation-level))
  169. ;; if we're looking at a non-nested citation, coerce it to our style
  170. (sc-tmp-nonnested-regexp (sc-cite-coerce-cited-line))
  171. ;; we must be looking at an uncited line. if we are in nested
  172. ;; citations, just add a citation level
  173. (sc-nested-citation-p (sc-add-citation-level))
  174. ;; we're looking at an uncited line and we are in non-nested
  175. ;; citations, so cite it with a non-nested citation
  176. (t (sc-cite-line))
  177. ;; be sure when we're done that we fill the last cited paragraph.
  178. (end (sc-fill-if-different "")))
  179. "Default REGI frame for citing a region."
  180. :type '(repeat (repeat sexp))
  181. :group 'supercite-frames)
  182. (put 'sc-default-cite-frame 'risky-local-variable t)
  183. (defcustom sc-default-uncite-frame
  184. '(;; do nothing on a blank line
  185. ("^[ \t]*$" nil)
  186. ;; if the line is cited, uncite it
  187. ((sc-cite-regexp) (sc-uncite-line)))
  188. "Default REGI frame for unciting a region."
  189. :type '(repeat (repeat sexp))
  190. :group 'supercite-frames)
  191. (put 'sc-default-uncite-frame 'risky-local-variable t)
  192. (defcustom sc-default-recite-frame
  193. '(;; initialize fill state when entering frame
  194. (begin (sc-fill-if-different))
  195. ;; do nothing on a blank line
  196. ("^[ \t]*$" nil)
  197. ;; if we're looking at a cited line, recite it
  198. ((sc-cite-regexp) (sc-recite-line (sc-cite-regexp)))
  199. ;; otherwise, the line is uncited, so just cite it
  200. (t (sc-cite-line))
  201. ;; be sure when we're done that we fill the last cited paragraph.
  202. (end (sc-fill-if-different "")))
  203. "Default REGI frame for reciting a region."
  204. :type '(repeat (repeat sexp))
  205. :group 'supercite-frames)
  206. (put 'sc-default-recite-frame 'risky-local-variable t)
  207. (defcustom sc-cite-region-limit t
  208. "This variable controls automatic citation of yanked text.
  209. Valid values are:
  210. non-nil -- cite the entire region, regardless of its size
  211. nil -- do not cite the region at all
  212. <integer> -- a number indicating the threshold for citation. When
  213. the number of lines in the region is greater than this
  214. value, a warning message will be printed and the region
  215. will not be cited. Lines in region are counted with
  216. `count-lines'.
  217. The gathering of attribution information is not affected by the value
  218. of this variable. The number of lines in the region is calculated
  219. *after* all mail headers are removed. This variable is only consulted
  220. during the initial citing via `sc-cite-original'."
  221. :type '(choice (const :tag "do not cite" nil)
  222. (integer :tag "citation threshold")
  223. (other :tag "always cite" t))
  224. :group 'supercite-cite)
  225. (defcustom sc-confirm-always-p t
  226. "If non-nil, always confirm attribution string before citing text body."
  227. :type 'boolean
  228. :group 'supercite-attr)
  229. (defcustom sc-default-attribution "Anon"
  230. "String used when author's attribution cannot be determined."
  231. :type 'string
  232. :group 'supercite-attr)
  233. (defcustom sc-default-author-name "Anonymous"
  234. "String used when author's name cannot be determined."
  235. :type 'string
  236. :group 'supercite-attr)
  237. (defcustom sc-downcase-p nil
  238. "Non-nil means downcase the attribution and citation strings."
  239. :type 'boolean
  240. :group 'supercite-attr
  241. :group 'supercite-cite)
  242. (defcustom sc-electric-circular-p t
  243. "If non-nil, treat electric references as circular."
  244. :type 'boolean
  245. :group 'supercite-attr)
  246. (defcustom sc-electric-mode-hook nil
  247. "Hook for `sc-electric-mode' electric references mode."
  248. :type 'hook
  249. :group 'supercite-hooks)
  250. (defcustom sc-electric-references-p nil
  251. "Use electric references if non-nil."
  252. :type 'boolean
  253. :group 'supercite)
  254. (defcustom sc-fixup-whitespace-p nil
  255. "If non-nil, delete all leading white space before citing."
  256. :type 'boolean
  257. :group 'supercite)
  258. (defcustom sc-load-hook nil
  259. "Hook which gets run once after Supercite loads."
  260. :type 'hook
  261. :group 'supercite-hooks)
  262. (defcustom sc-pre-hook nil
  263. "Hook which gets run before each invocation of `sc-cite-original'."
  264. :type 'hook
  265. :group 'supercite-hooks)
  266. (defcustom sc-post-hook nil
  267. "Hook which gets run after each invocation of `sc-cite-original'."
  268. :type 'hook
  269. :group 'supercite-hooks)
  270. (defcustom sc-mail-warn-if-non-rfc822-p t
  271. "Warn if mail headers don't conform to RFC822."
  272. :type 'boolean
  273. :group 'supercite-attr)
  274. (defcustom sc-mumble ""
  275. "Value returned by `sc-mail-field' if field isn't in mail headers."
  276. :type 'string
  277. :group 'supercite-attr)
  278. (defcustom sc-name-filter-alist
  279. '(("^\\(Mr\\|Mrs\\|Ms\\|Dr\\)[.]?$" . 0)
  280. ("^\\(Jr\\|Sr\\)[.]?$" . last)
  281. ("^ASTS$" . 0)
  282. ("^[I]+$" . last))
  283. "Name list components which are filtered out as noise.
  284. This variable contains an association list where each element is of
  285. the form: (REGEXP . POSITION).
  286. REGEXP is a regular expression which matches the name list component.
  287. Match is performed using `string-match'. POSITION is the position in
  288. the name list which can match the regular expression, starting at zero
  289. for the first element. Use `last' to match the last element in the
  290. list and `any' to match all elements."
  291. :type '(repeat (cons regexp (choice (const last) (const any)
  292. (integer :tag "position"))))
  293. :group 'supercite-attr)
  294. (defcustom sc-nested-citation-p nil
  295. "Controls whether to use nested or non-nested citation style.
  296. Non-nil uses nested citations, nil uses non-nested citations."
  297. :type 'boolean
  298. :group 'supercite)
  299. (defcustom sc-nuke-mail-headers 'all
  300. "Controls mail header nuking.
  301. Used in conjunction with `sc-nuke-mail-header-list'. Valid values are:
  302. `all' -- nuke all mail headers
  303. `none' -- don't nuke any mail headers
  304. `specified' -- nuke headers specified in `sc-nuke-mail-header-list'
  305. `keep' -- keep headers specified in `sc-nuke-mail-header-list'"
  306. :type '(choice (const all) (const none)
  307. (const specified) (const keep))
  308. :group 'supercite)
  309. (defcustom sc-nuke-mail-header-list nil
  310. "List of mail header regexps to remove or keep in body of reply.
  311. This list contains regular expressions describing the mail headers to
  312. keep or nuke, depending on the value of `sc-nuke-mail-headers'."
  313. :type '(repeat regexp)
  314. :group 'supercite)
  315. (defcustom sc-preferred-attribution-list
  316. '("sc-lastchoice" "x-attribution" "firstname" "initials" "lastname")
  317. "Specifies what to use as the attribution string.
  318. Supercite creates a list of possible attributions when it scans the
  319. mail headers from the original message. Each attribution choice is
  320. associated with a key in an attribution alist. Supercite tries to
  321. pick a \"preferred\" attribution by matching the attribution alist
  322. keys against the elements in `sc-preferred-attribution-list' in order.
  323. The first non-empty string value found is used as the preferred
  324. attribution.
  325. Note that Supercite now honors the X-Attribution: mail field. If
  326. present in the original message, the value of this field should always
  327. be used to select the most preferred attribution since it reflects how
  328. the original author would like to be distinguished. It should be
  329. considered bad taste to put any attribution preference key before
  330. \"x-attribution\" in this list, except perhaps for \"sc-lastchoice\"
  331. \(see below).
  332. Supercite remembers the last attribution used when reciting an already
  333. cited paragraph. This attribution will always be saved with the
  334. \"sc-lastchoice\" key, which can be used in this list. Note that the
  335. last choice is always reset after every call of `sc-cite-original'.
  336. Barring error conditions, the following preferences are always present
  337. in the attribution alist:
  338. \"emailname\" -- email terminus name
  339. \"initials\" -- initials of author
  340. \"firstname\" -- first name of author
  341. \"lastname\" -- last name of author
  342. \"middlename-1\" -- first middle name of author
  343. \"middlename-2\" -- second middle name of author
  344. ...
  345. Middle name indexes can be any positive integer greater than 0,
  346. although it is unlikely that many authors will supply more than one
  347. middle name, if that many. The string of all middle names is
  348. associated with the key \"middlenames\"."
  349. :type '(repeat string)
  350. :group 'supercite-attr)
  351. (defcustom sc-attrib-selection-list nil
  352. "An alist for selecting preferred attribution based on mail headers.
  353. Each element of this list has the following form:
  354. (INFOKEY ((REGEXP . ATTRIBUTION)
  355. (REGEXP . ATTRIBUTION)
  356. (...)))
  357. Where INFOKEY is a key for `sc-mail-field', REGEXP is a regular
  358. expression to match against the INFOKEY's value. ATTRIBUTION can be a
  359. string or a list. If it's a string, then it is the attribution that is
  360. selected by `sc-select-attribution'. If it is a list, it is `eval'd
  361. and the return value must be a string, which is used as the selected
  362. attribution. Note that the variable `sc-preferred-attribution-list'
  363. must contain an element of the string \"sc-consult\" for this variable
  364. to be consulted during attribution selection."
  365. :type '(repeat (list string
  366. (repeat (cons regexp
  367. (choice (sexp :tag "List to eval")
  368. string)))))
  369. :group 'supercite-attr)
  370. (put 'sc-attrib-selection-list 'risky-local-variable t)
  371. (defcustom sc-attribs-preselect-hook nil
  372. "Hook to run before selecting an attribution."
  373. :type 'hook
  374. :group 'supercite-attr
  375. :group 'supercite-hooks)
  376. (defcustom sc-attribs-postselect-hook nil
  377. "Hook to run after selecting an attribution, but before confirmation."
  378. :type 'hook
  379. :group 'supercite-attr
  380. :group 'supercite-hooks)
  381. (defcustom sc-pre-cite-hook nil
  382. "Hook to run before citing a region of text."
  383. :type 'hook
  384. :group 'supercite-cite
  385. :group 'supercite-hooks)
  386. (defcustom sc-pre-uncite-hook nil
  387. "Hook to run before unciting a region of text."
  388. :type 'hook
  389. :group 'supercite-cite
  390. :group 'supercite-hooks)
  391. (defcustom sc-pre-recite-hook nil
  392. "Hook to run before reciting a region of text."
  393. :type 'hook
  394. :group 'supercite-cite
  395. :group 'supercite-hooks)
  396. (defcustom sc-preferred-header-style 4
  397. "Index into `sc-rewrite-header-list' specifying preferred header style.
  398. Index zero accesses the first function in the list."
  399. :type 'integer
  400. :group 'supercite)
  401. (defcustom sc-reference-tag-string ">>>>> "
  402. "String used at the beginning of built-in reference headers."
  403. :type 'string
  404. :group 'supercite)
  405. (defcustom sc-rewrite-header-list
  406. '((sc-no-header)
  407. (sc-header-on-said)
  408. (sc-header-inarticle-writes)
  409. (sc-header-regarding-adds)
  410. (sc-header-attributed-writes)
  411. (sc-header-author-writes)
  412. (sc-header-verbose)
  413. (sc-no-blank-line-or-header))
  414. "List of reference header rewrite functions.
  415. The variable `sc-preferred-header-style' controls which function in
  416. this list is chosen for automatic reference header insertions.
  417. Electric reference mode will cycle through this list of functions."
  418. :type '(repeat sexp)
  419. :group 'supercite)
  420. (put 'sc-rewrite-header-list 'risky-local-variable t)
  421. (defcustom sc-titlecue-regexp "\\s +-+\\s +"
  422. "Regular expression describing the separator between names and titles.
  423. Set to nil to treat entire field as a name."
  424. :type '(choice (const :tag "entire field as name" nil)
  425. regexp)
  426. :group 'supercite-attr)
  427. (defcustom sc-use-only-preference-p nil
  428. "Controls what happens when the preferred attribution cannot be found.
  429. If non-nil, then `sc-default-attribution' will be used. If nil, then
  430. some secondary scheme will be employed to find a suitable attribution
  431. string."
  432. :type 'boolean
  433. :group 'supercite-attr)
  434. (defcustom sc-mode-map-prefix "\C-c\C-p"
  435. "Key binding to install Supercite keymap."
  436. :type 'string
  437. :group 'supercite)
  438. ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  439. ;; end user configuration variables
  440. (define-obsolete-variable-alias 'sc-version 'emacs-version "23.1")
  441. (defvar sc-mail-info nil
  442. "Alist of mail header information gleaned from reply buffer.")
  443. (defvar sc-attributions nil
  444. "Alist of attributions for use when citing.")
  445. (defvar sc-tmp-nested-regexp nil
  446. "Temporary regexp describing nested citations.")
  447. (defvar sc-tmp-nonnested-regexp nil
  448. "Temporary regexp describing non-nested citations.")
  449. (defvar sc-tmp-dumb-regexp nil
  450. "Temp regexp describing non-nested citation cited with a nesting citer.")
  451. (make-variable-buffer-local 'sc-mail-info)
  452. (make-variable-buffer-local 'sc-attributions)
  453. ;; ======================================================================
  454. ;; supercite keymaps
  455. (defvar sc-T-keymap
  456. (let ((map (make-sparse-keymap)))
  457. (define-key map "a" 'sc-S-preferred-attribution-list)
  458. (define-key map "b" 'sc-T-mail-nuke-blank-lines)
  459. (define-key map "c" 'sc-T-confirm-always)
  460. (define-key map "d" 'sc-T-downcase)
  461. (define-key map "e" 'sc-T-electric-references)
  462. (define-key map "f" 'sc-T-auto-fill-region)
  463. (define-key map "h" 'sc-T-describe)
  464. (define-key map "l" 'sc-S-cite-region-limit)
  465. (define-key map "n" 'sc-S-mail-nuke-mail-headers)
  466. (define-key map "N" 'sc-S-mail-header-nuke-list)
  467. (define-key map "o" 'sc-T-electric-circular)
  468. (define-key map "p" 'sc-S-preferred-header-style)
  469. (define-key map "s" 'sc-T-nested-citation)
  470. (define-key map "u" 'sc-T-use-only-preferences)
  471. (define-key map "w" 'sc-T-fixup-whitespace)
  472. (define-key map "?" 'sc-T-describe)
  473. map)
  474. "Keymap for sub-keymap of setting and toggling functions.")
  475. (defvar sc-mode-map
  476. (let ((map (make-sparse-keymap)))
  477. (define-key map "c" 'sc-cite-region)
  478. (define-key map "f" 'sc-mail-field-query)
  479. (define-key map "g" 'sc-mail-process-headers)
  480. (define-key map "h" 'sc-describe)
  481. (define-key map "i" 'sc-insert-citation)
  482. (define-key map "o" 'sc-open-line)
  483. (define-key map "r" 'sc-recite-region)
  484. (define-key map "\C-p" 'sc-raw-mode-toggle)
  485. (define-key map "u" 'sc-uncite-region)
  486. (define-key map "v" 'sc-version)
  487. (define-key map "w" 'sc-insert-reference)
  488. (define-key map "\C-t" sc-T-keymap)
  489. (define-key map "\C-b" 'sc-submit-bug-report)
  490. (define-key map "?" 'sc-describe)
  491. map)
  492. "Keymap for Supercite quasi-mode.")
  493. (defvar sc-electric-mode-map
  494. (let ((map (make-sparse-keymap)))
  495. (define-key map "p" 'sc-eref-prev)
  496. (define-key map "n" 'sc-eref-next)
  497. (define-key map "s" 'sc-eref-setn)
  498. (define-key map "j" 'sc-eref-jump)
  499. (define-key map "x" 'sc-eref-abort)
  500. (define-key map "q" 'sc-eref-abort)
  501. (define-key map "\r" 'sc-eref-exit)
  502. (define-key map "\n" 'sc-eref-exit)
  503. (define-key map "g" 'sc-eref-goto)
  504. (define-key map "?" 'describe-mode)
  505. (define-key map "\C-h" 'describe-mode)
  506. (define-key map [f1] 'describe-mode)
  507. (define-key map [help] 'describe-mode)
  508. map)
  509. "Keymap for `sc-electric-mode' electric references mode.")
  510. (defvar sc-minibuffer-local-completion-map
  511. (let ((map (copy-keymap minibuffer-local-completion-map)))
  512. (define-key map "\C-t" 'sc-toggle-fn)
  513. (define-key map " " 'self-insert-command)
  514. map)
  515. "Keymap for minibuffer confirmation of attribution strings.")
  516. (defvar sc-minibuffer-local-map
  517. (let ((map (copy-keymap minibuffer-local-map)))
  518. (define-key map "\C-t" 'sc-toggle-fn)
  519. map)
  520. "Keymap for minibuffer confirmation of attribution strings.")
  521. ;; ======================================================================
  522. ;; utility functions
  523. (defun sc-ask (alist)
  524. "Ask a question in the minibuffer requiring a single character answer.
  525. This function is kind of an extension of `y-or-n-p' where a single
  526. letter is used to answer a question. Question is formed from ALIST
  527. which has members of the form: (WORD . LETTER). WORD is the long
  528. word form, while LETTER is the letter for selecting that answer. The
  529. selected letter is returned, or nil if the question was not answered.
  530. Note that WORD is a string and LETTER is a character. All LETTERs in
  531. the list should be unique."
  532. (let* ((prompt (concat
  533. (mapconcat (lambda (elt) (car elt)) alist ", ")
  534. "? ("
  535. (mapconcat
  536. (lambda (elt) (char-to-string (cdr elt))) alist "/")
  537. ") "))
  538. (p prompt)
  539. (event
  540. (if (fboundp 'allocate-event)
  541. (allocate-event)
  542. nil)))
  543. (while (stringp p)
  544. (if (let ((cursor-in-echo-area t)
  545. (inhibit-quit t))
  546. (message "%s" p)
  547. (setq event (read-event))
  548. (prog1 quit-flag (setq quit-flag nil)))
  549. (progn
  550. (message "%s%s" p (single-key-description event))
  551. (if (fboundp 'deallocate-event)
  552. (deallocate-event event))
  553. (setq quit-flag nil)
  554. (signal 'quit '())))
  555. (let ((char
  556. (if (featurep 'xemacs)
  557. (let* ((key (and (key-press-event-p event) (event-key event)))
  558. (char (and key (event-to-character event))))
  559. char)
  560. event))
  561. elt)
  562. (if char (setq char (downcase char)))
  563. (cond
  564. ((setq elt (rassq char alist))
  565. (message "%s%s" p (car elt))
  566. (setq p (cdr elt)))
  567. ((if (fboundp 'button-release-event-p)
  568. (button-release-event-p event)) ; ignore them
  569. nil)
  570. (t
  571. (message "%s%s" p (single-key-description event))
  572. (if (featurep 'xemacs)
  573. (ding nil 'y-or-n-p)
  574. (ding))
  575. (discard-input)
  576. (if (eq p prompt)
  577. (setq p (concat "Try again. " prompt)))))))
  578. (if (fboundp 'deallocate-event)
  579. (deallocate-event event))
  580. p))
  581. (defun sc-scan-info-alist (alist)
  582. "Find a match in the info alist that matches a regexp in ALIST."
  583. (let ((sc-mumble "")
  584. rtnvalue)
  585. (while alist
  586. (let* ((elem (car alist))
  587. (infokey (car elem))
  588. (infoval (sc-mail-field infokey))
  589. (mlist (cadr elem)))
  590. (while mlist
  591. (let* ((ml-elem (car mlist))
  592. (regexp (car ml-elem))
  593. (thing (cdr ml-elem)))
  594. (if (string-match regexp infoval)
  595. ;; we found a match, time to return
  596. (setq rtnvalue thing
  597. mlist nil
  598. alist nil)
  599. ;; else we didn't find a match
  600. (setq mlist (cdr mlist))))) ;end of mlist loop
  601. (setq alist (cdr alist)))) ;end of alist loop
  602. rtnvalue))
  603. ;; ======================================================================
  604. ;; extract mail field information from headers in reply buffer
  605. ;; holder variables for bc happiness
  606. (defvar sc-mail-headers-start nil
  607. "Start of header fields.")
  608. (defvar sc-mail-headers-end nil
  609. "End of header fields.")
  610. (defvar sc-mail-field-history nil
  611. "For minibuffer completion on mail field queries.")
  612. (defvar sc-mail-field-modification-history nil
  613. "For minibuffer completion on mail field modifications.")
  614. (defvar sc-mail-glom-frame
  615. '((begin (setq sc-mail-headers-start (point)))
  616. ("^From " (sc-mail-check-from) nil nil)
  617. ("^x-attribution:[ \t]+.*$" (sc-mail-fetch-field t) nil t)
  618. ("^\\S +:.*$" (sc-mail-fetch-field) nil t)
  619. ("^$" (list 'abort '(step . 0)))
  620. ("^[ \t]+" (sc-mail-append-field))
  621. (sc-mail-warn-if-non-rfc822-p (sc-mail-error-in-mail-field))
  622. (end (setq sc-mail-headers-end (point))))
  623. "Regi frame for glomming mail header information.")
  624. (put 'sc-mail-glom-frame 'risky-local-variable t)
  625. (defvar curline) ; dynamic bondage
  626. ;; regi functions
  627. ;; http://lists.gnu.org/archive/html/emacs-devel/2009-02/msg00691.html
  628. ;; When rmail replies to a message with full headers visible, the "From "
  629. ;; line can be included.
  630. (defun sc-mail-check-from ()
  631. "Deal with a \"From \" line in the header.
  632. Such a line should only occur at the very start of the headers."
  633. (and sc-mail-warn-if-non-rfc822-p
  634. (/= (point) sc-mail-headers-start)
  635. (sc-mail-error-in-mail-field)))
  636. (defun sc-mail-fetch-field (&optional attribs-p)
  637. "Insert a key and value into `sc-mail-info' alist.
  638. If optional ATTRIBS-P is non-nil, the key/value pair is placed in
  639. `sc-attributions' too."
  640. (if (string-match "^\\(\\S *\\)\\s *:\\s +\\(.*\\)$" curline)
  641. (let* ((key (downcase (match-string-no-properties 1 curline)))
  642. (val (match-string-no-properties 2 curline))
  643. (keyval (cons key val)))
  644. (push keyval sc-mail-info)
  645. (if attribs-p
  646. (push keyval sc-attributions))))
  647. nil)
  648. (defun sc-mail-append-field ()
  649. "Append a continuation line onto the last fetched mail field's info."
  650. (let ((keyval (car sc-mail-info)))
  651. (if (and keyval (string-match "^\\s *\\(.*\\)$" curline))
  652. (setcdr keyval (concat (cdr keyval) " "
  653. (match-string-no-properties 1 curline)))))
  654. nil)
  655. (defun sc-mail-error-in-mail-field ()
  656. "Issue warning that mail headers don't conform to RFC 822."
  657. (let* ((len (min (length curline) 10))
  658. (ellipsis (if (< len (length curline)) "..." ""))
  659. (msg "Mail header \"%s%s\" doesn't conform to RFC 822. skipping..."))
  660. (message msg (substring curline 0 len) ellipsis))
  661. (beep)
  662. (sit-for 2)
  663. nil)
  664. ;; mail header nuking
  665. (defvar sc-mail-last-header-nuked-p nil
  666. "True if the last header was nuked.")
  667. (defun sc-mail-nuke-line ()
  668. "Nuke the current mail header line."
  669. (delete-region (line-beginning-position) (line-beginning-position 2))
  670. '((step . -1)))
  671. (defun sc-mail-nuke-header-line ()
  672. "Delete current-line and set up for possible continuation."
  673. (setq sc-mail-last-header-nuked-p t)
  674. (sc-mail-nuke-line))
  675. (defun sc-mail-nuke-continuation-line ()
  676. "Delete a continuation line if the last header line was deleted."
  677. (if sc-mail-last-header-nuked-p
  678. (sc-mail-nuke-line)))
  679. (defun sc-mail-cleanup-blank-lines ()
  680. "Leave some blank lines after original mail headers are nuked.
  681. The number of lines left is specified by `sc-blank-lines-after-headers'."
  682. (if sc-blank-lines-after-headers
  683. (save-restriction
  684. (widen)
  685. (skip-chars-backward " \t\n")
  686. (forward-line 1)
  687. (delete-blank-lines)
  688. (beginning-of-line)
  689. (if (looking-at "[ \t]*$")
  690. (delete-region (line-beginning-position)
  691. (line-beginning-position 2)))
  692. (insert-char ?\n sc-blank-lines-after-headers)))
  693. nil)
  694. (defun sc-mail-build-nuke-frame ()
  695. "Build the regiframe for nuking mail headers."
  696. (let (every-func entry-func nonentry-func)
  697. (cond
  698. ((eq sc-nuke-mail-headers 'all)
  699. (setq every-func '(progn (forward-line -1) (sc-mail-nuke-line))))
  700. ((eq sc-nuke-mail-headers 'specified)
  701. (setq entry-func '(sc-mail-nuke-header-line)
  702. nonentry-func '(setq sc-mail-last-header-nuked-p nil)))
  703. ((eq sc-nuke-mail-headers 'keep)
  704. (setq entry-func '(setq sc-mail-last-header-nuked-p nil)
  705. nonentry-func '(sc-mail-nuke-header-line)))
  706. ;; we never get far enough to interpret a frame if s-n-m-h == 'none
  707. ((eq sc-nuke-mail-headers 'none))
  708. (t (error "Invalid value for sc-nuke-mail-headers: %s"
  709. sc-nuke-mail-headers))) ; end-cond
  710. (append
  711. (and entry-func
  712. (regi-mapcar sc-nuke-mail-header-list entry-func nil t))
  713. (and nonentry-func (list (list "^\\S +:.*$" nonentry-func)))
  714. (and (not every-func)
  715. '(("^[ \t]+" (sc-mail-nuke-continuation-line))))
  716. '((begin (setq sc-mail-last-header-zapped-p nil)))
  717. '((end (sc-mail-cleanup-blank-lines)))
  718. (and every-func (list (list 'every every-func))))))
  719. ;; mail processing and zapping. this is the top level entry defun to
  720. ;; all header processing.
  721. (defun sc-mail-process-headers (start end)
  722. "Process original mail message's mail headers.
  723. After processing, mail headers may be nuked. Header information is
  724. stored in `sc-mail-info', and any old information is lost unless an
  725. error occurs."
  726. (interactive "r")
  727. (let ((info (copy-alist sc-mail-info))
  728. (attribs (copy-alist sc-attributions)))
  729. (setq sc-mail-info nil
  730. sc-attributions nil)
  731. (regi-interpret sc-mail-glom-frame start end)
  732. (if (null sc-mail-info)
  733. (progn
  734. (message "No mail headers found! Restoring old information.")
  735. (setq sc-mail-info info
  736. sc-attributions attribs))
  737. (regi-interpret (sc-mail-build-nuke-frame)
  738. sc-mail-headers-start sc-mail-headers-end))))
  739. ;; let the user change mail field information
  740. (defun sc-mail-field (field)
  741. "Return the mail header field value associated with FIELD.
  742. If there was no mail header with FIELD as its key, return the value of
  743. `sc-mumble'. FIELD is case insensitive."
  744. (or (cdr (assoc-string field sc-mail-info 'case-fold)) sc-mumble))
  745. (defun sc-mail-field-query (arg)
  746. "View the value of a mail field.
  747. With `\\[universal-argument]', prompts for action on mail field.
  748. Action can be one of: View, Modify, Add, or Delete."
  749. (interactive "P")
  750. (let* ((alist '(("view" . ?v) ("modify" . ?m) ("add" . ?a) ("delete" . ?d)))
  751. (action (if (not arg) ?v (sc-ask alist)))
  752. key)
  753. (if (not action)
  754. ()
  755. (setq key (completing-read
  756. (concat (car (rassq action alist))
  757. " information key: ")
  758. sc-mail-info nil
  759. (if (eq action ?a) nil 'noexit)
  760. nil 'sc-mail-field-history))
  761. (cond
  762. ((eq action ?v)
  763. (message "%s: %s" key (cdr (assoc key sc-mail-info))))
  764. ((eq action ?d)
  765. (setq sc-mail-info (delq (assoc key sc-mail-info) sc-mail-info)))
  766. ((eq action ?m)
  767. (let ((keyval (assoc key sc-mail-info)))
  768. ;; first put initial value onto list if not already there
  769. (if (not (member (cdr keyval)
  770. sc-mail-field-modification-history))
  771. (setq sc-mail-field-modification-history
  772. (cons (cdr keyval) sc-mail-field-modification-history)))
  773. (setcdr keyval (read-string
  774. (concat key ": ") (cdr keyval)
  775. 'sc-mail-field-modification-history))))
  776. ((eq action ?a)
  777. (push (cons key (read-string (concat key ": "))) sc-mail-info))))))
  778. ;; ======================================================================
  779. ;; attributions
  780. (defvar sc-attribution-confirmation-history nil
  781. "History for confirmation of attribution strings.")
  782. (defvar sc-citation-confirmation-history nil
  783. "History for confirmation of attribution prefixes.")
  784. (defun sc-attribs-%@-addresses (from &optional delim)
  785. "Extract the author's email terminus from email address FROM.
  786. Match addresses of the style ``name%[stuff].'' when called with DELIM
  787. of \"%\" and addresses of the style ``[stuff]name@[stuff]'' when
  788. called with DELIM \"@\". If DELIM is nil or not provided, matches
  789. addresses of the style ``name''."
  790. (and (string-match (concat "[-[:alnum:]_.]+" delim) from 0)
  791. (substring from
  792. (match-beginning 0)
  793. (- (match-end 0) (if (null delim) 0 1)))))
  794. (defun sc-attribs-!-addresses (from)
  795. "Extract the author's email terminus from email address FROM.
  796. Match addresses of the style ``[stuff]![stuff]...!name[stuff].''"
  797. (let ((eos (length from))
  798. (mstart (string-match "![-[:alnum:]_.]+\\([^-![:alnum:]_.]\\|$\\)"
  799. from 0))
  800. (mend (match-end 0)))
  801. (and mstart
  802. (substring from (1+ mstart) (- mend (if (= mend eos) 0 1))))))
  803. (defun sc-attribs-<>-addresses (from)
  804. "Extract the author's email terminus from email address FROM.
  805. Match addresses of the style ``<name[stuff]>.''"
  806. (and (string-match "<\\(.*\\)>" from)
  807. (match-string 1 from)))
  808. (defun sc-get-address (from author)
  809. "Get the full email address path from FROM.
  810. AUTHOR is the author's name (which is removed from the address)."
  811. (let ((eos (length from)))
  812. (if (string-match (concat "\\`\"?" (regexp-quote author)
  813. "\"?\\s +") from 0)
  814. (let ((address (substring from (match-end 0) eos)))
  815. (if (and (= (aref address 0) ?<)
  816. (= (aref address (1- (length address))) ?>))
  817. (substring address 1 (1- (length address)))
  818. address))
  819. (if (string-match "[-[:alnum:]!@%._]+" from 0)
  820. (match-string 0 from)
  821. ""))))
  822. (defun sc-attribs-emailname (from)
  823. "Get the email terminus name from FROM."
  824. (or
  825. (sc-attribs-%@-addresses from "%")
  826. (sc-attribs-%@-addresses from "@")
  827. (sc-attribs-!-addresses from)
  828. (sc-attribs-<>-addresses from)
  829. (sc-attribs-%@-addresses from)
  830. (substring from 0 10)))
  831. (defun sc-name-substring (string start end extend)
  832. "Extract the specified substring of STRING from START to END.
  833. EXTEND is the number of characters on each side to extend the
  834. substring."
  835. (and start
  836. (let ((sos (+ start extend))
  837. (eos (- end extend)))
  838. (substring string sos
  839. (or (string-match sc-titlecue-regexp string sos) eos)))))
  840. (defun sc-attribs-extract-namestring (from)
  841. "Extract the name string from FROM.
  842. This should be the author's full name minus an optional title."
  843. ;; FIXME: we probably should use mail-extract-address-components.
  844. (let ((namestring
  845. (or
  846. ;; If there is a <...> in the name,
  847. ;; treat everything before that as the full name.
  848. ;; Even if it contains parens, use the whole thing.
  849. ;; On the other hand, we do look for quotes in the usual way.
  850. (and (string-match " *<.*>" from 0)
  851. (let ((before-angles
  852. (sc-name-substring from 0 (match-beginning 0) 0)))
  853. (if (string-match "\".*\"" before-angles 0)
  854. (sc-name-substring
  855. before-angles (match-beginning 0) (match-end 0) 1)
  856. before-angles)))
  857. (sc-name-substring
  858. from (string-match "(.*)" from 0) (match-end 0) 1)
  859. (sc-name-substring
  860. from (string-match "\".*\"" from 0) (match-end 0) 1)
  861. (sc-name-substring
  862. from (string-match "\\([-.[:alnum:]_]+\\s +\\)+<" from 0)
  863. (match-end 1) 0)
  864. (sc-attribs-emailname from))))
  865. ;; strip off any leading or trailing whitespace
  866. (if namestring
  867. (let ((bos 0)
  868. (eos (1- (length namestring))))
  869. (while (and (<= bos eos)
  870. (memq (aref namestring bos) '(32 ?\t)))
  871. (setq bos (1+ bos)))
  872. (while (and (> eos bos)
  873. (memq (aref namestring eos) '(32 ?\t)))
  874. (setq eos (1- eos)))
  875. (substring namestring bos (1+ eos))))))
  876. (defun sc-attribs-chop-namestring (namestring)
  877. "Convert NAMESTRING to a list of names.
  878. example: (sc-attribs-chop-namestring \"John Xavier Doe\")
  879. => (\"John\" \"Xavier\" \"Doe\")"
  880. (if (string-match "\\([ \t]*\\)\\([^ \t._]+\\)\\([ \t]*\\)" namestring)
  881. (cons (match-string 2 namestring)
  882. (sc-attribs-chop-namestring (substring namestring (match-end 3))))))
  883. (defun sc-attribs-strip-initials (namelist)
  884. "Extract the author's initials from the NAMELIST."
  885. (mapconcat
  886. (lambda (name)
  887. (if (< 0 (length name))
  888. (substring name 0 1)))
  889. namelist ""))
  890. (defun sc-guess-attribution (&optional string)
  891. "Guess attribution string on current line.
  892. If attribution cannot be guessed, nil is returned. Optional STRING if
  893. supplied, is used instead of the line point is on in the current buffer."
  894. (let ((start 0)
  895. (string (or string (buffer-substring (line-beginning-position)
  896. (line-end-position))))
  897. attribution)
  898. (and
  899. (= start (or (string-match sc-citation-leader-regexp string start) -1))
  900. (setq start (match-end 0))
  901. (= start (or (string-match sc-citation-root-regexp string start) 1))
  902. (setq attribution (match-string 0 string)
  903. start (match-end 0))
  904. (= start (or (string-match sc-citation-delimiter-regexp string start) -1))
  905. (setq start (match-end 0))
  906. (= start (or (string-match sc-citation-separator-regexp string start) -1))
  907. attribution)))
  908. (defun sc-attribs-filter-namelist (namelist)
  909. "Filter out noise in NAMELIST according to `sc-name-filter-alist'."
  910. (let ((elements (length namelist))
  911. (position -1)
  912. keepers filtered-list)
  913. (mapc
  914. (lambda (name)
  915. (setq position (1+ position))
  916. (let ((keep-p t))
  917. (mapc
  918. (function
  919. (lambda (filter)
  920. (let ((regexp (car filter))
  921. (pos (cdr filter)))
  922. (if (and (string-match regexp name)
  923. (or (and (numberp pos)
  924. (= pos position))
  925. (and (eq pos 'last)
  926. (= position (1- elements)))
  927. (eq pos 'any)))
  928. (setq keep-p nil)))))
  929. sc-name-filter-alist)
  930. (if keep-p
  931. (setq keepers (cons position keepers)))))
  932. namelist)
  933. (mapc
  934. (lambda (position)
  935. (setq filtered-list (cons (nth position namelist) filtered-list)))
  936. keepers)
  937. filtered-list))
  938. (defun sc-attribs-chop-address (from)
  939. "Extract attribution information from FROM.
  940. This populates the `sc-attributions' with the list of possible attributions."
  941. (if (and (stringp from)
  942. (< 0 (length from)))
  943. (let* ((sc-mumble "")
  944. (namestring (sc-attribs-extract-namestring from))
  945. (namelist (sc-attribs-filter-namelist
  946. (sc-attribs-chop-namestring namestring)))
  947. (revnames (reverse (cdr namelist)))
  948. (firstname (car namelist))
  949. (midnames (reverse (cdr revnames)))
  950. (lastname (car revnames))
  951. (initials (sc-attribs-strip-initials namelist))
  952. (emailname (sc-attribs-emailname from))
  953. (n 1)
  954. author middlenames)
  955. ;; put basic information
  956. (setq
  957. ;; put middle names and build sc-author entry
  958. middlenames (mapconcat
  959. (lambda (midname)
  960. (let ((key-attribs (format "middlename-%d" n))
  961. (key-mail (format "sc-middlename-%d" n)))
  962. (push (cons key-attribs midname) sc-attributions)
  963. (push (cons key-mail midname) sc-mail-info)
  964. (setq n (1+ n))
  965. midname))
  966. midnames " ")
  967. author (concat firstname " " middlenames (and midnames " ") lastname)
  968. sc-attributions (append
  969. (list
  970. (cons "firstname" firstname)
  971. (cons "lastname" lastname)
  972. (cons "emailname" emailname)
  973. (cons "initials" initials))
  974. sc-attributions)
  975. sc-mail-info (append
  976. (list
  977. (cons "sc-firstname" firstname)
  978. (cons "sc-middlenames" middlenames)
  979. (cons "sc-lastname" lastname)
  980. (cons "sc-emailname" emailname)
  981. (cons "sc-initials" initials)
  982. (cons "sc-author" author)
  983. (cons "sc-from-address" (sc-get-address
  984. (sc-mail-field "from")
  985. namestring))
  986. (cons "sc-reply-address" (sc-get-address
  987. (sc-mail-field "reply-to")
  988. namestring))
  989. (cons "sc-sender-address" (sc-get-address
  990. (sc-mail-field "sender")
  991. namestring)))
  992. sc-mail-info)))
  993. ;; from string is empty
  994. (push (cons "sc-author" sc-default-author-name) sc-mail-info)))
  995. (defvar sc-attrib-or-cite nil
  996. "Used to toggle between attribution input or citation input.")
  997. (defun sc-toggle-fn ()
  998. "Toggle between attribution selection and citation selection.
  999. Only used during confirmation."
  1000. (interactive)
  1001. (setq sc-attrib-or-cite (not sc-attrib-or-cite))
  1002. (throw 'sc-reconfirm t))
  1003. (defun sc-select-attribution ()
  1004. "Select an attribution from `sc-attributions'.
  1005. Variables involved in selection process include:
  1006. `sc-preferred-attribution-list'
  1007. `sc-use-only-preference-p'
  1008. `sc-confirm-always-p'
  1009. `sc-default-attribution'
  1010. `sc-attrib-selection-list'.
  1011. Runs the hook `sc-attribs-preselect-hook' before selecting an
  1012. attribution and the hook `sc-attribs-postselect-hook' after making the
  1013. selection but before querying is performed. During
  1014. `sc-attribs-postselect-hook' the variable `citation' is bound to the
  1015. auto-selected citation string and the variable `attribution' is bound
  1016. to the auto-selected attribution string."
  1017. (run-hooks 'sc-attribs-preselect-hook)
  1018. (let ((query-p sc-confirm-always-p)
  1019. attribution citation
  1020. (attriblist sc-preferred-attribution-list))
  1021. ;; first cruise through sc-preferred-attribution-list looking for
  1022. ;; a match in either sc-attributions or sc-mail-info. if the
  1023. ;; element is "sc-consult", then we have to do the alist
  1024. ;; consultation phase
  1025. (while attriblist
  1026. (let* ((preferred (car attriblist)))
  1027. (cond
  1028. ((string= preferred "sc-consult")
  1029. ;; we've been told to consult the attribution vs. mail
  1030. ;; header key alist. we do this until we find a match in
  1031. ;; the sc-attrib-selection-list. if we do not find a match,
  1032. ;; we continue scanning attriblist
  1033. (let ((attrib (sc-scan-info-alist sc-attrib-selection-list)))
  1034. (cond
  1035. ((not attrib)
  1036. (setq attriblist (cdr attriblist)))
  1037. ((stringp attrib)
  1038. (setq attribution attrib
  1039. attriblist nil))
  1040. ((listp attrib)
  1041. (setq attribution (eval attrib))
  1042. (if (stringp attribution)
  1043. (setq attriblist nil)
  1044. (setq attribution nil
  1045. attriblist (cdr attriblist))))
  1046. (t (error "%s did not evaluate to a string or list!"
  1047. "sc-attrib-selection-list")))))
  1048. ((setq attribution (cdr (assoc preferred sc-attributions)))
  1049. (setq attriblist nil))
  1050. (t
  1051. (setq attriblist (cdr attriblist))))))
  1052. ;; if preference was not found, we may use a secondary method to
  1053. ;; find a valid attribution
  1054. (if (and (not attribution)
  1055. (not sc-use-only-preference-p))
  1056. ;; secondary method tries to find a preference in this order
  1057. ;; 1. sc-lastchoice
  1058. ;; 2. x-attribution
  1059. ;; 3. firstname
  1060. ;; 4. lastname
  1061. ;; 5. initials
  1062. ;; 6. first non-empty attribution in alist
  1063. (setq attribution
  1064. (or (cdr (assoc "sc-lastchoice" sc-attributions))
  1065. (cdr (assoc "x-attribution" sc-attributions))
  1066. (cdr (assoc "firstname" sc-attributions))
  1067. (cdr (assoc "lastname" sc-attributions))
  1068. (cdr (assoc "initials" sc-attributions))
  1069. (cdr (car sc-attributions)))))
  1070. ;; still couldn't find an attribution. we're now limited to using
  1071. ;; the default attribution, but we'll force a query when this happens
  1072. (if (not attribution)
  1073. (setq attribution sc-default-attribution
  1074. query-p t))
  1075. ;; create the attribution prefix
  1076. (setq citation (sc-make-citation attribution))
  1077. ;; run the post selection hook before querying the user
  1078. (run-hooks 'sc-attribs-postselect-hook)
  1079. ;; query for confirmation
  1080. (if query-p
  1081. (let* ((query-alist (mapcar (lambda (entry) (list (cdr entry)))
  1082. sc-attributions))
  1083. (minibuffer-local-completion-map
  1084. sc-minibuffer-local-completion-map)
  1085. (minibuffer-local-map sc-minibuffer-local-map)
  1086. (initial attribution)
  1087. (completer-disable t) ; in case completer.el is used
  1088. choice)
  1089. (setq sc-attrib-or-cite nil) ; nil==attribution, t==citation
  1090. (while
  1091. (catch 'sc-reconfirm
  1092. (progn
  1093. (setq choice
  1094. (if sc-attrib-or-cite
  1095. (read-string
  1096. "Enter citation prefix: "
  1097. citation
  1098. 'sc-citation-confirmation-history)
  1099. (completing-read
  1100. "Complete attribution name: "
  1101. query-alist nil nil
  1102. (cons initial 0)
  1103. 'sc-attribution-confirmation-history)))
  1104. nil)))
  1105. (if sc-attrib-or-cite
  1106. ;; since the citation was chosen, we have to guess at
  1107. ;; the attribution
  1108. (setq citation choice
  1109. attribution (or (sc-guess-attribution citation)
  1110. citation))
  1111. (setq citation (sc-make-citation choice)
  1112. attribution choice))))
  1113. ;; its possible that the user wants to downcase the citation and
  1114. ;; attribution
  1115. (if sc-downcase-p
  1116. (setq citation (downcase citation)
  1117. attribution (downcase attribution)))
  1118. ;; set up mail info alist
  1119. (let* ((ckey "sc-citation")
  1120. (akey "sc-attribution")
  1121. (ckeyval (assoc ckey sc-mail-info))
  1122. (akeyval (assoc akey sc-mail-info)))
  1123. (if ckeyval
  1124. (setcdr ckeyval citation)
  1125. (push (cons ckey citation) sc-mail-info))
  1126. (if akeyval
  1127. (setcdr akeyval attribution)
  1128. (push (cons akey attribution) sc-mail-info)))
  1129. ;; set the sc-lastchoice attribution
  1130. (let* ((lkey "sc-lastchoice")
  1131. (lastchoice (assoc lkey sc-attributions)))
  1132. (if lastchoice
  1133. (setcdr lastchoice attribution)
  1134. (push (cons lkey attribution) sc-attributions)))))
  1135. ;; ======================================================================
  1136. ;; filladapt hooks for supercite 3.1. you shouldn't need anything
  1137. ;; extra to make gin-mode understand supercited lines. Even this
  1138. ;; stuff might not be entirely necessary...
  1139. (defun sc-cite-regexp (&optional root-regexp)
  1140. "Return a regexp describing a Supercited line.
  1141. The regexp is the concatenation of `sc-citation-leader-regexp',
  1142. `sc-citation-root-regexp', `sc-citation-delimiter-regexp', and
  1143. `sc-citation-separator-regexp'. If optional ROOT-REGEXP is supplied,
  1144. use it instead of `sc-citation-root-regexp'."
  1145. (concat sc-citation-leader-regexp
  1146. (or root-regexp sc-citation-root-regexp)
  1147. sc-citation-delimiter-regexp
  1148. sc-citation-separator-regexp))
  1149. (defun sc-make-citation (attribution)
  1150. "Make a non-nested citation from ATTRIBUTION."
  1151. (concat sc-citation-leader
  1152. attribution
  1153. sc-citation-delimiter
  1154. sc-citation-separator))
  1155. (defvar filladapt-prefix-table)
  1156. (defun sc-setup-filladapt ()
  1157. "Setup `filladapt-prefix-table' to handle Supercited paragraphs."
  1158. (let* ((fa-sc-elt 'filladapt-supercite-included-text)
  1159. (elt (rassq fa-sc-elt filladapt-prefix-table)))
  1160. (if elt (setcar elt (sc-cite-regexp))
  1161. (message "Filladapt doesn't seem to know about Supercite.")
  1162. (beep))))
  1163. ;; ======================================================================
  1164. ;; citing and unciting regions of text
  1165. (defvar sc-fill-begin 1
  1166. "Buffer position to begin filling.")
  1167. (defvar sc-fill-line-prefix ""
  1168. "Fill prefix of previous line")
  1169. ;; filling
  1170. (defun sc-fill-if-different (&optional prefix)
  1171. "Fill the region bounded by `sc-fill-begin' and point.
  1172. Only fill if optional PREFIX is different than `sc-fill-line-prefix'.
  1173. If `sc-auto-fill-region-p' is nil, do not fill region. If PREFIX is
  1174. not supplied, initialize fill variables. This is useful for a regi
  1175. `begin' frame-entry."
  1176. (if (not prefix)
  1177. (setq sc-fill-line-prefix ""
  1178. sc-fill-begin (line-beginning-position))
  1179. (if (and sc-auto-fill-region-p
  1180. (not (string= prefix sc-fill-line-prefix)))
  1181. (let ((fill-prefix sc-fill-line-prefix))
  1182. (if (not (string= fill-prefix ""))
  1183. (fill-region sc-fill-begin (line-beginning-position)))
  1184. (setq sc-fill-line-prefix prefix
  1185. sc-fill-begin (line-beginning-position)))))
  1186. nil)
  1187. (defun sc-cite-coerce-cited-line ()
  1188. "Coerce a Supercited line to look like our style."
  1189. (let* ((attribution (sc-guess-attribution))
  1190. (regexp (sc-cite-regexp attribution))
  1191. (prefix (sc-make-citation attribution)))
  1192. (if (and attribution
  1193. (looking-at regexp))
  1194. (progn
  1195. (delete-region
  1196. (match-beginning 0)
  1197. (save-excursion
  1198. (goto-char (match-end 0))
  1199. (if (bolp) (forward-char -1))
  1200. (point)))
  1201. (insert prefix)
  1202. (sc-fill-if-different prefix)))
  1203. nil))
  1204. (defun sc-cite-coerce-dumb-citer ()
  1205. "Coerce a non-nested citation that's been cited with a dumb nesting citer."
  1206. (delete-region (match-beginning 1) (match-end 1))
  1207. (beginning-of-line)
  1208. (sc-cite-coerce-cited-line))
  1209. (defun sc-guess-nesting (&optional string)
  1210. "Guess the citation nesting on the current line.
  1211. If nesting cannot be guessed, nil is returned. Optional STRING if
  1212. supplied, is used instead of the line point is on in the current
  1213. buffer."
  1214. (let ((start 0)
  1215. (string (or string (buffer-substring (line-beginning-position)
  1216. (line-end-position))))
  1217. nesting)
  1218. (and
  1219. (= start (or (string-match sc-citation-leader-regexp string start) -1))
  1220. (setq start (match-end 0))
  1221. (= start (or (string-match sc-citation-delimiter-regexp string start) -1))
  1222. (setq nesting (match-string 0 string)
  1223. start (match-end 0))
  1224. (= start (or (string-match sc-citation-separator-regexp string start) -1))
  1225. nesting)))
  1226. (defun sc-add-citation-level ()
  1227. "Add a citation level for nested citation style w/ coercion."
  1228. (let* ((nesting (sc-guess-nesting))
  1229. (citation (make-string (1+ (length nesting))
  1230. (string-to-char sc-citation-delimiter)))
  1231. (prefix (concat sc-citation-leader citation sc-citation-separator)))
  1232. (if (looking-at (sc-cite-regexp ""))
  1233. (delete-region (match-beginning 0) (match-end 0)))
  1234. (insert prefix)
  1235. (sc-fill-if-different prefix)))
  1236. (defun sc-cite-line (&optional citation)
  1237. "Cite a single line of uncited text.
  1238. Optional CITATION overrides any citation automatically selected."
  1239. (if sc-fixup-whitespace-p
  1240. (fixup-whitespace))
  1241. (let ((prefix (or citation
  1242. (cdr (assoc "sc-citation" sc-mail-info))
  1243. sc-default-attribution)))
  1244. (insert prefix)
  1245. (sc-fill-if-different prefix))
  1246. nil)
  1247. (defun sc-uncite-line ()
  1248. "Remove citation from current line."
  1249. (let ((cited (looking-at (sc-cite-regexp))))
  1250. (if cited
  1251. (delete-region (match-beginning 0) (match-end 0))))
  1252. nil)
  1253. (defun sc-recite-line (regexp)
  1254. "Remove citation matching REGEXP from current line and recite line."
  1255. (let ((cited (looking-at (concat "^" regexp)))
  1256. (prefix (cdr (assoc "sc-citation" sc-mail-info))))
  1257. (if cited
  1258. (delete-region (match-beginning 0) (match-end 0)))
  1259. (insert (or prefix sc-default-attribution))
  1260. (sc-fill-if-different prefix))
  1261. nil)
  1262. ;; interactive functions
  1263. (defun sc-cite-region (start end &optional confirm-p interactive)
  1264. "Cite a region delineated by START and END.
  1265. If optional CONFIRM-P is non-nil, the attribution is confirmed before
  1266. its use in the citation string. This function first runs
  1267. `sc-pre-cite-hook'.
  1268. When called interactively, the optional arg INTERACTIVE is non-nil,
  1269. and that means call `sc-select-attribution' too."
  1270. (interactive "r\nP\np")
  1271. (undo-boundary)
  1272. (let ((frame (sc-scan-info-alist sc-cite-frame-alist))
  1273. (sc-confirm-always-p (if confirm-p t sc-confirm-always-p)))
  1274. (if (and frame (symbolp frame))
  1275. (setq frame (symbol-value frame)))
  1276. (or frame (setq frame sc-default-cite-frame))
  1277. (run-hooks 'sc-pre-cite-hook)
  1278. (if interactive
  1279. (sc-select-attribution))
  1280. (regi-interpret frame start end)))
  1281. (defun sc-uncite-region (start end)
  1282. "Uncite a region delineated by START and END.
  1283. First runs `sc-pre-uncite-hook'."
  1284. (interactive "r")
  1285. (undo-boundary)
  1286. (let ((frame (sc-scan-info-alist sc-uncite-frame-alist)))
  1287. (if (and frame (symbolp frame))
  1288. (setq frame (symbol-value frame)))
  1289. (or frame (setq frame sc-default-uncite-frame))
  1290. (run-hooks 'sc-pre-uncite-hook)
  1291. (regi-interpret frame start end)))
  1292. (defun sc-recite-region (start end)
  1293. "Recite a region delineated by START and END.
  1294. First runs `sc-pre-recite-hook'."
  1295. (interactive "r")
  1296. (let ((sc-confirm-always-p t))
  1297. (sc-select-attribution))
  1298. (undo-boundary)
  1299. (let ((frame (sc-scan-info-alist sc-recite-frame-alist)))
  1300. (if (and frame (symbolp frame))
  1301. (setq frame (symbol-value frame)))
  1302. (or frame (setq frame sc-default-recite-frame))
  1303. (run-hooks 'sc-pre-recite-hook)
  1304. (regi-interpret frame start end)))
  1305. ;; ======================================================================
  1306. ;; building headers
  1307. (defun sc-hdr (prefix field &optional sep return-nil-p)
  1308. "Returns a concatenation of PREFIX and FIELD.
  1309. If FIELD is not a string or is the empty string, the empty string will
  1310. be returned. Optional third argument SEP is concatenated on the end if
  1311. it is a string. Returns empty string, unless optional RETURN-NIL-P is
  1312. non-nil."
  1313. (if (and (stringp field)
  1314. (not (string= field "")))
  1315. (concat prefix field (or sep ""))
  1316. (and (not return-nil-p) "")))
  1317. (defun sc-whofrom ()
  1318. "Return the value of (sc-mail-field \"from\") or nil."
  1319. (let ((sc-mumble nil))
  1320. (sc-mail-field "from")))
  1321. (defun sc-no-header ()
  1322. "Does nothing. Use this instead of nil to get a blank header."
  1323. ())
  1324. (declare-function mh-in-header-p "mh-utils" ())
  1325. (defun sc-no-blank-line-or-header ()
  1326. "Similar to `sc-no-header' except it removes the preceding blank line."
  1327. (and (not (bobp))
  1328. (eolp)
  1329. (progn (forward-line -1)
  1330. (or (= (point)
  1331. (save-excursion
  1332. (rfc822-goto-eoh)
  1333. (line-beginning-position 2)))
  1334. (and (eq major-mode 'mh-letter-mode)
  1335. (mh-in-header-p))))
  1336. (progn
  1337. (forward-line)
  1338. (kill-line))))
  1339. (defun sc-header-on-said ()
  1340. "\"On <date>, <from> said:\" unless:
  1341. 1. the \"from\" field cannot be found, in which case nothing is inserted;
  1342. 2. the \"date\" field is missing in which case only the from part is printed."
  1343. (let ((sc-mumble "")
  1344. (whofrom (sc-whofrom)))
  1345. (if whofrom
  1346. (insert sc-reference-tag-string
  1347. (sc-hdr "On " (sc-mail-field "date") ", ")
  1348. whofrom " said:\n"))))
  1349. (defun sc-header-inarticle-writes ()
  1350. "\"In article <message-id>, <from> writes:\"
  1351. Treats \"message-id\" and \"from\" fields similar to `sc-header-on-said'."
  1352. (let ((sc-mumble "")
  1353. (whofrom (sc-mail-field "from")))
  1354. (if whofrom
  1355. (insert sc-reference-tag-string
  1356. (sc-hdr "In article " (sc-mail-field "message-id") ", ")
  1357. whofrom " writes:\n"))))
  1358. (defun sc-header-regarding-adds ()
  1359. "\"Regarding <subject>; <from> adds:\"
  1360. Treats \"subject\" and \"from\" fields similar to `sc-header-on-said'."
  1361. (let ((sc-mumble "")
  1362. (whofrom (sc-whofrom)))
  1363. (if whofrom
  1364. (insert sc-reference-tag-string
  1365. (sc-hdr "Regarding " (sc-mail-field "subject") "; ")
  1366. whofrom " adds:\n"))))
  1367. (defun sc-header-attributed-writes ()
  1368. "\"<sc-attribution>\" == <sc-author> <address> writes:
  1369. Treats these fields in a similar manner to `sc-header-on-said'."
  1370. (let ((sc-mumble "")
  1371. (whofrom (sc-whofrom)))
  1372. (if whofrom
  1373. (insert sc-reference-tag-string
  1374. (sc-hdr "\"" (sc-mail-field "sc-attribution") "\" == ")
  1375. (sc-hdr "" (sc-mail-field "sc-author") " ")
  1376. (or (sc-hdr "<" (sc-mail-field "sc-from-address") ">" t)
  1377. (sc-hdr "<" (sc-mail-field "sc-reply-address") ">" t)
  1378. "")
  1379. " writes:\n"))))
  1380. (defun sc-header-author-writes ()
  1381. "<sc-author> writes:"
  1382. (let ((sc-mumble "")
  1383. (whofrom (sc-whofrom)))
  1384. (if whofrom
  1385. (insert sc-reference-tag-string
  1386. (sc-hdr "" (sc-mail-field "sc-author"))
  1387. " writes:\n"))))
  1388. (defun sc-header-verbose ()
  1389. "Very verbose, some say gross."
  1390. (let ((sc-mumble "")
  1391. (whofrom (sc-whofrom))
  1392. (tag sc-reference-tag-string))
  1393. (if whofrom
  1394. (insert (sc-hdr (concat tag "On ") (sc-mail-field "date") ",\n")
  1395. (or (sc-hdr tag (sc-mail-field "sc-author") "\n" t)
  1396. (concat tag whofrom "\n"))
  1397. (sc-hdr (concat tag "from the organization of ")
  1398. (sc-mail-field "organization") "\n")
  1399. (let ((rtag (concat tag "who can be reached at: ")))
  1400. (or (sc-hdr rtag (sc-mail-field "sc-from-address") "\n" t)
  1401. (sc-hdr rtag (sc-mail-field "sc-reply-address") "\n" t)
  1402. ""))
  1403. (sc-hdr
  1404. (concat tag "(whose comments are cited below with \"")
  1405. (sc-mail-field "sc-citation") "\"),\n")
  1406. (sc-hdr (concat tag "had this to say in article ")
  1407. (sc-mail-field "message-id") "\n")
  1408. (sc-hdr (concat tag "in newsgroups ")
  1409. (sc-mail-field "newsgroups") "\n")
  1410. (sc-hdr (concat tag "concerning the subject of ")
  1411. (sc-mail-field "subject") "\n")
  1412. (sc-hdr (concat tag "(see ")
  1413. (sc-mail-field "references")
  1414. " for more details)\n")))))
  1415. ;; ======================================================================
  1416. ;; header rewrites
  1417. (defconst sc-electric-bufname " *sc-erefs* "
  1418. "Supercite electric reference mode's buffer name.")
  1419. (defvar sc-eref-style 0
  1420. "Current electric reference style.")
  1421. (defun sc-valid-index-p (index)
  1422. "Returns INDEX if it is a valid index into `sc-rewrite-header-list'.
  1423. Otherwise returns nil."
  1424. ;; a number, and greater than or equal to zero
  1425. ;; less than or equal to the last index
  1426. (and (natnump index)
  1427. (< index (length sc-rewrite-header-list))
  1428. index))
  1429. (defun sc-eref-insert-selected (&optional nomsg)
  1430. "Insert the selected reference header in the current buffer.
  1431. Optional NOMSG, if non-nil, inhibits printing messages, unless an
  1432. error occurs."
  1433. (let ((ref (nth sc-eref-style sc-rewrite-header-list)))
  1434. (condition-case err
  1435. (progn
  1436. (eval ref)
  1437. (let ((lines (count-lines (point-min) (point-max))))
  1438. (or nomsg (message "Ref header %d [%d line%s]: %s"
  1439. sc-eref-style lines
  1440. (if (= lines 1) "" "s")
  1441. ref))))
  1442. (void-function
  1443. (progn (message
  1444. "Symbol's function definition is void: %s (Header %d)"
  1445. (cadr err) sc-eref-style)
  1446. (beep))))))
  1447. (defun sc-electric-mode (&optional style)
  1448. "Mode for viewing Supercite reference headers. Commands are:
  1449. \n\\{sc-electric-mode-map}
  1450. `sc-electric-mode' is not intended to be run interactively, but rather
  1451. accessed through Supercite's electric reference feature. See
  1452. `sc-insert-reference' for more details. Optional STYLE is the initial
  1453. header style to use, unless not supplied or invalid, in which case
  1454. `sc-preferred-header-style' is used."
  1455. (let ((info sc-mail-info))
  1456. (setq sc-eref-style
  1457. (or (sc-valid-index-p style)
  1458. (sc-valid-index-p sc-preferred-header-style)
  1459. 0))
  1460. (get-buffer-create sc-electric-bufname)
  1461. ;; set up buffer and enter command loop
  1462. (save-excursion
  1463. (save-window-excursion
  1464. (pop-to-buffer sc-electric-bufname)
  1465. (kill-all-local-variables)
  1466. (let ((sc-mail-info info)
  1467. (buffer-read-only t)
  1468. (mode-name "SC Electric Refs")
  1469. (major-mode 'sc-electric-mode))
  1470. (use-local-map sc-electric-mode-map)
  1471. (sc-eref-show sc-eref-style)
  1472. (run-mode-hooks 'sc-electric-mode-hook)
  1473. (recursive-edit))))
  1474. (and sc-eref-style
  1475. (sc-eref-insert-selected))
  1476. (kill-buffer sc-electric-bufname)))
  1477. ;; functions for electric reference mode
  1478. (defun sc-eref-show (index)
  1479. "Show reference INDEX in `sc-rewrite-header-list'."
  1480. (let ((msg "No %ing reference headers in list.")
  1481. (last (length sc-rewrite-header-list)))
  1482. (setq sc-eref-style
  1483. (cond
  1484. ((sc-valid-index-p index) index)
  1485. ((< index 0)
  1486. (if sc-electric-circular-p
  1487. (1- last)
  1488. (progn (error msg "preced") 0)))
  1489. ((>= index last)
  1490. (if sc-electric-circular-p
  1491. 0
  1492. (progn (error msg "follow") (1- last))))))
  1493. (with-current-buffer sc-electric-bufname
  1494. (let ((inhibit-read-only t))
  1495. (erase-buffer)
  1496. (goto-char (point-min))
  1497. (sc-eref-insert-selected)
  1498. ;; now shrink the window to just contain the electric reference
  1499. ;; header.
  1500. (let ((hdrlines (count-lines (point-min) (point-max)))
  1501. (winlines (1- (window-height))))
  1502. (if (/= hdrlines winlines)
  1503. (if (> hdrlines winlines)
  1504. ;; we have to enlarge the window
  1505. (enlarge-window (- hdrlines winlines))
  1506. ;; we have to shrink the window
  1507. (shrink-window (- winlines (max hdrlines
  1508. window-min-height))))))))))
  1509. (defun sc-eref-next ()
  1510. "Display next reference in other buffer."
  1511. (interactive)
  1512. (sc-eref-show (1+ sc-eref-style)))
  1513. (defun sc-eref-prev ()
  1514. "Display previous reference in other buffer."
  1515. (interactive)
  1516. (sc-eref-show (1- sc-eref-style)))
  1517. (defun sc-eref-setn ()
  1518. "Set reference header selected as preferred."
  1519. (interactive)
  1520. (setq sc-preferred-header-style sc-eref-style)
  1521. (message "Preferred reference style set to header %d." sc-eref-style))
  1522. (defun sc-eref-goto (refnum)
  1523. "Show reference style indexed by REFNUM.
  1524. If REFNUM is an invalid index, don't go to that reference and return
  1525. nil."
  1526. (interactive "NGoto Reference: ")
  1527. (if (sc-valid-index-p refnum)
  1528. (sc-eref-show refnum)
  1529. (error "Invalid reference: %d. (Range: [%d .. %d])"
  1530. refnum 0 (1- (length sc-rewrite-header-list)))))
  1531. (defun sc-eref-jump ()
  1532. "Set reference header to preferred header."
  1533. (interactive)
  1534. (sc-eref-show sc-preferred-header-style))
  1535. (defun sc-eref-abort ()
  1536. "Exit from electric reference mode without inserting reference."
  1537. (interactive)
  1538. (setq sc-eref-style nil)
  1539. (exit-recursive-edit))
  1540. (defun sc-eref-exit ()
  1541. "Exit from electric reference mode and insert selected reference."
  1542. (interactive)
  1543. (exit-recursive-edit))
  1544. (defun sc-insert-reference (arg)
  1545. "Insert, at point, a reference header in the body of the reply.
  1546. Numeric ARG indicates which header style from `sc-rewrite-header-list'
  1547. to use when rewriting the header. No supplied ARG indicates use of
  1548. `sc-preferred-header-style'.
  1549. With just `\\[universal-argument]', electric reference insert mode is
  1550. entered, regardless of the value of `sc-electric-references-p'. See
  1551. `sc-electric-mode' for more information."
  1552. (interactive "P")
  1553. (if (consp arg)
  1554. (sc-electric-mode)
  1555. (let ((preference (or (sc-valid-index-p arg)
  1556. (sc-valid-index-p sc-preferred-header-style)
  1557. sc-preferred-header-style
  1558. 0)))
  1559. (if sc-electric-references-p
  1560. (sc-electric-mode preference)
  1561. (sc-eref-insert-selected t)))))
  1562. ;; ======================================================================
  1563. ;; variable toggling
  1564. (defun sc-raw-mode-toggle ()
  1565. "Toggle, in one fell swoop, two important SC variables:
  1566. `sc-fixup-whitespace-p' and `sc-auto-fill-region-p'"
  1567. (interactive)
  1568. (setq sc-fixup-whitespace-p (not sc-fixup-whitespace-p)
  1569. sc-auto-fill-region-p (not sc-auto-fill-region-p))
  1570. (force-mode-line-update))
  1571. (defun sc-toggle-var (variable)
  1572. "Boolean toggle VARIABLE's value.
  1573. VARIABLE must be a bound symbol. nil values change to t, non-nil
  1574. values are changed to nil."
  1575. (message "%s changed from %s to %s"
  1576. variable (symbol-value variable)
  1577. (set variable (not (symbol-value variable)))))
  1578. (defun sc-set-variable (var)
  1579. "Set the Supercite VARIABLE.
  1580. This function mimics `set-variable', except that the variable to set
  1581. is determined non-interactively. The value is queried for in the
  1582. minibuffer exactly the same way that `set-variable' does it.
  1583. You can see the current value of the variable when the minibuffer is
  1584. querying you by typing `C-h'. Note that the format is changed
  1585. slightly from that used by `set-variable' -- the current value is
  1586. printed just after the variable's name instead of at the bottom of the
  1587. help window."
  1588. (let* ((minibuffer-help-form '(funcall myhelp))
  1589. (myhelp
  1590. (lambda ()
  1591. (with-output-to-temp-buffer "*Help*"
  1592. (prin1 var)
  1593. (if (boundp var)
  1594. (let ((print-length 20))
  1595. (princ "\t(Current value: ")
  1596. (prin1 (symbol-value var))
  1597. (princ ")")))
  1598. (princ "\n\nDocumentation:\n")
  1599. (princ (substring (documentation-property
  1600. var
  1601. 'variable-documentation)
  1602. 1))
  1603. (with-current-buffer standard-output
  1604. (help-mode))
  1605. nil))))
  1606. (set var (eval-minibuffer (format "Set %s to value: " var)))))
  1607. (defmacro sc-toggle-symbol (rootname)
  1608. `(defun ,(intern (concat "sc-T-" rootname)) ()
  1609. (interactive)
  1610. (sc-toggle-var ',(intern (concat "sc-" rootname "-p")))))
  1611. (defmacro sc-setvar-symbol (rootname)
  1612. `(defun ,(intern (concat "sc-S-" rootname)) ()
  1613. (interactive)
  1614. (sc-set-variable ',(intern (concat "sc-" rootname)))))
  1615. (sc-toggle-symbol "confirm-always")
  1616. (sc-toggle-symbol "downcase")
  1617. (sc-toggle-symbol "electric-references")
  1618. (sc-toggle-symbol "auto-fill-region")
  1619. (sc-toggle-symbol "mail-nuke-blank-lines")
  1620. (sc-toggle-symbol "nested-citation")
  1621. (sc-toggle-symbol "electric-circular")
  1622. (sc-toggle-symbol "use-only-preferences")
  1623. (sc-toggle-symbol "fixup-whitespace")
  1624. (sc-setvar-symbol "preferred-attribution-list")
  1625. (sc-setvar-symbol "preferred-header-style")
  1626. (sc-setvar-symbol "mail-nuke-mail-headers")
  1627. (sc-setvar-symbol "mail-header-nuke-list")
  1628. (sc-setvar-symbol "cite-region-limit")
  1629. (defun sc-T-describe ()
  1630. "
  1631. Supercite provides a number of key bindings which simplify the process
  1632. of setting or toggling certain variables controlling its operation.
  1633. Note on function names in this list: all functions of the form
  1634. `sc-S-<name>' actually call `sc-set-variable' on the corresponding
  1635. `sc-<name>' variable. All functions of the form `sc-T-<name>' call
  1636. `sc-toggle-var' on the corresponding `sc-<name>-p' variable.
  1637. \\{sc-T-keymap}"
  1638. (interactive)
  1639. (describe-function 'sc-T-describe))
  1640. ;; ======================================================================
  1641. ;; published interface to mail and news readers
  1642. (define-minor-mode sc-minor-mode nil
  1643. :group 'supercite
  1644. :lighter (" SC" (sc-auto-fill-region-p
  1645. (":f" (sc-fixup-whitespace-p "w"))
  1646. (sc-fixup-whitespace-p ":w")))
  1647. :keymap `((,sc-mode-map-prefix . ,sc-mode-map)))
  1648. ;;;###autoload
  1649. (defun sc-cite-original ()
  1650. "Workhorse citing function which performs the initial citation.
  1651. This is callable from the various mail and news readers' reply
  1652. function according to the agreed upon standard. See the associated
  1653. info node `(SC)Top' for more details.
  1654. `sc-cite-original' does not do any yanking of the
  1655. original message but it does require a few things:
  1656. 1) The reply buffer is the current buffer.
  1657. 2) The original message has been yanked and inserted into the
  1658. reply buffer.
  1659. 3) Verbose mail headers from the original message have been
  1660. inserted into the reply buffer directly before the text of the
  1661. original message.
  1662. 4) Point is at the beginning of the verbose headers.
  1663. 5) Mark is at the end of the body of text to be cited.
  1664. The region need not be active (and typically isn't when this
  1665. function is called). Also, the hook `sc-pre-hook' is run before,
  1666. and `sc-post-hook' is run after the guts of this function."
  1667. (run-hooks 'sc-pre-hook)
  1668. (sc-minor-mode 1)
  1669. (undo-boundary)
  1670. ;; grab point and mark since the region is probably not active when
  1671. ;; this function gets automatically called. we want point to be a
  1672. ;; mark so any deleting before point works properly
  1673. (let* ((zmacs-regions nil) ; for XEemacs
  1674. (mark-active t) ; for Emacs
  1675. (point (point-marker))
  1676. (mark (copy-marker (mark-marker))))
  1677. ;; make sure point comes before mark, not all functions are
  1678. ;; interactive "r"
  1679. (if (< mark point)
  1680. (let ((tmp point))
  1681. (setq point mark
  1682. mark tmp)))
  1683. ;; first process mail headers, and populate sc-mail-info
  1684. (sc-mail-process-headers point mark)
  1685. ;; now get possible attributions
  1686. (sc-attribs-chop-address (or (sc-mail-field "from")
  1687. (sc-mail-field "reply")
  1688. (sc-mail-field "reply-to")
  1689. (sc-mail-field "sender")))
  1690. ;; select the attribution
  1691. (sc-select-attribution)
  1692. ;; cite the region, but first check the value of sc-cite-region-limit
  1693. (let ((linecnt (count-lines point mark)))
  1694. (and sc-cite-region-limit
  1695. (if (or (not (numberp sc-cite-region-limit))
  1696. (<= linecnt sc-cite-region-limit))
  1697. (progn
  1698. ;; cite the region and insert the header rewrite
  1699. (sc-cite-region point mark)
  1700. (goto-char point)
  1701. (let ((sc-eref-style (or sc-preferred-header-style 0)))
  1702. (if sc-electric-references-p
  1703. (sc-electric-mode sc-eref-style)
  1704. (sc-eref-insert-selected t))))
  1705. (beep)
  1706. (message
  1707. "Region not cited. %d lines exceeds sc-cite-region-limit: %d"
  1708. linecnt sc-cite-region-limit))))
  1709. ;; finally, free the point-marker
  1710. (set-marker point nil)
  1711. (set-marker mark nil))
  1712. (run-hooks 'sc-post-hook))
  1713. ;; ======================================================================
  1714. ;; bug reporting and miscellaneous commands
  1715. (defun sc-open-line (arg)
  1716. "Like `open-line', but insert the citation prefix at the front of the line.
  1717. With numeric ARG, inserts that many new lines."
  1718. (interactive "p")
  1719. (save-excursion
  1720. (let ((start (point))
  1721. (prefix (or (progn (beginning-of-line)
  1722. (if (looking-at (sc-cite-regexp))
  1723. (match-string 0)))
  1724. "")))
  1725. (goto-char start)
  1726. (open-line arg)
  1727. (forward-line 1)
  1728. (while (< 0 arg)
  1729. (insert prefix)
  1730. (forward-line 1)
  1731. (setq arg (1- arg))))))
  1732. (defun sc-insert-citation (arg)
  1733. "Insert citation string at beginning of current line if not already cited.
  1734. With `\\[universal-argument]' insert citation even if line is already
  1735. cited."
  1736. (interactive "P")
  1737. (save-excursion
  1738. (beginning-of-line)
  1739. (if (or (not (looking-at (sc-cite-regexp)))
  1740. (looking-at "^[ \t]*$")
  1741. (consp arg))
  1742. (insert (sc-mail-field "sc-citation"))
  1743. (error "Line is already cited"))))
  1744. ;; The argument logic here is crazy.
  1745. (defun sc-version (message)
  1746. "Return the current Supercite version.
  1747. If MESSAGE is non-nil (interactively, with no prefix argument),
  1748. echoes the version in the minibuffer. Otherwise, inserts the
  1749. version at point."
  1750. (interactive (list (not current-prefix-arg)))
  1751. (let ((verstr (format "Using Supercite.el %s" emacs-version)))
  1752. (if message
  1753. (message verstr)
  1754. (insert "`sc-version' says: " verstr))))
  1755. (make-obsolete 'sc-version 'emacs-version "23.1")
  1756. (defun sc-describe ()
  1757. "Read the Supercite info node."
  1758. (interactive)
  1759. (info "(SC)top"))
  1760. (make-obsolete 'sc-describe "read the SC manual using `info'." "23.1")
  1761. (define-obsolete-function-alias 'sc-submit-bug-report 'report-emacs-bug "23.1")
  1762. ;; useful stuff
  1763. (provide 'supercite)
  1764. (run-hooks 'sc-load-hook)
  1765. ;;; supercite.el ends here