org-list.el 69 KB

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