org-list.el 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  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.01trans
  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. (declare-function org-invisible-p "org" ())
  38. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  39. (declare-function outline-next-heading "outline" ())
  40. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  41. (declare-function org-back-over-empty-lines "org" ())
  42. (declare-function org-trim "org" (s))
  43. (declare-function org-get-indentation "org" (&optional line))
  44. (declare-function org-timer-item "org-timer" (&optional arg))
  45. (declare-function org-timer-hms-to-secs "org-timer" (hms))
  46. (declare-function org-combine-plists "org" (&rest plists))
  47. (declare-function org-entry-get "org"
  48. (pom property &optional inherit literal-nil))
  49. (declare-function org-narrow-to-subtree "org" ())
  50. (declare-function org-show-subtree "org" ())
  51. (defgroup org-plain-lists nil
  52. "Options concerning plain lists in Org-mode."
  53. :tag "Org Plain lists"
  54. :group 'org-structure)
  55. (defcustom org-cycle-include-plain-lists t
  56. "When t, make TAB cycle visibility on plain list items.
  57. Cycling plain lists works only when the cursor is on a plain list
  58. item. When the cursor is on an outline heading, plain lists are
  59. treated as text. This is the most stable way of handling this,
  60. which is why it is the default.
  61. When this is the symbol `integrate', then during cycling, plain
  62. list items will *temporarily* be interpreted as outline headlines
  63. with a level given by 1000+i where i is the indentation of the
  64. bullet. This setting can lead to strange effects when switching
  65. visibility to `children', because the first \"child\" in a
  66. subtree decides what children should be listed. If that first
  67. \"child\" is a plain list item with an implied large level
  68. number, all true children and grand children of the outline
  69. heading will be exposed in a children' view."
  70. :group 'org-plain-lists
  71. :type '(choice
  72. (const :tag "Never" nil)
  73. (const :tag "With cursor in plain list (recommended)" t)
  74. (const :tag "As children of outline headings" integrate)))
  75. (defcustom org-list-demote-modify-bullet nil
  76. "Default bullet type installed when demoting an item.
  77. This is an association list, for each bullet type, this alist will point
  78. to the bullet that should be used when this item is demoted.
  79. For example,
  80. (setq org-list-demote-modify-bullet
  81. '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
  82. will make
  83. + Movies
  84. + Silence of the Lambs
  85. + My Cousin Vinny
  86. + Books
  87. + The Hunt for Red October
  88. + The Road to Omaha
  89. into
  90. + Movies
  91. - Silence of the Lambs
  92. - My Cousin Vinny
  93. + Books
  94. - The Hunt for Red October
  95. - The Road to Omaha"
  96. :group 'org-plain-lists
  97. :type '(repeat
  98. (cons
  99. (choice :tag "If the current bullet is "
  100. (const "-")
  101. (const "+")
  102. (const "*")
  103. (const "1.")
  104. (const "1)"))
  105. (choice :tag "demotion will change it to"
  106. (const "-")
  107. (const "+")
  108. (const "*")
  109. (const "1.")
  110. (const "1)")))))
  111. (defcustom org-plain-list-ordered-item-terminator t
  112. "The character that makes a line with leading number an ordered list item.
  113. Valid values are ?. and ?\). To get both terminators, use t. While
  114. ?. may look nicer, it creates the danger that a line with leading
  115. number may be incorrectly interpreted as an item. ?\) therefore is
  116. the safe choice."
  117. :group 'org-plain-lists
  118. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  119. (const :tag "paren like in \"2)\"" ?\))
  120. (const :tab "both" t)))
  121. (defcustom org-list-two-spaces-after-bullet-regexp nil
  122. "A regular expression matching bullets that should have 2 spaces after them.
  123. When nil, no bullet will have two spaces after them.
  124. When a string, it will be used as a regular expression. When the
  125. bullet type of a list is changed, the new bullet type will be
  126. matched against this regexp. If it matches, there will be two
  127. spaces instead of one after the bullet in each item of the list."
  128. :group 'org-plain-lists
  129. :type '(choice
  130. (const :tag "never" nil)
  131. (regexp)))
  132. (defcustom org-list-ending-method 'regexp
  133. "Determine where plain lists should end.
  134. Valid values are: `regexp', `indent' or `both'.
  135. When set to `regexp', Org will look into two variables,
  136. `org-empty-line-terminates-plain-lists' and the more general
  137. `org-list-end-regexp', to know what will end lists. This is the
  138. default value.
  139. When set to `indent', indentation of the last non-blank line will
  140. determine if point is in a list. If that line is less indented
  141. than the previous item in the section, if any, list has ended.
  142. When set to `both', each of the preceding methods must confirm
  143. that point is in a list."
  144. :group 'org-plain-lists
  145. :type '(choice
  146. (const :tag "With a well defined ending (recommended)" regexp)
  147. (const :tag "With indentation of the current line" indent)
  148. (const :tag "With both methods" both)))
  149. (defcustom org-empty-line-terminates-plain-lists nil
  150. "Non-nil means an empty line ends all plain list levels.
  151. This variable only makes sense if `org-list-ending-method' is set
  152. to regexp or both. This is then equivalent to set
  153. `org-list-end-regexp' to \"^[ \\t]*$\"."
  154. :group 'org-plain-lists
  155. :type 'boolean)
  156. (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
  157. "Regexp matching the end of all plain list levels.
  158. It must start with \"^\" and end with \"\\n\". It defaults to 2
  159. blank lines. `org-empty-line-terminates-plain-lists' has
  160. precedence over it."
  161. :group 'org-plain-lists
  162. :type 'string)
  163. (defcustom org-list-automatic-rules '((bullet . t)
  164. (checkbox . t)
  165. (indent . t)
  166. (insert . t))
  167. "Non-nil means apply set of rules when acting on lists.
  168. By default, automatic actions are taken when using
  169. \\[org-shiftmetaup], \\[org-shiftmetadown], \\[org-meta-return],
  170. \\[org-metaright], \\[org-metaleft], \\[org-shiftmetaright],
  171. \\[org-shiftmetaleft], \\[org-ctrl-c-minus],
  172. \\[org-toggle-checkbox] or \\[org-insert-todo-heading]. You can
  173. disable individually these rules by setting them to nil. Valid
  174. rules are:
  175. bullet when non-nil, cycling bullet do not allow lists at
  176. column 0 to have * as a bullet and descriptions lists
  177. to be numbered.
  178. checkbox when non-nil, checkbox statistics is updated each time
  179. you either insert a new checkbox or toggle a checkbox.
  180. It also prevents from inserting a checkbox in a
  181. description item.
  182. indent when non-nil, indenting or outdenting list top-item
  183. with its subtree will move the whole list and
  184. outdenting a list whose bullet is * to column 0 will
  185. change that bullet to -
  186. insert when non-nil, trying to insert an item inside a block
  187. will insert it right before the block instead of
  188. throwing an error."
  189. :group 'org-plain-lists
  190. :type '(alist :tag "Sets of rules"
  191. :key-type
  192. (choice
  193. (const :tag "Bullet" bullet)
  194. (const :tag "Checkbox" checkbox)
  195. (const :tag "Indent" indent)
  196. (const :tag "Insert" insert))
  197. :value-type
  198. (boolean :tag "Activate" :value t)))
  199. (defcustom org-hierarchical-checkbox-statistics t
  200. "Non-nil means checkbox statistics counts only the state of direct children.
  201. When nil, all boxes below the cookie are counted.
  202. This can be set to nil on a per-node basis using a COOKIE_DATA property
  203. with the word \"recursive\" in the value."
  204. :group 'org-plain-lists
  205. :type 'boolean)
  206. (defcustom org-description-max-indent 20
  207. "Maximum indentation for the second line of a description list.
  208. When the indentation would be larger than this, it will become
  209. 5 characters instead."
  210. :group 'org-plain-lists
  211. :type 'integer)
  212. (defcustom org-list-radio-list-templates
  213. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  214. % END RECEIVE ORGLST %n
  215. \\begin{comment}
  216. #+ORGLST: SEND %n org-list-to-latex
  217. -
  218. \\end{comment}\n")
  219. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  220. @c END RECEIVE ORGLST %n
  221. @ignore
  222. #+ORGLST: SEND %n org-list-to-texinfo
  223. -
  224. @end ignore\n")
  225. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  226. <!-- END RECEIVE ORGLST %n -->
  227. <!--
  228. #+ORGLST: SEND %n org-list-to-html
  229. -
  230. -->\n"))
  231. "Templates for radio lists in different major modes.
  232. All occurrences of %n in a template will be replaced with the name of the
  233. list, obtained by prompting the user."
  234. :group 'org-plain-lists
  235. :type '(repeat
  236. (list (symbol :tag "Major mode")
  237. (string :tag "Format"))))
  238. ;;; Internal functions
  239. (defun org-list-end-re ()
  240. "Return the regex corresponding to the end of a list.
  241. It depends on `org-empty-line-terminates-plain-lists'."
  242. (if org-empty-line-terminates-plain-lists
  243. "^[ \t]*\n"
  244. org-list-end-regexp))
  245. (defun org-item-re (&optional general)
  246. "Return the correct regular expression for plain lists.
  247. If GENERAL is non-nil, return the general regexp independent of the value
  248. of `org-plain-list-ordered-item-terminator'."
  249. (cond
  250. ((or general (eq org-plain-list-ordered-item-terminator t))
  251. "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  252. ((= org-plain-list-ordered-item-terminator ?.)
  253. "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  254. ((= org-plain-list-ordered-item-terminator ?\))
  255. "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  256. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
  257. (defconst org-item-beginning-re (concat "^" (org-item-re))
  258. "Regexp matching the beginning of a plain list item.")
  259. (defun org-list-terminator-between (min max &optional firstp)
  260. "Find the position of a list ender between MIN and MAX, or nil.
  261. This function looks for `org-list-end-re' outside a block.
  262. If FIRSTP in non-nil, return the point at the beginning of the
  263. nearest valid terminator from MIN. Otherwise, return the point at
  264. the end of the nearest terminator from MAX."
  265. (save-excursion
  266. (let* ((start (if firstp min max))
  267. (end (if firstp max min))
  268. (search-fun (if firstp
  269. #'org-search-forward-unenclosed
  270. #'org-search-backward-unenclosed))
  271. (list-end-p (progn
  272. (goto-char start)
  273. (funcall search-fun (org-list-end-re) end t))))
  274. ;; Is there a valid list terminator somewhere ?
  275. (and list-end-p
  276. ;; we want to be on the first line of the list ender
  277. (match-beginning 0)))))
  278. (defun org-list-maybe-skip-block (search limit)
  279. "Return non-nil value if point is in a block, skipping it on the way.
  280. It looks for the boundary of the block in SEARCH direction."
  281. (save-match-data
  282. (let ((case-fold-search t)
  283. (boundary (if (eq search 're-search-forward) 3 5)))
  284. (when (save-excursion
  285. (and (funcall search "^[ \t]*#\\+\\(begin\\|end\\)_" limit t)
  286. (= (length (match-string 1)) boundary)))
  287. ;; We're in a block: get out of it
  288. (goto-char (match-beginning 0))))))
  289. (defun org-list-search-unenclosed-generic (search re bound noerr)
  290. "Search for RE with SEARCH outside blocks and protected places."
  291. (catch 'exit
  292. (let ((origin (point)))
  293. (while t
  294. ;; 1. No match: return to origin or bound, depending on NOERR.
  295. (unless (funcall search re bound noerr)
  296. (throw 'exit (and (goto-char (if (booleanp noerr) origin bound))
  297. nil)))
  298. ;; 2. Match not in block or protected: return point. Else
  299. ;; skip the block and carry on.
  300. (unless (or (get-text-property (match-beginning 0) 'org-protected)
  301. (org-list-maybe-skip-block search bound))
  302. (throw 'exit (point)))))))
  303. (defun org-search-backward-unenclosed (regexp &optional bound noerror)
  304. "Like `re-search-backward' but don't stop inside blocks or protected places."
  305. (org-list-search-unenclosed-generic
  306. #'re-search-backward regexp (or bound (point-min)) noerror))
  307. (defun org-search-forward-unenclosed (regexp &optional bound noerror)
  308. "Like `re-search-forward' but don't stop inside blocks or protected places."
  309. (org-list-search-unenclosed-generic
  310. #'re-search-forward regexp (or bound (point-max)) noerror))
  311. (defun org-list-in-item-p-with-indent (limit)
  312. "Is the cursor inside a plain list?
  313. Plain lists are considered ending when a non-blank line is less
  314. indented than the previous item within LIMIT.
  315. Return the position of the previous item, if applicable."
  316. (save-excursion
  317. (beginning-of-line)
  318. ;; do not start searching at a blank line or inside a block
  319. (while (or (and (org-list-maybe-skip-block #'re-search-backward limit)
  320. (goto-char (1- (point-at-bol))))
  321. (and (looking-at "^[ \t]*$") (not (bobp))))
  322. (skip-chars-backward " \r\t\n")
  323. (beginning-of-line))
  324. (or (and (org-at-item-p) (point-at-bol))
  325. (let* ((pos (point))
  326. (ind (org-get-indentation))
  327. (bound (save-excursion
  328. (goto-char limit)
  329. (and (org-search-forward-unenclosed
  330. org-item-beginning-re pos t)
  331. (point-at-bol)))))
  332. (when bound
  333. (catch 'exit
  334. (while t
  335. (cond
  336. ((or (bobp) (< (point) bound)) (throw 'exit nil))
  337. ;; skip blank lines..
  338. ((and (not (looking-at "[ \t]*$"))
  339. ;; blocks...
  340. (not (org-list-maybe-skip-block
  341. #'re-search-backward bound))
  342. ;; and items more indented.
  343. (< (org-get-indentation) ind))
  344. (throw 'exit (and (org-at-item-p) (point-at-bol))))
  345. (t (beginning-of-line 0))))))))))
  346. (defun org-list-in-item-p-with-regexp (limit)
  347. "Is the cursor inside a plain list?
  348. Plain lists end when `org-list-end-regexp' is matched, or at a
  349. blank line if `org-empty-line-terminates-plain-lists' is true."
  350. (save-excursion
  351. (let* ((actual-pos (goto-char (point-at-eol)))
  352. ;; Moved to eol so current line can be matched by
  353. ;; `org-item-re'.
  354. (last-item-start (save-excursion
  355. (org-search-backward-unenclosed
  356. org-item-beginning-re limit t)))
  357. (list-ender (org-list-terminator-between
  358. last-item-start actual-pos)))
  359. ;; We are in a list when we are on an item line or when we can
  360. ;; find an item before point and there is no valid list ender
  361. ;; between it and the point.
  362. (and last-item-start
  363. (not list-ender)))))
  364. (defun org-list-top-point-with-regexp (limit)
  365. "Return point at the top level item in a list, or nil if not in a list.
  366. List ending is determined by regexp. See
  367. `org-list-ending-method'. for more information."
  368. (save-excursion
  369. (and (org-list-in-item-p-with-regexp limit)
  370. (let ((pos (point-at-eol)))
  371. ;; Is there some list above this one ? If so, go to its ending.
  372. ;; Otherwise, go back to the heading above or bob.
  373. (goto-char (or (org-list-terminator-between limit pos) limit))
  374. ;; From there, search down our list.
  375. (org-search-forward-unenclosed org-item-beginning-re pos t)
  376. (point-at-bol)))))
  377. (defun org-list-bottom-point-with-regexp (limit)
  378. "Return point just before list ending or nil if not in a list.
  379. List ending is determined by regexp. See
  380. `org-list-ending-method'. for more information."
  381. (save-excursion
  382. (and (org-in-item-p)
  383. (let ((pos (point)))
  384. ;; The list ending is either first point matching
  385. ;; `org-list-end-re', point at first white-line before next
  386. ;; heading, or eob.
  387. (or (org-list-terminator-between (min pos limit) limit t) limit)))))
  388. (defun org-list-top-point-with-indent (limit)
  389. "Return point at the top level in a list, or nil if not in a list.
  390. List ending is determined by indentation of text. See
  391. `org-list-ending-method'. for more information."
  392. (save-excursion
  393. (let ((prev-p (org-list-in-item-p-with-indent limit)))
  394. (and prev-p
  395. (catch 'exit
  396. (while t
  397. (cond
  398. ((not prev-p) (throw 'exit (1+ (point-at-eol))))
  399. ((= prev-p limit) (throw 'exit limit))
  400. (t
  401. (goto-char prev-p)
  402. (beginning-of-line 0)
  403. (setq prev-p (org-list-in-item-p-with-indent limit))))))))))
  404. (defun org-list-bottom-point-with-indent (limit)
  405. "Return point just before list ending or nil if not in a list.
  406. List ending is determined by the indentation of text. See
  407. `org-list-ending-method' for more information."
  408. (save-excursion
  409. (let* ((ind (save-excursion
  410. (ignore-errors (org-beginning-of-item))
  411. (org-get-indentation)))
  412. (end-item (lambda ()
  413. (save-excursion
  414. (catch 'end
  415. (while t
  416. (beginning-of-line 2)
  417. (cond
  418. ((>= (point) limit) (throw 'end limit))
  419. ((or (looking-at "^[ \t]*$")
  420. (org-list-maybe-skip-block
  421. #'re-search-forward limit)
  422. (> (org-get-indentation) ind)))
  423. (t (throw 'end (point-at-bol))))))))))
  424. (and (org-in-item-p)
  425. (catch 'exit
  426. (while t
  427. (goto-char (funcall end-item))
  428. (if (looking-at org-item-beginning-re)
  429. (setq ind (org-get-indentation))
  430. (skip-chars-backward " \r\t\n")
  431. (throw 'exit (1+ (point-at-eol))))))))))
  432. (defun org-list-at-regexp-after-bullet-p (regexp)
  433. "Is point at a list item with REGEXP after bullet?"
  434. (and (org-at-item-p)
  435. (save-excursion
  436. (goto-char (match-end 0))
  437. ;; Ignore counter if any
  438. (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
  439. (goto-char (match-end 0)))
  440. (looking-at regexp))))
  441. (defun org-list-get-item-same-level (search-fun pos limit pre-move)
  442. "Return point at the beginning of next item at the same level.
  443. Search items using function SEARCH-FUN, from POS to LIMIT. It
  444. uses PRE-MOVE before search. Return nil if no item was found."
  445. (save-excursion
  446. (goto-char pos)
  447. (let ((ind (progn
  448. (org-beginning-of-item)
  449. (org-get-indentation)))
  450. (start (point-at-bol)))
  451. ;; We don't want to match the current line.
  452. (funcall pre-move)
  453. ;; Skip any sublist on the way
  454. (while (and (funcall search-fun org-item-beginning-re limit t)
  455. (> (org-get-indentation) ind)))
  456. (when (and (/= (point-at-bol) start) ; Have we moved ?
  457. (= (org-get-indentation) ind))
  458. (point-at-bol)))))
  459. (defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
  460. "Insert a new list item at POS.
  461. If POS is before first character after bullet of the item, the
  462. new item will be created before the current one.
  463. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  464. after the bullet. Cursor will be after this text once the
  465. function ends."
  466. (goto-char pos)
  467. ;; Is point in a special block?
  468. (when (org-in-regexps-block-p
  469. "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  470. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
  471. (if (not (cdr (assq 'insert org-list-automatic-rules)))
  472. ;; Rule in `org-list-automatic-rules' forbids insertion.
  473. (error "Cannot insert item inside a block.")
  474. ;; Else, move before it prior to add a new item.
  475. (end-of-line)
  476. (re-search-backward "^[ \t]*#\\+\\(begin\\|BEGIN\\)_" nil t)
  477. (end-of-line 0)))
  478. (let* ((true-pos (point))
  479. (bullet (and (org-beginning-of-item)
  480. (org-list-bullet-string (org-get-bullet))))
  481. (ind (org-get-indentation))
  482. (before-p (progn
  483. ;; Description item: text starts after colons.
  484. (or (org-at-item-description-p)
  485. ;; At a checkbox: text starts after it.
  486. (org-at-item-checkbox-p)
  487. ;; Otherwise, text starts after bullet.
  488. (org-at-item-p))
  489. (<= true-pos (match-end 0))))
  490. ;; Guess number of blank lines used to separate items.
  491. (blank-lines-nb
  492. (let ((insert-blank-p
  493. (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  494. usr-blank)
  495. (cond
  496. ;; Trivial cases where there should be none.
  497. ((or (and (not (eq org-list-ending-method 'indent))
  498. org-empty-line-terminates-plain-lists)
  499. (not insert-blank-p)) 0)
  500. ;; When `org-blank-before-new-entry' says so, it is 1.
  501. ((eq insert-blank-p t) 1)
  502. ;; plain-list-item is 'auto. Count blank lines separating
  503. ;; neighbours items in list.
  504. (t (let ((next-p (org-get-next-item (point)
  505. (org-list-bottom-point))))
  506. (cond
  507. ;; Is there a next item?
  508. (next-p (goto-char next-p)
  509. (org-back-over-empty-lines))
  510. ;; Is there a previous item?
  511. ((not (org-list-first-item-p)) (org-back-over-empty-lines))
  512. ;; User inserted blank lines, trust him
  513. ((and (> true-pos (org-end-of-item-before-blank))
  514. (> (save-excursion
  515. (goto-char true-pos)
  516. (skip-chars-backward " \t")
  517. (setq usr-blank (org-back-over-empty-lines))) 0))
  518. usr-blank)
  519. ;; Item alone: count lines separating it from parent, if any
  520. ((/= (org-list-top-point) (point-at-bol))
  521. (org-back-over-empty-lines))
  522. ;; Are there blank lines inside the item ?
  523. ((save-excursion
  524. (org-search-forward-unenclosed
  525. "^[ \t]*$" (org-end-of-item-before-blank) t)) 1)
  526. ;; No parent: no blank line.
  527. (t 0)))))))
  528. (insert-fun
  529. (lambda (text)
  530. ;; insert bullet above item in order to avoid bothering
  531. ;; with possible blank lines ending last item.
  532. (org-beginning-of-item)
  533. (indent-to-column ind)
  534. (insert (concat bullet (when checkbox "[ ] ") after-bullet))
  535. ;; Stay between after-bullet and before text.
  536. (save-excursion
  537. (insert (concat text (make-string (1+ blank-lines-nb) ?\n))))
  538. (unless before-p (org-move-item-down))
  539. (when checkbox (org-update-checkbox-count-maybe)))))
  540. (goto-char true-pos)
  541. (cond
  542. (before-p (funcall insert-fun nil)
  543. (org-list-repair) t)
  544. ;; Can't split item: insert bullet at the end of item.
  545. ((not (org-get-alist-option org-M-RET-may-split-line 'item))
  546. (funcall insert-fun nil) t)
  547. ;; else, insert a new bullet along with everything from point
  548. ;; down to last non-blank line of item.
  549. (t
  550. (delete-horizontal-space)
  551. ;; Get pos again in case previous command modified line.
  552. (let* ((pos (point))
  553. (end-before-blank (org-end-of-item-before-blank))
  554. (after-text
  555. (when (< pos end-before-blank)
  556. (prog1
  557. (delete-and-extract-region pos end-before-blank)
  558. ;; delete any blank line at and before point.
  559. (beginning-of-line)
  560. (while (looking-at "^[ \t]*$")
  561. (delete-region (point-at-bol) (1+ (point-at-eol)))
  562. (beginning-of-line 0))))))
  563. (funcall insert-fun after-text) t)))))
  564. ;;; Predicates
  565. (defun org-in-item-p ()
  566. "Is the cursor inside a plain list?
  567. This checks `org-list-ending-method'."
  568. (unless (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
  569. (let ((bound (or (save-excursion (outline-previous-heading))
  570. (point-min))))
  571. (cond
  572. ((eq org-list-ending-method 'indent)
  573. (org-list-in-item-p-with-indent bound))
  574. ((eq org-list-ending-method 'both)
  575. (and (org-list-in-item-p-with-regexp bound)
  576. (org-list-in-item-p-with-indent bound)))
  577. (t (org-list-in-item-p-with-regexp bound))))))
  578. (defun org-list-first-item-p ()
  579. "Is this item the first item in a plain list?
  580. Assume point is at an item."
  581. (save-excursion
  582. (beginning-of-line)
  583. (let ((ind (org-get-indentation)))
  584. (or (not (org-search-backward-unenclosed
  585. org-item-beginning-re (org-list-top-point) t))
  586. (< (org-get-indentation) ind)))))
  587. (defun org-at-item-p ()
  588. "Is point in a line starting a hand-formatted item?"
  589. (save-excursion
  590. (beginning-of-line) (looking-at org-item-beginning-re)))
  591. (defun org-at-item-bullet-p ()
  592. "Is point at the bullet of a plain list item?"
  593. (and (org-at-item-p)
  594. (not (member (char-after) '(?\ ?\t)))
  595. (< (point) (match-end 0))))
  596. (defun org-at-item-timer-p ()
  597. "Is point at a line starting a plain list item with a timer?"
  598. (org-list-at-regexp-after-bullet-p
  599. "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  600. (defun org-at-item-description-p ()
  601. "Is point at a description list item?"
  602. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
  603. (defun org-at-item-checkbox-p ()
  604. "Is point at a line starting a plain-list item with a checklet?"
  605. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  606. (defun org-checkbox-blocked-p ()
  607. "Is the current checkbox blocked from for being checked now?
  608. A checkbox is blocked if all of the following conditions are fulfilled:
  609. 1. The checkbox is not checked already.
  610. 2. The current entry has the ORDERED property set.
  611. 3. There is an unchecked checkbox in this entry before the current line."
  612. (catch 'exit
  613. (save-match-data
  614. (save-excursion
  615. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  616. (when (equal (match-string 1) "[X]")
  617. ;; the box is already checked!
  618. (throw 'exit nil))
  619. (let ((end (point-at-bol)))
  620. (condition-case nil (org-back-to-heading t)
  621. (error (throw 'exit nil)))
  622. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  623. (when (org-search-forward-unenclosed
  624. "^[ \t]*[-+*0-9.)]+[ \t]+\\(\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[[- ]\\]" end t)
  625. (org-current-line)))))))
  626. ;;; Navigate
  627. (defun org-list-top-point ()
  628. "Return point at the top level in a list, or nil if not in a list."
  629. (let ((limit (or (save-excursion (outline-previous-heading))
  630. (point-min))))
  631. (cond
  632. ((eq org-list-ending-method 'indent)
  633. (org-list-top-point-with-indent limit))
  634. ((eq org-list-ending-method 'both)
  635. (let ((top-re (org-list-top-point-with-regexp limit))
  636. (top-ind (org-list-top-point-with-indent limit)))
  637. (if (and top-re top-ind)
  638. (max top-re top-ind)
  639. (or top-re top-ind))))
  640. (t (org-list-top-point-with-regexp limit)))))
  641. (defun org-list-bottom-point ()
  642. "Return point just before list ending or nil if not in a list."
  643. (let ((limit (or (save-excursion
  644. (and (let ((outline-regexp org-outline-regexp))
  645. ;; Use default regexp because folding
  646. ;; changes OUTLINE-REGEXP.
  647. (outline-next-heading))
  648. (skip-chars-backward " \r\t\n")
  649. (1+ (point-at-eol))))
  650. (point-max))))
  651. (cond
  652. ((eq org-list-ending-method 'indent)
  653. (org-list-bottom-point-with-indent limit))
  654. ((eq org-list-ending-method 'both)
  655. (let ((bottom-re (org-list-bottom-point-with-regexp limit))
  656. (bottom-ind (org-list-bottom-point-with-indent limit)))
  657. (if (and bottom-re bottom-ind)
  658. (min bottom-re bottom-ind)
  659. (or bottom-re bottom-ind))))
  660. (t (org-list-bottom-point-with-regexp limit)))))
  661. (defun org-beginning-of-item ()
  662. "Go to the beginning of the current hand-formatted item.
  663. If the cursor is not in an item, throw an error. Return point."
  664. (interactive)
  665. (if (not (org-in-item-p))
  666. (error "Not in an item")
  667. ;; Possibly match the current line.
  668. (end-of-line)
  669. (org-search-backward-unenclosed org-item-beginning-re nil t)
  670. (goto-char (point-at-bol))))
  671. (defun org-end-of-item ()
  672. "Go to the end of the current hand-formatted item.
  673. If the cursor is not in an item, throw an error. Return point."
  674. (interactive)
  675. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  676. (if next-p (goto-char next-p) (org-end-of-item-list))))
  677. (defun org-end-of-item-or-at-child ()
  678. "Move to the end of the item text, stops before the first child if any."
  679. (let ((limit (org-list-bottom-point)))
  680. (end-of-line)
  681. (goto-char
  682. (if (org-search-forward-unenclosed org-item-beginning-re limit t)
  683. (point-at-bol)
  684. limit))))
  685. (defun org-end-of-item-before-blank ()
  686. "Return point at end of item, before any blank line.
  687. Point returned is at eol."
  688. (save-excursion
  689. (org-end-of-item)
  690. (skip-chars-backward " \r\t\n")
  691. (point-at-eol)))
  692. (defun org-get-next-item (pos limit)
  693. "Get the point of the next item at the same level as POS.
  694. Stop searching at LIMIT. Return nil if no item is found. This
  695. function does not move point."
  696. (org-list-get-item-same-level
  697. #'org-search-forward-unenclosed pos limit #'end-of-line))
  698. (defun org-get-previous-item (pos limit)
  699. "Get the point of the previous item at the same level as POS.
  700. Stop searching at LIMIT. Return nil if no item is found. This
  701. function does not move point."
  702. (org-list-get-item-same-level
  703. #'org-search-backward-unenclosed pos limit #'beginning-of-line))
  704. (defun org-next-item ()
  705. "Move to the beginning of the next item.
  706. Item is at the same level in the current plain list. Error if not
  707. in a plain list, or if this is the last item in the list."
  708. (interactive)
  709. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  710. (if next-p (goto-char next-p) (error "On last item"))))
  711. (defun org-previous-item ()
  712. "Move to the beginning of the previous item.
  713. Item is at the same level in the current plain list. Error if not
  714. in a plain list, or if this is the first item in the list."
  715. (interactive)
  716. (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
  717. (if prev-p (goto-char prev-p) (error "On first item"))))
  718. (defun org-beginning-of-item-list ()
  719. "Go to the beginning item of the current list or sublist.
  720. Return point."
  721. (interactive)
  722. (let ((limit (org-list-top-point))
  723. prev-p)
  724. (while (setq prev-p (org-get-previous-item (point) limit))
  725. (goto-char prev-p))
  726. (goto-char (point-at-bol))))
  727. (defun org-end-of-item-list ()
  728. "Go to the end of the current list or sublist.
  729. Return point."
  730. (interactive)
  731. (org-beginning-of-item)
  732. (let ((limit (org-list-bottom-point))
  733. (ind (org-get-indentation)))
  734. (while (and (/= (point) limit)
  735. (>= (org-get-indentation) ind))
  736. (org-search-forward-unenclosed org-item-beginning-re limit 'move))
  737. (if (= (point) limit) limit (goto-char (point-at-bol)))))
  738. ;;; Manipulate
  739. (defun org-list-exchange-items (beg-A beg-B)
  740. "Swap item starting at BEG-A with item starting at BEG-B.
  741. Blank lines at the end of items are left in place. Assumes
  742. BEG-A is lesser than BEG-B."
  743. (save-excursion
  744. (let* ((end-of-item-no-blank (lambda (pos)
  745. (goto-char pos)
  746. (goto-char (org-end-of-item-before-blank))))
  747. (end-A-no-blank (funcall end-of-item-no-blank beg-A))
  748. (end-B-no-blank (funcall end-of-item-no-blank beg-B))
  749. (body-A (buffer-substring beg-A end-A-no-blank))
  750. (body-B (buffer-substring beg-B end-B-no-blank))
  751. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
  752. (goto-char beg-A)
  753. (delete-region beg-A end-B-no-blank)
  754. (insert (concat body-B between-A-no-blank-and-B body-A)))))
  755. (defun org-move-item-down ()
  756. "Move the plain list item at point down, i.e. swap with following item.
  757. Subitems (items with larger indentation) are considered part of the item,
  758. so this really moves item trees."
  759. (interactive)
  760. (let ((pos (point))
  761. (col (current-column))
  762. (actual-item (org-beginning-of-item))
  763. (next-item (org-get-next-item (point) (org-list-bottom-point))))
  764. (if (not next-item)
  765. (progn
  766. (goto-char pos)
  767. (error "Cannot move this item further down"))
  768. (org-list-exchange-items actual-item next-item)
  769. (org-list-repair)
  770. (org-next-item)
  771. (move-to-column col))))
  772. (defun org-move-item-up ()
  773. "Move the plain list item at point up, i.e. swap with previous item.
  774. Subitems (items with larger indentation) are considered part of the item,
  775. so this really moves item trees."
  776. (interactive)
  777. (let ((pos (point))
  778. (col (current-column))
  779. (actual-item (org-beginning-of-item))
  780. (prev-item (org-get-previous-item (point) (org-list-top-point))))
  781. (if (not prev-item)
  782. (progn
  783. (goto-char pos)
  784. (error "Cannot move this item further up"))
  785. (org-list-exchange-items prev-item actual-item)
  786. (org-list-repair)
  787. (move-to-column col))))
  788. (defun org-insert-item (&optional checkbox)
  789. "Insert a new item at the current level.
  790. If cursor is before first character after bullet of the item, the
  791. new item will be created before the current one. Return t when
  792. things worked, nil when we are not in an item, or item is
  793. invisible."
  794. (unless (or (not (org-in-item-p))
  795. (org-invisible-p))
  796. (if (save-excursion
  797. (org-beginning-of-item)
  798. (org-at-item-timer-p))
  799. ;; Timer list: delegate to `org-timer-item'.
  800. (progn (org-timer-item) t)
  801. ;; if we're in a description list, ask for the new term.
  802. (let ((desc-text (when (save-excursion
  803. (and (org-beginning-of-item)
  804. (org-at-item-description-p)))
  805. (concat (read-string "Term: ") " :: "))))
  806. ;; Don't insert a checkbox if checkbox rule is applied and it
  807. ;; is a description item.
  808. (org-list-insert-item-generic
  809. (point) (and checkbox
  810. (or (not desc-text)
  811. (not (cdr (assq 'checkbox org-list-automatic-rules)))))
  812. desc-text)))))
  813. ;;; Structures
  814. ;; The idea behind structures is to avoid moving back and forth in the
  815. ;; buffer on costly operations like indenting or fixing bullets.
  816. ;; It achieves this by taking a snapshot of an interesting part of the
  817. ;; list, in the shape of an alist, with `org-list-struct'.
  818. ;; It then proceeds to changes directly on the alist. When those are
  819. ;; done, `org-list-struct-apply-struct' applies the changes in the
  820. ;; buffer.
  821. (defun org-list-struct-assoc-at-point ()
  822. "Return the structure association at point.
  823. It is a cons-cell whose key is point and values are indentation,
  824. bullet string and bullet counter, if any."
  825. (save-excursion
  826. (beginning-of-line)
  827. (list (point-at-bol)
  828. (org-get-indentation)
  829. (progn
  830. (looking-at "^[ \t]*\\([-+*0-9.)]+[ \t]+\\)")
  831. (match-string 1))
  832. (progn
  833. (goto-char (match-end 0))
  834. (and (looking-at "\\[@\\(?:start:\\)?\\([0-9]+\\)\\]")
  835. (match-string 1))))))
  836. (defun org-list-struct (begin end &optional outdent)
  837. "Return the structure containing the list between BEGIN and END.
  838. A structure is an alist where key is point of item and values
  839. are, in that order, indentation, bullet string and value of
  840. counter, if any. A structure contains every list and sublist that
  841. has items between BEGIN and END along with their common ancestor.
  842. If no such ancestor can be found, the function will add a virtual
  843. ancestor at position 0.
  844. If OUTDENT is non-nil, it will also grab all of the parent list
  845. and the grand-parent. Setting OUTDENT to t is mandatory when next
  846. change is an outdent."
  847. (save-excursion
  848. (let* ((top (org-list-top-point))
  849. (bottom (org-list-bottom-point))
  850. struct
  851. (extend
  852. (lambda (struct)
  853. (let* ((ind-min (apply 'min (mapcar 'cadr struct)))
  854. (begin (caar struct))
  855. (end (caar (last struct)))
  856. pre-list post-list)
  857. (goto-char begin)
  858. ;; Find beginning of most outdented list (min list)
  859. (while (and (org-search-backward-unenclosed
  860. org-item-beginning-re top t)
  861. (>= (org-get-indentation) ind-min))
  862. (setq pre-list (cons (org-list-struct-assoc-at-point)
  863. pre-list)))
  864. ;; Now get the parent. If none, add a virtual ancestor
  865. (if (< (org-get-indentation) ind-min)
  866. (setq pre-list (cons (org-list-struct-assoc-at-point)
  867. pre-list))
  868. (setq pre-list (cons (list 0 (org-get-indentation) "" nil)
  869. pre-list)))
  870. ;; Find end of min list
  871. (goto-char end)
  872. (end-of-line)
  873. (while (and (org-search-forward-unenclosed
  874. org-item-beginning-re bottom 'move)
  875. (>= (org-get-indentation) ind-min))
  876. (setq post-list (cons (org-list-struct-assoc-at-point)
  877. post-list)))
  878. ;; Is list is malformed? If some items are less
  879. ;; indented that top-item, add them anyhow.
  880. (when (and (= (caar pre-list) 0) (org-at-item-p))
  881. (setq post-list (cons (org-list-struct-assoc-at-point)
  882. post-list))
  883. (while (org-search-forward-unenclosed
  884. org-item-beginning-re bottom t)
  885. (setq post-list (cons (org-list-struct-assoc-at-point)
  886. post-list))))
  887. (append pre-list struct (reverse post-list))))))
  888. ;; Here we start: first get the core zone...
  889. (goto-char end)
  890. (while (org-search-backward-unenclosed org-item-beginning-re begin t)
  891. (setq struct (cons (org-list-struct-assoc-at-point) struct)))
  892. ;; ... then, extend it to make it a structure...
  893. (let ((extended (funcall extend struct)))
  894. ;; ... twice when OUTDENT is non-nil and struct still can be
  895. ;; extended
  896. (if (and outdent (> (caar extended) 0))
  897. (funcall extend extended)
  898. extended)))))
  899. (defun org-list-struct-origins (struct)
  900. "Return an alist where key is item's position and value parent's."
  901. (let* ((struct-rev (reverse struct))
  902. (acc (list (cons (nth 1 (car struct)) 0)))
  903. (prev-item (lambda (item)
  904. (car (nth 1 (member (assq item struct) struct-rev)))))
  905. (get-origins
  906. (lambda (item)
  907. (let* ((item-pos (car item))
  908. (ind (nth 1 item))
  909. (prev-ind (caar acc)))
  910. (cond
  911. ;; List closing.
  912. ((> prev-ind ind)
  913. (let ((current-origin (or (member (assq ind acc) acc)
  914. ;; needed if top-point is
  915. ;; not the most outdented
  916. (last acc))))
  917. (setq acc current-origin)
  918. (cons item-pos (cdar acc))))
  919. ;; New list
  920. ((< prev-ind ind)
  921. (let ((origin (funcall prev-item item-pos)))
  922. (setq acc (cons (cons ind origin) acc))
  923. (cons item-pos origin)))
  924. ;; Current list going on
  925. (t (cons item-pos (cdar acc))))))))
  926. (cons '(0 . 0) (mapcar get-origins (cdr struct)))))
  927. (defun org-list-struct-get-parent (item struct origins)
  928. "Return parent association of ITEM in STRUCT or nil."
  929. (let* ((parent-pos (cdr (assq (car item) origins))))
  930. (when (> parent-pos 0) (assq parent-pos struct))))
  931. (defun org-list-struct-get-child (item struct)
  932. "Return child association of ITEM in STRUCT or nil."
  933. (let ((ind (nth 1 item))
  934. (next-item (cadr (member item struct))))
  935. (when (and next-item (> (nth 1 next-item) ind)) next-item)))
  936. (defun org-list-struct-fix-bul (struct origins)
  937. "Verify and correct bullets for every association in STRUCT.
  938. This function modifies STRUCT."
  939. (let* (acc
  940. (init-bul (lambda (item)
  941. (let ((counter (nth 3 item))
  942. (bullet (org-list-bullet-string (nth 2 item))))
  943. (cond
  944. ((and (string-match "[0-9]+" bullet) counter)
  945. (replace-match counter nil nil bullet))
  946. ((string-match "[0-9]+" bullet)
  947. (replace-match "1" nil nil bullet))
  948. (t bullet)))))
  949. (set-bul (lambda (item bullet)
  950. (setcdr item (list (nth 1 item) bullet (nth 3 item)))))
  951. (get-bul (lambda (item bullet)
  952. (let* ((counter (nth 3 item)))
  953. (if (and counter (string-match "[0-9]+" bullet))
  954. (replace-match counter nil nil bullet)
  955. bullet))))
  956. (fix-bul
  957. (lambda (item) struct
  958. (let* ((parent (cdr (assq (car item) origins)))
  959. (orig-ref (assq parent acc)))
  960. (if orig-ref
  961. ;; Continuing previous list
  962. (let* ((prev-bul (cdr orig-ref))
  963. (new-bul (funcall get-bul item prev-bul)))
  964. (setcdr orig-ref (org-list-inc-bullet-maybe new-bul))
  965. (funcall set-bul item new-bul))
  966. ;; A new list is starting
  967. (let ((new-bul (funcall init-bul item)))
  968. (funcall set-bul item new-bul)
  969. (setq acc (cons (cons parent
  970. (org-list-inc-bullet-maybe new-bul))
  971. acc))))))))
  972. (mapc fix-bul (cdr struct))))
  973. (defun org-list-struct-fix-ind (struct origins)
  974. "Verify and correct indentation for every association in STRUCT.
  975. This function modifies STRUCT."
  976. (let* ((headless (cdr struct))
  977. (ancestor (car struct))
  978. (top-ind (+ (nth 1 ancestor) (length (nth 2 ancestor))))
  979. (new-ind
  980. (lambda (item)
  981. (let* ((parent (org-list-struct-get-parent item headless origins)))
  982. (if parent
  983. ;; Indent like parent + length of parent's bullet
  984. (setcdr item (cons (+ (length (nth 2 parent)) (nth 1 parent))
  985. (cddr item)))
  986. ;; If no parent, indent like top-point
  987. (setcdr item (cons top-ind (cddr item))))))))
  988. (mapc new-ind headless)))
  989. (defun org-list-struct-fix-struct (struct origins)
  990. "Return STRUCT with correct bullets and indentation.
  991. Only elements of STRUCT that have changed are returned."
  992. (let ((old (copy-alist struct)))
  993. (org-list-struct-fix-bul struct origins)
  994. (org-list-struct-fix-ind struct origins)
  995. (delq nil (mapcar (lambda (e) (when (not (equal (pop old) e)) e)) struct))))
  996. (defun org-list-struct-outdent (start end origins)
  997. "Outdent items in ORIGINS between BEGIN and END.
  998. BEGIN is included and END excluded."
  999. (let* (acc
  1000. (out (lambda (cell)
  1001. (let* ((item (car cell))
  1002. (parent (cdr cell)))
  1003. (cond
  1004. ;; Item not yet in zone: keep association
  1005. ((< item start) cell)
  1006. ;; Item out of zone: follow associations in acc
  1007. ((>= item end)
  1008. (let ((convert (assq parent acc)))
  1009. (if convert (cons item (cdr convert)) cell)))
  1010. ;; Item has no parent: error
  1011. ((<= parent 0)
  1012. (error "Cannot outdent top-level items"))
  1013. ;; Parent is outdented: keep association
  1014. ((>= parent start)
  1015. (setq acc (cons (cons parent item) acc)) cell)
  1016. (t
  1017. ;; Parent isn't outdented: reparent to grand-parent
  1018. (let ((grand-parent (cdr (assq parent origins))))
  1019. (setq acc (cons (cons parent item) acc))
  1020. (cons item grand-parent))))))))
  1021. (mapcar out origins)))
  1022. (defun org-list-struct-indent (start end origins struct)
  1023. "Indent items in ORIGINS between BEGIN and END.
  1024. BEGIN is included and END excluded.
  1025. STRUCT may be modified if `org-list-demote-modify-bullet' is
  1026. concerning bullets between START and END."
  1027. (let* (acc
  1028. (orig-rev (reverse origins))
  1029. (get-prev-item
  1030. (lambda (cell parent)
  1031. (car (rassq parent (cdr (memq cell orig-rev))))))
  1032. (set-assoc
  1033. (lambda (cell)
  1034. (setq acc (cons cell acc)) cell))
  1035. (change-bullet-maybe
  1036. (lambda (item)
  1037. (let* ((full-item (assq item struct))
  1038. (item-bul (org-trim (nth 2 full-item)))
  1039. (new-bul-p (cdr (assoc item-bul org-list-demote-modify-bullet))))
  1040. (when new-bul-p
  1041. ;; new bullet is stored without space to ensure item
  1042. ;; will be modified
  1043. (setcdr full-item
  1044. (list (nth 1 full-item)
  1045. new-bul-p
  1046. (nth 3 full-item)))))))
  1047. (ind
  1048. (lambda (cell)
  1049. (let* ((item (car cell))
  1050. (parent (cdr cell)))
  1051. (cond
  1052. ;; Item not yet in zone: keep association
  1053. ((< item start) cell)
  1054. ((>= item end)
  1055. ;; Item out of zone: follow associations in acc
  1056. (let ((convert (assq parent acc)))
  1057. (if convert (cons item (cdr convert)) cell)))
  1058. (t
  1059. ;; Item is in zone...
  1060. (let ((prev (funcall get-prev-item cell parent)))
  1061. ;; Check if bullet needs to be changed
  1062. (funcall change-bullet-maybe item)
  1063. (cond
  1064. ;; First item indented but not parent: error
  1065. ((and (or (not prev) (= prev 0)) (< parent start))
  1066. (error "Cannot indent the first item of a list"))
  1067. ;; First item and parent indented: keep same parent
  1068. ((or (not prev) (= prev 0))
  1069. (funcall set-assoc cell))
  1070. ;; Previous item not indented: reparent to it
  1071. ((< prev start)
  1072. (funcall set-assoc (cons item prev)))
  1073. ;; Previous item indented: reparent like it
  1074. (t
  1075. (funcall set-assoc (cons item
  1076. (cdr (assq prev acc)))))))))))))
  1077. (mapcar ind origins)))
  1078. (defun org-list-struct-apply-struct (struct)
  1079. "Apply modifications to list so it mirrors STRUCT.
  1080. Initial position is restored after the changes."
  1081. (let* ((pos (copy-marker (point)))
  1082. (ancestor (caar struct))
  1083. (modify
  1084. (lambda (item)
  1085. (goto-char (car item))
  1086. (let* ((new-ind (nth 1 item))
  1087. (new-bul (org-list-bullet-string (nth 2 item)))
  1088. (old-ind (org-get-indentation))
  1089. (old-bul (progn
  1090. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  1091. (match-string 1)))
  1092. (old-body-ind (+ (length old-bul) old-ind))
  1093. (new-body-ind (+ (length new-bul) new-ind)))
  1094. ;; 1. Shift item's body
  1095. (unless (= old-body-ind new-body-ind)
  1096. (org-shift-item-indentation (- new-body-ind old-body-ind)))
  1097. ;; 2. Replace bullet
  1098. (unless (equal new-bul old-bul)
  1099. (save-excursion
  1100. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  1101. (replace-match new-bul nil nil nil 1)))
  1102. ;; 3. Indent item to appropriate column
  1103. (unless (= new-ind old-ind)
  1104. (delete-region (point-at-bol)
  1105. (progn
  1106. (skip-chars-forward " \t")
  1107. (point)))
  1108. (indent-to new-ind)))))
  1109. ;; Remove ancestor if it is left.
  1110. (struct-to-apply (if (or (not ancestor) (= 0 ancestor))
  1111. (cdr struct)
  1112. struct)))
  1113. ;; Apply changes from bottom to top
  1114. (mapc modify (nreverse struct-to-apply))
  1115. (goto-char pos)))
  1116. ;;; Indentation
  1117. (defun org-get-string-indentation (s)
  1118. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  1119. (let ((n -1) (i 0) (w tab-width) c)
  1120. (catch 'exit
  1121. (while (< (setq n (1+ n)) (length s))
  1122. (setq c (aref s n))
  1123. (cond ((= c ?\ ) (setq i (1+ i)))
  1124. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  1125. (t (throw 'exit t)))))
  1126. i))
  1127. (defun org-shift-item-indentation (delta)
  1128. "Shift the indentation in current item by DELTA.
  1129. Sub-items are not moved."
  1130. (save-excursion
  1131. (let ((beg (point-at-bol))
  1132. (end (org-end-of-item-or-at-child)))
  1133. (beginning-of-line (unless (eolp) 0))
  1134. (while (> (point) beg)
  1135. (when (looking-at "[ \t]*\\S-")
  1136. ;; this is not an empty line
  1137. (let ((i (org-get-indentation)))
  1138. (when (and (> i 0) (> (+ i delta) 0))
  1139. (indent-line-to (+ i delta)))))
  1140. (beginning-of-line 0)))))
  1141. (defun org-outdent-item (arg)
  1142. "Outdent a local list item, but not its children."
  1143. (interactive "p")
  1144. (org-indent-item-tree (- arg) t))
  1145. (defun org-indent-item (arg)
  1146. "Indent a local list item, but not its children."
  1147. (interactive "p")
  1148. (org-indent-item-tree arg t))
  1149. (defun org-outdent-item-tree (arg &optional no-subtree)
  1150. "Outdent a local list item including its children.
  1151. If NO-SUBTREE is set, only outdent the item itself, not its children."
  1152. (interactive "p")
  1153. (org-indent-item-tree (- arg) no-subtree))
  1154. (defvar org-last-indent-begin-marker (make-marker))
  1155. (defvar org-last-indent-end-marker (make-marker))
  1156. (defun org-indent-item-tree (arg &optional no-subtree)
  1157. "Indent a local list item including its children.
  1158. If NO-SUBTREE is set, only indent the item itself, not its
  1159. children. Return t if successful."
  1160. (interactive "p")
  1161. (unless (org-at-item-p)
  1162. (error "Not on an item"))
  1163. ;; Determine begin and end points of zone to indent. If moving by
  1164. ;; subtrees, ensure we don't drag additional items on subsequent
  1165. ;; moves.
  1166. (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  1167. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  1168. (if (org-region-active-p)
  1169. (progn
  1170. (set-marker org-last-indent-begin-marker (region-beginning))
  1171. (set-marker org-last-indent-end-marker (region-end)))
  1172. (set-marker org-last-indent-begin-marker
  1173. (save-excursion (org-beginning-of-item)))
  1174. (set-marker org-last-indent-end-marker
  1175. (save-excursion
  1176. (if no-subtree
  1177. (org-end-of-item-or-at-child)
  1178. (org-end-of-item))))))
  1179. ;; Get everything ready
  1180. (let* ((beg (marker-position org-last-indent-begin-marker))
  1181. (end (marker-position org-last-indent-end-marker))
  1182. (struct (org-list-struct beg end (< arg 0)))
  1183. (origins (org-list-struct-origins struct))
  1184. (beg-item (assq beg struct))
  1185. (end-item (save-excursion
  1186. (goto-char end)
  1187. (skip-chars-backward " \r\t\n")
  1188. (org-beginning-of-item)
  1189. (org-list-struct-assoc-at-point)))
  1190. (top (org-list-top-point)))
  1191. (cond
  1192. ;; Special case: moving top-item with indent rule
  1193. ((and (= top beg)
  1194. (cdr (assq 'indent org-list-automatic-rules))
  1195. (not no-subtree))
  1196. (let* ((level-skip (org-level-increment))
  1197. (offset (if (< arg 0) (- level-skip) level-skip))
  1198. (top-ind (nth 1 beg-item)))
  1199. (if (< (+ top-ind offset) 0)
  1200. (error "Cannot outdent beyond margin")
  1201. ;; Change bullet if necessary
  1202. (when (and (= (+ top-ind offset) 0)
  1203. (string-match "*" (nth 2 beg-item)))
  1204. (setcdr beg-item (list (nth 1 beg-item)
  1205. (org-list-bullet-string "-"))))
  1206. ;; Shift ancestor
  1207. (let ((anc (car struct)))
  1208. (setcdr anc (list (+ (nth 1 anc) offset) "" nil)))
  1209. (org-list-struct-fix-struct struct origins)
  1210. (org-list-struct-apply-struct struct))))
  1211. ;; Forbidden move
  1212. ((and (< arg 0)
  1213. (or (and no-subtree
  1214. (not (org-region-active-p))
  1215. (org-list-struct-get-child beg-item struct))
  1216. (org-list-struct-get-child end-item struct)))
  1217. (error "Cannot outdent an item without its children"))
  1218. ;; Normal shifting
  1219. (t
  1220. (let* ((shifted-ori (if (< arg 0)
  1221. (org-list-struct-outdent beg end origins)
  1222. (org-list-struct-indent beg end origins struct))))
  1223. (org-list-struct-fix-struct struct shifted-ori)
  1224. (org-list-struct-apply-struct struct)))))
  1225. ;; Return value
  1226. t)
  1227. (defvar org-tab-ind-state)
  1228. (defun org-cycle-item-indentation ()
  1229. (let ((org-adapt-indentation nil))
  1230. (when (and (or (org-at-item-description-p)
  1231. (org-at-item-checkbox-p)
  1232. (org-at-item-p))
  1233. (>= (match-end 0) (save-excursion
  1234. (org-end-of-item-or-at-child)
  1235. (skip-chars-backward " \r\t\n")
  1236. (point))))
  1237. (setq this-command 'org-cycle-item-indentation)
  1238. ;; When in the middle of the cycle, try to outdent first. If it
  1239. ;; fails, and point is still at initial position, indent. Else,
  1240. ;; go back to original position.
  1241. (if (eq last-command 'org-cycle-item-indentation)
  1242. (cond
  1243. ((ignore-errors (org-indent-item -1)))
  1244. ((and (= (org-get-indentation) (car org-tab-ind-state))
  1245. (ignore-errors (org-indent-item 1))))
  1246. (t (back-to-indentation)
  1247. (indent-to-column (car org-tab-ind-state))
  1248. (end-of-line)
  1249. (org-list-repair (cdr org-tab-ind-state))
  1250. ;; Break cycle
  1251. (setq this-command 'identity)))
  1252. ;; If a cycle is starting, remember indentation and bullet,
  1253. ;; then try to indent. If it fails, try to outdent.
  1254. (setq org-tab-ind-state
  1255. (cons (org-get-indentation) (org-get-bullet)))
  1256. (cond
  1257. ((ignore-errors (org-indent-item 1)))
  1258. ((ignore-errors (org-indent-item -1)))
  1259. (t (error "Cannot move item"))))
  1260. t)))
  1261. ;;; Bullets
  1262. (defun org-get-bullet ()
  1263. "Return the bullet of the item at point.
  1264. Assume cursor is at an item."
  1265. (and (looking-at "[ \t]*\\(\\S-+\\)") (match-string 1)))
  1266. (defun org-list-bullet-string (bullet)
  1267. "Return BULLET with the correct number of whitespaces.
  1268. It determines the number of whitespaces to append by looking at
  1269. `org-list-two-spaces-after-bullet-regexp'."
  1270. (save-match-data
  1271. (string-match "\\S-+\\([ \t]*\\)" bullet)
  1272. (replace-match
  1273. (save-match-data
  1274. (concat
  1275. " "
  1276. ;; Do we need to concat another white space ?
  1277. (when (and org-list-two-spaces-after-bullet-regexp
  1278. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  1279. " ")))
  1280. nil nil bullet 1)))
  1281. (defun org-list-inc-bullet-maybe (bullet)
  1282. "Increment numbered bullets."
  1283. (if (string-match "[0-9]+" bullet)
  1284. (replace-match
  1285. (number-to-string (1+ (string-to-number (match-string 0 bullet))))
  1286. nil nil bullet)
  1287. bullet))
  1288. (defun org-list-repair (&optional force-bullet)
  1289. "Make sure all items are correctly indented, with the right bullet.
  1290. This function scans the list at point, along with any sublist.
  1291. If FORCE-BULLET is a string, ensure all items in list share this
  1292. bullet, or a logical successor in the case of an ordered list.
  1293. Item's body is not indented, only shifted with the bullet."
  1294. (interactive)
  1295. (unless (org-at-item-p) (error "This is not a list"))
  1296. (let* ((struct (org-list-struct (point-at-bol) (point-at-eol)))
  1297. (origins (org-list-struct-origins struct))
  1298. fixed-struct)
  1299. (if (stringp force-bullet)
  1300. (let ((begin (nth 1 struct)))
  1301. (setcdr begin (list (nth 1 begin)
  1302. (org-list-bullet-string force-bullet)
  1303. (nth 3 begin)))
  1304. (setq fixed-struct
  1305. (cons begin (org-list-struct-fix-struct struct origins))))
  1306. (setq fixed-struct (org-list-struct-fix-struct struct origins)))
  1307. (org-list-struct-apply-struct fixed-struct)))
  1308. (defun org-cycle-list-bullet (&optional which)
  1309. "Cycle through the different itemize/enumerate bullets.
  1310. This cycle the entire list level through the sequence:
  1311. `-' -> `+' -> `*' -> `1.' -> `1)'
  1312. If WHICH is a valid string, use that as the new bullet. If WHICH
  1313. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  1314. 'previous, cycle backwards."
  1315. (interactive "P")
  1316. (org-preserve-lc
  1317. (let* ((bullet (progn (org-beginning-of-item-list)
  1318. (org-get-bullet)))
  1319. (current (cond
  1320. ((string-match "\\." bullet) "1.")
  1321. ((string-match ")" bullet) "1)")
  1322. (t bullet)))
  1323. (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
  1324. (bullet-list (append '("-" "+" )
  1325. ;; *-bullets are not allowed at column 0
  1326. (unless (and bullet-rule-p
  1327. (looking-at "\\S-")) '("*"))
  1328. ;; Description items cannot be numbered
  1329. (unless (and bullet-rule-p
  1330. (or (eq org-plain-list-ordered-item-terminator ?.)
  1331. (org-at-item-description-p))) '("1)"))
  1332. (unless (and bullet-rule-p
  1333. (or (eq org-plain-list-ordered-item-terminator ?\))
  1334. (org-at-item-description-p))) '("1."))))
  1335. (len (length bullet-list))
  1336. (item-index (- len (length (member current bullet-list))))
  1337. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  1338. (new (cond
  1339. ((member which bullet-list) which)
  1340. ((numberp which) (funcall get-value which))
  1341. ((eq 'previous which) (funcall get-value (1- item-index)))
  1342. (t (funcall get-value (1+ item-index))))))
  1343. (org-list-repair new))))
  1344. ;;; Checkboxes
  1345. (defun org-toggle-checkbox (&optional toggle-presence)
  1346. "Toggle the checkbox in the current line.
  1347. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  1348. double prefix, set checkbox to [-].
  1349. When there is an active region, toggle status or presence of the
  1350. checkbox in the first line, and make every item in the region
  1351. have the same status or presence, respectively.
  1352. If the cursor is in a headline, apply this to all checkbox items
  1353. in the text below the heading, taking as reference the first item
  1354. in subtree."
  1355. (interactive "P")
  1356. ;; Bounds is a list of type (beg end single-p) where single-p is t
  1357. ;; when `org-toggle-checkbox' is applied to a single item. Only
  1358. ;; toggles on single items will return errors.
  1359. (let* ((bounds
  1360. (cond
  1361. ((org-region-active-p)
  1362. (list (region-beginning) (region-end) nil))
  1363. ((org-on-heading-p)
  1364. ;; In this case, reference line is the first item in subtree
  1365. (let ((limit (save-excursion (outline-next-heading) (point))))
  1366. (save-excursion
  1367. (org-search-forward-unenclosed
  1368. org-item-beginning-re limit 'move)
  1369. (list (point) limit nil))))
  1370. ((org-at-item-p)
  1371. (list (point-at-bol) (point-at-eol) t))
  1372. (t (error "Not at an item or heading, and no active region"))))
  1373. ;; marker is needed because deleting checkboxes will change END
  1374. (end (copy-marker (nth 1 bounds)))
  1375. (single-p (nth 2 bounds))
  1376. (ref-presence (save-excursion
  1377. (goto-char (car bounds))
  1378. (org-at-item-checkbox-p)))
  1379. (ref-status (equal (match-string 1) "[X]"))
  1380. (act-on-item
  1381. (lambda (ref-pres ref-stat)
  1382. (if (equal toggle-presence '(4))
  1383. (cond
  1384. ((and ref-pres (org-at-item-checkbox-p))
  1385. (replace-match ""))
  1386. ((and (not ref-pres)
  1387. (not (org-at-item-checkbox-p))
  1388. (org-at-item-p))
  1389. (goto-char (match-end 0))
  1390. ;; Ignore counter, if any
  1391. (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
  1392. (goto-char (match-end 0)))
  1393. (let ((desc-p (and (org-at-item-description-p)
  1394. (cdr (assq 'checkbox org-list-automatic-rules)))))
  1395. (cond
  1396. ((and single-p desc-p)
  1397. (error "Cannot add a checkbox in a description list"))
  1398. ((not desc-p) (insert "[ ] "))))))
  1399. (let ((blocked (org-checkbox-blocked-p)))
  1400. (cond
  1401. ((and blocked single-p)
  1402. (error "Checkbox blocked because of unchecked box in line %d" blocked))
  1403. (blocked nil)
  1404. ((org-at-item-checkbox-p)
  1405. (replace-match
  1406. (cond ((equal toggle-presence '(16)) "[-]")
  1407. (ref-stat "[ ]")
  1408. (t "[X]"))
  1409. t t nil 1))))))))
  1410. (save-excursion
  1411. (beginning-of-line)
  1412. (while (< (point) end)
  1413. (funcall act-on-item ref-presence ref-status)
  1414. (org-search-forward-unenclosed org-item-beginning-re end 'move)))
  1415. (org-update-checkbox-count-maybe)))
  1416. (defun org-reset-checkbox-state-subtree ()
  1417. "Reset all checkboxes in an entry subtree."
  1418. (interactive "*")
  1419. (save-restriction
  1420. (save-excursion
  1421. (org-narrow-to-subtree)
  1422. (org-show-subtree)
  1423. (goto-char (point-min))
  1424. (let ((end (point-max)))
  1425. (while (< (point) end)
  1426. (when (org-at-item-checkbox-p)
  1427. (replace-match "[ ]" t t nil 1))
  1428. (beginning-of-line 2))))
  1429. (org-update-checkbox-count-maybe)))
  1430. (defvar org-checkbox-statistics-hook nil
  1431. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  1432. This hook runs even if checkbox rule in
  1433. `org-list-automatic-rules' does not apply, so it can be used to
  1434. implement alternative ways of collecting statistics
  1435. information.")
  1436. (defun org-update-checkbox-count-maybe ()
  1437. "Update checkbox statistics unless turned off by user."
  1438. (when (cdr (assq 'checkbox org-list-automatic-rules))
  1439. (org-update-checkbox-count))
  1440. (run-hooks 'org-checkbox-statistics-hook))
  1441. (defun org-update-checkbox-count (&optional all)
  1442. "Update the checkbox statistics in the current section.
  1443. This will find all statistic cookies like [57%] and [6/12] and update them
  1444. with the current numbers. With optional prefix argument ALL, do this for
  1445. the whole buffer."
  1446. (interactive "P")
  1447. (save-excursion
  1448. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  1449. (beg (condition-case nil
  1450. (progn (org-back-to-heading) (point))
  1451. (error (point-min))))
  1452. (end (move-marker (make-marker)
  1453. (progn (outline-next-heading) (point))))
  1454. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  1455. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  1456. (re-find (concat re "\\|" re-box))
  1457. beg-cookie end-cookie is-percent c-on c-off lim new
  1458. eline curr-ind next-ind continue-from startsearch
  1459. (recursive
  1460. (or (not org-hierarchical-checkbox-statistics)
  1461. (string-match "\\<recursive\\>"
  1462. (or (ignore-errors
  1463. (org-entry-get nil "COOKIE_DATA"))
  1464. ""))))
  1465. (cstat 0))
  1466. (when all
  1467. (goto-char (point-min))
  1468. (outline-next-heading)
  1469. (setq beg (point) end (point-max)))
  1470. (goto-char end)
  1471. ;; find each statistics cookie
  1472. (while (and (org-search-backward-unenclosed re-find beg t)
  1473. (not (save-match-data
  1474. (and (org-on-heading-p)
  1475. (string-match "\\<todo\\>"
  1476. (downcase
  1477. (or (org-entry-get
  1478. nil "COOKIE_DATA")
  1479. "")))))))
  1480. (setq beg-cookie (match-beginning 1)
  1481. end-cookie (match-end 1)
  1482. cstat (+ cstat (if end-cookie 1 0))
  1483. startsearch (point-at-eol)
  1484. continue-from (match-beginning 0)
  1485. is-percent (match-beginning 2)
  1486. lim (cond
  1487. ((org-on-heading-p) (outline-next-heading) (point))
  1488. ((org-at-item-p) (org-end-of-item) (point))
  1489. (t nil))
  1490. c-on 0
  1491. c-off 0)
  1492. (when lim
  1493. ;; find first checkbox for this cookie and gather
  1494. ;; statistics from all that are at this indentation level
  1495. (goto-char startsearch)
  1496. (if (org-search-forward-unenclosed re-box lim t)
  1497. (progn
  1498. (org-beginning-of-item)
  1499. (setq curr-ind (org-get-indentation))
  1500. (setq next-ind curr-ind)
  1501. (while (and (bolp) (org-at-item-p)
  1502. (if recursive
  1503. (<= curr-ind next-ind)
  1504. (= curr-ind next-ind)))
  1505. (setq eline (point-at-eol))
  1506. (if (org-search-forward-unenclosed re-box eline t)
  1507. (if (member (match-string 2) '("[ ]" "[-]"))
  1508. (setq c-off (1+ c-off))
  1509. (setq c-on (1+ c-on))))
  1510. (if (not recursive)
  1511. ;; org-get-next-item goes through list-enders
  1512. ;; with proper limit.
  1513. (goto-char (or (org-get-next-item (point) lim) lim))
  1514. (end-of-line)
  1515. (when (org-search-forward-unenclosed
  1516. org-item-beginning-re lim t)
  1517. (beginning-of-line)))
  1518. (setq next-ind (org-get-indentation)))))
  1519. (goto-char continue-from)
  1520. ;; update cookie
  1521. (when end-cookie
  1522. (setq new (if is-percent
  1523. (format "[%d%%]" (/ (* 100 c-on)
  1524. (max 1 (+ c-on c-off))))
  1525. (format "[%d/%d]" c-on (+ c-on c-off))))
  1526. (goto-char beg-cookie)
  1527. (insert new)
  1528. (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
  1529. ;; update items checkbox if it has one
  1530. (when (org-at-item-p)
  1531. (org-beginning-of-item)
  1532. (when (and (> (+ c-on c-off) 0)
  1533. (org-search-forward-unenclosed re-box (point-at-eol) t))
  1534. (setq beg-cookie (match-beginning 2)
  1535. end-cookie (match-end 2))
  1536. (delete-region beg-cookie end-cookie)
  1537. (goto-char beg-cookie)
  1538. (cond ((= c-off 0) (insert "[X]"))
  1539. ((= c-on 0) (insert "[ ]"))
  1540. (t (insert "[-]")))
  1541. )))
  1542. (goto-char continue-from))
  1543. (when (interactive-p)
  1544. (message "Checkbox statistics updated %s (%d places)"
  1545. (if all "in entire file" "in current outline entry") cstat)))))
  1546. (defun org-get-checkbox-statistics-face ()
  1547. "Select the face for checkbox statistics.
  1548. The face will be `org-done' when all relevant boxes are checked.
  1549. Otherwise it will be `org-todo'."
  1550. (if (match-end 1)
  1551. (if (equal (match-string 1) "100%")
  1552. 'org-checkbox-statistics-done
  1553. 'org-checkbox-statistics-todo)
  1554. (if (and (> (match-end 2) (match-beginning 2))
  1555. (equal (match-string 2) (match-string 3)))
  1556. 'org-checkbox-statistics-done
  1557. 'org-checkbox-statistics-todo)))
  1558. ;;; Misc Tools
  1559. (defun org-apply-on-list (function init-value &rest args)
  1560. "Call FUNCTION for each item of a the list under point.
  1561. FUNCTION must be called with at least one argument: a return
  1562. value that will contain the value returned by the function at the
  1563. previous item, plus ARGS extra arguments. INIT-VALUE will be the
  1564. value passed to the function at the first item of the list.
  1565. As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
  1566. will return the number of items in the current list.
  1567. Sublists of the list are skipped. Cursor is always at the
  1568. beginning of the item."
  1569. (let* ((pos (copy-marker (point)))
  1570. (end (copy-marker (org-list-bottom-point)))
  1571. (next-p (copy-marker (save-excursion (org-beginning-of-item-list))))
  1572. (value init-value))
  1573. (while (< next-p end)
  1574. (goto-char next-p)
  1575. (set-marker next-p (or (org-get-next-item (point) end) end))
  1576. (setq value (apply function value args)))
  1577. (goto-char pos)
  1578. value))
  1579. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  1580. "Sort plain list items.
  1581. The cursor may be at any item of the list that should be sorted.
  1582. Sublists are not sorted. Checkboxes, if any, are ignored.
  1583. Sorting can be alphabetically, numerically, by date/time as given by
  1584. a time stamp, by a property or by priority.
  1585. The command prompts for the sorting type unless it has been given
  1586. to the function through the SORTING-TYPE argument, which needs to
  1587. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  1588. meaning of each character:
  1589. n Numerically, by converting the beginning of the item to a number.
  1590. a Alphabetically. Only the first line of item is checked.
  1591. t By date/time, either the first active time stamp in the entry, if
  1592. any, or by the first inactive one. In a timer list, sort the timers.
  1593. Capital letters will reverse the sort order.
  1594. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
  1595. function to be called with point at the beginning of the record.
  1596. It must return either a string or a number that should serve as
  1597. the sorting key for that record.
  1598. Comparing entries ignores case by default. However, with an
  1599. optional argument WITH-CASE, the sorting considers case as well."
  1600. (interactive "P")
  1601. (let* ((case-func (if with-case 'identity 'downcase))
  1602. (start (org-beginning-of-item-list))
  1603. (end (save-excursion (org-end-of-item-list)))
  1604. (sorting-type
  1605. (progn
  1606. (message
  1607. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  1608. (read-char-exclusive)))
  1609. (getkey-func (and (= (downcase sorting-type) ?f)
  1610. (org-icompleting-read "Sort using function: "
  1611. obarray 'fboundp t nil nil)
  1612. (intern getkey-func))))
  1613. (message "Sorting items...")
  1614. (save-restriction
  1615. (narrow-to-region start end)
  1616. (let* ((dcst (downcase sorting-type))
  1617. (case-fold-search nil)
  1618. (now (current-time))
  1619. (sort-func (cond
  1620. ((= dcst ?a) 'string<)
  1621. ((= dcst ?f) compare-func)
  1622. ((= dcst ?t) '<)
  1623. (t nil)))
  1624. (begin-record (lambda ()
  1625. (skip-chars-forward " \r\t\n")
  1626. (beginning-of-line)))
  1627. (end-record (lambda ()
  1628. (goto-char (org-end-of-item-before-blank))))
  1629. (value-to-sort
  1630. (lambda ()
  1631. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  1632. (cond
  1633. ((= dcst ?n)
  1634. (string-to-number (buffer-substring (match-end 0)
  1635. (point-at-eol))))
  1636. ((= dcst ?a)
  1637. (buffer-substring (match-end 0) (point-at-eol)))
  1638. ((= dcst ?t)
  1639. (cond
  1640. ;; If it is a timer list, convert timer to seconds
  1641. ((org-at-item-timer-p)
  1642. (org-timer-hms-to-secs (match-string 1)))
  1643. ((or (org-search-forward-unenclosed org-ts-regexp
  1644. (point-at-eol) t)
  1645. (org-search-forward-unenclosed org-ts-regexp-both
  1646. (point-at-eol) t))
  1647. (org-time-string-to-seconds (match-string 0)))
  1648. (t (org-float-time now))))
  1649. ((= dcst ?f)
  1650. (if getkey-func
  1651. (let ((value (funcall getkey-func)))
  1652. (if (stringp value)
  1653. (funcall case-func value)
  1654. value))
  1655. (error "Invalid key function `%s'" getkey-func)))
  1656. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  1657. (sort-subr (/= dcst sorting-type)
  1658. begin-record
  1659. end-record
  1660. value-to-sort
  1661. nil
  1662. sort-func)
  1663. (org-list-repair)
  1664. (run-hooks 'org-after-sorting-entries-or-items-hook)
  1665. (message "Sorting items...done")))))
  1666. ;;; Send and receive lists
  1667. (defun org-list-parse-list (&optional delete)
  1668. "Parse the list at point and maybe DELETE it.
  1669. Return a list containing first level items as strings and
  1670. sublevels as a list of strings."
  1671. (let* ((start (goto-char (org-list-top-point)))
  1672. (end (org-list-bottom-point))
  1673. output itemsep ltype)
  1674. (while (org-search-forward-unenclosed org-item-beginning-re end t)
  1675. (save-excursion
  1676. (beginning-of-line)
  1677. (setq ltype (cond ((looking-at-p "^[ \t]*[0-9]") 'ordered)
  1678. ((org-at-item-description-p) 'descriptive)
  1679. (t 'unordered))))
  1680. (let* ((indent1 (org-get-indentation))
  1681. (nextitem (or (org-get-next-item (point) end) end))
  1682. (item (org-trim (buffer-substring (point)
  1683. (org-end-of-item-or-at-child))))
  1684. (nextindent (if (= (point) end) 0 (org-get-indentation)))
  1685. (item (if (string-match
  1686. "^\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[\\([xX ]\\)\\]"
  1687. item)
  1688. (replace-match (if (equal (match-string 1 item) " ")
  1689. "CBOFF"
  1690. "CBON")
  1691. t nil item 1)
  1692. item)))
  1693. (push item output)
  1694. (when (> nextindent indent1)
  1695. (save-restriction
  1696. (narrow-to-region (point) nextitem)
  1697. (push (org-list-parse-list) output)))))
  1698. (when delete
  1699. (delete-region start end)
  1700. (save-match-data
  1701. (when (and (not (eq org-list-ending-method 'indent))
  1702. (looking-at (org-list-end-re)))
  1703. (replace-match "\n"))))
  1704. (setq output (nreverse output))
  1705. (push ltype output)))
  1706. (defun org-list-make-subtree ()
  1707. "Convert the plain list at point into a subtree."
  1708. (interactive)
  1709. (if (not (org-in-item-p))
  1710. (error "Not in a list.")
  1711. (goto-char (org-list-top-point))
  1712. (let ((list (org-list-parse-list t)) nstars)
  1713. (save-excursion
  1714. (if (ignore-errors
  1715. (org-back-to-heading))
  1716. (progn (org-search-forward-unenclosed
  1717. org-complex-heading-regexp nil t)
  1718. (setq nstars (length (match-string 1))))
  1719. (setq nstars 0)))
  1720. (org-list-make-subtrees list (1+ nstars)))))
  1721. (defun org-list-make-subtrees (list level)
  1722. "Convert LIST into subtrees starting at LEVEL."
  1723. (if (symbolp (car list))
  1724. (org-list-make-subtrees (cdr list) level)
  1725. (mapcar (lambda (item)
  1726. (if (stringp item)
  1727. (insert (make-string
  1728. (if org-odd-levels-only
  1729. (1- (* 2 level)) level) ?*) " " item "\n")
  1730. (org-list-make-subtrees item (1+ level))))
  1731. list)))
  1732. (defun org-list-insert-radio-list ()
  1733. "Insert a radio list template appropriate for this major mode."
  1734. (interactive)
  1735. (let* ((e (assq major-mode org-list-radio-list-templates))
  1736. (txt (nth 1 e))
  1737. name pos)
  1738. (unless e (error "No radio list setup defined for %s" major-mode))
  1739. (setq name (read-string "List name: "))
  1740. (while (string-match "%n" txt)
  1741. (setq txt (replace-match name t t txt)))
  1742. (or (bolp) (insert "\n"))
  1743. (setq pos (point))
  1744. (insert txt)
  1745. (goto-char pos)))
  1746. (defun org-list-send-list (&optional maybe)
  1747. "Send a transformed version of this list to the receiver position.
  1748. With argument MAYBE, fail quietly if no transformation is defined for
  1749. this list."
  1750. (interactive)
  1751. (catch 'exit
  1752. (unless (org-at-item-p) (error "Not at a list"))
  1753. (save-excursion
  1754. (re-search-backward "#\\+ORGLST" nil t)
  1755. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  1756. (if maybe
  1757. (throw 'exit nil)
  1758. (error "Don't know how to transform this list"))))
  1759. (let* ((name (match-string 1))
  1760. (transform (intern (match-string 2)))
  1761. (bottom-point
  1762. (save-excursion
  1763. (re-search-forward
  1764. "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  1765. (match-beginning 0)))
  1766. (top-point
  1767. (progn
  1768. (re-search-backward "#\\+ORGLST" nil t)
  1769. (re-search-forward org-item-beginning-re bottom-point t)
  1770. (match-beginning 0)))
  1771. (list (save-restriction
  1772. (narrow-to-region top-point bottom-point)
  1773. (org-list-parse-list)))
  1774. beg txt)
  1775. (unless (fboundp transform)
  1776. (error "No such transformation function %s" transform))
  1777. (let ((txt (funcall transform list)))
  1778. ;; Find the insertion place
  1779. (save-excursion
  1780. (goto-char (point-min))
  1781. (unless (re-search-forward
  1782. (concat "BEGIN RECEIVE ORGLST +"
  1783. name
  1784. "\\([ \t]\\|$\\)") nil t)
  1785. (error "Don't know where to insert translated list"))
  1786. (goto-char (match-beginning 0))
  1787. (beginning-of-line 2)
  1788. (setq beg (point))
  1789. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1790. (error "Cannot find end of insertion region"))
  1791. (delete-region beg (point-at-bol))
  1792. (goto-char beg)
  1793. (insert txt "\n")))
  1794. (message "List converted and installed at receiver location"))))
  1795. (defun org-list-to-generic (list params)
  1796. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1797. Valid parameters PARAMS are
  1798. :ustart String to start an unordered list
  1799. :uend String to end an unordered list
  1800. :ostart String to start an ordered list
  1801. :oend String to end an ordered list
  1802. :dstart String to start a descriptive list
  1803. :dend String to end a descriptive list
  1804. :dtstart String to start a descriptive term
  1805. :dtend String to end a descriptive term
  1806. :ddstart String to start a description
  1807. :ddend String to end a description
  1808. :splice When set to t, return only list body lines, don't wrap
  1809. them into :[u/o]start and :[u/o]end. Default is nil.
  1810. :istart String to start a list item
  1811. :iend String to end a list item
  1812. :isep String to separate items
  1813. :lsep String to separate sublists
  1814. :cboff String to insert for an unchecked checkbox
  1815. :cbon String to insert for a checked checkbox"
  1816. (interactive)
  1817. (let* ((p params) sublist
  1818. (splicep (plist-get p :splice))
  1819. (ostart (plist-get p :ostart))
  1820. (oend (plist-get p :oend))
  1821. (ustart (plist-get p :ustart))
  1822. (uend (plist-get p :uend))
  1823. (dstart (plist-get p :dstart))
  1824. (dend (plist-get p :dend))
  1825. (dtstart (plist-get p :dtstart))
  1826. (dtend (plist-get p :dtend))
  1827. (ddstart (plist-get p :ddstart))
  1828. (ddend (plist-get p :ddend))
  1829. (istart (plist-get p :istart))
  1830. (iend (plist-get p :iend))
  1831. (isep (plist-get p :isep))
  1832. (lsep (plist-get p :lsep))
  1833. (cbon (plist-get p :cbon))
  1834. (cboff (plist-get p :cboff)))
  1835. (let ((wrapper
  1836. (cond ((eq (car list) 'ordered)
  1837. (concat ostart "\n%s" oend "\n"))
  1838. ((eq (car list) 'unordered)
  1839. (concat ustart "\n%s" uend "\n"))
  1840. ((eq (car list) 'descriptive)
  1841. (concat dstart "\n%s" dend "\n"))))
  1842. rtn term defstart defend)
  1843. (while (setq sublist (pop list))
  1844. (cond ((symbolp sublist) nil)
  1845. ((stringp sublist)
  1846. (when (string-match "^\\(\\S-+\\)[ \t]+::" sublist)
  1847. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1848. (match-string 1 sublist))))
  1849. (setq sublist (concat ddstart
  1850. (org-trim (substring sublist
  1851. (match-end 0)))
  1852. ddend)))
  1853. (if (string-match "\\[CBON\\]" sublist)
  1854. (setq sublist (replace-match cbon t t sublist)))
  1855. (if (string-match "\\[CBOFF\\]" sublist)
  1856. (setq sublist (replace-match cboff t t sublist)))
  1857. (if (string-match "\\[-\\]" sublist)
  1858. (setq sublist (replace-match "$\\boxminus$" t t sublist)))
  1859. (setq rtn (concat rtn istart term sublist iend isep)))
  1860. (t (setq rtn (concat rtn ;; previous list
  1861. lsep ;; list separator
  1862. (org-list-to-generic sublist p)
  1863. lsep ;; list separator
  1864. )))))
  1865. (format wrapper rtn))))
  1866. (defun org-list-to-latex (list &optional params)
  1867. "Convert LIST into a LaTeX list.
  1868. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1869. with overruling parameters for `org-list-to-generic'."
  1870. (org-list-to-generic
  1871. list
  1872. (org-combine-plists
  1873. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1874. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1875. :dstart "\\begin{description}" :dend "\\end{description}"
  1876. :dtstart "[" :dtend "]"
  1877. :ddstart "" :ddend ""
  1878. :istart "\\item " :iend ""
  1879. :isep "\n" :lsep "\n"
  1880. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1881. params)))
  1882. (defun org-list-to-html (list &optional params)
  1883. "Convert LIST into a HTML list.
  1884. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1885. with overruling parameters for `org-list-to-generic'."
  1886. (org-list-to-generic
  1887. list
  1888. (org-combine-plists
  1889. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1890. :ustart "<ul>" :uend "</ul>"
  1891. :dstart "<dl>" :dend "</dl>"
  1892. :dtstart "<dt>" :dtend "</dt>"
  1893. :ddstart "<dd>" :ddend "</dd>"
  1894. :istart "<li>" :iend "</li>"
  1895. :isep "\n" :lsep "\n"
  1896. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1897. params)))
  1898. (defun org-list-to-texinfo (list &optional params)
  1899. "Convert LIST into a Texinfo list.
  1900. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1901. with overruling parameters for `org-list-to-generic'."
  1902. (org-list-to-generic
  1903. list
  1904. (org-combine-plists
  1905. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1906. :ustart "@enumerate" :uend "@end enumerate"
  1907. :dstart "@table" :dend "@end table"
  1908. :dtstart "@item " :dtend "\n"
  1909. :ddstart "" :ddend ""
  1910. :istart "@item\n" :iend ""
  1911. :isep "\n" :lsep "\n"
  1912. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1913. params)))
  1914. (provide 'org-list)
  1915. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1916. ;;; org-list.el ends here