org-list.el 104 KB

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