org-list.el 83 KB

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