org-list.el 76 KB

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