org-list.el 98 KB

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