org-list.el 100 KB

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