org-list.el 100 KB

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