org-list.el 101 KB

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