org-list.el 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985
  1. ;;; org-list.el --- Plain lists for Org-mode
  2. ;;
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Bastien Guerry <bzg AT altern DOT org>
  8. ;; Keywords: outlines, hypermedia, calendar, wp
  9. ;; Homepage: http://orgmode.org
  10. ;; Version: 7.5
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;; This file contains the code dealing with plain lists in Org-mode.
  28. ;; The fundamental idea behind lists work is to use structures. A
  29. ;; structure is a snapshot of the list, in the shape of data tree (see
  30. ;; `org-list-struct').
  31. ;; Once the list structure is stored, it is possible to make changes
  32. ;; directly on it or get useful information about the list, with the
  33. ;; two helper functions, namely `org-list-parents-alist' and
  34. ;; `org-list-prevs-alist', and using accessors or methods.
  35. ;; Structure is eventually applied to the buffer with
  36. ;; `org-list-write-struct'. This function repairs (bullets,
  37. ;; indentation, checkboxes) the structure before applying it. It
  38. ;; should be called near the end of any function working on
  39. ;; structures.
  40. ;; Thus, a function applying to lists should usually follow this
  41. ;; template:
  42. ;; 1. Verify point is in a list and grab item beginning (with the same
  43. ;; function `org-in-item-p'). If the function requires the cursor
  44. ;; to be at item's bullet, `org-at-item-p' is more selective. If
  45. ;; the cursor is amidst the buffer, it is possible to find the
  46. ;; closest item with `org-list-search-backward', or
  47. ;; `org-list-search-forward', applied to `org-item-beginning-re'.
  48. ;; 2. Get list structure with `org-list-struct'.
  49. ;; 3. Compute one, or both, helper functions,
  50. ;; (`org-list-parents-alist', `org-list-prevs-alist') depending on
  51. ;; needed accessors.
  52. ;; 4. Proceed with the modifications, using methods and accessors.
  53. ;; 5. Verify and apply structure to buffer, using
  54. ;; `org-list-write-struct'. Possibly use
  55. ;; `org-update-checkbox-count-maybe' if checkboxes might have been
  56. ;; modified.
  57. ;; Computing a list structure can be a costly operation on huge lists
  58. ;; (a few thousand lines long). Thus, code should follow the rule :
  59. ;; "collect once, use many". As a corollary, it is usally a bad idea
  60. ;; to use directly an interactive function inside the code, as those,
  61. ;; being independant entities, read the whole list structure another
  62. ;; time.
  63. ;;; Code:
  64. (eval-when-compile
  65. (require 'cl))
  66. (require 'org-macs)
  67. (require 'org-compat)
  68. (defvar org-blank-before-new-entry)
  69. (defvar org-complex-heading-regexp)
  70. (defvar org-description-max-indent)
  71. (defvar org-drawer-regexp)
  72. (defvar org-drawers)
  73. (defvar org-M-RET-may-split-line)
  74. (defvar org-odd-levels-only)
  75. (defvar org-outline-regexp)
  76. (defvar org-ts-regexp)
  77. (defvar org-ts-regexp-both)
  78. (declare-function org-at-heading-p "org" (&optional ignored))
  79. (declare-function org-back-over-empty-lines "org" ())
  80. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  81. (declare-function org-combine-plists "org" (&rest plists))
  82. (declare-function org-count "org" (cl-item cl-seq))
  83. (declare-function org-current-level "org" ())
  84. (declare-function org-entry-get "org"
  85. (pom property &optional inherit literal-nil))
  86. (declare-function org-get-indentation "org" (&optional line))
  87. (declare-function org-icompleting-read "org" (&rest args))
  88. (declare-function org-in-regexp "org" (re &optional nlines visually))
  89. (declare-function org-in-regexps-block-p "org"
  90. (start-re end-re &optional bound))
  91. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  92. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  93. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  94. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  95. (declare-function org-level-increment "org" ())
  96. (declare-function org-narrow-to-subtree "org" ())
  97. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  98. (declare-function org-previous-line-empty-p "org" ())
  99. (declare-function org-remove-if "org" (predicate seq))
  100. (declare-function org-show-subtree "org" ())
  101. (declare-function org-time-string-to-seconds "org" (s))
  102. (declare-function org-timer-hms-to-secs "org-timer" (hms))
  103. (declare-function org-timer-item "org-timer" (&optional arg))
  104. (declare-function org-trim "org" (s))
  105. (declare-function org-uniquify "org" (list))
  106. (declare-function outline-invisible-p "outline" (&optional pos))
  107. (declare-function outline-flag-region "outline" (from to flag))
  108. (declare-function outline-next-heading "outline" ())
  109. (declare-function outline-previous-heading "outline" ())
  110. ;;; Configuration variables
  111. (defgroup org-plain-lists nil
  112. "Options concerning plain lists in Org-mode."
  113. :tag "Org Plain lists"
  114. :group 'org-structure)
  115. (defcustom org-cycle-include-plain-lists t
  116. "When t, make TAB cycle visibility on plain list items.
  117. Cycling plain lists works only when the cursor is on a plain list
  118. item. When the cursor is on an outline heading, plain lists are
  119. treated as text. This is the most stable way of handling this,
  120. which is why it is the default.
  121. When this is the symbol `integrate', then during cycling, plain
  122. list items will *temporarily* be interpreted as outline headlines
  123. with a level given by 1000+i where i is the indentation of the
  124. bullet. This setting can lead to strange effects when switching
  125. visibility to `children', because the first \"child\" in a
  126. subtree decides what children should be listed. If that first
  127. \"child\" is a plain list item with an implied large level
  128. number, all true children and grand children of the outline
  129. heading will be exposed in a children' view."
  130. :group 'org-plain-lists
  131. :type '(choice
  132. (const :tag "Never" nil)
  133. (const :tag "With cursor in plain list (recommended)" t)
  134. (const :tag "As children of outline headings" integrate)))
  135. (defcustom org-list-demote-modify-bullet nil
  136. "Default bullet type installed when demoting an item.
  137. This is an association list, for each bullet type, this alist will point
  138. to the bullet that should be used when this item is demoted.
  139. For example,
  140. (setq org-list-demote-modify-bullet
  141. '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
  142. will make
  143. + Movies
  144. + Silence of the Lambs
  145. + My Cousin Vinny
  146. + Books
  147. + The Hunt for Red October
  148. + The Road to Omaha
  149. into
  150. + Movies
  151. - Silence of the Lambs
  152. - My Cousin Vinny
  153. + Books
  154. - The Hunt for Red October
  155. - The Road to Omaha"
  156. :group 'org-plain-lists
  157. :type '(repeat
  158. (cons
  159. (choice :tag "If the current bullet is "
  160. (const "-")
  161. (const "+")
  162. (const "*")
  163. (const "1.")
  164. (const "1)"))
  165. (choice :tag "demotion will change it to"
  166. (const "-")
  167. (const "+")
  168. (const "*")
  169. (const "1.")
  170. (const "1)")))))
  171. (defcustom org-plain-list-ordered-item-terminator t
  172. "The character that makes a line with leading number an ordered list item.
  173. Valid values are ?. and ?\). To get both terminators, use t. While
  174. ?. may look nicer, it creates the danger that a line with leading
  175. number may be incorrectly interpreted as an item. ?\) therefore is
  176. the safe choice."
  177. :group 'org-plain-lists
  178. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  179. (const :tag "paren like in \"2)\"" ?\))
  180. (const :tab "both" t)))
  181. (defcustom org-alphabetical-lists nil
  182. "Non-nil means single character alphabetical bullets are allowed.
  183. Both uppercase and lowercase are handled. Lists with more than 26
  184. items will fallback to standard numbering. Alphabetical counters
  185. like \"[@c]\" will be recognized."
  186. :group 'org-plain-lists
  187. :type 'boolean)
  188. (defcustom org-list-two-spaces-after-bullet-regexp nil
  189. "A regular expression matching bullets that should have 2 spaces after them.
  190. When nil, no bullet will have two spaces after them.
  191. When a string, it will be used as a regular expression. When the
  192. bullet type of a list is changed, the new bullet type will be
  193. matched against this regexp. If it matches, there will be two
  194. spaces instead of one after the bullet in each item of the list."
  195. :group 'org-plain-lists
  196. :type '(choice
  197. (const :tag "never" nil)
  198. (regexp)))
  199. (defcustom org-list-ending-method 'both
  200. "Determine where plain lists should end.
  201. Valid values are: `regexp', `indent' or `both'.
  202. When set to `regexp', Org will look into two variables,
  203. `org-empty-line-terminates-plain-lists' and the more general
  204. `org-list-end-regexp', to determine what will end lists.
  205. When set to `indent', a list will end whenever a line following
  206. an item, but not starting one, is less or equally indented than
  207. the first item of the list.
  208. When set to `both', each of the preceding methods is applied to
  209. determine lists endings. This is the default method."
  210. :group 'org-plain-lists
  211. :type '(choice
  212. (const :tag "With a regexp defining ending" regexp)
  213. (const :tag "With indentation of regular (no bullet) text" indent)
  214. (const :tag "With both methods" both)))
  215. (defcustom org-empty-line-terminates-plain-lists nil
  216. "Non-nil means an empty line ends all plain list levels.
  217. This variable only makes sense if `org-list-ending-method' is set
  218. to `regexp' or `both'. This is then equivalent to set
  219. `org-list-end-regexp' to \"^[ \\t]*$\"."
  220. :group 'org-plain-lists
  221. :type 'boolean)
  222. (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
  223. "Regexp matching the end of all plain list levels.
  224. It must start with \"^\" and end with \"\\n\". It defaults to 2
  225. blank lines. `org-empty-line-terminates-plain-lists' has
  226. precedence over it."
  227. :group 'org-plain-lists
  228. :type 'string)
  229. (defcustom org-list-automatic-rules '((bullet . t)
  230. (checkbox . t)
  231. (indent . t))
  232. "Non-nil means apply set of rules when acting on lists.
  233. By default, automatic actions are taken when using
  234. \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
  235. \\[org-shiftmetaright], \\[org-shiftmetaleft],
  236. \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
  237. \\[org-insert-todo-heading]. You can disable individually these
  238. rules by setting them to nil. Valid rules are:
  239. bullet when non-nil, cycling bullet do not allow lists at
  240. column 0 to have * as a bullet and descriptions lists
  241. to be numbered.
  242. checkbox when non-nil, checkbox statistics is updated each time
  243. you either insert a new checkbox or toggle a checkbox.
  244. It also prevents from inserting a checkbox in a
  245. description item.
  246. indent when non-nil, indenting or outdenting list top-item
  247. with its subtree will move the whole list and
  248. outdenting a list whose bullet is * to column 0 will
  249. change that bullet to \"-\"."
  250. :group 'org-plain-lists
  251. :type '(alist :tag "Sets of rules"
  252. :key-type
  253. (choice
  254. (const :tag "Bullet" bullet)
  255. (const :tag "Checkbox" checkbox)
  256. (const :tag "Indent" indent))
  257. :value-type
  258. (boolean :tag "Activate" :value t)))
  259. (defvar org-checkbox-statistics-hook nil
  260. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  261. This hook runs even if checkbox rule in
  262. `org-list-automatic-rules' does not apply, so it can be used to
  263. implement alternative ways of collecting statistics
  264. information.")
  265. (defcustom org-hierarchical-checkbox-statistics t
  266. "Non-nil means checkbox statistics counts only the state of direct children.
  267. When nil, all boxes below the cookie are counted.
  268. This can be set to nil on a per-node basis using a COOKIE_DATA property
  269. with the word \"recursive\" in the value."
  270. :group 'org-plain-lists
  271. :type 'boolean)
  272. (defcustom org-description-max-indent 20
  273. "Maximum indentation for the second line of a description list.
  274. When the indentation would be larger than this, it will become
  275. 5 characters instead."
  276. :group 'org-plain-lists
  277. :type 'integer)
  278. (defcustom org-list-radio-list-templates
  279. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  280. % END RECEIVE ORGLST %n
  281. \\begin{comment}
  282. #+ORGLST: SEND %n org-list-to-latex
  283. -
  284. \\end{comment}\n")
  285. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  286. @c END RECEIVE ORGLST %n
  287. @ignore
  288. #+ORGLST: SEND %n org-list-to-texinfo
  289. -
  290. @end ignore\n")
  291. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  292. <!-- END RECEIVE ORGLST %n -->
  293. <!--
  294. #+ORGLST: SEND %n org-list-to-html
  295. -
  296. -->\n"))
  297. "Templates for radio lists in different major modes.
  298. All occurrences of %n in a template will be replaced with the name of the
  299. list, obtained by prompting the user."
  300. :group 'org-plain-lists
  301. :type '(repeat
  302. (list (symbol :tag "Major mode")
  303. (string :tag "Format"))))
  304. (defvar org-list-forbidden-blocks '("example" "verse" "src")
  305. "Names of blocks where lists are not allowed.
  306. Names must be in lower case.")
  307. (defvar org-list-export-context '(block inlinetask)
  308. "Context types where lists will be interpreted during export.
  309. Valid types are `drawer', `inlinetask' and `block'. More
  310. specifically, type `block' is determined by the variable
  311. `org-list-forbidden-blocks'.")
  312. ;;; Predicates and regexps
  313. (defconst org-list-end-re (if org-empty-line-terminates-plain-lists
  314. "^[ \t]*\n"
  315. org-list-end-regexp)
  316. "Regex corresponding to the end of a list.
  317. It depends on `org-empty-line-terminates-plain-lists'.")
  318. (defconst org-list-full-item-re
  319. (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]*\\)"
  320. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\]\\)?"
  321. "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
  322. "\\(?:\\(.*\\)[ \t]+::[ \t]+\\)?")
  323. "Matches a list item and puts everything into groups:
  324. group 1: bullet
  325. group 2: counter
  326. group 3: checkbox
  327. group 4: description tag")
  328. (defun org-item-re ()
  329. "Return the correct regular expression for plain lists."
  330. (let ((term (cond
  331. ((eq org-plain-list-ordered-item-terminator t) "[.)]")
  332. ((= org-plain-list-ordered-item-terminator ?\)) ")")
  333. ((= org-plain-list-ordered-item-terminator ?.) "\\.")
  334. (t "[.)]")))
  335. (alpha (if org-alphabetical-lists "\\|[A-Za-z]" "")))
  336. (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha "\\)" term
  337. "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
  338. (defsubst org-item-beginning-re ()
  339. "Regexp matching the beginning of a plain list item."
  340. (concat "^" (org-item-re)))
  341. (defun org-list-at-regexp-after-bullet-p (regexp)
  342. "Is point at a list item with REGEXP after bullet?"
  343. (and (org-at-item-p)
  344. (save-excursion
  345. (goto-char (match-end 0))
  346. ;; Ignore counter if any
  347. (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
  348. (goto-char (match-end 0)))
  349. (looking-at regexp))))
  350. (defun org-in-item-p ()
  351. "Return item beginning position when in a plain list, nil otherwise.
  352. This checks `org-list-ending-method'."
  353. (save-excursion
  354. (beginning-of-line)
  355. (let* ((case-fold-search t)
  356. (context (org-list-context))
  357. (lim-up (car context))
  358. (inlinetask-re (and (featurep 'org-inlinetask)
  359. (org-inlinetask-outline-regexp)))
  360. (item-re (org-item-re))
  361. ;; Indentation isn't meaningful when point starts at an empty
  362. ;; line or an inline task.
  363. (ind-ref (if (or (looking-at "^[ \t]*$")
  364. (and inlinetask-re (looking-at inlinetask-re)))
  365. 10000
  366. (org-get-indentation))))
  367. (cond
  368. ((eq (nth 2 context) 'invalid) nil)
  369. ((looking-at item-re) (point))
  370. (t
  371. ;; Detect if cursor in amidst `org-list-end-re'. First, count
  372. ;; number HL of hard lines it takes, then call `org-in-regexp'
  373. ;; to compute its boundaries END-BOUNDS. When point is
  374. ;; in-between, move cursor before regexp beginning.
  375. (let ((hl 0) (i -1) end-bounds)
  376. (when (and (not (eq org-list-ending-method 'indent))
  377. (progn
  378. (while (setq i (string-match
  379. "[\r\n]" org-list-end-re (1+ i)))
  380. (setq hl (1+ hl)))
  381. (setq end-bounds (org-in-regexp org-list-end-re hl)))
  382. (>= (point) (car end-bounds))
  383. (< (point) (cdr end-bounds)))
  384. (goto-char (car end-bounds))
  385. (forward-line -1)))
  386. ;; Look for an item, less indented that reference line if
  387. ;; `org-list-ending-method' isn't `regexp'.
  388. (catch 'exit
  389. (while t
  390. (let ((ind (org-get-indentation)))
  391. (cond
  392. ;; This is exactly what we want.
  393. ((and (looking-at item-re)
  394. (or (< ind ind-ref)
  395. (eq org-list-ending-method 'regexp)))
  396. (throw 'exit (point)))
  397. ;; At upper bound of search or looking at the end of a
  398. ;; previous list: search is over.
  399. ((<= (point) lim-up) (throw 'exit nil))
  400. ((and (not (eq org-list-ending-method 'indent))
  401. (looking-at org-list-end-re))
  402. (throw 'exit nil))
  403. ;; Skip blocks, drawers, inline-tasks, blank lines
  404. ((looking-at "^[ \t]*#\\+end_")
  405. (re-search-backward "^[ \t]*#\\+begin_" nil t))
  406. ((looking-at "^[ \t]*:END:")
  407. (re-search-backward org-drawer-regexp nil t)
  408. (beginning-of-line))
  409. ((and inlinetask-re (looking-at inlinetask-re))
  410. (org-inlinetask-goto-beginning)
  411. (forward-line -1))
  412. ((looking-at "^[ \t]*$") (forward-line -1))
  413. ;; Text at column 0 cannot belong to a list: stop.
  414. ((zerop ind) (throw 'exit nil))
  415. ;; Normal text less indented than reference line, take
  416. ;; it as new reference.
  417. ((< ind ind-ref)
  418. (setq ind-ref ind)
  419. (forward-line -1))
  420. (t (forward-line -1)))))))))))
  421. (defun org-at-item-p ()
  422. "Is point in a line starting a hand-formatted item?"
  423. (save-excursion
  424. (beginning-of-line)
  425. (and (looking-at (org-item-re))
  426. (not (eq (nth 2 (org-list-context)) 'invalid)))))
  427. (defun org-at-item-bullet-p ()
  428. "Is point at the bullet of a plain list item?"
  429. (and (org-at-item-p)
  430. (not (member (char-after) '(?\ ?\t)))
  431. (< (point) (match-end 0))))
  432. (defun org-at-item-timer-p ()
  433. "Is point at a line starting a plain list item with a timer?"
  434. (org-list-at-regexp-after-bullet-p
  435. "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  436. (defun org-at-item-description-p ()
  437. "Is point at a description list item?"
  438. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
  439. (defun org-at-item-checkbox-p ()
  440. "Is point at a line starting a plain-list item with a checklet?"
  441. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  442. (defun org-at-item-counter-p ()
  443. "Is point at a line starting a plain-list item with a counter?"
  444. (and (org-at-item-p)
  445. (looking-at org-list-full-item-re)
  446. (match-string 2)))
  447. ;;; Structures and helper functions
  448. (defun org-list-context ()
  449. "Determine context, and its boundaries, around point.
  450. Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
  451. are boundaries and CONTEXT is a symbol among `drawer', `block',
  452. `invalid', `inlinetask' and nil.
  453. Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
  454. (save-match-data
  455. (save-excursion
  456. (org-with-limited-levels
  457. (beginning-of-line)
  458. (let ((case-fold-search t) (pos (point)) beg end context-type
  459. ;; Get positions of surrounding headings. This is the
  460. ;; default context.
  461. (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t))
  462. (point)))
  463. (point-min)))
  464. (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
  465. ;; Is point inside a drawer?
  466. (let ((end-re "^[ \t]*:END:")
  467. ;; Can't use org-drawers-regexp as this function
  468. ;; might be called in buffers not in Org mode
  469. (beg-re (concat "^[ \t]*:\\("
  470. (mapconcat 'regexp-quote org-drawers "\\|")
  471. "\\):[ \t]*$")))
  472. (when (save-excursion
  473. (and (not (looking-at beg-re))
  474. (not (looking-at end-re))
  475. (setq beg (and (re-search-backward beg-re lim-up t)
  476. (1+ (point-at-eol))))
  477. (setq end (or (and (re-search-forward end-re lim-down t)
  478. (1- (match-beginning 0)))
  479. lim-down))
  480. (>= end pos)))
  481. (setq lim-up beg lim-down end context-type 'drawer)))
  482. ;; Is point strictly in a block, and of which type?
  483. (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type)
  484. (when (save-excursion
  485. (and (not (looking-at block-re))
  486. (setq beg (and (re-search-backward block-re lim-up t)
  487. (1+ (point-at-eol))))
  488. (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
  489. (setq type (downcase (match-string 1)))
  490. (goto-char beg)
  491. (setq end (or (and (re-search-forward block-re lim-down t)
  492. (1- (point-at-bol)))
  493. lim-down))
  494. (>= end pos)
  495. (equal (downcase (match-string 1)) "end")))
  496. (setq lim-up beg lim-down end
  497. context-type (if (member type org-list-forbidden-blocks)
  498. 'invalid 'block))))
  499. ;; Is point in an inlinetask?
  500. (when (and (featurep 'org-inlinetask)
  501. (save-excursion
  502. (let* ((beg-re (org-inlinetask-outline-regexp))
  503. (end-re (concat beg-re "END[ \t]*$")))
  504. (and (not (looking-at "^\\*+"))
  505. (setq beg (and (re-search-backward beg-re lim-up t)
  506. (1+ (point-at-eol))))
  507. (not (looking-at end-re))
  508. (setq end (and (re-search-forward end-re lim-down t)
  509. (1- (match-beginning 0))))
  510. (> (point) pos)))))
  511. (setq lim-up beg lim-down end context-type 'inlinetask))
  512. ;; Return context boundaries and type.
  513. (list lim-up lim-down context-type))))))
  514. (defun org-list-struct ()
  515. "Return structure of list at point.
  516. A list structure is an alist where key is point at item, and
  517. values are:
  518. 1. indentation,
  519. 2. bullet with trailing whitespace,
  520. 3. bullet counter, if any,
  521. 4. checkbox, if any,
  522. 5. position at item end,
  523. 6. description tag, if any.
  524. Thus the following list, where numbers in parens are
  525. point-at-bol:
  526. - [X] first item (1)
  527. 1. sub-item 1 (18)
  528. 5. [@5] sub-item 2 (34)
  529. some other text belonging to first item (55)
  530. - last item (97)
  531. + tag :: description (109)
  532. (131)
  533. will get the following structure:
  534. \(\(1 0 \"- \" nil \"[X]\" nil 97\)
  535. \(18 2 \"1. \" nil nil nil 34\)
  536. \(34 2 \"5. \" \"5\" nil nil 55\)
  537. \(97 0 \"- \" nil nil nil 131\)
  538. \(109 2 \"+ \" nil nil \"tag\" 131\)
  539. Assume point is at an item."
  540. (save-excursion
  541. (beginning-of-line)
  542. (let* ((case-fold-search t)
  543. (context (org-list-context))
  544. (lim-up (car context))
  545. (lim-down (nth 1 context))
  546. (text-min-ind 10000)
  547. (item-re (org-item-re))
  548. (drawers-re (concat "^[ \t]*:\\("
  549. (mapconcat 'regexp-quote org-drawers "\\|")
  550. "\\):[ \t]*$"))
  551. (inlinetask-re (and (featurep 'org-inlinetask)
  552. (org-inlinetask-outline-regexp)))
  553. (beg-cell (cons (point) (org-get-indentation)))
  554. ind itm-lst itm-lst-2 end-lst end-lst-2 struct
  555. (assoc-at-point
  556. (function
  557. ;; Return association at point.
  558. (lambda (ind)
  559. (looking-at org-list-full-item-re)
  560. (list (point)
  561. ind
  562. (match-string-no-properties 1) ; bullet
  563. (match-string-no-properties 2) ; counter
  564. (match-string-no-properties 3) ; checkbox
  565. (match-string-no-properties 4))))) ; description tag
  566. (end-before-blank
  567. (function
  568. ;; Ensure list ends at the first blank line.
  569. (lambda ()
  570. (skip-chars-backward " \r\t\n")
  571. (min (1+ (point-at-eol)) lim-down)))))
  572. ;; 1. Read list from starting item to its beginning, and save
  573. ;; top item position and indentation in BEG-CELL. Also store
  574. ;; ending position of items in END-LST.
  575. (save-excursion
  576. (catch 'exit
  577. (while t
  578. (let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
  579. (org-get-indentation))))
  580. (cond
  581. ((<= (point) lim-up)
  582. ;; At upward limit: if we ended at an item, store it,
  583. ;; else dimiss useless data recorded above BEG-CELL.
  584. ;; Jump to part 2.
  585. (throw 'exit
  586. (setq itm-lst
  587. (if (or (not (looking-at item-re))
  588. (get-text-property (point) 'org-example))
  589. (memq (assq (car beg-cell) itm-lst) itm-lst)
  590. (setq beg-cell (cons (point) ind))
  591. (cons (funcall assoc-at-point ind) itm-lst)))))
  592. ;; At a verbatim block, go before its beginning. Move
  593. ;; from eol to ensure `previous-single-property-change'
  594. ;; will return a value.
  595. ((get-text-property (point) 'org-example)
  596. (goto-char (previous-single-property-change
  597. (point-at-eol) 'org-example nil lim-up))
  598. (forward-line -1))
  599. ;; Looking at a list ending regexp. Dismiss useless
  600. ;; data recorded above BEG-CELL. Jump to part 2.
  601. ((and (not (eq org-list-ending-method 'indent))
  602. (looking-at org-list-end-re))
  603. (throw 'exit
  604. (setq itm-lst
  605. (memq (assq (car beg-cell) itm-lst) itm-lst))))
  606. ;; Skip blocks, drawers, inline tasks, blank lines
  607. ;; along the way.
  608. ((looking-at "^[ \t]*#\\+end_")
  609. (re-search-backward "^[ \t]*#\\+begin_" nil t))
  610. ((looking-at "^[ \t]*:END:")
  611. (re-search-backward drawers-re nil t)
  612. (beginning-of-line))
  613. ((and inlinetask-re (looking-at inlinetask-re))
  614. (org-inlinetask-goto-beginning)
  615. (forward-line -1))
  616. ((looking-at "^[ \t]*$")
  617. (forward-line -1))
  618. ((looking-at item-re)
  619. ;; Point is at an item. Add data to ITM-LST. It may
  620. ;; also end a previous item: save it in END-LST. If
  621. ;; ind is less or equal than BEG-CELL and there is no
  622. ;; end at this ind or lesser, this item becomes the
  623. ;; new BEG-CELL.
  624. (push (funcall assoc-at-point ind) itm-lst)
  625. (push (cons ind (point)) end-lst)
  626. (when (or (and (eq org-list-ending-method 'regexp)
  627. (<= ind (cdr beg-cell)))
  628. (< ind text-min-ind))
  629. (setq beg-cell (cons (point) ind)))
  630. (forward-line -1))
  631. ;; From there, point is not at an item. Unless ending
  632. ;; method is `regexp', interpret line's indentation:
  633. ;; - text at column 0 is necessarily out of any list.
  634. ;; Dismiss data recorded above BEG-CELL. Jump to
  635. ;; part 2.
  636. ;; - any other case, it can possibly be an ending
  637. ;; position for an item above. Save it and proceed.
  638. ((eq org-list-ending-method 'regexp) (forward-line -1))
  639. ((zerop ind)
  640. (throw 'exit
  641. (setq itm-lst
  642. (memq (assq (car beg-cell) itm-lst) itm-lst))))
  643. (t
  644. (when (< ind text-min-ind) (setq text-min-ind ind))
  645. (push (cons ind (point)) end-lst)
  646. (forward-line -1)))))))
  647. ;; 2. Read list from starting point to its end, that is until we
  648. ;; get out of context, or a non-item line is less or equally
  649. ;; indented that BEG-CELL's cdr. Also store ending position
  650. ;; of items in END-LST-2.
  651. (catch 'exit
  652. (while t
  653. (let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
  654. (org-get-indentation))))
  655. (cond
  656. ((>= (point) lim-down)
  657. ;; At downward limit: this is de facto the end of the
  658. ;; list. Save point as an ending position, and jump to
  659. ;; part 3.
  660. (throw 'exit
  661. (push (cons 0 (funcall end-before-blank)) end-lst-2)))
  662. ;; At a verbatim block, move to its end. Point is at bol
  663. ;; and 'org-example property is set by whole lines:
  664. ;; `next-single-property-change' always return a value.
  665. ((get-text-property (point) 'org-example)
  666. (goto-char
  667. (next-single-property-change (point) 'org-example nil lim-down)))
  668. ;; Looking at a list ending regexp. Save point as an
  669. ;; ending position and jump to part 3.
  670. ((and (not (eq org-list-ending-method 'indent))
  671. (looking-at org-list-end-re))
  672. (throw 'exit (push (cons 0 (point)) end-lst-2)))
  673. ;; Skip blocks, drawers, inline tasks and blank lines
  674. ;; along the way
  675. ((looking-at "^[ \t]*#\\+begin_")
  676. (re-search-forward "^[ \t]*#\\+end_")
  677. (forward-line 1))
  678. ((looking-at drawers-re)
  679. (re-search-forward "^[ \t]*:END:" nil t)
  680. (forward-line 1))
  681. ((and inlinetask-re (looking-at inlinetask-re))
  682. (org-inlinetask-goto-end))
  683. ((looking-at "^[ \t]*$")
  684. (forward-line 1))
  685. ((looking-at item-re)
  686. ;; Point is at an item. Add data to ITM-LST-2. It may also
  687. ;; end a previous item, so save it in END-LST-2.
  688. (push (funcall assoc-at-point ind) itm-lst-2)
  689. (push (cons ind (point)) end-lst-2)
  690. (forward-line 1))
  691. ;; From there, point is not at an item. If ending method
  692. ;; is not `regexp', two situations are of interest:
  693. ;; - ind is lesser or equal than BEG-CELL's. The list is
  694. ;; over. Store point as an ending position and jump to
  695. ;; part 3.
  696. ;; - ind is lesser or equal than previous item's. This
  697. ;; is an ending position. Store it and proceed.
  698. ((eq org-list-ending-method 'regexp) (forward-line 1))
  699. ((<= ind (cdr beg-cell))
  700. (throw 'exit
  701. (push (cons 0 (funcall end-before-blank)) end-lst-2)))
  702. ((<= ind (nth 1 (car itm-lst-2)))
  703. (push (cons ind (point)) end-lst-2)
  704. (forward-line 1))
  705. (t (forward-line 1))))))
  706. (setq struct (append itm-lst (cdr (nreverse itm-lst-2))))
  707. (setq end-lst (append end-lst (cdr (nreverse end-lst-2))))
  708. ;; 3. Correct ill-formed lists by ensuring top item is the least
  709. ;; indented.
  710. (let ((min-ind (nth 1 (car struct))))
  711. (mapc (lambda (item)
  712. (let ((ind (nth 1 item))
  713. (bul (nth 2 item)))
  714. (when (< ind min-ind)
  715. (setcar (cdr item) min-ind)
  716. ;; Modify bullet to be sure item will be modified
  717. (setcar (nthcdr 2 item) (org-trim bul)))))
  718. struct))
  719. ;; 4. Associate each item to its end pos.
  720. (org-list-struct-assoc-end struct end-lst)
  721. ;; 5. Return STRUCT
  722. struct)))
  723. (defun org-list-struct-assoc-end (struct end-list)
  724. "Associate proper ending point to items in STRUCT.
  725. END-LIST is a pseudo-alist where car is indentation and cdr is
  726. ending position.
  727. This function modifies STRUCT."
  728. (let ((endings end-list))
  729. (mapc
  730. (lambda (elt)
  731. (let ((pos (car elt))
  732. (ind (nth 1 elt)))
  733. ;; Remove end candidates behind current item
  734. (while (or (<= (cdar endings) pos))
  735. (pop endings))
  736. ;; Add end position to item assoc
  737. (let ((old-end (nthcdr 6 elt))
  738. (new-end (assoc-default ind endings '<=)))
  739. (if old-end
  740. (setcar old-end new-end)
  741. (setcdr elt (append (cdr elt) (list new-end)))))))
  742. struct)))
  743. (defun org-list-prevs-alist (struct)
  744. "Return alist between item and previous item in STRUCT."
  745. (let ((item-end-alist (mapcar (lambda (e) (cons (car e) (nth 6 e)))
  746. struct)))
  747. (mapcar (lambda (e)
  748. (let ((prev (car (rassq (car e) item-end-alist))))
  749. (cons (car e) prev)))
  750. struct)))
  751. (defun org-list-parents-alist (struct)
  752. "Return alist between item and parent in STRUCT."
  753. (let ((ind-to-ori (list (list (nth 1 (car struct)))))
  754. (prev-pos (list (caar struct))))
  755. (cons prev-pos
  756. (mapcar (lambda (item)
  757. (let ((pos (car item))
  758. (ind (nth 1 item))
  759. (prev-ind (caar ind-to-ori)))
  760. (push pos prev-pos)
  761. (cond
  762. ((> prev-ind ind)
  763. (setq ind-to-ori
  764. (member (assq ind ind-to-ori) ind-to-ori))
  765. (cons pos (cdar ind-to-ori)))
  766. ((< prev-ind ind)
  767. (let ((origin (nth 1 prev-pos)))
  768. (push (cons ind origin) ind-to-ori)
  769. (cons pos origin)))
  770. (t (cons pos (cdar ind-to-ori))))))
  771. (cdr struct)))))
  772. ;;; Accessors
  773. (defsubst org-list-get-nth (n key struct)
  774. "Return the Nth value of KEY in STRUCT."
  775. (nth n (assq key struct)))
  776. (defun org-list-set-nth (n key struct new)
  777. "Set the Nth value of KEY in STRUCT to NEW.
  778. \nThis function modifies STRUCT."
  779. (setcar (nthcdr n (assq key struct)) new))
  780. (defsubst org-list-get-ind (item struct)
  781. "Return indentation of ITEM in STRUCT."
  782. (org-list-get-nth 1 item struct))
  783. (defun org-list-set-ind (item struct ind)
  784. "Set indentation of ITEM in STRUCT to IND.
  785. \nThis function modifies STRUCT."
  786. (org-list-set-nth 1 item struct ind))
  787. (defsubst org-list-get-bullet (item struct)
  788. "Return bullet of ITEM in STRUCT."
  789. (org-list-get-nth 2 item struct))
  790. (defun org-list-set-bullet (item struct bullet)
  791. "Set bullet of ITEM in STRUCT to BULLET.
  792. \nThis function modifies STRUCT."
  793. (org-list-set-nth 2 item struct bullet))
  794. (defsubst org-list-get-counter (item struct)
  795. "Return counter of ITEM in STRUCT."
  796. (org-list-get-nth 3 item struct))
  797. (defsubst org-list-get-checkbox (item struct)
  798. "Return checkbox of ITEM in STRUCT or nil."
  799. (org-list-get-nth 4 item struct))
  800. (defun org-list-set-checkbox (item struct checkbox)
  801. "Set checkbox of ITEM in STRUCT to CHECKBOX.
  802. \nThis function modifies STRUCT."
  803. (org-list-set-nth 4 item struct checkbox))
  804. (defsubst org-list-get-tag (item struct)
  805. "Return end position of ITEM in STRUCT."
  806. (org-list-get-nth 5 item struct))
  807. (defun org-list-get-item-end (item struct)
  808. "Return end position of ITEM in STRUCT."
  809. (org-list-get-nth 6 item struct))
  810. (defun org-list-get-item-end-before-blank (item struct)
  811. "Return point at end of ITEM in STRUCT, before any blank line.
  812. Point returned is at end of line."
  813. (save-excursion
  814. (goto-char (org-list-get-item-end item struct))
  815. (skip-chars-backward " \r\t\n")
  816. (point-at-eol)))
  817. (defun org-list-get-parent (item struct parents)
  818. "Return parent of ITEM or nil.
  819. STRUCT is the list structure. PARENTS is the alist of parents, as
  820. returned by `org-list-parents-alist'."
  821. (let ((parents (or parents (org-list-parents-alist struct))))
  822. (cdr (assq item parents))))
  823. (defun org-list-has-child-p (item struct)
  824. "Non-nil if ITEM has a child.
  825. STRUCT is the list structure.
  826. Value returned is the position of the first child of ITEM."
  827. (let ((ind (org-list-get-ind item struct))
  828. (child-maybe (car (nth 1 (member (assq item struct) struct)))))
  829. (when (and child-maybe
  830. (< ind (org-list-get-ind child-maybe struct)))
  831. child-maybe)))
  832. (defun org-list-get-next-item (item struct prevs)
  833. "Return next item in same sub-list as ITEM, or nil.
  834. STRUCT is the list structure. PREVS is the alist of previous
  835. items, as returned by `org-list-prevs-alist'."
  836. (car (rassq item prevs)))
  837. (defun org-list-get-prev-item (item struct prevs)
  838. "Return previous item in same sub-list as ITEM, or nil.
  839. STRUCT is the list structure. PREVS is the alist of previous
  840. items, as returned by `org-list-prevs-alist'."
  841. (cdr (assq item prevs)))
  842. (defun org-list-get-subtree (item struct)
  843. "List all items having ITEM as a common ancestor, or nil.
  844. STRUCT is the list structure."
  845. (let* ((item-end (org-list-get-item-end item struct))
  846. (sub-struct (cdr (member (assq item struct) struct)))
  847. subtree)
  848. (catch 'exit
  849. (mapc (lambda (e)
  850. (let ((pos (car e)))
  851. (if (< pos item-end) (push pos subtree) (throw 'exit nil))))
  852. sub-struct))
  853. (nreverse subtree)))
  854. (defun org-list-get-all-items (item struct prevs)
  855. "List all items in the same sub-list as ITEM.
  856. STRUCT is the list structure. PREVS is the alist of previous
  857. items, as returned by `org-list-prevs-alist'."
  858. (let ((prev-item item)
  859. (next-item item)
  860. before-item after-item)
  861. (while (setq prev-item (org-list-get-prev-item prev-item struct prevs))
  862. (push prev-item before-item))
  863. (while (setq next-item (org-list-get-next-item next-item struct prevs))
  864. (push next-item after-item))
  865. (append before-item (list item) (nreverse after-item))))
  866. (defun org-list-get-children (item struct parents)
  867. "List all children of ITEM, or nil.
  868. STRUCT is the list structure. PARENTS is the alist of parents, as
  869. returned by `org-list-parents-alist'."
  870. (let (all child)
  871. (while (setq child (car (rassq item parents)))
  872. (setq parents (cdr (member (assq child parents) parents)))
  873. (push child all))
  874. (nreverse all)))
  875. (defun org-list-get-top-point (struct)
  876. "Return point at beginning of list.
  877. STRUCT is the list structure."
  878. (caar struct))
  879. (defun org-list-get-bottom-point (struct)
  880. "Return point at bottom of list.
  881. STRUCT is the list structure."
  882. (apply 'max
  883. (mapcar (lambda (e) (org-list-get-item-end (car e) struct)) struct)))
  884. (defun org-list-get-list-begin (item struct prevs)
  885. "Return point at beginning of sub-list ITEM belongs.
  886. STRUCT is the list structure. PREVS is the alist of previous
  887. items, as returned by `org-list-prevs-alist'."
  888. (let ((first-item item) prev-item)
  889. (while (setq prev-item (org-list-get-prev-item first-item struct prevs))
  890. (setq first-item prev-item))
  891. first-item))
  892. (defalias 'org-list-get-first-item 'org-list-get-list-begin)
  893. (defun org-list-get-last-item (item struct prevs)
  894. "Return point at last item of sub-list ITEM belongs.
  895. STRUCT is the list structure. PREVS is the alist of previous
  896. items, as returned by `org-list-prevs-alist'."
  897. (let ((last-item item) next-item)
  898. (while (setq next-item (org-list-get-next-item last-item struct prevs))
  899. (setq last-item next-item))
  900. last-item))
  901. (defun org-list-get-list-end (item struct prevs)
  902. "Return point at end of sub-list ITEM belongs.
  903. STRUCT is the list structure. PREVS is the alist of previous
  904. items, as returned by `org-list-prevs-alist'."
  905. (org-list-get-item-end (org-list-get-last-item item struct prevs) struct))
  906. (defun org-list-get-list-type (item struct prevs)
  907. "Return the type of the list containing ITEM, as a symbol.
  908. STRUCT is the list structure. PREVS is the alist of previous
  909. items, as returned by `org-list-prevs-alist'.
  910. Possible types are `descriptive', `ordered' and `unordered'. The
  911. type is determined by the first item of the list."
  912. (let ((first (org-list-get-list-begin item struct prevs)))
  913. (cond
  914. ((org-list-get-tag first struct) 'descriptive)
  915. ((string-match "[[:alnum:]]" (org-list-get-bullet first struct)) 'ordered)
  916. (t 'unordered))))
  917. ;;; Searching
  918. (defun org-list-search-generic (search re bound noerr)
  919. "Search a string in valid contexts for lists.
  920. Arguments SEARCH, RE, BOUND and NOERR are similar to those used
  921. in `re-search-forward'."
  922. (catch 'exit
  923. (let ((origin (point)))
  924. (while t
  925. ;; 1. No match: return to origin or bound, depending on NOERR.
  926. (unless (funcall search re bound noerr)
  927. (throw 'exit (and (goto-char (if (memq noerr '(t nil)) origin bound))
  928. nil)))
  929. ;; 2. Match in an `invalid' context: continue searching. Else,
  930. ;; return point.
  931. (unless (eq (org-list-context) 'invalid) (throw 'exit (point)))))))
  932. (defun org-list-search-backward (regexp &optional bound noerror)
  933. "Like `re-search-backward' but stop only where lists are recognized.
  934. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  935. `re-search-backward'."
  936. (org-list-search-generic #'re-search-backward
  937. regexp (or bound (point-min)) noerror))
  938. (defun org-list-search-forward (regexp &optional bound noerror)
  939. "Like `re-search-forward' but stop only where lists are recognized.
  940. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  941. `re-search-forward'."
  942. (org-list-search-generic #'re-search-forward
  943. regexp (or bound (point-max)) noerror))
  944. ;;; Methods on structures
  945. (defsubst org-list-bullet-string (bullet)
  946. "Return BULLET with the correct number of whitespaces.
  947. It determines the number of whitespaces to append by looking at
  948. `org-list-two-spaces-after-bullet-regexp'."
  949. (save-match-data
  950. (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
  951. (string-match
  952. org-list-two-spaces-after-bullet-regexp bullet))
  953. " "
  954. " ")))
  955. (string-match "\\S-+\\([ \t]*\\)" bullet)
  956. (replace-match spaces nil nil bullet 1))))
  957. (defun org-list-separating-blank-lines-number (pos struct prevs)
  958. "Return number of blank lines that should separate items in list.
  959. POS is the position at item beginning to be considered.
  960. STRUCT is the list structure. PREVS is the alist of previous
  961. items, as returned by `org-list-prevs-alist'.
  962. Assume point is at item's beginning. If the item is alone, apply
  963. some heuristics to guess the result."
  964. (save-excursion
  965. (let ((insert-blank-p
  966. (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  967. usr-blank)
  968. (cond
  969. ;; Trivial cases where there should be none.
  970. ((or (and (not (eq org-list-ending-method 'indent))
  971. org-empty-line-terminates-plain-lists)
  972. (not insert-blank-p)) 0)
  973. ;; When `org-blank-before-new-entry' says so, it is 1.
  974. ((eq insert-blank-p t) 1)
  975. ;; plain-list-item is 'auto. Count blank lines separating
  976. ;; neighbours items in list.
  977. (t (let ((next-p (org-list-get-next-item (point) struct prevs)))
  978. (cond
  979. ;; Is there a next item?
  980. (next-p (goto-char next-p)
  981. (org-back-over-empty-lines))
  982. ;; Is there a previous item?
  983. ((org-list-get-prev-item (point) struct prevs)
  984. (org-back-over-empty-lines))
  985. ;; User inserted blank lines, trust him
  986. ((and (> pos (org-list-get-item-end-before-blank pos struct))
  987. (> (save-excursion
  988. (goto-char pos)
  989. (skip-chars-backward " \t")
  990. (setq usr-blank (org-back-over-empty-lines))) 0))
  991. usr-blank)
  992. ;; Are there blank lines inside the item ?
  993. ((save-excursion
  994. (org-list-search-forward
  995. "^[ \t]*$" (org-list-get-item-end-before-blank pos struct) t))
  996. 1)
  997. ;; No parent: no blank line.
  998. (t 0))))))))
  999. (defun org-list-insert-item (pos struct prevs &optional checkbox after-bullet)
  1000. "Insert a new list item at POS and return the new structure.
  1001. If POS is before first character after bullet of the item, the
  1002. new item will be created before the current one.
  1003. STRUCT is the list structure. PREVS is the the alist of previous
  1004. items, as returned by `org-list-prevs-alist'.
  1005. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  1006. after the bullet. Cursor will be after this text once the
  1007. function ends.
  1008. This function modifies STRUCT."
  1009. (let ((case-fold-search t))
  1010. ;; 1. Get information about list: structure, usual helper
  1011. ;; functions, position of point with regards to item start
  1012. ;; (BEFOREP), blank lines number separating items (BLANK-NB),
  1013. ;; position of split (POS) if we're allowed to (SPLIT-LINE-P).
  1014. (let* ((item (goto-char (org-list-get-item-begin)))
  1015. (item-end (org-list-get-item-end item struct))
  1016. (item-end-no-blank (org-list-get-item-end-before-blank item struct))
  1017. (beforep (and (looking-at org-list-full-item-re)
  1018. (<= pos (match-end 0))))
  1019. (split-line-p (org-get-alist-option org-M-RET-may-split-line 'item))
  1020. (blank-nb (org-list-separating-blank-lines-number
  1021. item struct prevs))
  1022. ;; 2. Build the new item to be created. Concatenate same
  1023. ;; bullet as item, checkbox, text AFTER-BULLET if
  1024. ;; provided, and text cut from point to end of item
  1025. ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
  1026. ;; BEFOREP and SPLIT-LINE-P. The difference of size
  1027. ;; between what was cut and what was inserted in buffer
  1028. ;; is stored in SIZE-OFFSET.
  1029. (ind (let ((ind-ref (org-list-get-ind item struct)))
  1030. (if (not indent-tabs-mode)
  1031. ind-ref
  1032. (+ (/ ind-ref tab-width) (mod ind-ref tab-width)))))
  1033. (bullet (org-list-bullet-string (org-list-get-bullet item struct)))
  1034. (box (when checkbox "[ ]"))
  1035. (text-cut
  1036. (and (not beforep) split-line-p
  1037. (progn
  1038. (goto-char pos)
  1039. (skip-chars-backward " \r\t\n")
  1040. (setq pos (point))
  1041. (delete-and-extract-region pos item-end-no-blank))))
  1042. (body (concat bullet (when box (concat box " ")) after-bullet
  1043. (or (and text-cut
  1044. (if (string-match "\\`[ \t]+" text-cut)
  1045. (replace-match "" t t text-cut)
  1046. text-cut))
  1047. "")))
  1048. (item-sep (make-string (1+ blank-nb) ?\n))
  1049. (item-size (+ ind (length body) (length item-sep)))
  1050. (size-offset (- item-size (length text-cut))))
  1051. ;; 4. Insert effectively item into buffer
  1052. (goto-char item)
  1053. (org-indent-to-column ind)
  1054. (insert body item-sep)
  1055. ;; 5. Add new item to STRUCT.
  1056. (mapc (lambda (e)
  1057. (let ((p (car e))
  1058. (end (nth 6 e)))
  1059. (cond
  1060. ;; Before inserted item, positions don't change but
  1061. ;; an item ending after insertion has its end shifted
  1062. ;; by SIZE-OFFSET.
  1063. ((< p item)
  1064. (when (> end item) (setcar (nthcdr 6 e) (+ end size-offset))))
  1065. ;; Trivial cases where current item isn't split in
  1066. ;; two. Just shift every item after new one by
  1067. ;; ITEM-SIZE.
  1068. ((or beforep (not split-line-p))
  1069. (setcar e (+ p item-size))
  1070. (setcar (nthcdr 6 e) (+ end item-size)))
  1071. ;; Item is split in two: elements before POS are just
  1072. ;; shifted by ITEM-SIZE. In the case item would end
  1073. ;; after split POS, ending is only shifted by
  1074. ;; SIZE-OFFSET.
  1075. ((< p pos)
  1076. (setcar e (+ p item-size))
  1077. (if (< end pos)
  1078. (setcar (nthcdr 6 e) (+ end item-size))
  1079. (setcar (nthcdr 6 e) (+ end size-offset))))
  1080. ;; Elements after POS are moved into new item. Length
  1081. ;; of ITEM-SEP has to be removed as ITEM-SEP
  1082. ;; doesn't appear in buffer yet.
  1083. ((< p item-end)
  1084. (setcar e (+ p size-offset (- item pos (length item-sep))))
  1085. (if (= end item-end)
  1086. (setcar (nthcdr 6 e) (+ item item-size))
  1087. (setcar (nthcdr 6 e)
  1088. (+ end size-offset
  1089. (- item pos (length item-sep))))))
  1090. ;; Elements at ITEM-END or after are only shifted by
  1091. ;; SIZE-OFFSET.
  1092. (t (setcar e (+ p size-offset))
  1093. (setcar (nthcdr 6 e) (+ end size-offset))))))
  1094. struct)
  1095. (push (list item ind bullet nil box nil (+ item item-size)) struct)
  1096. (setq struct (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))
  1097. ;; 6. If not BEFOREP, new item must appear after ITEM, so
  1098. ;; exchange ITEM with the next item in list. Position cursor
  1099. ;; after bullet, counter, checkbox, and label.
  1100. (if beforep
  1101. (goto-char item)
  1102. (setq struct (org-list-exchange-items item (+ item item-size) struct))
  1103. (goto-char (org-list-get-next-item
  1104. item struct (org-list-prevs-alist struct))))
  1105. struct)))
  1106. (defun org-list-exchange-items (beg-A beg-B struct)
  1107. "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
  1108. Blank lines at the end of items are left in place. Return the new
  1109. structure after the changes.
  1110. Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
  1111. to the same sub-list.
  1112. This function modifies STRUCT."
  1113. (save-excursion
  1114. (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct))
  1115. (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct))
  1116. (end-A (org-list-get-item-end beg-A struct))
  1117. (end-B (org-list-get-item-end beg-B struct))
  1118. (size-A (- end-A-no-blank beg-A))
  1119. (size-B (- end-B-no-blank beg-B))
  1120. (body-A (buffer-substring beg-A end-A-no-blank))
  1121. (body-B (buffer-substring beg-B end-B-no-blank))
  1122. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B))
  1123. (sub-A (cons beg-A (org-list-get-subtree beg-A struct)))
  1124. (sub-B (cons beg-B (org-list-get-subtree beg-B struct))))
  1125. ;; 1. Move effectively items in buffer.
  1126. (goto-char beg-A)
  1127. (delete-region beg-A end-B-no-blank)
  1128. (insert (concat body-B between-A-no-blank-and-B body-A))
  1129. ;; 2. Now modify struct. No need to re-read the list, the
  1130. ;; transformation is just a shift of positions. Some special
  1131. ;; attention is required for items ending at END-A and END-B
  1132. ;; as empty spaces are not moved there. In others words, item
  1133. ;; BEG-A will end with whitespaces that were at the end of
  1134. ;; BEG-B and the same applies to BEG-B.
  1135. (mapc (lambda (e)
  1136. (let ((pos (car e)))
  1137. (cond
  1138. ((< pos beg-A))
  1139. ((memq pos sub-A)
  1140. (let ((end-e (nth 6 e)))
  1141. (setcar e (+ pos (- end-B-no-blank end-A-no-blank)))
  1142. (setcar (nthcdr 6 e)
  1143. (+ end-e (- end-B-no-blank end-A-no-blank)))
  1144. (when (= end-e end-A) (setcar (nthcdr 6 e) end-B))))
  1145. ((memq pos sub-B)
  1146. (let ((end-e (nth 6 e)))
  1147. (setcar e (- (+ pos beg-A) beg-B))
  1148. (setcar (nthcdr 6 e) (+ end-e (- beg-A beg-B)))
  1149. (when (= end-e end-B)
  1150. (setcar (nthcdr 6 e)
  1151. (+ beg-A size-B (- end-A end-A-no-blank))))))
  1152. ((< pos beg-B)
  1153. (let ((end-e (nth 6 e)))
  1154. (setcar e (+ pos (- size-B size-A)))
  1155. (setcar (nthcdr 6 e) (+ end-e (- size-B size-A))))))))
  1156. struct)
  1157. (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))))
  1158. (defun org-list-struct-outdent (start end struct parents)
  1159. "Outdent items between positions START and END.
  1160. STRUCT is the list structure. PARENTS is the alist of items'
  1161. parents, as returned by `org-list-parents-alist'.
  1162. START is included, END excluded."
  1163. (let* (acc
  1164. (out (lambda (cell)
  1165. (let* ((item (car cell))
  1166. (parent (cdr cell)))
  1167. (cond
  1168. ;; Item not yet in zone: keep association
  1169. ((< item start) cell)
  1170. ;; Item out of zone: follow associations in acc
  1171. ((>= item end)
  1172. (let ((convert (and parent (assq parent acc))))
  1173. (if convert (cons item (cdr convert)) cell)))
  1174. ;; Item has no parent: error
  1175. ((not parent)
  1176. (error "Cannot outdent top-level items"))
  1177. ;; Parent is outdented: keep association
  1178. ((>= parent start)
  1179. (push (cons parent item) acc) cell)
  1180. (t
  1181. ;; Parent isn't outdented: reparent to grand-parent
  1182. (let ((grand-parent (org-list-get-parent
  1183. parent struct parents)))
  1184. (push (cons parent item) acc)
  1185. (cons item grand-parent))))))))
  1186. (mapcar out parents)))
  1187. (defun org-list-struct-indent (start end struct parents prevs)
  1188. "Indent items between positions START and END.
  1189. STRUCT is the list structure. PARENTS is the alist of parents and
  1190. PREVS is the alist of previous items, returned by, respectively,
  1191. `org-list-parents-alist' and `org-list-prevs-alist'.
  1192. START is included and END excluded.
  1193. STRUCT may be modified if `org-list-demote-modify-bullet' matches
  1194. bullets between START and END."
  1195. (let* (acc
  1196. (set-assoc (lambda (cell) (push cell acc) cell))
  1197. (change-bullet-maybe
  1198. (function
  1199. (lambda (item)
  1200. (let* ((bul (org-trim (org-list-get-bullet item struct)))
  1201. (new-bul-p (cdr (assoc bul org-list-demote-modify-bullet))))
  1202. (when new-bul-p (org-list-set-bullet item struct new-bul-p))))))
  1203. (ind
  1204. (lambda (cell)
  1205. (let* ((item (car cell))
  1206. (parent (cdr cell)))
  1207. (cond
  1208. ;; Item not yet in zone: keep association
  1209. ((< item start) cell)
  1210. ((>= item end)
  1211. ;; Item out of zone: follow associations in acc
  1212. (let ((convert (assq parent acc)))
  1213. (if convert (cons item (cdr convert)) cell)))
  1214. (t
  1215. ;; Item is in zone...
  1216. (let ((prev (org-list-get-prev-item item struct prevs)))
  1217. ;; Check if bullet needs to be changed
  1218. (funcall change-bullet-maybe item)
  1219. (cond
  1220. ;; First item indented but not parent: error
  1221. ((and (not prev) (< parent start))
  1222. (error "Cannot indent the first item of a list"))
  1223. ;; First item and parent indented: keep same parent
  1224. ((not prev) (funcall set-assoc cell))
  1225. ;; Previous item not indented: reparent to it
  1226. ((< prev start) (funcall set-assoc (cons item prev)))
  1227. ;; Previous item indented: reparent like it
  1228. (t
  1229. (funcall set-assoc
  1230. (cons item (cdr (assq prev acc)))))))))))))
  1231. (mapcar ind parents)))
  1232. ;;; Repairing structures
  1233. (defun org-list-use-alpha-bul-p (first struct prevs)
  1234. "Non-nil if list starting at FIRST can have alphabetical bullets.
  1235. STRUCT is list structure. PREVS is the alist of previous items,
  1236. as returned by `org-list-prevs-alist'."
  1237. (and org-alphabetical-lists
  1238. (catch 'exit
  1239. (let ((item first) (ascii 64) (case-fold-search nil))
  1240. ;; Pretend that bullets are uppercase and check if alphabet
  1241. ;; is sufficient, taking counters into account.
  1242. (while item
  1243. (let ((bul (org-list-get-bullet item struct))
  1244. (count (org-list-get-counter item struct)))
  1245. ;; Virtually determine current bullet
  1246. (if (and count (string-match "[a-zA-Z]" count))
  1247. ;; Counters are not case-sensitive.
  1248. (setq ascii (string-to-char (upcase count)))
  1249. (setq ascii (1+ ascii)))
  1250. ;; Test if bullet would be over z or Z.
  1251. (if (> ascii 90)
  1252. (throw 'exit nil)
  1253. (setq item (org-list-get-next-item item struct prevs)))))
  1254. ;; All items checked. All good.
  1255. t))))
  1256. (defun org-list-inc-bullet-maybe (bullet)
  1257. "Increment BULLET if applicable."
  1258. (let ((case-fold-search nil))
  1259. (cond
  1260. ;; Num bullet: increment it.
  1261. ((string-match "[0-9]+" bullet)
  1262. (replace-match
  1263. (number-to-string (1+ (string-to-number (match-string 0 bullet))))
  1264. nil nil bullet))
  1265. ;; Alpha bullet: increment it.
  1266. ((string-match "[A-Za-z]" bullet)
  1267. (replace-match
  1268. (char-to-string (1+ (string-to-char (match-string 0 bullet))))
  1269. nil nil bullet))
  1270. ;; Unordered bullet: leave it.
  1271. (t bullet))))
  1272. (defun org-list-struct-fix-bul (struct prevs)
  1273. "Verify and correct bullets in STRUCT.
  1274. PREVS is the alist of previous items, as returned by
  1275. `org-list-prevs-alist'.
  1276. This function modifies STRUCT."
  1277. (let ((case-fold-search nil)
  1278. (fix-bul
  1279. (function
  1280. ;; Set bullet of ITEM in STRUCT, depending on the type of
  1281. ;; first item of the list, the previous bullet and counter
  1282. ;; if any.
  1283. (lambda (item)
  1284. (let* ((prev (org-list-get-prev-item item struct prevs))
  1285. (prev-bul (and prev (org-list-get-bullet prev struct)))
  1286. (counter (org-list-get-counter item struct))
  1287. (bullet (org-list-get-bullet item struct))
  1288. (alphap (and (not prev)
  1289. (org-list-use-alpha-bul-p item struct prevs))))
  1290. (org-list-set-bullet
  1291. item struct
  1292. (org-list-bullet-string
  1293. (cond
  1294. ;; Alpha counter in alpha list: use counter.
  1295. ((and prev counter
  1296. (string-match "[a-zA-Z]" counter)
  1297. (string-match "[a-zA-Z]" prev-bul))
  1298. ;; Use cond to be sure `string-match' is used in
  1299. ;; both cases.
  1300. (let ((real-count
  1301. (cond
  1302. ((string-match "[a-z]" prev-bul) (downcase counter))
  1303. ((string-match "[A-Z]" prev-bul) (upcase counter)))))
  1304. (replace-match real-count nil nil prev-bul)))
  1305. ;; Num counter in a num list: use counter.
  1306. ((and prev counter
  1307. (string-match "[0-9]+" counter)
  1308. (string-match "[0-9]+" prev-bul))
  1309. (replace-match counter nil nil prev-bul))
  1310. ;; No counter: increase, if needed, previous bullet.
  1311. (prev
  1312. (org-list-inc-bullet-maybe (org-list-get-bullet prev struct)))
  1313. ;; Alpha counter at first item: use counter.
  1314. ((and counter (org-list-use-alpha-bul-p item struct prevs)
  1315. (string-match "[A-Za-z]" counter)
  1316. (string-match "[A-Za-z]" bullet))
  1317. (let ((real-count
  1318. (cond
  1319. ((string-match "[a-z]" bullet) (downcase counter))
  1320. ((string-match "[A-Z]" bullet) (upcase counter)))))
  1321. (replace-match real-count nil nil bullet)))
  1322. ;; Num counter at first item: use counter.
  1323. ((and counter
  1324. (string-match "[0-9]+" counter)
  1325. (string-match "[0-9]+" bullet))
  1326. (replace-match counter nil nil bullet))
  1327. ;; First bullet is alpha uppercase: use "A".
  1328. ((and alphap (string-match "[A-Z]" bullet))
  1329. (replace-match "A" nil nil bullet))
  1330. ;; First bullet is alpha lowercase: use "a".
  1331. ((and alphap (string-match "[a-z]" bullet))
  1332. (replace-match "a" nil nil bullet))
  1333. ;; First bullet is num: use "1".
  1334. ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet)
  1335. (replace-match "1" nil nil bullet))
  1336. ;; Not an ordered list: keep bullet.
  1337. (t bullet)))))))))
  1338. (mapc fix-bul (mapcar 'car struct))))
  1339. (defun org-list-struct-fix-ind (struct parents &optional bullet-size)
  1340. "Verify and correct indentation in STRUCT.
  1341. PARENTS is the alist of parents, as returned by
  1342. `org-list-parents-alist'.
  1343. If numeric optional argument BULLET-SIZE is set, assume all
  1344. bullets in list have this length to determine new indentation.
  1345. This function modifies STRUCT."
  1346. (let* ((ancestor (org-list-get-top-point struct))
  1347. (top-ind (org-list-get-ind ancestor struct))
  1348. (new-ind
  1349. (lambda (item)
  1350. (let ((parent (org-list-get-parent item struct parents)))
  1351. (if parent
  1352. ;; Indent like parent + length of parent's bullet
  1353. (org-list-set-ind
  1354. item struct (+ (or bullet-size
  1355. (length
  1356. (org-list-get-bullet parent struct)))
  1357. (org-list-get-ind parent struct)))
  1358. ;; If no parent, indent like top-point
  1359. (org-list-set-ind item struct top-ind))))))
  1360. (mapc new-ind (mapcar 'car (cdr struct)))))
  1361. (defun org-list-struct-fix-box (struct parents prevs &optional ordered)
  1362. "Verify and correct checkboxes in STRUCT.
  1363. PARENTS is the alist of parents and PREVS is the alist of
  1364. previous items, as returned by, respectively,
  1365. `org-list-parents-alist' and `org-list-prevs-alist'.
  1366. If ORDERED is non-nil, a checkbox can only be checked when every
  1367. checkbox before it is checked too. If there was an attempt to
  1368. break this rule, the function will return the blocking item. In
  1369. all others cases, the return value will be nil.
  1370. This function modifies STRUCT."
  1371. (let ((all-items (mapcar 'car struct))
  1372. (set-parent-box
  1373. (function
  1374. (lambda (item)
  1375. (let* ((box-list
  1376. (mapcar (lambda (child)
  1377. (org-list-get-checkbox child struct))
  1378. (org-list-get-children item struct parents))))
  1379. (org-list-set-checkbox
  1380. item struct
  1381. (cond
  1382. ((and (member "[ ]" box-list) (member "[X]" box-list)) "[-]")
  1383. ((member "[-]" box-list) "[-]")
  1384. ((member "[X]" box-list) "[X]")
  1385. ((member "[ ]" box-list) "[ ]")
  1386. ;; parent has no boxed child: leave box as-is
  1387. (t (org-list-get-checkbox item struct))))))))
  1388. parent-list)
  1389. ;; 1. List all parents with a checkbox
  1390. (mapc
  1391. (lambda (e)
  1392. (let* ((parent (org-list-get-parent e struct parents))
  1393. (parent-box-p (org-list-get-checkbox parent struct)))
  1394. (when (and parent-box-p (not (memq parent parent-list)))
  1395. (push parent parent-list))))
  1396. all-items)
  1397. ;; 2. Sort those parents by decreasing indentation
  1398. (setq parent-list (sort parent-list
  1399. (lambda (e1 e2)
  1400. (> (org-list-get-ind e1 struct)
  1401. (org-list-get-ind e2 struct)))))
  1402. ;; 3. For each parent, get all children's checkboxes to determine
  1403. ;; and set its checkbox accordingly
  1404. (mapc set-parent-box parent-list)
  1405. ;; 4. If ORDERED is set, see if we need to uncheck some boxes
  1406. (when ordered
  1407. (let* ((box-list
  1408. (mapcar (lambda (e) (org-list-get-checkbox e struct)) all-items))
  1409. (after-unchecked (member "[ ]" box-list)))
  1410. ;; there are boxes checked after an unchecked one: fix that
  1411. (when (member "[X]" after-unchecked)
  1412. (let ((index (- (length struct) (length after-unchecked))))
  1413. (mapc (lambda (e) (org-list-set-checkbox e struct "[ ]"))
  1414. (nthcdr index all-items))
  1415. ;; Verify once again the structure, without ORDERED
  1416. (org-list-struct-fix-box struct parents prevs nil)
  1417. ;; return blocking item
  1418. (nth index all-items)))))))
  1419. (defun org-list-struct-apply-struct (struct old-struct)
  1420. "Apply set-difference between STRUCT and OLD-STRUCT to the buffer.
  1421. OLD-STRUCT is the structure before any modifications, and STRUCT
  1422. the structure to be applied. The function will only modify parts
  1423. of the list which have changed.
  1424. Initial position of cursor is restored after the changes."
  1425. (let* ((pos (copy-marker (point)))
  1426. (inlinetask-re (and (featurep 'org-inlinetask)
  1427. (org-inlinetask-outline-regexp)))
  1428. (item-re (org-item-re))
  1429. (box-rule-p (cdr (assq 'checkbox org-list-automatic-rules)))
  1430. (shift-body-ind
  1431. (function
  1432. ;; Shift the indentation between END and BEG by DELTA.
  1433. ;; Start from the line before END.
  1434. (lambda (end beg delta)
  1435. (goto-char end)
  1436. (skip-chars-backward " \r\t\n")
  1437. (beginning-of-line)
  1438. (while (or (> (point) beg)
  1439. (and (= (point) beg)
  1440. (not (looking-at item-re))))
  1441. (cond
  1442. ;; Skip inline tasks
  1443. ((and inlinetask-re (looking-at inlinetask-re))
  1444. (org-inlinetask-goto-beginning))
  1445. ;; Shift only non-empty lines
  1446. ((org-looking-at-p "^[ \t]*\\S-")
  1447. (let ((i (org-get-indentation)))
  1448. (org-indent-line-to (+ i delta)))))
  1449. (forward-line -1)))))
  1450. (modify-item
  1451. (function
  1452. ;; Replace ITEM first line elements with new elements from
  1453. ;; STRUCT, if appropriate.
  1454. (lambda (item)
  1455. (goto-char item)
  1456. (let* ((new-ind (org-list-get-ind item struct))
  1457. (old-ind (org-get-indentation))
  1458. (new-bul (org-list-bullet-string
  1459. (org-list-get-bullet item struct)))
  1460. (old-bul (org-list-get-bullet item old-struct))
  1461. (new-box (org-list-get-checkbox item struct)))
  1462. (looking-at org-list-full-item-re)
  1463. ;; a. Replace bullet
  1464. (unless (equal old-bul new-bul)
  1465. (replace-match new-bul nil nil nil 1))
  1466. ;; b. Replace checkbox
  1467. (cond
  1468. ((and new-box box-rule-p
  1469. (save-match-data (org-at-item-description-p)))
  1470. (message "Cannot add a checkbox to a description list item"))
  1471. ((equal (match-string 3) new-box))
  1472. ((and (match-string 3) new-box)
  1473. (replace-match new-box nil nil nil 3))
  1474. ((match-string 3)
  1475. (goto-char (or (match-end 2) (match-end 1)))
  1476. (looking-at "\\[[ X-]\\][ \t]+")
  1477. (replace-match ""))
  1478. (t (goto-char (or (match-end 2) (match-end 1)))
  1479. (insert (concat new-box " "))))
  1480. ;; c. Indent item to appropriate column
  1481. (unless (= new-ind old-ind)
  1482. (delete-region (goto-char (point-at-bol))
  1483. (progn (skip-chars-forward " \t") (point)))
  1484. (indent-to new-ind)))))))
  1485. ;; 1. First get list of items and position endings. We maintain
  1486. ;; two alists: ITM-SHIFT, determining indentation shift needed
  1487. ;; at item, and END-POS, a pseudo-alist where key is ending
  1488. ;; position and value point
  1489. (let (end-list acc-end itm-shift all-ends sliced-struct)
  1490. (mapc (lambda (e)
  1491. (let* ((pos (car e))
  1492. (ind-pos (org-list-get-ind pos struct))
  1493. (ind-old (org-list-get-ind pos old-struct))
  1494. (bul-pos (org-list-get-bullet pos struct))
  1495. (bul-old (org-list-get-bullet pos old-struct))
  1496. (ind-shift (- (+ ind-pos (length bul-pos))
  1497. (+ ind-old (length bul-old))))
  1498. (end-pos (org-list-get-item-end pos old-struct)))
  1499. (push (cons pos ind-shift) itm-shift)
  1500. (unless (assq end-pos old-struct)
  1501. ;; To determine real ind of an ending position that is
  1502. ;; not at an item, we have to find the item it belongs
  1503. ;; to: it is the last item (ITEM-UP), whose ending is
  1504. ;; further than the position we're interested in.
  1505. (let ((item-up (assoc-default end-pos acc-end '>)))
  1506. (push (cons end-pos item-up) end-list)))
  1507. (push (cons end-pos pos) acc-end)))
  1508. old-struct)
  1509. ;; 2. Slice the items into parts that should be shifted by the
  1510. ;; same amount of indentation. The slices are returned in
  1511. ;; reverse order so changes modifying buffer do not change
  1512. ;; positions they refer to.
  1513. (setq all-ends (sort (append (mapcar 'car itm-shift)
  1514. (org-uniquify (mapcar 'car end-list)))
  1515. '<))
  1516. (while (cdr all-ends)
  1517. (let* ((up (pop all-ends))
  1518. (down (car all-ends))
  1519. (ind (if (assq up struct)
  1520. (cdr (assq up itm-shift))
  1521. (cdr (assq (cdr (assq up end-list)) itm-shift)))))
  1522. (push (list down up ind) sliced-struct)))
  1523. ;; 3. Shift each slice in buffer, provided delta isn't 0, from
  1524. ;; end to beginning. Take a special action when beginning is
  1525. ;; at item bullet.
  1526. (mapc (lambda (e)
  1527. (unless (zerop (nth 2 e)) (apply shift-body-ind e))
  1528. (let* ((beg (nth 1 e))
  1529. (cell (assq beg struct)))
  1530. (unless (or (not cell) (equal cell (assq beg old-struct)))
  1531. (funcall modify-item beg))))
  1532. sliced-struct))
  1533. ;; 4. Go back to initial position
  1534. (goto-char pos)))
  1535. (defun org-list-write-struct (struct parents)
  1536. "Correct bullets, checkboxes and indentation in list at point.
  1537. STRUCT is the list structure. PARENTS is the alist of parents, as
  1538. returned by `org-list-parents-alist'."
  1539. ;; Order of functions matters here: checkboxes and endings need
  1540. ;; correct indentation to be set, and indentation needs correct
  1541. ;; bullets.
  1542. ;;
  1543. ;; 0. Save a copy of structure before modifications
  1544. (let ((old-struct (copy-tree struct)))
  1545. ;; 1. Set a temporary, but coherent with PARENTS, indentation in
  1546. ;; order to get items endings and bullets properly
  1547. (org-list-struct-fix-ind struct parents 2)
  1548. ;; 2. Get pseudo-alist of ending positions and sort it by position.
  1549. ;; Then associate them to the structure.
  1550. (let (end-list acc-end)
  1551. (mapc (lambda (e)
  1552. (let* ((pos (car e))
  1553. (ind-pos (org-list-get-ind pos struct))
  1554. (end-pos (org-list-get-item-end pos struct)))
  1555. (unless (assq end-pos struct)
  1556. ;; To determine real ind of an ending position that is
  1557. ;; not at an item, we have to find the item it belongs
  1558. ;; to: it is the last item (ITEM-UP), whose ending is
  1559. ;; further than the position we're interested in.
  1560. (let ((item-up (assoc-default end-pos acc-end '>)))
  1561. (push (cons
  1562. ;; Else part is for the bottom point
  1563. (if item-up (+ (org-list-get-ind item-up struct) 2) 0)
  1564. end-pos)
  1565. end-list)))
  1566. (push (cons ind-pos pos) end-list)
  1567. (push (cons end-pos pos) acc-end)))
  1568. struct)
  1569. (setq end-list (sort end-list (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
  1570. (org-list-struct-assoc-end struct end-list))
  1571. ;; 3. Get bullets right
  1572. (let ((prevs (org-list-prevs-alist struct)))
  1573. (org-list-struct-fix-bul struct prevs)
  1574. ;; 4. Now get real indentation
  1575. (org-list-struct-fix-ind struct parents)
  1576. ;; 5. Eventually fix checkboxes
  1577. (org-list-struct-fix-box struct parents prevs))
  1578. ;; 6. Apply structure modifications to buffer
  1579. (org-list-struct-apply-struct struct old-struct)))
  1580. ;;; Misc Tools
  1581. (defun org-apply-on-list (function init-value &rest args)
  1582. "Call FUNCTION on each item of the list at point.
  1583. FUNCTION must be called with at least one argument: INIT-VALUE,
  1584. that will contain the value returned by the function at the
  1585. previous item, plus ARGS extra arguments.
  1586. FUNCTION is applied on items in reverse order.
  1587. As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
  1588. will return the number of items in the current list.
  1589. Sublists of the list are skipped. Cursor is always at the
  1590. beginning of the item."
  1591. (let* ((struct (org-list-struct))
  1592. (prevs (org-list-prevs-alist struct))
  1593. (item (copy-marker (point-at-bol)))
  1594. (all (org-list-get-all-items (marker-position item) struct prevs))
  1595. (value init-value))
  1596. (mapc (lambda (e)
  1597. (goto-char e)
  1598. (setq value (apply function value args)))
  1599. (nreverse all))
  1600. (goto-char item)
  1601. value))
  1602. (defun org-list-set-item-visibility (item struct view)
  1603. "Set visibility of ITEM in STRUCT to VIEW.
  1604. Possible values are: `folded', `children' or `subtree'. See
  1605. `org-cycle' for more information."
  1606. (cond
  1607. ((eq view 'folded)
  1608. (let ((item-end (org-list-get-item-end-before-blank item struct)))
  1609. ;; Hide from eol
  1610. (outline-flag-region (save-excursion (goto-char item) (point-at-eol))
  1611. item-end t)))
  1612. ((eq view 'children)
  1613. ;; First show everything.
  1614. (org-list-set-item-visibility item struct 'subtree)
  1615. ;; Then fold every child.
  1616. (let* ((parents (org-list-parents-alist struct))
  1617. (children (org-list-get-children item struct parents)))
  1618. (mapc (lambda (e)
  1619. (org-list-set-item-visibility e struct 'folded))
  1620. children)))
  1621. ((eq view 'subtree)
  1622. ;; Show everything
  1623. (let ((item-end (org-list-get-item-end item struct)))
  1624. (outline-flag-region item item-end nil)))))
  1625. (defun org-list-item-body-column (item)
  1626. "Return column at which body of ITEM should start."
  1627. (let (bpos bcol tpos tcol)
  1628. (save-excursion
  1629. (goto-char item)
  1630. (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?[ \t]+")
  1631. (setq bpos (match-beginning 1) tpos (match-end 0)
  1632. bcol (progn (goto-char bpos) (current-column))
  1633. tcol (progn (goto-char tpos) (current-column)))
  1634. (when (> tcol (+ bcol org-description-max-indent))
  1635. (setq tcol (+ bcol 5))))
  1636. tcol))
  1637. ;;; Interactive functions
  1638. (defalias 'org-list-get-item-begin 'org-in-item-p)
  1639. (defun org-beginning-of-item ()
  1640. "Go to the beginning of the current item.
  1641. Throw an error when not in a list."
  1642. (interactive)
  1643. (let ((begin (org-in-item-p)))
  1644. (if begin (goto-char begin) (error "Not in an item"))))
  1645. (defun org-beginning-of-item-list ()
  1646. "Go to the beginning item of the current list or sublist.
  1647. Throw an error when not in a list."
  1648. (interactive)
  1649. (let ((begin (org-in-item-p)))
  1650. (if (not begin)
  1651. (error "Not in an item")
  1652. (goto-char begin)
  1653. (let* ((struct (org-list-struct))
  1654. (prevs (org-list-prevs-alist struct)))
  1655. (goto-char (org-list-get-list-begin begin struct prevs))))))
  1656. (defun org-end-of-item-list ()
  1657. "Go to the end of the current list or sublist.
  1658. Throw an error when not in a list."
  1659. (interactive)
  1660. (let ((begin (org-in-item-p)))
  1661. (if (not begin)
  1662. (error "Not in an item")
  1663. (goto-char begin)
  1664. (let* ((struct (org-list-struct))
  1665. (prevs (org-list-prevs-alist struct)))
  1666. (goto-char (org-list-get-list-end begin struct prevs))))))
  1667. (defun org-end-of-item ()
  1668. "Go to the end of the current item.
  1669. Throw an error when not in a list."
  1670. (interactive)
  1671. (let ((begin (org-in-item-p)))
  1672. (if (not begin)
  1673. (error "Not in an item")
  1674. (goto-char begin)
  1675. (let ((struct (org-list-struct)))
  1676. (goto-char (org-list-get-item-end begin struct))))))
  1677. (defun org-previous-item ()
  1678. "Move to the beginning of the previous item.
  1679. Throw an error when not in a list, or at first item."
  1680. (interactive)
  1681. (let ((begin (org-in-item-p)))
  1682. (if (not begin)
  1683. (error "Not in an item")
  1684. (goto-char begin)
  1685. (let* ((struct (org-list-struct))
  1686. (prevs (org-list-prevs-alist struct))
  1687. (prevp (org-list-get-prev-item begin struct prevs)))
  1688. (if prevp (goto-char prevp) (error "On first item"))))))
  1689. (defun org-next-item ()
  1690. "Move to the beginning of the next item.
  1691. Throw an error when not in a plain list, or at last item."
  1692. (interactive)
  1693. (let ((begin (org-in-item-p)))
  1694. (if (not begin)
  1695. (error "Not in an item")
  1696. (goto-char begin)
  1697. (let* ((struct (org-list-struct))
  1698. (prevs (org-list-prevs-alist struct))
  1699. (prevp (org-list-get-next-item begin struct prevs)))
  1700. (if prevp (goto-char prevp) (error "On last item"))))))
  1701. (defun org-move-item-down ()
  1702. "Move the item at point down, i.e. swap with following item.
  1703. Subitems (items with larger indentation) are considered part of
  1704. the item, so this really moves item trees."
  1705. (interactive)
  1706. (unless (org-at-item-p) (error "Not at an item"))
  1707. (let* ((pos (point))
  1708. (col (current-column))
  1709. (actual-item (point-at-bol))
  1710. (struct (org-list-struct))
  1711. (prevs (org-list-prevs-alist struct))
  1712. (next-item (org-list-get-next-item (point-at-bol) struct prevs)))
  1713. (if (not next-item)
  1714. (progn
  1715. (goto-char pos)
  1716. (error "Cannot move this item further down"))
  1717. (setq struct
  1718. (org-list-exchange-items actual-item next-item struct))
  1719. ;; Use a short variation of `org-list-write-struct' as there's
  1720. ;; no need to go through all the steps.
  1721. (let ((old-struct (copy-tree struct))
  1722. (prevs (org-list-prevs-alist struct))
  1723. (parents (org-list-parents-alist struct)))
  1724. (org-list-struct-fix-bul struct prevs)
  1725. (org-list-struct-fix-ind struct parents)
  1726. (org-list-struct-apply-struct struct old-struct)
  1727. (goto-char (org-list-get-next-item (point-at-bol) struct prevs)))
  1728. (org-move-to-column col))))
  1729. (defun org-move-item-up ()
  1730. "Move the item at point up, i.e. swap with previous item.
  1731. Subitems (items with larger indentation) are considered part of
  1732. the item, so this really moves item trees."
  1733. (interactive)
  1734. (unless (org-at-item-p) (error "Not at an item"))
  1735. (let* ((pos (point))
  1736. (col (current-column))
  1737. (actual-item (point-at-bol))
  1738. (struct (org-list-struct))
  1739. (prevs (org-list-prevs-alist struct))
  1740. (prev-item (org-list-get-prev-item (point-at-bol) struct prevs)))
  1741. (if (not prev-item)
  1742. (progn
  1743. (goto-char pos)
  1744. (error "Cannot move this item further up"))
  1745. (setq struct
  1746. (org-list-exchange-items prev-item actual-item struct))
  1747. ;; Use a short variation of `org-list-write-struct' as there's
  1748. ;; no need to go through all the steps.
  1749. (let ((old-struct (copy-tree struct))
  1750. (prevs (org-list-prevs-alist struct))
  1751. (parents (org-list-parents-alist struct)))
  1752. (org-list-struct-fix-bul struct prevs)
  1753. (org-list-struct-fix-ind struct parents)
  1754. (org-list-struct-apply-struct struct old-struct))
  1755. (org-move-to-column col))))
  1756. (defun org-insert-item (&optional checkbox)
  1757. "Insert a new item at the current level.
  1758. If cursor is before first character after bullet of the item, the
  1759. new item will be created before the current one.
  1760. If CHECKBOX is non-nil, add a checkbox next to the bullet.
  1761. Return t when things worked, nil when we are not in an item, or
  1762. item is invisible."
  1763. (let ((itemp (org-in-item-p))
  1764. (pos (point)))
  1765. ;; If cursor isn't is a list or if list is invisible, return nil.
  1766. (unless (or (not itemp)
  1767. (save-excursion
  1768. (goto-char itemp)
  1769. (outline-invisible-p)))
  1770. (if (save-excursion
  1771. (goto-char itemp)
  1772. (org-at-item-timer-p))
  1773. ;; Timer list: delegate to `org-timer-item'.
  1774. (progn (org-timer-item) t)
  1775. (goto-char itemp)
  1776. (let* ((struct (org-list-struct))
  1777. (prevs (org-list-prevs-alist struct))
  1778. ;; If we're in a description list, ask for the new term.
  1779. (desc (when (org-list-get-tag itemp struct)
  1780. (concat (read-string "Term: ") " :: ")))
  1781. ;; Don't insert a checkbox if checkbox rule is applied
  1782. ;; and it is a description item.
  1783. (checkp (and checkbox
  1784. (or (not desc)
  1785. (not (cdr (assq 'checkbox
  1786. org-list-automatic-rules)))))))
  1787. (setq struct
  1788. (org-list-insert-item pos struct prevs checkp desc))
  1789. (org-list-write-struct struct (org-list-parents-alist struct))
  1790. (when checkp (org-update-checkbox-count-maybe))
  1791. (looking-at org-list-full-item-re)
  1792. (goto-char (match-end 0))
  1793. t)))))
  1794. (defun org-list-repair ()
  1795. "Fix indentation, bullets and checkboxes is the list at point."
  1796. (interactive)
  1797. (unless (org-at-item-p) (error "This is not a list"))
  1798. (let* ((struct (org-list-struct))
  1799. (parents (org-list-parents-alist struct)))
  1800. (org-list-write-struct struct parents)))
  1801. (defun org-cycle-list-bullet (&optional which)
  1802. "Cycle through the different itemize/enumerate bullets.
  1803. This cycle the entire list level through the sequence:
  1804. `-' -> `+' -> `*' -> `1.' -> `1)'
  1805. If WHICH is a valid string, use that as the new bullet. If WHICH
  1806. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  1807. `previous', cycle backwards."
  1808. (interactive "P")
  1809. (unless (org-at-item-p) (error "Not at an item"))
  1810. (save-excursion
  1811. (beginning-of-line)
  1812. (let* ((struct (org-list-struct))
  1813. (parents (org-list-parents-alist struct))
  1814. (prevs (org-list-prevs-alist struct))
  1815. (list-beg (org-list-get-first-item (point) struct prevs))
  1816. (bullet (org-list-get-bullet list-beg struct))
  1817. (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
  1818. (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs))
  1819. (case-fold-search nil)
  1820. (current (cond
  1821. ((string-match "[a-z]\\." bullet) "a.")
  1822. ((string-match "[a-z])" bullet) "a)")
  1823. ((string-match "[A-Z]\\." bullet) "A.")
  1824. ((string-match "[A-Z])" bullet) "A)")
  1825. ((string-match "\\." bullet) "1.")
  1826. ((string-match ")" bullet) "1)")
  1827. (t (org-trim bullet))))
  1828. ;; Compute list of possible bullets, depending on context
  1829. (bullet-list
  1830. (append '("-" "+" )
  1831. ;; *-bullets are not allowed at column 0
  1832. (unless (and bullet-rule-p
  1833. (looking-at "\\S-")) '("*"))
  1834. ;; Description items cannot be numbered
  1835. (unless (or (eq org-plain-list-ordered-item-terminator ?\))
  1836. (and bullet-rule-p (org-at-item-description-p)))
  1837. '("1."))
  1838. (unless (or (eq org-plain-list-ordered-item-terminator ?.)
  1839. (and bullet-rule-p (org-at-item-description-p)))
  1840. '("1)"))
  1841. (unless (or (not alpha-p)
  1842. (eq org-plain-list-ordered-item-terminator ?\))
  1843. (and bullet-rule-p (org-at-item-description-p)))
  1844. '("a." "A."))
  1845. (unless (or (not alpha-p)
  1846. (eq org-plain-list-ordered-item-terminator ?.)
  1847. (and bullet-rule-p (org-at-item-description-p)))
  1848. '("a)" "A)"))))
  1849. (len (length bullet-list))
  1850. (item-index (- len (length (member current bullet-list))))
  1851. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  1852. (new (cond
  1853. ((member which bullet-list) which)
  1854. ((numberp which) (funcall get-value which))
  1855. ((eq 'previous which) (funcall get-value (1- item-index)))
  1856. (t (funcall get-value (1+ item-index))))))
  1857. ;; Use a short variation of `org-list-write-struct' as there's
  1858. ;; no need to go through all the steps.
  1859. (let ((old-struct (copy-tree struct)))
  1860. (org-list-set-bullet list-beg struct (org-list-bullet-string new))
  1861. (org-list-struct-fix-bul struct prevs)
  1862. (org-list-struct-fix-ind struct parents)
  1863. (org-list-struct-apply-struct struct old-struct)))))
  1864. (defun org-toggle-checkbox (&optional toggle-presence)
  1865. "Toggle the checkbox in the current line.
  1866. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  1867. double prefix, set checkbox to [-].
  1868. When there is an active region, toggle status or presence of the
  1869. first checkbox there, and make every item inside have the same
  1870. status or presence, respectively.
  1871. If the cursor is in a headline, apply this to all checkbox items
  1872. in the text below the heading, taking as reference the first item
  1873. in subtree, ignoring drawers."
  1874. (interactive "P")
  1875. (save-excursion
  1876. (let* (singlep
  1877. block-item
  1878. lim-up
  1879. lim-down
  1880. (orderedp (org-entry-get nil "ORDERED"))
  1881. (bounds
  1882. ;; In a region, start at first item in region
  1883. (cond
  1884. ((org-region-active-p)
  1885. (let ((limit (region-end)))
  1886. (goto-char (region-beginning))
  1887. (if (org-list-search-forward (org-item-beginning-re) limit t)
  1888. (setq lim-up (point-at-bol))
  1889. (error "No item in region"))
  1890. (setq lim-down (copy-marker limit))))
  1891. ((org-on-heading-p)
  1892. ;; On an heading, start at first item after drawers
  1893. (let ((limit (save-excursion (outline-next-heading) (point))))
  1894. (forward-line 1)
  1895. (when (looking-at org-drawer-regexp)
  1896. (re-search-forward "^[ \t]*:END:" limit nil))
  1897. (if (org-list-search-forward (org-item-beginning-re) limit t)
  1898. (setq lim-up (point-at-bol))
  1899. (error "No item in subtree"))
  1900. (setq lim-down (copy-marker limit))))
  1901. ;; Just one item: set singlep flag
  1902. ((org-at-item-p)
  1903. (setq singlep t)
  1904. (setq lim-up (point-at-bol)
  1905. lim-down (point-at-eol)))
  1906. (t (error "Not at an item or heading, and no active region"))))
  1907. ;; Determine the checkbox going to be applied to all items
  1908. ;; within bounds
  1909. (ref-checkbox
  1910. (progn
  1911. (goto-char lim-up)
  1912. (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
  1913. (cond
  1914. ((equal toggle-presence '(16)) "[-]")
  1915. ((equal toggle-presence '(4))
  1916. (unless cbox "[ ]"))
  1917. ((equal "[ ]" cbox) "[X]")
  1918. (t "[ ]"))))))
  1919. ;; When an item is found within bounds, grab the full list at
  1920. ;; point structure, then: 1. set checkbox of all its items
  1921. ;; within bounds to ref-checkbox; 2. fix checkboxes of the whole
  1922. ;; list; 3. move point after the list.
  1923. (goto-char lim-up)
  1924. (while (and (< (point) lim-down)
  1925. (org-list-search-forward (org-item-beginning-re)
  1926. lim-down 'move))
  1927. (let* ((struct (org-list-struct))
  1928. (struct-copy (copy-tree struct))
  1929. (parents (org-list-parents-alist struct))
  1930. (prevs (org-list-prevs-alist struct))
  1931. (bottom (copy-marker (org-list-get-bottom-point struct)))
  1932. (items-to-toggle (org-remove-if
  1933. (lambda (e) (or (< e lim-up) (> e lim-down)))
  1934. (mapcar 'car struct))))
  1935. (mapc (lambda (e) (org-list-set-checkbox
  1936. e struct
  1937. ;; if there is no box at item, leave as-is
  1938. ;; unless function was called with C-u prefix
  1939. (let ((cur-box (org-list-get-checkbox e struct)))
  1940. (if (or cur-box (equal toggle-presence '(4)))
  1941. ref-checkbox
  1942. cur-box))))
  1943. items-to-toggle)
  1944. (setq block-item (org-list-struct-fix-box
  1945. struct parents prevs orderedp))
  1946. ;; Report some problems due to ORDERED status of subtree. If
  1947. ;; only one box was being checked, throw an error, else,
  1948. ;; only signal problems.
  1949. (cond
  1950. ((and singlep block-item (> lim-up block-item))
  1951. (error
  1952. "Checkbox blocked because of unchecked box at line %d"
  1953. (org-current-line block-item)))
  1954. (block-item
  1955. (message
  1956. "Checkboxes were removed due to unchecked box at line %d"
  1957. (org-current-line block-item))))
  1958. (goto-char bottom)
  1959. (org-list-struct-apply-struct struct struct-copy))))
  1960. (org-update-checkbox-count-maybe)))
  1961. (defun org-reset-checkbox-state-subtree ()
  1962. "Reset all checkboxes in an entry subtree."
  1963. (interactive "*")
  1964. (save-restriction
  1965. (save-excursion
  1966. (org-narrow-to-subtree)
  1967. (org-show-subtree)
  1968. (goto-char (point-min))
  1969. (let ((end (point-max)))
  1970. (while (< (point) end)
  1971. (when (org-at-item-checkbox-p)
  1972. (replace-match "[ ]" t t nil 1))
  1973. (beginning-of-line 2))))
  1974. (org-update-checkbox-count-maybe)))
  1975. (defun org-update-checkbox-count (&optional all)
  1976. "Update the checkbox statistics in the current section.
  1977. This will find all statistic cookies like [57%] and [6/12] and
  1978. update them with the current numbers.
  1979. With optional prefix argument ALL, do this for the whole buffer."
  1980. (interactive "P")
  1981. (save-excursion
  1982. (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  1983. (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
  1984. (recursivep
  1985. (or (not org-hierarchical-checkbox-statistics)
  1986. (string-match "\\<recursive\\>"
  1987. (or (org-entry-get nil "COOKIE_DATA") ""))))
  1988. (bounds (if all
  1989. (cons (point-min) (point-max))
  1990. (cons (or (ignore-errors (org-back-to-heading t) (point))
  1991. (point-min))
  1992. (save-excursion (outline-next-heading) (point)))))
  1993. (count-boxes
  1994. (function
  1995. ;; Return number of checked boxes and boxes of all types
  1996. ;; in all structures in STRUCTS. If RECURSIVEP is non-nil,
  1997. ;; also count boxes in sub-lists. If ITEM is nil, count
  1998. ;; across the whole structure, else count only across
  1999. ;; subtree whose ancestor is ITEM.
  2000. (lambda (item structs recursivep)
  2001. (let ((c-on 0) (c-all 0))
  2002. (mapc
  2003. (lambda (s)
  2004. (let* ((pre (org-list-prevs-alist s))
  2005. (par (org-list-parents-alist s))
  2006. (items
  2007. (cond
  2008. ((and recursivep item) (org-list-get-subtree item s))
  2009. (recursivep (mapcar 'car s))
  2010. (item (org-list-get-children item s par))
  2011. (t (org-list-get-all-items
  2012. (org-list-get-top-point s) s pre))))
  2013. (cookies (delq nil (mapcar
  2014. (lambda (e)
  2015. (org-list-get-checkbox e s))
  2016. items))))
  2017. (setq c-all (+ (length cookies) c-all)
  2018. c-on (+ (org-count "[X]" cookies) c-on))))
  2019. structs)
  2020. (cons c-on c-all)))))
  2021. (backup-end 1)
  2022. cookies-list structs-bak box-num)
  2023. (goto-char (car bounds))
  2024. ;; 1. Build an alist for each cookie found within BOUNDS. The
  2025. ;; key will be position at beginning of cookie and values
  2026. ;; ending position, format of cookie, and a cell whose car is
  2027. ;; number of checked boxes to report, and cdr total number of
  2028. ;; boxes.
  2029. (while (re-search-forward cookie-re (cdr bounds) t)
  2030. (catch 'skip
  2031. (save-excursion
  2032. (push
  2033. (list
  2034. (match-beginning 1) ; cookie start
  2035. (match-end 1) ; cookie end
  2036. (match-string 2) ; percent?
  2037. (cond ; boxes count
  2038. ;; Cookie is at an heading, but specifically for todo,
  2039. ;; not for checkboxes: skip it.
  2040. ((and (org-on-heading-p)
  2041. (string-match "\\<todo\\>"
  2042. (downcase
  2043. (or (org-entry-get nil "COOKIE_DATA") ""))))
  2044. (throw 'skip nil))
  2045. ;; Cookie is at an heading, but all lists before next
  2046. ;; heading already have been read. Use data collected
  2047. ;; in STRUCTS-BAK. This should only happen when heading
  2048. ;; has more than one cookie on it.
  2049. ((and (org-on-heading-p)
  2050. (<= (save-excursion (outline-next-heading) (point))
  2051. backup-end))
  2052. (funcall count-boxes nil structs-bak recursivep))
  2053. ;; Cookie is at a fresh heading. Grab structure of
  2054. ;; every list containing a checkbox between point and
  2055. ;; next headline, and save them in STRUCTS-BAK.
  2056. ((org-on-heading-p)
  2057. (setq backup-end (save-excursion
  2058. (outline-next-heading) (point)))
  2059. (while (org-list-search-forward box-re backup-end 'move)
  2060. (let* ((struct (org-list-struct))
  2061. (bottom (org-list-get-bottom-point struct)))
  2062. (push struct structs-bak)
  2063. (goto-char bottom)))
  2064. (funcall count-boxes nil structs-bak recursivep))
  2065. ;; Cookie is at an item, and we already have list
  2066. ;; structure stored in STRUCTS-BAK.
  2067. ((and (org-at-item-p)
  2068. (< (point-at-bol) backup-end)
  2069. ;; Only lists in no special context are stored.
  2070. (not (nth 2 (org-list-context))))
  2071. (funcall count-boxes (point-at-bol) structs-bak recursivep))
  2072. ;; Cookie is at an item, but we need to compute list
  2073. ;; structure.
  2074. ((org-at-item-p)
  2075. (let ((struct (org-list-struct)))
  2076. (setq backup-end (org-list-get-bottom-point struct)
  2077. structs-bak (list struct)))
  2078. (funcall count-boxes (point-at-bol) structs-bak recursivep))
  2079. ;; Else, cookie found is at a wrong place. Skip it.
  2080. (t (throw 'skip nil))))
  2081. cookies-list))))
  2082. ;; 2. Apply alist to buffer, in reverse order so positions stay
  2083. ;; unchanged after cookie modifications.
  2084. (mapc (lambda (cookie)
  2085. (let* ((beg (car cookie))
  2086. (end (nth 1 cookie))
  2087. (percentp (nth 2 cookie))
  2088. (checked (car (nth 3 cookie)))
  2089. (total (cdr (nth 3 cookie)))
  2090. (new (if percentp
  2091. (format "[%d%%]" (/ (* 100 checked)
  2092. (max 1 total)))
  2093. (format "[%d/%d]" checked total))))
  2094. (goto-char beg)
  2095. (insert new)
  2096. (delete-region (point) (+ (point) (- end beg)))))
  2097. cookies-list))))
  2098. (defun org-get-checkbox-statistics-face ()
  2099. "Select the face for checkbox statistics.
  2100. The face will be `org-done' when all relevant boxes are checked.
  2101. Otherwise it will be `org-todo'."
  2102. (if (match-end 1)
  2103. (if (equal (match-string 1) "100%")
  2104. 'org-checkbox-statistics-done
  2105. 'org-checkbox-statistics-todo)
  2106. (if (and (> (match-end 2) (match-beginning 2))
  2107. (equal (match-string 2) (match-string 3)))
  2108. 'org-checkbox-statistics-done
  2109. 'org-checkbox-statistics-todo)))
  2110. (defun org-update-checkbox-count-maybe ()
  2111. "Update checkbox statistics unless turned off by user."
  2112. (when (cdr (assq 'checkbox org-list-automatic-rules))
  2113. (org-update-checkbox-count))
  2114. (run-hooks 'org-checkbox-statistics-hook))
  2115. (defvar org-last-indent-begin-marker (make-marker))
  2116. (defvar org-last-indent-end-marker (make-marker))
  2117. (defun org-list-indent-item-generic (arg no-subtree struct)
  2118. "Indent a local list item including its children.
  2119. When number ARG is a negative, item will be outdented, otherwise
  2120. it will be indented.
  2121. If a region is active, all items inside will be moved.
  2122. If NO-SUBTREE is non-nil, only indent the item itself, not its
  2123. children.
  2124. STRUCT is the list structure.
  2125. Return t if successful."
  2126. (save-excursion
  2127. (beginning-of-line)
  2128. (let* ((regionp (org-region-active-p))
  2129. (rbeg (and regionp (region-beginning)))
  2130. (rend (and regionp (region-end)))
  2131. (top (org-list-get-top-point struct))
  2132. (parents (org-list-parents-alist struct))
  2133. (prevs (org-list-prevs-alist struct))
  2134. ;; Are we going to move the whole list?
  2135. (specialp
  2136. (and (= top (point))
  2137. (cdr (assq 'indent org-list-automatic-rules))
  2138. (if no-subtree
  2139. (error
  2140. "First item of list cannot move without its subtree")
  2141. t))))
  2142. ;; Determine begin and end points of zone to indent. If moving
  2143. ;; more than one item, save them for subsequent moves.
  2144. (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  2145. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  2146. (if regionp
  2147. (progn
  2148. (set-marker org-last-indent-begin-marker rbeg)
  2149. (set-marker org-last-indent-end-marker rend))
  2150. (set-marker org-last-indent-begin-marker (point))
  2151. (set-marker org-last-indent-end-marker
  2152. (cond
  2153. (specialp (org-list-get-bottom-point struct))
  2154. (no-subtree (1+ (point)))
  2155. (t (org-list-get-item-end (point) struct))))))
  2156. (let* ((beg (marker-position org-last-indent-begin-marker))
  2157. (end (marker-position org-last-indent-end-marker)))
  2158. (cond
  2159. ;; Special case: moving top-item with indent rule
  2160. (specialp
  2161. (let* ((level-skip (org-level-increment))
  2162. (offset (if (< arg 0) (- level-skip) level-skip))
  2163. (top-ind (org-list-get-ind beg struct))
  2164. (old-struct (copy-tree struct)))
  2165. (if (< (+ top-ind offset) 0)
  2166. (error "Cannot outdent beyond margin")
  2167. ;; Change bullet if necessary
  2168. (when (and (= (+ top-ind offset) 0)
  2169. (string-match "*"
  2170. (org-list-get-bullet beg struct)))
  2171. (org-list-set-bullet beg struct
  2172. (org-list-bullet-string "-")))
  2173. ;; Shift every item by OFFSET and fix bullets. Then
  2174. ;; apply changes to buffer.
  2175. (mapc (lambda (e)
  2176. (let ((ind (org-list-get-ind (car e) struct)))
  2177. (org-list-set-ind (car e) struct (+ ind offset))))
  2178. struct)
  2179. (org-list-struct-fix-bul struct prevs)
  2180. (org-list-struct-apply-struct struct old-struct))))
  2181. ;; Forbidden move:
  2182. ((and (< arg 0)
  2183. ;; If only one item is moved, it mustn't have a child
  2184. (or (and no-subtree
  2185. (not regionp)
  2186. (org-list-has-child-p beg struct))
  2187. ;; If a subtree or region is moved, the last item
  2188. ;; of the subtree mustn't have a child
  2189. (let ((last-item (caar
  2190. (reverse
  2191. (org-remove-if
  2192. (lambda (e) (>= (car e) end))
  2193. struct)))))
  2194. (org-list-has-child-p last-item struct))))
  2195. (error "Cannot outdent an item without its children"))
  2196. ;; Normal shifting
  2197. (t
  2198. (let* ((new-parents
  2199. (if (< arg 0)
  2200. (org-list-struct-outdent beg end struct parents)
  2201. (org-list-struct-indent beg end struct parents prevs))))
  2202. (org-list-write-struct struct new-parents))
  2203. (org-update-checkbox-count-maybe))))))
  2204. t)
  2205. (defun org-outdent-item ()
  2206. "Outdent a local list item, but not its children.
  2207. If a region is active, all items inside will be moved."
  2208. (interactive)
  2209. (if (org-at-item-p)
  2210. (let ((struct (org-list-struct)))
  2211. (org-list-indent-item-generic -1 t struct))
  2212. (error "Not at an item")))
  2213. (defun org-indent-item ()
  2214. "Indent a local list item, but not its children.
  2215. If a region is active, all items inside will be moved."
  2216. (interactive)
  2217. (if (org-at-item-p)
  2218. (let ((struct (org-list-struct)))
  2219. (org-list-indent-item-generic 1 t struct))
  2220. (error "Not at an item")))
  2221. (defun org-outdent-item-tree ()
  2222. "Outdent a local list item including its children.
  2223. If a region is active, all items inside will be moved."
  2224. (interactive)
  2225. (let ((regionp (org-region-active-p)))
  2226. (cond
  2227. ((or (org-at-item-p)
  2228. (and (org-region-active-p)
  2229. (goto-char (region-beginning))
  2230. (org-at-item-p)))
  2231. (let ((struct (org-list-struct)))
  2232. (org-list-indent-item-generic -1 nil struct)))
  2233. (regionp (error "Region not starting at an item"))
  2234. (t (error "Not at an item")))))
  2235. (defun org-indent-item-tree ()
  2236. "Indent a local list item including its children.
  2237. If a region is active, all items inside will be moved."
  2238. (interactive)
  2239. (let ((regionp (org-region-active-p)))
  2240. (cond
  2241. ((or (org-at-item-p)
  2242. (and (org-region-active-p)
  2243. (goto-char (region-beginning))
  2244. (org-at-item-p)))
  2245. (let ((struct (org-list-struct)))
  2246. (org-list-indent-item-generic 1 nil struct)))
  2247. (regionp (error "Region not starting at an item"))
  2248. (t (error "Not at an item")))))
  2249. (defvar org-tab-ind-state)
  2250. (defun org-cycle-item-indentation ()
  2251. "Cycle levels of indentation of an empty item.
  2252. The first run indents the item, if applicable. Subsequents runs
  2253. outdent it at meaningful levels in the list. When done, item is
  2254. put back at its original position with its original bullet.
  2255. Return t at each successful move."
  2256. (when (org-at-item-p)
  2257. (let* ((org-adapt-indentation nil)
  2258. (struct (org-list-struct))
  2259. (ind (org-list-get-ind (point-at-bol) struct))
  2260. (bullet (org-list-get-bullet (point-at-bol) struct)))
  2261. ;; Accept empty items or if cycle has already started.
  2262. (when (or (eq last-command 'org-cycle-item-indentation)
  2263. (and (save-excursion
  2264. (beginning-of-line)
  2265. (looking-at org-list-full-item-re))
  2266. (>= (match-end 0) (save-excursion
  2267. (goto-char (org-list-get-item-end
  2268. (point-at-bol) struct))
  2269. (skip-chars-backward " \r\t\n")
  2270. (point)))))
  2271. (setq this-command 'org-cycle-item-indentation)
  2272. ;; When in the middle of the cycle, try to outdent first. If it
  2273. ;; fails, and point is still at initial position, indent. Else,
  2274. ;; go back to original position.
  2275. (if (eq last-command 'org-cycle-item-indentation)
  2276. (cond
  2277. ((ignore-errors (org-list-indent-item-generic -1 t struct)))
  2278. ((and (= ind (car org-tab-ind-state))
  2279. (ignore-errors (org-list-indent-item-generic 1 t struct))))
  2280. (t (delete-region (point-at-bol) (point-at-eol))
  2281. (org-indent-to-column (car org-tab-ind-state))
  2282. (insert (cdr org-tab-ind-state))
  2283. ;; Break cycle
  2284. (setq this-command 'identity)))
  2285. ;; If a cycle is starting, remember indentation and bullet,
  2286. ;; then try to indent. If it fails, try to outdent.
  2287. (setq org-tab-ind-state (cons ind bullet))
  2288. (cond
  2289. ((ignore-errors (org-list-indent-item-generic 1 t struct)))
  2290. ((ignore-errors (org-list-indent-item-generic -1 t struct)))
  2291. (t (error "Cannot move item"))))
  2292. t))))
  2293. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  2294. "Sort list items.
  2295. The cursor may be at any item of the list that should be sorted.
  2296. Sublists are not sorted. Checkboxes, if any, are ignored.
  2297. Sorting can be alphabetically, numerically, by date/time as given by
  2298. a time stamp, by a property or by priority.
  2299. Comparing entries ignores case by default. However, with an
  2300. optional argument WITH-CASE, the sorting considers case as well.
  2301. The command prompts for the sorting type unless it has been given
  2302. to the function through the SORTING-TYPE argument, which needs to
  2303. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  2304. meaning of each character:
  2305. n Numerically, by converting the beginning of the item to a number.
  2306. a Alphabetically. Only the first line of item is checked.
  2307. t By date/time, either the first active time stamp in the entry, if
  2308. any, or by the first inactive one. In a timer list, sort the timers.
  2309. Capital letters will reverse the sort order.
  2310. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
  2311. function to be called with point at the beginning of the record.
  2312. It must return either a string or a number that should serve as
  2313. the sorting key for that record. It will then use COMPARE-FUNC to
  2314. compare entries."
  2315. (interactive "P")
  2316. (let* ((case-func (if with-case 'identity 'downcase))
  2317. (struct (org-list-struct))
  2318. (prevs (org-list-prevs-alist struct))
  2319. (start (org-list-get-list-begin (point-at-bol) struct prevs))
  2320. (end (org-list-get-list-end (point-at-bol) struct prevs))
  2321. (sorting-type
  2322. (progn
  2323. (message
  2324. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  2325. (read-char-exclusive)))
  2326. (getkey-func (and (= (downcase sorting-type) ?f)
  2327. (org-icompleting-read "Sort using function: "
  2328. obarray 'fboundp t nil nil)
  2329. (intern getkey-func))))
  2330. (message "Sorting items...")
  2331. (save-restriction
  2332. (narrow-to-region start end)
  2333. (goto-char (point-min))
  2334. (let* ((dcst (downcase sorting-type))
  2335. (case-fold-search nil)
  2336. (now (current-time))
  2337. (sort-func (cond
  2338. ((= dcst ?a) 'string<)
  2339. ((= dcst ?f) compare-func)
  2340. ((= dcst ?t) '<)
  2341. (t nil)))
  2342. (next-record (lambda ()
  2343. (skip-chars-forward " \r\t\n")
  2344. (beginning-of-line)))
  2345. (end-record (lambda ()
  2346. (goto-char (org-list-get-item-end-before-blank
  2347. (point) struct))))
  2348. (value-to-sort
  2349. (lambda ()
  2350. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  2351. (cond
  2352. ((= dcst ?n)
  2353. (string-to-number (buffer-substring (match-end 0)
  2354. (point-at-eol))))
  2355. ((= dcst ?a)
  2356. (buffer-substring (match-end 0) (point-at-eol)))
  2357. ((= dcst ?t)
  2358. (cond
  2359. ;; If it is a timer list, convert timer to seconds
  2360. ((org-at-item-timer-p)
  2361. (org-timer-hms-to-secs (match-string 1)))
  2362. ((or (re-search-forward org-ts-regexp (point-at-eol) t)
  2363. (re-search-forward org-ts-regexp-both
  2364. (point-at-eol) t))
  2365. (org-time-string-to-seconds (match-string 0)))
  2366. (t (org-float-time now))))
  2367. ((= dcst ?f)
  2368. (if getkey-func
  2369. (let ((value (funcall getkey-func)))
  2370. (if (stringp value)
  2371. (funcall case-func value)
  2372. value))
  2373. (error "Invalid key function `%s'" getkey-func)))
  2374. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  2375. (sort-subr (/= dcst sorting-type)
  2376. next-record
  2377. end-record
  2378. value-to-sort
  2379. nil
  2380. sort-func)
  2381. ;; Read and fix list again, as `sort-subr' probably destroyed
  2382. ;; its structure.
  2383. (org-list-repair)
  2384. (run-hooks 'org-after-sorting-entries-or-items-hook)
  2385. (message "Sorting items...done")))))
  2386. ;;; Send and receive lists
  2387. (defun org-list-parse-list (&optional delete)
  2388. "Parse the list at point and maybe DELETE it.
  2389. Return a list whose car is a symbol of list type, among
  2390. `ordered', `unordered' and `descriptive'. Then, each item is a
  2391. list whose car is counter, and cdr are strings and other
  2392. sub-lists. Inside strings, checkboxes are replaced by \"[CBON]\"
  2393. and \"[CBOFF]\".
  2394. For example, the following list:
  2395. 1. first item
  2396. + sub-item one
  2397. + [X] sub-item two
  2398. more text in first item
  2399. 2. [@3] last item
  2400. will be parsed as:
  2401. \(ordered
  2402. \(nil \"first item\"
  2403. \(unordered
  2404. \(nil \"sub-item one\"\)
  2405. \(nil \"[CBON] sub-item two\"\)\)
  2406. \"more text in first item\"\)
  2407. \(3 \"last item\"\)\)
  2408. Point is left at list end."
  2409. (let* ((struct (org-list-struct))
  2410. (prevs (org-list-prevs-alist struct))
  2411. (parents (org-list-parents-alist struct))
  2412. (top (org-list-get-top-point struct))
  2413. (bottom (org-list-get-bottom-point struct))
  2414. out
  2415. parse-item ; for byte-compiler
  2416. (get-text
  2417. (function
  2418. ;; Return text between BEG and END, trimmed, with
  2419. ;; checkboxes replaced.
  2420. (lambda (beg end)
  2421. (let ((text (org-trim (buffer-substring beg end))))
  2422. (if (string-match "\\`\\[\\([X ]\\)\\]" text)
  2423. (replace-match
  2424. (if (equal (match-string 1 text) " ") "CBOFF" "CBON")
  2425. t nil text 1)
  2426. text)))))
  2427. (parse-sublist
  2428. (function
  2429. ;; Return a list whose car is list type and cdr a list of
  2430. ;; items' body.
  2431. (lambda (e)
  2432. (cons (org-list-get-list-type (car e) struct prevs)
  2433. (mapcar parse-item e)))))
  2434. (parse-item
  2435. (function
  2436. ;; Return a list containing counter of item, if any, text
  2437. ;; and any sublist inside it.
  2438. (lambda (e)
  2439. (let ((start (save-excursion
  2440. (goto-char e)
  2441. (looking-at "[ \t]*\\S-+[ \t]+\\(\\[@[:[:alnum:]]+\\][ \t]*\\)?")
  2442. (match-end 0)))
  2443. ;; Get counter number. For alphabetic counter, get
  2444. ;; its position in the alphabet.
  2445. (counter (let ((c (org-list-get-counter e struct)))
  2446. (cond
  2447. ((not c) nil)
  2448. ((string-match "[A-Za-z]" c)
  2449. (- (string-to-char (upcase (match-string 0 c)))
  2450. 64))
  2451. ((string-match "[0-9]+" c)
  2452. (string-to-number (match-string 0 c))))))
  2453. (childp (org-list-has-child-p e struct))
  2454. (end (org-list-get-item-end e struct)))
  2455. ;; If item has a child, store text between bullet and
  2456. ;; next child, then recursively parse all sublists. At
  2457. ;; the end of each sublist, check for the presence of
  2458. ;; text belonging to the original item.
  2459. (if childp
  2460. (let* ((children (org-list-get-children e struct parents))
  2461. (body (list (funcall get-text start childp))))
  2462. (while children
  2463. (let* ((first (car children))
  2464. (sub (org-list-get-all-items first struct prevs))
  2465. (last-c (car (last sub)))
  2466. (last-end (org-list-get-item-end last-c struct)))
  2467. (push (funcall parse-sublist sub) body)
  2468. ;; Remove children from the list just parsed.
  2469. (setq children (cdr (member last-c children)))
  2470. ;; There is a chunk of text belonging to the
  2471. ;; item if last child doesn't end where next
  2472. ;; child starts or where item ends.
  2473. (unless (= (or (car children) end) last-end)
  2474. (push (funcall get-text
  2475. last-end (or (car children) end))
  2476. body))))
  2477. (cons counter (nreverse body)))
  2478. (list counter (funcall get-text start end))))))))
  2479. ;; Store output, take care of cursor position and deletion of
  2480. ;; list, then return output.
  2481. (setq out (funcall parse-sublist (org-list-get-all-items top struct prevs)))
  2482. (goto-char top)
  2483. (when delete
  2484. (delete-region top bottom)
  2485. (when (and (not (eq org-list-ending-method 'indent))
  2486. (looking-at org-list-end-re))
  2487. (replace-match "\n")))
  2488. out))
  2489. (defun org-list-make-subtree ()
  2490. "Convert the plain list at point into a subtree."
  2491. (interactive)
  2492. (if (not (ignore-errors (goto-char (org-in-item-p))))
  2493. (error "Not in a list")
  2494. (let ((list (save-excursion (org-list-parse-list t))))
  2495. (insert (org-list-to-subtree list)))))
  2496. (defun org-list-insert-radio-list ()
  2497. "Insert a radio list template appropriate for this major mode."
  2498. (interactive)
  2499. (let* ((e (assq major-mode org-list-radio-list-templates))
  2500. (txt (nth 1 e))
  2501. name pos)
  2502. (unless e (error "No radio list setup defined for %s" major-mode))
  2503. (setq name (read-string "List name: "))
  2504. (while (string-match "%n" txt)
  2505. (setq txt (replace-match name t t txt)))
  2506. (or (bolp) (insert "\n"))
  2507. (setq pos (point))
  2508. (insert txt)
  2509. (goto-char pos)))
  2510. (defun org-list-send-list (&optional maybe)
  2511. "Send a transformed version of this list to the receiver position.
  2512. With argument MAYBE, fail quietly if no transformation is defined for
  2513. this list."
  2514. (interactive)
  2515. (catch 'exit
  2516. (unless (org-at-item-p) (error "Not at a list item"))
  2517. (save-excursion
  2518. (re-search-backward "#\\+ORGLST" nil t)
  2519. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  2520. (if maybe
  2521. (throw 'exit nil)
  2522. (error "Don't know how to transform this list"))))
  2523. (let* ((name (match-string 1))
  2524. (transform (intern (match-string 2)))
  2525. (bottom-point
  2526. (save-excursion
  2527. (re-search-forward
  2528. "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  2529. (match-beginning 0)))
  2530. (top-point
  2531. (progn
  2532. (re-search-backward "#\\+ORGLST" nil t)
  2533. (re-search-forward (org-item-beginning-re) bottom-point t)
  2534. (match-beginning 0)))
  2535. (list (save-restriction
  2536. (narrow-to-region top-point bottom-point)
  2537. (org-list-parse-list)))
  2538. beg txt)
  2539. (unless (fboundp transform)
  2540. (error "No such transformation function %s" transform))
  2541. (let ((txt (funcall transform list)))
  2542. ;; Find the insertion place
  2543. (save-excursion
  2544. (goto-char (point-min))
  2545. (unless (re-search-forward
  2546. (concat "BEGIN RECEIVE ORGLST +"
  2547. name
  2548. "\\([ \t]\\|$\\)") nil t)
  2549. (error "Don't know where to insert translated list"))
  2550. (goto-char (match-beginning 0))
  2551. (beginning-of-line 2)
  2552. (setq beg (point))
  2553. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  2554. (error "Cannot find end of insertion region"))
  2555. (delete-region beg (point-at-bol))
  2556. (goto-char beg)
  2557. (insert txt "\n")))
  2558. (message "List converted and installed at receiver location"))))
  2559. (defun org-list-to-generic (list params)
  2560. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  2561. Valid parameters PARAMS are
  2562. :ustart String to start an unordered list
  2563. :uend String to end an unordered list
  2564. :ostart String to start an ordered list
  2565. :oend String to end an ordered list
  2566. :dstart String to start a descriptive list
  2567. :dend String to end a descriptive list
  2568. :dtstart String to start a descriptive term
  2569. :dtend String to end a descriptive term
  2570. :ddstart String to start a description
  2571. :ddend String to end a description
  2572. :splice When set to t, return only list body lines, don't wrap
  2573. them into :[u/o]start and :[u/o]end. Default is nil.
  2574. :istart String to start a list item.
  2575. :icount String to start an item with a counter.
  2576. :iend String to end a list item
  2577. :isep String to separate items
  2578. :lsep String to separate sublists
  2579. :csep String to separate text from a sub-list
  2580. :cboff String to insert for an unchecked checkbox
  2581. :cbon String to insert for a checked checkbox
  2582. Alternatively, each parameter can also be a form returning a
  2583. string. These sexp can use keywords `counter' and `depth',
  2584. reprensenting respectively counter associated to the current
  2585. item, and depth of the current sub-list, starting at 0.
  2586. Obviously, `counter' is only available for parameters applying to
  2587. items."
  2588. (interactive)
  2589. (let* ((p params)
  2590. (splicep (plist-get p :splice))
  2591. (ostart (plist-get p :ostart))
  2592. (oend (plist-get p :oend))
  2593. (ustart (plist-get p :ustart))
  2594. (uend (plist-get p :uend))
  2595. (dstart (plist-get p :dstart))
  2596. (dend (plist-get p :dend))
  2597. (dtstart (plist-get p :dtstart))
  2598. (dtend (plist-get p :dtend))
  2599. (ddstart (plist-get p :ddstart))
  2600. (ddend (plist-get p :ddend))
  2601. (istart (plist-get p :istart))
  2602. (icount (plist-get p :icount))
  2603. (iend (plist-get p :iend))
  2604. (isep (plist-get p :isep))
  2605. (lsep (plist-get p :lsep))
  2606. (csep (plist-get p :csep))
  2607. (cbon (plist-get p :cbon))
  2608. (cboff (plist-get p :cboff))
  2609. export-sublist ; for byte-compiler
  2610. (export-item
  2611. (function
  2612. ;; Export an item ITEM of type TYPE, at DEPTH. First string
  2613. ;; in item is treated in a special way as it can bring
  2614. ;; extra information that needs to be processed.
  2615. (lambda (item type depth)
  2616. (let* ((counter (pop item))
  2617. (fmt (concat (cond
  2618. ((eq type 'descriptive)
  2619. (concat (org-trim (eval istart)) "%s"
  2620. (eval ddend)))
  2621. ((and counter (eq type 'ordered))
  2622. (concat (eval icount) "%s"))
  2623. (t (concat (eval istart) "%s")))
  2624. (eval iend)))
  2625. (first (car item)))
  2626. ;; Replace checkbox if any is found.
  2627. (cond
  2628. ((string-match "\\[CBON\\]" first)
  2629. (setq first (replace-match cbon t t first)))
  2630. ((string-match "\\[CBOFF\\]" first)
  2631. (setq first (replace-match cboff t t first)))
  2632. ((string-match "\\[-\\]" first)
  2633. (setq first (replace-match "$\\boxminus$" t t first))))
  2634. ;; Insert descriptive term if TYPE is `descriptive'.
  2635. (when (and (eq type 'descriptive)
  2636. (string-match "^\\(.*\\)[ \t]+::" first))
  2637. (setq first (concat
  2638. (eval dtstart) (org-trim (match-string 1 first))
  2639. (eval dtend) (eval ddstart)
  2640. (org-trim (substring first (match-end 0))) "")))
  2641. (setcar item first)
  2642. (format fmt
  2643. (mapconcat (lambda (e)
  2644. (if (stringp e) e
  2645. (funcall export-sublist e (1+ depth))))
  2646. item (or (eval csep) "")))))))
  2647. (export-sublist
  2648. (function
  2649. ;; Export sublist SUB at DEPTH
  2650. (lambda (sub depth)
  2651. (let* ((type (car sub))
  2652. (items (cdr sub))
  2653. (fmt (concat (cond
  2654. (splicep "%s")
  2655. ((eq type 'ordered)
  2656. (concat (eval ostart) "\n%s" (eval oend)))
  2657. ((eq type 'descriptive)
  2658. (concat (eval dstart) "\n%s" (eval dend)))
  2659. (t (concat (eval ustart) "\n%s" (eval uend))))
  2660. (eval lsep))))
  2661. (format fmt (mapconcat (lambda (e)
  2662. (funcall export-item e type depth))
  2663. items (or (eval isep) ""))))))))
  2664. (concat (funcall export-sublist list 0) "\n")))
  2665. (defun org-list-to-latex (list &optional params)
  2666. "Convert LIST into a LaTeX list.
  2667. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2668. with overruling parameters for `org-list-to-generic'."
  2669. (org-list-to-generic
  2670. list
  2671. (org-combine-plists
  2672. '(:splice nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  2673. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  2674. :dstart "\\begin{description}" :dend "\\end{description}"
  2675. :dtstart "[" :dtend "] "
  2676. :istart "\\item " :iend "\n"
  2677. :icount (let ((enum (nth depth '("i" "ii" "iii" "iv"))))
  2678. (if enum
  2679. (format "\\setcounter{enum%s}{%s}\n\\item "
  2680. enum counter)
  2681. "\\item "))
  2682. :csep "\n"
  2683. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  2684. params)))
  2685. (defun org-list-to-html (list &optional params)
  2686. "Convert LIST into a HTML list.
  2687. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2688. with overruling parameters for `org-list-to-generic'."
  2689. (org-list-to-generic
  2690. list
  2691. (org-combine-plists
  2692. '(:splice nil :ostart "<ol>" :oend "\n</ol>"
  2693. :ustart "<ul>" :uend "\n</ul>"
  2694. :dstart "<dl>" :dend "</dl>"
  2695. :dtstart "<dt>" :dtend "</dt>\n"
  2696. :ddstart "<dd>" :ddend "</dd>"
  2697. :istart "<li>" :iend "\n</li>"
  2698. :icount (format "<li value=\"%s\">" counter)
  2699. :isep "\n" :lsep "\n" :csep "\n"
  2700. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  2701. params)))
  2702. (defun org-list-to-texinfo (list &optional params)
  2703. "Convert LIST into a Texinfo list.
  2704. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2705. with overruling parameters for `org-list-to-generic'."
  2706. (org-list-to-generic
  2707. list
  2708. (org-combine-plists
  2709. '(:splice nil :ostart "@itemize @minus" :oend "@end itemize"
  2710. :ustart "@enumerate" :uend "@end enumerate"
  2711. :dstart "@table @asis" :dend "@end table"
  2712. :dtstart " " :dtend "\n"
  2713. :istart "@item\n" :iend "\n"
  2714. :icount "@item\n"
  2715. :csep "\n"
  2716. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  2717. params)))
  2718. (defun org-list-to-subtree (list &optional params)
  2719. "Convert LIST into an Org subtree.
  2720. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2721. with overruling parameters for `org-list-to-generic'."
  2722. (let* ((rule (cdr (assq 'heading org-blank-before-new-entry)))
  2723. (level (or (org-current-level) 0))
  2724. (blankp (or (eq rule t)
  2725. (and (eq rule 'auto)
  2726. (save-excursion
  2727. (outline-previous-heading)
  2728. (org-previous-line-empty-p)))))
  2729. (get-stars
  2730. (function
  2731. ;; Return the string for the heading, depending on depth D
  2732. ;; of current sub-list.
  2733. (lambda (d)
  2734. (concat
  2735. (make-string (+ level
  2736. (if org-odd-levels-only (* 2 (1+ d)) (1+ d)))
  2737. ?*)
  2738. " ")))))
  2739. (org-list-to-generic
  2740. list
  2741. (org-combine-plists
  2742. '(:splice t
  2743. :dtstart " " :dtend " "
  2744. :istart (funcall get-stars depth)
  2745. :icount (funcall get-stars depth)
  2746. :isep (if blankp "\n\n" "\n")
  2747. :csep (if blankp "\n\n" "\n")
  2748. :cbon "DONE" :cboff "TODO")
  2749. params))))
  2750. (provide 'org-list)
  2751. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  2752. ;;; org-list.el ends here