org-list.el 82 KB

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