org-list.el 92 KB

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