org-list.el 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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-skip-whitespace "org" ())
  43. (declare-function org-trim "org" (s))
  44. (declare-function org-get-indentation "org" (&optional line))
  45. (declare-function org-timer-item "org-timer" (&optional arg))
  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-empty-line-terminates-plain-lists nil
  133. "Non-nil means an empty line ends all plain list levels.
  134. Otherwise, look for `org-list-end-regexp'."
  135. :group 'org-plain-lists
  136. :type 'boolean)
  137. (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
  138. "Regexp matching the end of all plain list levels.
  139. It must start with \"^\" and end with \"\\n\". It defaults to 2
  140. blank lines. `org-empty-line-terminates-plain-lists' has
  141. precedence over it."
  142. :group 'org-plain-lists
  143. :type 'string)
  144. (defcustom org-list-automatic-rules '((bullet . t)
  145. (checkbox . t)
  146. (indent . t)
  147. (insert . t)
  148. (renumber . t))
  149. "Non-nil means apply set of rules when acting on lists.
  150. By default, automatic actions are taken when using
  151. \\[org-shiftmetaup], \\[org-shiftmetadown], \\[org-meta-return],
  152. \\[org-metaright], \\[org-metaleft], \\[org-shiftmetaright],
  153. \\[org-shiftmetaleft], \\[org-ctrl-c-minus],
  154. \\[org-toggle-checkbox] or \\[org-insert-todo-heading]. You can
  155. disable individually these rules by setting them to nil. Valid
  156. rules are:
  157. bullet when non-nil, cycling bullet do not allow lists at
  158. column 0 to have * as a bullet and descriptions lists
  159. to be numbered.
  160. checkbox when non-nil, checkbox statistics is updated each time
  161. you either insert a new checkbox or toggle a checkbox.
  162. It also prevents from inserting a checkbox in a
  163. description item.
  164. indent when non-nil indenting or outdenting list top-item will
  165. move the whole list, indenting the first item of a
  166. sub-list will be forbidden and outdenting a list whose
  167. bullet is * to column 0 will change that bullet to -.
  168. insert when non-nil, trying to insert an item inside a block
  169. will insert it right before the block instead of
  170. throwing an error.
  171. renumber when non-nil, renumber ordered plain lists whenever it
  172. is modified. You can always use \\[org-ctrl-c-ctrl-c]
  173. to trigger renumbering."
  174. :group 'org-plain-lists
  175. :type '(alist :tag "Sets of rules"
  176. :key-type
  177. (choice
  178. (const :tag "Bullet" bullet)
  179. (const :tag "Checkbox" checkbox)
  180. (const :tag "Indent" indent)
  181. (const :tag "Insert" insert)
  182. (const :tag "Renumber" renumber))
  183. :value-type
  184. (boolean :tag "Activate" :value t)))
  185. (defcustom org-hierarchical-checkbox-statistics t
  186. "Non-nil means checkbox statistics counts only the state of direct children.
  187. When nil, all boxes below the cookie are counted.
  188. This can be set to nil on a per-node basis using a COOKIE_DATA property
  189. with the word \"recursive\" in the value."
  190. :group 'org-plain-lists
  191. :type 'boolean)
  192. (defcustom org-description-max-indent 20
  193. "Maximum indentation for the second line of a description list.
  194. When the indentation would be larger than this, it will become
  195. 5 characters instead."
  196. :group 'org-plain-lists
  197. :type 'integer)
  198. (defcustom org-list-radio-list-templates
  199. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  200. % END RECEIVE ORGLST %n
  201. \\begin{comment}
  202. #+ORGLST: SEND %n org-list-to-latex
  203. -
  204. \\end{comment}\n")
  205. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  206. @c END RECEIVE ORGLST %n
  207. @ignore
  208. #+ORGLST: SEND %n org-list-to-texinfo
  209. -
  210. @end ignore\n")
  211. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  212. <!-- END RECEIVE ORGLST %n -->
  213. <!--
  214. #+ORGLST: SEND %n org-list-to-html
  215. -
  216. -->\n"))
  217. "Templates for radio lists in different major modes.
  218. All occurrences of %n in a template will be replaced with the name of the
  219. list, obtained by prompting the user."
  220. :group 'org-plain-lists
  221. :type '(repeat
  222. (list (symbol :tag "Major mode")
  223. (string :tag "Format"))))
  224. ;;; Internal functions
  225. (defun org-list-end-re ()
  226. "Return the regex corresponding to the end of a list.
  227. It depends on `org-empty-line-terminates-plain-lists'."
  228. (if org-empty-line-terminates-plain-lists
  229. "^[ \t]*\n"
  230. org-list-end-regexp))
  231. (defun org-item-re (&optional general)
  232. "Return the correct regular expression for plain lists.
  233. If GENERAL is non-nil, return the general regexp independent of the value
  234. of `org-plain-list-ordered-item-terminator'."
  235. (cond
  236. ((or general (eq org-plain-list-ordered-item-terminator t))
  237. "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  238. ((= org-plain-list-ordered-item-terminator ?.)
  239. "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  240. ((= org-plain-list-ordered-item-terminator ?\))
  241. "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  242. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
  243. (defconst org-item-beginning-re (concat "^" (org-item-re))
  244. "Regexp matching the beginning of a plain list item.")
  245. (defun org-list-terminator-between (min max &optional firstp)
  246. "Find the position of a list ender between MIN and MAX, or nil.
  247. This function looks for `org-list-end-re' outside a block.
  248. If FIRSTP in non-nil, return the point at the beginning of the
  249. nearest valid terminator from min. Otherwise, return the point at
  250. the end of the nearest terminator from max."
  251. (save-excursion
  252. (let* ((start (if firstp min max))
  253. (end (if firstp max min))
  254. (search-fun (if firstp
  255. #'org-search-forward-unenclosed
  256. #'org-search-backward-unenclosed))
  257. (list-end-p (progn
  258. (goto-char start)
  259. (funcall search-fun (org-list-end-re) end t))))
  260. ;; Is there a valid list terminator somewhere ?
  261. (and list-end-p
  262. ;; we want to be on the first line of the list ender
  263. (match-beginning 0)))))
  264. (defun org-list-search-unenclosed-generic (search-fun regexp bound noerror count)
  265. "Search for REGEXP with SEARCH-FUN but don't stop inside blocks or at protected places."
  266. (let ((origin (point)))
  267. (cond
  268. ;; nothing found: return nil
  269. ((not (funcall search-fun regexp bound noerror count)) nil)
  270. ((or (save-match-data
  271. (org-in-regexps-block-p "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  272. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2))))
  273. (get-text-property (match-beginning 0) 'org-protected))
  274. ;; match is enclosed or protected: start again, searching one
  275. ;; occurrence away.
  276. (goto-char origin)
  277. (org-list-search-unenclosed-generic search-fun regexp bound noerror (1+ count)))
  278. ;; else return point.
  279. (t (point)))))
  280. (defun org-search-backward-unenclosed (regexp &optional bound noerror)
  281. "Like `re-search-backward' but don't stop inside blocks or at protected places."
  282. (org-list-search-unenclosed-generic #'re-search-backward regexp bound noerror 1))
  283. (defun org-search-forward-unenclosed (regexp &optional bound noerror)
  284. "Like `re-search-forward' but don't stop inside blocks or at protected places."
  285. (org-list-search-unenclosed-generic #'re-search-forward regexp bound noerror 1))
  286. (defun org-list-at-regexp-after-bullet-p (regexp)
  287. "Is point at a list item with REGEXP after bullet?"
  288. (and (org-at-item-p)
  289. (save-excursion
  290. (goto-char (match-end 0))
  291. ;; Ignore counter if any
  292. (when (looking-at "\\(?:\\[@start:[0-9]+\\][ \t]*\\)?")
  293. (goto-char (match-end 0)))
  294. (looking-at regexp))))
  295. (defun org-list-get-item-same-level (search-fun pos limit pre-move)
  296. "Return point at the beginning of next item at the same level.
  297. Search items using function SEARCH-FUN, from POS to LIMIT. It
  298. uses PRE-MOVE before search. Return nil if no item was found."
  299. (save-excursion
  300. (goto-char pos)
  301. (let ((ind (progn
  302. (org-beginning-of-item)
  303. (org-get-indentation)))
  304. (start (point-at-bol)))
  305. ;; We don't want to match the current line.
  306. (funcall pre-move)
  307. ;; Skip any sublist on the way
  308. (while (and (funcall search-fun org-item-beginning-re limit t)
  309. (> (org-get-indentation) ind)))
  310. (when (and (/= (point-at-bol) start) ; Have we moved ?
  311. (= (org-get-indentation) ind))
  312. (point-at-bol)))))
  313. (defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
  314. "Insert a new list item at POS.
  315. If POS is before first character after bullet of the item, the
  316. new item will be created before the current one.
  317. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  318. after the bullet. Cursor will be after this text once the
  319. function ends."
  320. (goto-char pos)
  321. ;; Is point in a special block?
  322. (when (org-in-regexps-block-p
  323. "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  324. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
  325. (if (not (cdr (assq 'insert org-list-automatic-rules)))
  326. ;; Rule in `org-list-automatic-rules' forbids insertion.
  327. (error "Cannot insert item inside a block.")
  328. ;; Else, move before it prior to add a new item.
  329. (end-of-line)
  330. (re-search-backward "^[ \t]*#\\+\\(begin\\|BEGIN\\)_" nil t)
  331. (end-of-line 0)))
  332. (let* ((true-pos (point))
  333. (bullet (and (org-beginning-of-item)
  334. (org-list-bullet-string (org-get-bullet))))
  335. (ind (org-get-indentation))
  336. (before-p (progn
  337. ;; Description item: text starts after colons.
  338. (or (org-at-item-description-p)
  339. ;; At a checkbox: text starts after it.
  340. (org-at-item-checkbox-p)
  341. ;; Otherwise, text starts after bullet.
  342. (org-at-item-p))
  343. (<= true-pos (match-end 0))))
  344. ;; Guess number of blank lines used to separate items.
  345. (blank-lines-nb
  346. (let ((insert-blank-p
  347. (cdr (assq 'plain-list-item org-blank-before-new-entry))))
  348. (cond
  349. ;; Trivial cases where there should be none.
  350. ((or org-empty-line-terminates-plain-lists
  351. (not insert-blank-p)) 0)
  352. ;; When `org-blank-before-new-entry' says so, it is 1.
  353. ((eq insert-blank-p t) 1)
  354. ;; plain-list-item is 'auto. Count blank lines separating
  355. ;; neighbours items in list.
  356. (t (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  357. (cond
  358. ;; Is there a next item?
  359. (next-p (goto-char next-p)
  360. (org-back-over-empty-lines))
  361. ;; Is there a previous item?
  362. ((not (org-list-first-item-p)) (org-back-over-empty-lines))
  363. ;; Item alone: count lines separating it from parent, if any
  364. ((/= (org-list-top-point) (point-at-bol))
  365. (org-back-over-empty-lines))
  366. ;; No parent: no blank line.
  367. (t 0)))))))
  368. (insert-fun
  369. (lambda (text)
  370. ;; insert bullet above item in order to avoid bothering
  371. ;; with possible blank lines ending last item.
  372. (org-beginning-of-item)
  373. (indent-to-column ind)
  374. (insert (concat bullet (when checkbox "[ ] ") after-bullet))
  375. ;; Stay between after-bullet and before text.
  376. (save-excursion
  377. (insert (concat text (make-string (1+ blank-lines-nb) ?\n))))
  378. (unless before-p (org-move-item-down))
  379. (when checkbox (org-update-checkbox-count-maybe)))))
  380. (goto-char true-pos)
  381. (cond
  382. (before-p (funcall insert-fun nil)
  383. ;; Not taking advantage of renumbering while moving
  384. ;; down. Need to call it directly.
  385. (org-maybe-renumber-ordered-list) t)
  386. ;; Can't split item: insert bullet at the end of item.
  387. ((not (org-get-alist-option org-M-RET-may-split-line 'item))
  388. (funcall insert-fun nil) t)
  389. ;; else, insert a new bullet along with everything from point
  390. ;; down to last non-blank line of item.
  391. (t
  392. (delete-horizontal-space)
  393. ;; Get pos again in case previous command modified line.
  394. (let* ((pos (point))
  395. (end-before-blank (org-end-of-item-before-blank))
  396. (after-text
  397. (when (< pos end-before-blank)
  398. (prog1
  399. (delete-and-extract-region pos end-before-blank)
  400. ;; delete any blank line at and before point.
  401. (beginning-of-line)
  402. (while (looking-at "^[ \t]*$")
  403. (delete-region (point-at-bol) (1+ (point-at-eol)))
  404. (beginning-of-line 0))))))
  405. (funcall insert-fun after-text) t)))))
  406. ;;; Predicates
  407. (defun org-in-item-p ()
  408. "Is the cursor inside a plain list ?"
  409. (unless (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
  410. (save-excursion
  411. (let* ((limit (save-excursion (outline-previous-heading)))
  412. ;; Move to eol so current line can be matched by `org-item-re'.
  413. (actual-pos (goto-char (point-at-eol)))
  414. (last-item-start (save-excursion
  415. (org-search-backward-unenclosed org-item-beginning-re limit t)))
  416. (list-ender (org-list-terminator-between last-item-start actual-pos)))
  417. ;; We are in a list when we are on an item line or when we can
  418. ;; find an item before point and there is no valid list ender
  419. ;; between it and the point.
  420. (and last-item-start
  421. (not list-ender))))))
  422. (defun org-list-first-item-p ()
  423. "Is this item the first item in a plain list?"
  424. (save-excursion
  425. (= (org-beginning-of-item) (org-beginning-of-item-list))))
  426. (defun org-at-item-p ()
  427. "Is point in a line starting a hand-formatted item?"
  428. (save-excursion
  429. (beginning-of-line) (looking-at org-item-beginning-re)))
  430. (defun org-at-item-bullet-p ()
  431. "Is point at the bullet of a plain list item?"
  432. (and (org-at-item-p)
  433. (not (member (char-after) '(?\ ?\t)))
  434. (< (point) (match-end 0))))
  435. (defun org-at-item-timer-p ()
  436. "Is point at a line starting a plain list item with a timer?"
  437. (org-list-at-regexp-after-bullet-p "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  438. (defun org-at-item-description-p ()
  439. "Is point at a description list item?"
  440. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
  441. (defun org-at-item-checkbox-p ()
  442. "Is point at a line starting a plain-list item with a checklet?"
  443. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  444. (defun org-item-has-child-p ()
  445. "Does the current item have subitems?"
  446. (save-excursion
  447. (org-beginning-of-item)
  448. (let ((ind (org-get-indentation)))
  449. (org-end-of-item-or-at-child)
  450. (and (org-at-item-p)
  451. (> (org-get-indentation) ind)))))
  452. (defun org-checkbox-blocked-p ()
  453. "Is the current checkbox blocked from for being checked now?
  454. A checkbox is blocked if all of the following conditions are fulfilled:
  455. 1. The checkbox is not checked already.
  456. 2. The current entry has the ORDERED property set.
  457. 3. There is an unchecked checkbox in this entry before the current line."
  458. (catch 'exit
  459. (save-match-data
  460. (save-excursion
  461. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  462. (when (equal (match-string 1) "[X]")
  463. ;; the box is already checked!
  464. (throw 'exit nil))
  465. (let ((end (point-at-bol)))
  466. (condition-case nil (org-back-to-heading t)
  467. (error (throw 'exit nil)))
  468. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  469. (when (org-search-forward-unenclosed
  470. "^[ \t]*[-+*0-9.)]+[ \t]+\\(\\[@start:[0-9]+\\][ \t]+\\)?\\[[- ]\\]" end t)
  471. (org-current-line)))))))
  472. ;;; Navigate
  473. (defun org-list-top-point ()
  474. "Return point at the top level item in a list, or nil if not in a list."
  475. (save-excursion
  476. (and (org-in-item-p)
  477. (let ((pos (point-at-eol))
  478. (bound (or (outline-previous-heading) (point-min))))
  479. ;; Is there some list above this one ? If so, go to its ending.
  480. ;; Otherwise, go back to the heading above or bob.
  481. (goto-char (or (org-list-terminator-between bound pos) bound))
  482. ;; From there, search down our list.
  483. (org-search-forward-unenclosed org-item-beginning-re pos t)
  484. (point-at-bol)))))
  485. (defun org-list-bottom-point ()
  486. "Return point just before list ending or nil if not in a list."
  487. (save-excursion
  488. (and (org-in-item-p)
  489. (let ((pos (org-beginning-of-item))
  490. (bound (or (and (let ((outline-regexp org-outline-regexp))
  491. ;; Use default regexp because folding
  492. ;; changes OUTLINE-REGEXP.
  493. (outline-next-heading))
  494. (skip-chars-backward " \t\r\n")
  495. (1+ (point-at-eol)))
  496. (point-max))))
  497. ;; The list ending is either first point matching
  498. ;; `org-list-end-re', point at first white-line before next
  499. ;; heading, or eob.
  500. (or (org-list-terminator-between pos bound t) bound)))))
  501. (defun org-beginning-of-item ()
  502. "Go to the beginning of the current hand-formatted item.
  503. If the cursor is not in an item, throw an error. Return point."
  504. (interactive)
  505. (if (not (org-in-item-p))
  506. (error "Not in an item")
  507. ;; Possibly match the current line.
  508. (end-of-line)
  509. (org-search-backward-unenclosed org-item-beginning-re nil t)
  510. (goto-char (point-at-bol))))
  511. (defun org-end-of-item ()
  512. "Go to the end of the current hand-formatted item.
  513. If the cursor is not in an item, throw an error."
  514. (interactive)
  515. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  516. (cond ((not (org-in-item-p)) (error "Not in an item"))
  517. (next-p (goto-char next-p))
  518. (t (org-end-of-item-list)))))
  519. (defun org-end-of-item-or-at-child ()
  520. "Move to the end of the item text, stops before the first child if any."
  521. (let ((limit (org-list-bottom-point)))
  522. (end-of-line)
  523. (goto-char
  524. (if (org-search-forward-unenclosed org-item-beginning-re limit t)
  525. (point-at-bol)
  526. limit))))
  527. (defun org-end-of-item-before-blank ()
  528. "Return point at end of item, before any blank line.
  529. Point returned is at eol."
  530. (save-excursion
  531. (org-end-of-item)
  532. (skip-chars-backward " \r\t\n")
  533. (point-at-eol)))
  534. (defun org-get-next-item (pos limit)
  535. "Get the point of the next item at the same level as POS.
  536. Stop searching at LIMIT. Return nil if no item is found. This
  537. function does not move point."
  538. (org-list-get-item-same-level
  539. #'org-search-forward-unenclosed pos limit #'end-of-line))
  540. (defun org-get-previous-item (pos limit)
  541. "Get the point of the previous item at the same level as POS.
  542. Stop searching at LIMIT. Return nil if no item is found. This
  543. function does not move point."
  544. (org-list-get-item-same-level
  545. #'org-search-backward-unenclosed pos limit #'beginning-of-line))
  546. (defun org-next-item ()
  547. "Move to the beginning of the next item.
  548. Item is at the same level in the current plain list. Error if not
  549. in a plain list, or if this is the last item in the list."
  550. (interactive)
  551. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  552. (if next-p
  553. (goto-char next-p)
  554. (error "On last item"))))
  555. (defun org-previous-item ()
  556. "Move to the beginning of the previous item.
  557. Item is at the same level in the current plain list. Error if not
  558. in a plain list, or if this is the first item in the list."
  559. (interactive)
  560. (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
  561. (if prev-p
  562. (goto-char prev-p)
  563. (error "On first item"))))
  564. (defun org-beginning-of-item-list ()
  565. "Go to the beginning item of the current list or sublist.
  566. Return point."
  567. (interactive)
  568. (org-beginning-of-item)
  569. (let ((limit (org-list-top-point))
  570. (move-up (lambda (pos bound)
  571. ;; prev-p: any item of same level before ?
  572. (let ((prev-p (org-get-previous-item pos bound)))
  573. ;; recurse until no more item of the same level
  574. ;; can be found.
  575. (if prev-p (funcall move-up prev-p bound) pos)))))
  576. ;; Go to the last item found and at bol in case we didn't move
  577. (goto-char (funcall move-up (point) limit))
  578. (goto-char (point-at-bol))))
  579. (defun org-end-of-item-list ()
  580. "Go to the end of the current list or sublist.
  581. Return point."
  582. (interactive)
  583. (org-beginning-of-item)
  584. (let ((limit (org-list-bottom-point))
  585. (ind (org-get-indentation))
  586. (get-last-item (lambda (pos bound)
  587. ;; next-p: any item of same level after ?
  588. (let ((next-p (org-get-next-item pos bound)))
  589. ;; recurse until no more item of the same level
  590. ;; can be found.
  591. (if next-p (funcall get-last-item next-p bound) pos)))))
  592. ;; Move to the last item of every list or sublist encountered, and
  593. ;; down to bol of a higher-level item, or limit.
  594. (while (and (/= (point) limit)
  595. (>= (org-get-indentation) ind))
  596. (goto-char (funcall get-last-item (point) limit))
  597. (end-of-line)
  598. (when (org-search-forward-unenclosed org-item-beginning-re limit 'move)
  599. (beginning-of-line)))
  600. (point)))
  601. ;;; Manipulate
  602. (defun org-list-exchange-items (beg-A beg-B)
  603. "Swap item starting at BEG-A with item starting at BEG-B.
  604. Blank lines at the end of items are left in place. Assumes
  605. BEG-A is lesser than BEG-B."
  606. (save-excursion
  607. (let* ((end-of-item-no-blank (lambda (pos)
  608. (goto-char pos)
  609. (goto-char (org-end-of-item-before-blank))))
  610. (end-A-no-blank (funcall end-of-item-no-blank beg-A))
  611. (end-B-no-blank (funcall end-of-item-no-blank beg-B))
  612. (body-A (buffer-substring beg-A end-A-no-blank))
  613. (body-B (buffer-substring beg-B end-B-no-blank))
  614. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
  615. (goto-char beg-A)
  616. (delete-region beg-A end-B-no-blank)
  617. (insert (concat body-B between-A-no-blank-and-B body-A)))))
  618. (defun org-move-item-down ()
  619. "Move the plain list item at point down, i.e. swap with following item.
  620. Subitems (items with larger indentation) are considered part of the item,
  621. so this really moves item trees."
  622. (interactive)
  623. (let ((pos (point))
  624. (col (current-column))
  625. (actual-item (org-beginning-of-item))
  626. (next-item (org-get-next-item (point) (save-excursion (org-end-of-item-list)))))
  627. (if (not next-item)
  628. (progn
  629. (goto-char pos)
  630. (error "Cannot move this item further down"))
  631. (org-list-exchange-items actual-item next-item)
  632. (org-maybe-renumber-ordered-list)
  633. (org-next-item)
  634. (move-to-column col))))
  635. (defun org-move-item-up ()
  636. "Move the plain list item at point up, i.e. swap with previous item.
  637. Subitems (items with larger indentation) are considered part of the item,
  638. so this really moves item trees."
  639. (interactive)
  640. (let ((pos (point))
  641. (col (current-column))
  642. (actual-item (org-beginning-of-item))
  643. (prev-item (org-get-previous-item (point) (save-excursion (org-beginning-of-item-list)))))
  644. (if (not prev-item)
  645. (progn
  646. (goto-char pos)
  647. (error "Cannot move this item further up"))
  648. (org-list-exchange-items prev-item actual-item)
  649. (org-maybe-renumber-ordered-list)
  650. (move-to-column col))))
  651. (defun org-insert-item (&optional checkbox)
  652. "Insert a new item at the current level.
  653. If cursor is before first character after bullet of the item, the
  654. new item will be created before the current one. Return t when
  655. things worked, nil when we are not in an item, or item is
  656. invisible."
  657. (unless (or (not (org-in-item-p))
  658. (org-invisible-p))
  659. (if (save-excursion
  660. (org-beginning-of-item)
  661. (org-at-item-timer-p))
  662. ;; Timer list: delegate to `org-timer-item'.
  663. (progn (org-timer-item) t)
  664. ;; if we're in a description list, ask for the new term.
  665. (let ((desc-text (when (save-excursion
  666. (and (org-beginning-of-item)
  667. (org-at-item-description-p)))
  668. (concat (read-string "Term: ") " :: "))))
  669. ;; Don't insert a checkbox if checkbox rule is applied and it
  670. ;; is a description item.
  671. (org-list-insert-item-generic
  672. (point) (and checkbox
  673. (or (not desc-text)
  674. (not (cdr (assq 'checkbox org-list-automatic-rules)))))
  675. desc-text)))))
  676. ;;; Indentation
  677. (defun org-get-string-indentation (s)
  678. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  679. (let ((n -1) (i 0) (w tab-width) c)
  680. (catch 'exit
  681. (while (< (setq n (1+ n)) (length s))
  682. (setq c (aref s n))
  683. (cond ((= c ?\ ) (setq i (1+ i)))
  684. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  685. (t (throw 'exit t)))))
  686. i))
  687. (defvar org-suppress-item-indentation) ; dynamically scoped parameter
  688. (defun org-shift-item-indentation (delta)
  689. "Shift the indentation in current item by DELTA."
  690. (unless (org-bound-and-true-p org-suppress-item-indentation)
  691. (save-excursion
  692. (let ((beg (point-at-bol))
  693. (end (org-end-of-item)))
  694. (beginning-of-line 0)
  695. (while (> (point) beg)
  696. (when (looking-at "[ \t]*\\S-")
  697. ;; this is not an empty line
  698. (let ((i (org-get-indentation)))
  699. (when (and (> i 0) (> (+ i delta) 0))
  700. (indent-line-to (+ i delta)))))
  701. (beginning-of-line 0))))))
  702. (defvar org-last-indent-begin-marker (make-marker))
  703. (defvar org-last-indent-end-marker (make-marker))
  704. (defun org-outdent-item (arg)
  705. "Outdent a local list item, but not its children."
  706. (interactive "p")
  707. (org-indent-item-tree (- arg) 'no-subtree))
  708. (defun org-indent-item (arg)
  709. "Indent a local list item, but not its children."
  710. (interactive "p")
  711. (org-indent-item-tree arg 'no-subtree))
  712. (defun org-outdent-item-tree (arg &optional no-subtree)
  713. "Outdent a local list item including its children.
  714. If NO-SUBTREE is set, only outdent the item itself, not its children."
  715. (interactive "p")
  716. (org-indent-item-tree (- arg) no-subtree))
  717. (defun org-indent-item-tree (arg &optional no-subtree)
  718. "Indent a local list item including its children.
  719. If NO-SUBTREE is set, only indent the item itself, not its
  720. children. Return t if sucessful."
  721. (interactive "p")
  722. (and (org-region-active-p) (org-cursor-to-region-beginning))
  723. (unless (org-at-item-p)
  724. (error "Not on an item"))
  725. (let ((line (org-current-line))
  726. (col (current-column))
  727. (pos (point))
  728. (origin-ind (save-excursion
  729. (goto-char (org-list-top-point))
  730. (org-get-indentation)))
  731. beg end ind ind1 ind-pos bullet delta ind-down ind-up)
  732. (setq end (and (org-region-active-p) (region-end)))
  733. ;; If moving a subtree, don't drag additional items on subsequent
  734. ;; moves.
  735. (if (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  736. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  737. (setq beg org-last-indent-begin-marker
  738. end org-last-indent-end-marker)
  739. (org-beginning-of-item)
  740. (setq beg (move-marker org-last-indent-begin-marker (point)))
  741. ;; Determine end point of indentation
  742. (if no-subtree (org-end-of-item-or-at-child) (org-end-of-item))
  743. (setq end (move-marker org-last-indent-end-marker (or end (point)))))
  744. ;; Get some information
  745. (goto-char beg)
  746. (setq ind-pos (org-item-indent-positions)
  747. bullet (cdr (car ind-pos))
  748. bul-up (cdr (nth 1 ind-pos))
  749. ind (caar ind-pos)
  750. ind-down (car (nth 2 ind-pos))
  751. ind-up (car (nth 1 ind-pos))
  752. delta (if (> arg 0)
  753. (if ind-down (- ind-down ind) 2)
  754. (if ind-up (- ind-up ind) -2)))
  755. ;; Make some checks before indenting.
  756. (cond
  757. ((< (+ delta ind) 0)
  758. (goto-char pos)
  759. (error "Cannot outdent beyond margin"))
  760. ;; Apply indent rules if activated.
  761. ((cdr (assq 'indent org-list-automatic-rules))
  762. (cond
  763. ;; 1. If at top-point move the whole list. Moreover, if
  764. ;; *-list is going to column 0, change bullet to "-".
  765. ((= (point-at-bol) (org-list-top-point))
  766. (when (and (= (+ delta ind) 0) (equal bullet "*"))
  767. (org-fix-bullet-type (setq bullet "-")))
  768. (setq end (set-marker org-last-indent-end-marker (org-list-bottom-point))))
  769. ;; 2. Do not indent before top-item.
  770. ((< (+ delta ind) origin-ind)
  771. (goto-char pos)
  772. (error "Cannot outdent beyond top level item"))
  773. ;; 3. Do not indent the first item of a list.
  774. ((and (org-list-first-item-p) (> delta 0))
  775. (goto-char pos)
  776. (error "Cannot indent the beginning of a sublist"))
  777. ;; 4. Do not outdent item that has children without moving.
  778. ;; In the case of a subtree, make sure the check applies to
  779. ;; its last item.
  780. ((and (< delta 0)
  781. (save-excursion (goto-char (1- end)) (org-item-has-child-p)))
  782. (goto-char pos)
  783. (error "Cannot outdent an item having children")))))
  784. ;; Replace bullet of current item with the bullet it is going to
  785. ;; have if we're outdenting. This is needed to prevent indentation
  786. ;; problems of subtrees when outdenting changes bullet size.
  787. (when (< delta 0)
  788. (let ((new-bul (org-list-bullet-string (or bul-up bullet))))
  789. (org-list-replace-bullet new-bul)))
  790. ;; Proceed to reindentation.
  791. (while (< (point) end)
  792. (beginning-of-line)
  793. (skip-chars-forward " \t") (setq ind1 (current-column))
  794. (delete-region (point-at-bol) (point))
  795. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  796. (beginning-of-line 2))
  797. ;; Get back to original position, shifted by delta
  798. (goto-line line)
  799. (move-to-column (max (+ delta col) 0))
  800. ;; Fix and reorder all lists and sublists from parent of the list
  801. ;; at point, or from list at point if it hasn't got any parent or
  802. ;; if we're outdenting.
  803. (save-excursion
  804. ;; Take care of parent list, if it makes sense.
  805. (org-beginning-of-item-list)
  806. (unless (or (< arg 0) (= (org-list-top-point) (point)))
  807. (beginning-of-line 0)
  808. (org-beginning-of-item)
  809. (org-fix-bullet-type)))
  810. ;; Take care of list at point. If demoting, look at
  811. ;; `org-list-demote-modify-bullet'.
  812. (org-fix-bullet-type
  813. (and (> arg 0)
  814. (cdr (assoc bullet org-list-demote-modify-bullet))))
  815. (save-excursion
  816. (when (org-item-has-child-p)
  817. ;; Take care of child, or of every sublist if we're moving a
  818. ;; subtree.
  819. (org-end-of-item-or-at-child)
  820. (if no-subtree
  821. (org-fix-bullet-type)
  822. (let ((fix-list (lambda (i)
  823. (when (org-list-first-item-p)
  824. (org-fix-bullet-type
  825. (and (> arg 0)
  826. (cdr (assoc (org-get-bullet) org-list-demote-modify-bullet)))))
  827. (when (org-item-has-child-p)
  828. (org-end-of-item-or-at-child)
  829. (org-apply-on-list fix-list nil)))))
  830. (org-apply-on-list fix-list nil))))))
  831. t)
  832. (defun org-item-indent-positions ()
  833. "Return indentation for plain list items.
  834. This returns a list with three values: The current indentation, the
  835. parent indentation and the indentation a child should have.
  836. Assumes cursor in item line."
  837. (let* ((bolpos (point-at-bol))
  838. (ind (org-get-indentation))
  839. (bullet (org-get-bullet))
  840. ind-down ind-up bullet-up bullet-down pos)
  841. (save-excursion
  842. (org-beginning-of-item-list)
  843. (skip-chars-backward "\n\r \t")
  844. (when (org-in-item-p)
  845. (org-beginning-of-item)
  846. (let ((prev-indent (org-get-indentation)))
  847. (when (< prev-indent ind)
  848. (setq ind-up prev-indent)
  849. (setq bullet-up (org-get-bullet))))))
  850. (setq pos (point))
  851. (save-excursion
  852. (cond
  853. ((and (ignore-errors (progn (org-previous-item) t))
  854. (or (end-of-line) t)
  855. (org-search-forward-unenclosed org-item-beginning-re bolpos t))
  856. (setq ind-down (org-get-indentation)
  857. bullet-down (org-get-bullet)))
  858. ((and (goto-char pos)
  859. (org-at-item-p))
  860. (goto-char (match-end 0))
  861. (skip-chars-forward " \t")
  862. (setq ind-down (current-column)
  863. bullet-down (org-get-bullet)))))
  864. (if (and bullet-down (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-down))
  865. (setq bullet-down (concat "1" (match-string 1 bullet-down))))
  866. (if (and bullet-up (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-up))
  867. (setq bullet-up (concat "1" (match-string 1 bullet-up))))
  868. (if (and bullet (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet))
  869. (setq bullet (concat "1" (match-string 1 bullet))))
  870. (list (cons ind bullet)
  871. (cons ind-up bullet-up)
  872. (cons ind-down bullet-down))))
  873. (defvar org-tab-ind-state)
  874. (defun org-cycle-item-indentation ()
  875. (let ((org-suppress-item-indentation t)
  876. (org-adapt-indentation nil))
  877. (when (and (or (org-at-item-description-p) (org-at-item-checkbox-p) (org-at-item-p))
  878. (>= (match-end 0) (save-excursion
  879. (org-end-of-item-or-at-child)
  880. (skip-chars-backward " \r\t\n")
  881. (point))))
  882. (setq this-command 'org-cycle-item-indentation)
  883. ;; When in the middle of the cycle, try to outdent first. If it
  884. ;; fails, and point is still at initial position, indent. Else,
  885. ;; go back to original position.
  886. (if (eq last-command 'org-cycle-item-indentation)
  887. (cond
  888. ((ignore-errors (org-indent-item -1)))
  889. ((and (= (org-get-indentation) (car org-tab-ind-state))
  890. (ignore-errors (org-indent-item 1))))
  891. (t (back-to-indentation)
  892. (indent-to-column (car org-tab-ind-state))
  893. (end-of-line)
  894. (org-fix-bullet-type (nth 1 org-tab-ind-state))
  895. ;; Break cycle
  896. (setq this-command 'identity)))
  897. ;; If a cycle is starting, remember indentation and bullet,
  898. ;; then try to indent. If it fails, try to outdent.
  899. (setq org-tab-ind-state
  900. (list (org-get-indentation) (org-get-bullet)))
  901. (cond
  902. ((ignore-errors (org-indent-item 1)))
  903. ((ignore-errors (org-indent-item -1)))
  904. (t (error "Cannot move item"))))
  905. t)))
  906. ;;; Bullets
  907. (defun org-get-bullet ()
  908. (and (org-at-item-p)
  909. (org-trim (match-string 1))))
  910. (defun org-list-bullet-string (bullet)
  911. "Concatenate BULLET with an appropriate number of whitespaces.
  912. It determines the number of whitespaces to append by looking at
  913. `org-list-two-spaces-after-bullet-regexp'."
  914. (concat
  915. bullet " "
  916. ;; Do we need to concat another white space ?
  917. (when (string-match org-list-two-spaces-after-bullet-regexp bullet) " ")))
  918. (defun org-list-replace-bullet (new-bullet)
  919. "Replace current item's bullet with NEW-BULLET.
  920. Assume point is at item. Indent body if needed."
  921. (save-excursion
  922. (let ((old (progn
  923. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  924. (match-string 1))))
  925. (unless (equal new-bullet old)
  926. (replace-match new-bullet nil nil nil 1)
  927. ;; When bullet lengths are differents, move the whole
  928. ;; sublist accordingly
  929. (org-shift-item-indentation (- (length new-bullet) (length old)))))))
  930. (defun org-fix-bullet-type (&optional force-bullet)
  931. "Make sure all items in this list have the same bullet as the first item.
  932. Also, fix the indentation."
  933. (interactive)
  934. (unless (org-at-item-p) (error "This is not a list"))
  935. (org-preserve-lc
  936. (let* ((ini-bul (progn (org-beginning-of-item-list) (org-get-bullet)))
  937. (bullet (org-list-bullet-string (or force-bullet ini-bul)))
  938. (replace-bullet
  939. (lambda (result bullet)
  940. (org-list-replace-bullet bullet))))
  941. (org-apply-on-list replace-bullet nil bullet)
  942. (org-maybe-renumber-ordered-list))))
  943. (defun org-renumber-ordered-list (&optional arg)
  944. "Renumber an ordered plain list.
  945. Cursor needs to be in the first line of an item, the line that starts
  946. with something like \"1.\" or \"2)\". Start to count at ARG or 1."
  947. (interactive "p")
  948. (unless (and (org-at-item-p)
  949. (match-beginning 3))
  950. (error "This is not an ordered list"))
  951. (org-preserve-lc
  952. (let* ((offset (progn
  953. (org-beginning-of-item)
  954. (or (and (looking-at "[ \t]*\\[@start:\\([0-9]+\\)")
  955. (string-to-number (match-string 1)))
  956. arg
  957. 1)))
  958. (item-fmt (progn
  959. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  960. (concat "%d" (or (match-string 1) "."))))
  961. ;; Here is the function applied at each item of the list.
  962. (renumber-item (lambda (counter off fmt)
  963. (let* ((new (format fmt (+ counter off)))
  964. (old (progn
  965. (looking-at org-item-beginning-re)
  966. (match-string 2)))
  967. (begin (match-beginning 2))
  968. (end (match-end 2)))
  969. (delete-region begin end)
  970. (goto-char begin)
  971. (insert new)
  972. ;; In case item number went from 9. to 10.
  973. ;; or the other way.
  974. (org-shift-item-indentation (- (length new) (length old)))
  975. (1+ counter)))))
  976. (org-apply-on-list renumber-item 0 offset item-fmt))))
  977. (defun org-maybe-renumber-ordered-list ()
  978. "Renumber the ordered list at point if setup allows it.
  979. This tests the if 'renumber rule is set in
  980. `org-list-automatic-rules' before doing the renumbering.
  981. Do not throw error on failure."
  982. (interactive)
  983. (when (cdr (assq 'renumber org-list-automatic-rules))
  984. (ignore-errors (org-renumber-ordered-list))))
  985. (defun org-cycle-list-bullet (&optional which)
  986. "Cycle through the different itemize/enumerate bullets.
  987. This cycle the entire list level through the sequence:
  988. `-' -> `+' -> `*' -> `1.' -> `1)'
  989. If WHICH is a valid string, use that as the new bullet. If WHICH
  990. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  991. 'previous, cycle backwards."
  992. (interactive "P")
  993. (org-preserve-lc
  994. (let* ((bullet (progn (org-beginning-of-item-list)
  995. (org-get-bullet)))
  996. (current (cond
  997. ((string-match "\\." bullet) "1.")
  998. ((string-match ")" bullet) "1)")
  999. (t bullet)))
  1000. (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
  1001. (bullet-list (append '("-" "+" )
  1002. ;; *-bullets are not allowed at column 0
  1003. (unless (and bullet-rule-p
  1004. (looking-at "\\S-")) '("*"))
  1005. ;; Description items cannot be numbered
  1006. (unless (and bullet-rule-p
  1007. (or (eq org-plain-list-ordered-item-terminator ?.)
  1008. (org-at-item-description-p))) '("1)"))
  1009. (unless (and bullet-rule-p
  1010. (or (eq org-plain-list-ordered-item-terminator ?\))
  1011. (org-at-item-description-p))) '("1."))))
  1012. (len (length bullet-list))
  1013. (item-index (- len (length (member current bullet-list))))
  1014. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  1015. (new (cond
  1016. ((member which bullet-list) which)
  1017. ((numberp which) (funcall get-value which))
  1018. ((eq 'previous which) (funcall get-value (1- item-index)))
  1019. (t (funcall get-value (1+ item-index))))))
  1020. (org-fix-bullet-type new))))
  1021. ;;; Checkboxes
  1022. (defun org-toggle-checkbox (&optional toggle-presence)
  1023. "Toggle the checkbox in the current line.
  1024. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  1025. double prefix, set checkbox to [-].
  1026. When there is an active region, toggle status or presence of the
  1027. checkbox in the first line, and make every item in the region
  1028. have the same status or presence, respectively.
  1029. If the cursor is in a headline, apply this to all checkbox items
  1030. in the text below the heading, taking as reference the first item
  1031. in subtree."
  1032. (interactive "P")
  1033. ;; Bounds is a list of type (beg end single-p) where single-p is t
  1034. ;; when `org-toggle-checkbox' is applied to a single item. Only
  1035. ;; toggles on single items will return errors.
  1036. (let* ((bounds
  1037. (cond
  1038. ((org-region-active-p)
  1039. (list (region-beginning) (region-end) nil))
  1040. ((org-on-heading-p)
  1041. ;; In this case, reference line is the first item in subtree
  1042. (let ((limit (save-excursion (outline-next-heading) (point))))
  1043. (save-excursion
  1044. (org-search-forward-unenclosed org-item-beginning-re limit 'move)
  1045. (list (point) limit nil))))
  1046. ((org-at-item-p)
  1047. (list (point-at-bol) (point-at-eol) t))
  1048. (t (error "Not at an item or heading, and no active region"))))
  1049. ;; marker is needed because deleting checkboxes will change END
  1050. (end (copy-marker (nth 1 bounds)))
  1051. (single-p (nth 2 bounds))
  1052. (ref-presence (save-excursion (goto-char (car bounds)) (org-at-item-checkbox-p)))
  1053. (ref-status (equal (match-string 1) "[X]"))
  1054. (act-on-item
  1055. (lambda (ref-pres ref-stat)
  1056. (if (equal toggle-presence '(4))
  1057. (cond
  1058. ((and ref-pres (org-at-item-checkbox-p))
  1059. (replace-match ""))
  1060. ((and (not ref-pres)
  1061. (not (org-at-item-checkbox-p))
  1062. (org-at-item-p))
  1063. (goto-char (match-end 0))
  1064. ;; Ignore counter, if any
  1065. (when (looking-at "\\(?:\\[@start:[0-9]+\\][ \t]*\\)?")
  1066. (goto-char (match-end 0)))
  1067. (let ((desc-p (and (org-at-item-description-p)
  1068. (cdr (assq 'checkbox org-list-automatic-rules)))))
  1069. (cond
  1070. ((and single-p desc-p)
  1071. (error "Cannot add a checkbox in a description list"))
  1072. ((not desc-p) (insert "[ ] "))))))
  1073. (let ((blocked (org-checkbox-blocked-p)))
  1074. (cond
  1075. ((and blocked single-p)
  1076. (error "Checkbox blocked because of unchecked box in line %d" blocked))
  1077. (blocked nil)
  1078. ((org-at-item-checkbox-p)
  1079. (replace-match
  1080. (cond ((equal toggle-presence '(16)) "[-]")
  1081. (ref-stat "[ ]")
  1082. (t "[X]"))
  1083. t t nil 1))))))))
  1084. (save-excursion
  1085. (while (< (point) end)
  1086. (funcall act-on-item ref-presence ref-status)
  1087. (org-search-forward-unenclosed org-item-beginning-re end 'move)))
  1088. (org-update-checkbox-count-maybe)))
  1089. (defun org-reset-checkbox-state-subtree ()
  1090. "Reset all checkboxes in an entry subtree."
  1091. (interactive "*")
  1092. (save-restriction
  1093. (save-excursion
  1094. (org-narrow-to-subtree)
  1095. (org-show-subtree)
  1096. (goto-char (point-min))
  1097. (let ((end (point-max)))
  1098. (while (< (point) end)
  1099. (when (org-at-item-checkbox-p)
  1100. (replace-match "[ ]" t t nil 1))
  1101. (beginning-of-line 2))))
  1102. (org-update-checkbox-count-maybe)))
  1103. (defvar org-checkbox-statistics-hook nil
  1104. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  1105. This hook runs even if 'checkbox rules in
  1106. `org-list-automatic-rules' do not apply, so it can be used to
  1107. implement alternative ways of collecting statistics
  1108. information.")
  1109. (defun org-update-checkbox-count-maybe ()
  1110. "Update checkbox statistics unless turned off by user."
  1111. (when (cdr (assq 'checkbox org-list-automatic-rules))
  1112. (org-update-checkbox-count))
  1113. (run-hooks 'org-checkbox-statistics-hook))
  1114. (defun org-update-checkbox-count (&optional all)
  1115. "Update the checkbox statistics in the current section.
  1116. This will find all statistic cookies like [57%] and [6/12] and update them
  1117. with the current numbers. With optional prefix argument ALL, do this for
  1118. the whole buffer."
  1119. (interactive "P")
  1120. (save-excursion
  1121. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  1122. (beg (condition-case nil
  1123. (progn (org-back-to-heading) (point))
  1124. (error (point-min))))
  1125. (end (move-marker (make-marker)
  1126. (progn (outline-next-heading) (point))))
  1127. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  1128. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  1129. (re-find (concat re "\\|" re-box))
  1130. beg-cookie end-cookie is-percent c-on c-off lim new
  1131. eline curr-ind next-ind continue-from startsearch
  1132. (recursive
  1133. (or (not org-hierarchical-checkbox-statistics)
  1134. (string-match "\\<recursive\\>"
  1135. (or (ignore-errors
  1136. (org-entry-get nil "COOKIE_DATA"))
  1137. ""))))
  1138. (cstat 0)
  1139. )
  1140. (when all
  1141. (goto-char (point-min))
  1142. (outline-next-heading)
  1143. (setq beg (point) end (point-max)))
  1144. (goto-char end)
  1145. ;; find each statistics cookie
  1146. (while (and (org-search-backward-unenclosed re-find beg t)
  1147. (not (save-match-data
  1148. (and (org-on-heading-p)
  1149. (string-match "\\<todo\\>"
  1150. (downcase
  1151. (or (org-entry-get
  1152. nil "COOKIE_DATA")
  1153. "")))))))
  1154. (setq beg-cookie (match-beginning 1)
  1155. end-cookie (match-end 1)
  1156. cstat (+ cstat (if end-cookie 1 0))
  1157. startsearch (point-at-eol)
  1158. continue-from (match-beginning 0)
  1159. is-percent (match-beginning 2)
  1160. lim (cond
  1161. ((org-on-heading-p) (outline-next-heading) (point))
  1162. ((org-at-item-p) (org-end-of-item) (point))
  1163. (t nil))
  1164. c-on 0
  1165. c-off 0)
  1166. (when lim
  1167. ;; find first checkbox for this cookie and gather
  1168. ;; statistics from all that are at this indentation level
  1169. (goto-char startsearch)
  1170. (if (org-search-forward-unenclosed re-box lim t)
  1171. (progn
  1172. (org-beginning-of-item)
  1173. (setq curr-ind (org-get-indentation))
  1174. (setq next-ind curr-ind)
  1175. (while (and (bolp) (org-at-item-p)
  1176. (if recursive
  1177. (<= curr-ind next-ind)
  1178. (= curr-ind next-ind)))
  1179. (setq eline (point-at-eol))
  1180. (if (org-search-forward-unenclosed re-box eline t)
  1181. (if (member (match-string 2) '("[ ]" "[-]"))
  1182. (setq c-off (1+ c-off))
  1183. (setq c-on (1+ c-on))))
  1184. (if (not recursive)
  1185. ;; org-get-next-item goes through list-enders
  1186. ;; with proper limit.
  1187. (goto-char (or (org-get-next-item (point) lim) lim))
  1188. (end-of-line)
  1189. (when (org-search-forward-unenclosed org-item-beginning-re lim t)
  1190. (beginning-of-line)))
  1191. (setq next-ind (org-get-indentation)))))
  1192. (goto-char continue-from)
  1193. ;; update cookie
  1194. (when end-cookie
  1195. (setq new (if is-percent
  1196. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  1197. (format "[%d/%d]" c-on (+ c-on c-off))))
  1198. (goto-char beg-cookie)
  1199. (insert new)
  1200. (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
  1201. ;; update items checkbox if it has one
  1202. (when (org-at-item-p)
  1203. (org-beginning-of-item)
  1204. (when (and (> (+ c-on c-off) 0)
  1205. (org-search-forward-unenclosed re-box (point-at-eol) t))
  1206. (setq beg-cookie (match-beginning 2)
  1207. end-cookie (match-end 2))
  1208. (delete-region beg-cookie end-cookie)
  1209. (goto-char beg-cookie)
  1210. (cond ((= c-off 0) (insert "[X]"))
  1211. ((= c-on 0) (insert "[ ]"))
  1212. (t (insert "[-]")))
  1213. )))
  1214. (goto-char continue-from))
  1215. (when (interactive-p)
  1216. (message "Checkbox statistics updated %s (%d places)"
  1217. (if all "in entire file" "in current outline entry") cstat)))))
  1218. (defun org-get-checkbox-statistics-face ()
  1219. "Select the face for checkbox statistics.
  1220. The face will be `org-done' when all relevant boxes are checked.
  1221. Otherwise it will be `org-todo'."
  1222. (if (match-end 1)
  1223. (if (equal (match-string 1) "100%")
  1224. 'org-checkbox-statistics-done
  1225. 'org-checkbox-statistics-todo)
  1226. (if (and (> (match-end 2) (match-beginning 2))
  1227. (equal (match-string 2) (match-string 3)))
  1228. 'org-checkbox-statistics-done
  1229. 'org-checkbox-statistics-todo)))
  1230. ;;; Misc Tools
  1231. (defun org-apply-on-list (function init-value &rest args)
  1232. "Call FUNCTION for each item of a the list under point.
  1233. FUNCTION must be called with at least one argument: a return
  1234. value that will contain the value returned by the function at the
  1235. previous item, plus ARGS extra arguments. INIT-VALUE will be the
  1236. value passed to the function at the first item of the list.
  1237. As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
  1238. will return the number of items in the current list.
  1239. Sublists of the list are skipped. Cursor is always at the
  1240. beginning of the item."
  1241. (save-excursion
  1242. (let ((end (copy-marker (save-excursion (org-end-of-item-list))))
  1243. (next-p (make-marker))
  1244. (move-down-action
  1245. (lambda (pos value &rest args)
  1246. (goto-char pos)
  1247. (set-marker next-p (org-get-next-item pos end))
  1248. (let ((return-value (apply function value args)))
  1249. (if (marker-position next-p)
  1250. (apply move-down-action next-p return-value args)
  1251. return-value)))))
  1252. (apply move-down-action (org-beginning-of-item-list) init-value args))))
  1253. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  1254. "Sort plain list items.
  1255. The cursor may be at any item of the list that should be sorted.
  1256. Sublists are not sorted. Checkboxes, if any, are ignored.
  1257. Sorting can be alphabetically, numerically, by date/time as given by
  1258. a time stamp, by a property or by priority.
  1259. The command prompts for the sorting type unless it has been given
  1260. to the function through the SORTING-TYPE argument, which needs to
  1261. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  1262. meaning of each character:
  1263. n Numerically, by converting the beginning of the item to a number.
  1264. a Alphabetically. Only the first line of item is checked.
  1265. t By date/time, either the first active time stamp in the entry, if
  1266. any, or by the first inactive one. In a timer list, sort the timers.
  1267. Capital letters will reverse the sort order.
  1268. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
  1269. function to be called with point at the beginning of the record.
  1270. It must return either a string or a number that should serve as
  1271. the sorting key for that record.
  1272. Comparing entries ignores case by default. However, with an
  1273. optional argument WITH-CASE, the sorting considers case as well."
  1274. (interactive "P")
  1275. (let* ((case-func (if with-case 'identity 'downcase))
  1276. (start (org-beginning-of-item-list))
  1277. (end (save-excursion (org-end-of-item-list)))
  1278. (sorting-type
  1279. (progn
  1280. (message
  1281. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  1282. (read-char-exclusive)))
  1283. (getkey-func (and (= (downcase sorting-type) ?f)
  1284. (org-icompleting-read "Sort using function: "
  1285. obarray 'fboundp t nil nil)
  1286. (intern getkey-func))))
  1287. (message "Sorting items...")
  1288. (save-restriction
  1289. (narrow-to-region start end)
  1290. (let* ((dcst (downcase sorting-type))
  1291. (case-fold-search nil)
  1292. (now (current-time))
  1293. (sort-func (cond
  1294. ((= dcst ?a) 'string<)
  1295. ((= dcst ?f) compare-func)
  1296. ((= dcst ?t) '<)
  1297. (t nil)))
  1298. (begin-record (lambda ()
  1299. (skip-chars-forward " \r\t\n")
  1300. (beginning-of-line)))
  1301. (end-record (lambda ()
  1302. (goto-char (org-end-of-item-before-blank))))
  1303. (value-to-sort
  1304. (lambda ()
  1305. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  1306. (cond
  1307. ((= dcst ?n)
  1308. (string-to-number (buffer-substring (match-end 0)
  1309. (point-at-eol))))
  1310. ((= dcst ?a)
  1311. (buffer-substring (match-end 0) (point-at-eol)))
  1312. ((= dcst ?t)
  1313. (cond
  1314. ;; If it is a timer list, convert timer to seconds
  1315. ((org-at-item-timer-p)
  1316. (org-timer-hms-to-secs (match-string 1)))
  1317. ((or (org-search-forward-unenclosed org-ts-regexp
  1318. (point-at-eol) t)
  1319. (org-search-forward-unenclosed org-ts-regexp-both
  1320. (point-at-eol) t))
  1321. (org-time-string-to-seconds (match-string 0)))
  1322. (t (org-float-time now))))
  1323. ((= dcst ?f)
  1324. (if getkey-func
  1325. (let ((value (funcall getkey-func)))
  1326. (if (stringp value)
  1327. (funcall case-func value)
  1328. value))
  1329. (error "Invalid key function `%s'" getkey-func)))
  1330. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  1331. (sort-subr (/= dcst sorting-type) begin-record end-record value-to-sort nil sort-func)
  1332. (org-maybe-renumber-ordered-list)
  1333. (run-hooks 'org-after-sorting-entries-or-items-hook)
  1334. (message "Sorting items...done")))))
  1335. ;;; Send and receive lists
  1336. (defun org-list-parse-list (&optional delete)
  1337. "Parse the list at point and maybe DELETE it.
  1338. Return a list containing first level items as strings and
  1339. sublevels as a list of strings."
  1340. (let* ((start (goto-char (org-list-top-point)))
  1341. (end (org-list-bottom-point))
  1342. output itemsep ltype)
  1343. (while (org-search-forward-unenclosed org-item-beginning-re end t)
  1344. (save-excursion
  1345. (beginning-of-line)
  1346. (setq ltype (cond ((looking-at-p "^[ \t]*[0-9]") 'ordered)
  1347. ((org-at-item-description-p) 'descriptive)
  1348. (t 'unordered))))
  1349. (let* ((indent1 (org-get-indentation))
  1350. (nextitem (or (org-get-next-item (point) end) end))
  1351. (item (org-trim (buffer-substring (point) (org-end-of-item-or-at-child))))
  1352. (nextindent (if (= (point) end) 0 (org-get-indentation)))
  1353. (item (if (string-match "^\\(?:\\[@start:[0-9]+\\][ \t]+\\)?\\[\\([xX ]\\)\\]" item)
  1354. (replace-match (if (equal (match-string 1 item) " ")
  1355. "CBOFF"
  1356. "CBON")
  1357. t nil item 1)
  1358. item)))
  1359. (push item output)
  1360. (when (> nextindent indent1)
  1361. (save-restriction
  1362. (narrow-to-region (point) nextitem)
  1363. (push (org-list-parse-list) output)))))
  1364. (when delete
  1365. (delete-region start end)
  1366. (save-match-data
  1367. (when (looking-at (org-list-end-re))
  1368. (replace-match "\n"))))
  1369. (setq output (nreverse output))
  1370. (push ltype output)))
  1371. (defun org-list-make-subtree ()
  1372. "Convert the plain list at point into a subtree."
  1373. (interactive)
  1374. (if (not (org-in-item-p))
  1375. (error "Not in a list.")
  1376. (goto-char (org-list-top-point))
  1377. (let ((list (org-list-parse-list t)) nstars)
  1378. (save-excursion
  1379. (if (ignore-errors
  1380. (org-back-to-heading))
  1381. (progn (org-search-forward-unenclosed org-complex-heading-regexp nil t)
  1382. (setq nstars (length (match-string 1))))
  1383. (setq nstars 0)))
  1384. (org-list-make-subtrees list (1+ nstars)))))
  1385. (defun org-list-make-subtrees (list level)
  1386. "Convert LIST into subtrees starting at LEVEL."
  1387. (if (symbolp (car list))
  1388. (org-list-make-subtrees (cdr list) level)
  1389. (mapcar (lambda (item)
  1390. (if (stringp item)
  1391. (insert (make-string
  1392. (if org-odd-levels-only
  1393. (1- (* 2 level)) level) ?*) " " item "\n")
  1394. (org-list-make-subtrees item (1+ level))))
  1395. list)))
  1396. (defun org-list-insert-radio-list ()
  1397. "Insert a radio list template appropriate for this major mode."
  1398. (interactive)
  1399. (let* ((e (assq major-mode org-list-radio-list-templates))
  1400. (txt (nth 1 e))
  1401. name pos)
  1402. (unless e (error "No radio list setup defined for %s" major-mode))
  1403. (setq name (read-string "List name: "))
  1404. (while (string-match "%n" txt)
  1405. (setq txt (replace-match name t t txt)))
  1406. (or (bolp) (insert "\n"))
  1407. (setq pos (point))
  1408. (insert txt)
  1409. (goto-char pos)))
  1410. (defun org-list-send-list (&optional maybe)
  1411. "Send a transformed version of this list to the receiver position.
  1412. With argument MAYBE, fail quietly if no transformation is defined for
  1413. this list."
  1414. (interactive)
  1415. (catch 'exit
  1416. (unless (org-at-item-p) (error "Not at a list"))
  1417. (save-excursion
  1418. (re-search-backward "#\\+ORGLST" nil t)
  1419. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  1420. (if maybe
  1421. (throw 'exit nil)
  1422. (error "Don't know how to transform this list"))))
  1423. (let* ((name (match-string 1))
  1424. (transform (intern (match-string 2)))
  1425. (bottom-point
  1426. (save-excursion
  1427. (re-search-forward "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  1428. (match-beginning 0)))
  1429. (top-point
  1430. (progn
  1431. (re-search-backward "#\\+ORGLST" nil t)
  1432. (re-search-forward org-item-beginning-re bottom-point t)
  1433. (match-beginning 0)))
  1434. (list (save-restriction
  1435. (narrow-to-region top-point bottom-point)
  1436. (org-list-parse-list)))
  1437. beg txt)
  1438. (unless (fboundp transform)
  1439. (error "No such transformation function %s" transform))
  1440. (let ((txt (funcall transform list)))
  1441. ;; Find the insertion place
  1442. (save-excursion
  1443. (goto-char (point-min))
  1444. (unless (re-search-forward
  1445. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1446. (error "Don't know where to insert translated list"))
  1447. (goto-char (match-beginning 0))
  1448. (beginning-of-line 2)
  1449. (setq beg (point))
  1450. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1451. (error "Cannot find end of insertion region"))
  1452. (delete-region beg (point-at-bol))
  1453. (goto-char beg)
  1454. (insert txt "\n")))
  1455. (message "List converted and installed at receiver location"))))
  1456. (defun org-list-to-generic (list params)
  1457. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1458. Valid parameters PARAMS are
  1459. :ustart String to start an unordered list
  1460. :uend String to end an unordered list
  1461. :ostart String to start an ordered list
  1462. :oend String to end an ordered list
  1463. :dstart String to start a descriptive list
  1464. :dend String to end a descriptive list
  1465. :dtstart String to start a descriptive term
  1466. :dtend String to end a descriptive term
  1467. :ddstart String to start a description
  1468. :ddend String to end a description
  1469. :splice When set to t, return only list body lines, don't wrap
  1470. them into :[u/o]start and :[u/o]end. Default is nil.
  1471. :istart String to start a list item
  1472. :iend String to end a list item
  1473. :isep String to separate items
  1474. :lsep String to separate sublists
  1475. :cboff String to insert for an unchecked checkbox
  1476. :cbon String to insert for a checked checkbox"
  1477. (interactive)
  1478. (let* ((p params) sublist
  1479. (splicep (plist-get p :splice))
  1480. (ostart (plist-get p :ostart))
  1481. (oend (plist-get p :oend))
  1482. (ustart (plist-get p :ustart))
  1483. (uend (plist-get p :uend))
  1484. (dstart (plist-get p :dstart))
  1485. (dend (plist-get p :dend))
  1486. (dtstart (plist-get p :dtstart))
  1487. (dtend (plist-get p :dtend))
  1488. (ddstart (plist-get p :ddstart))
  1489. (ddend (plist-get p :ddend))
  1490. (istart (plist-get p :istart))
  1491. (iend (plist-get p :iend))
  1492. (isep (plist-get p :isep))
  1493. (lsep (plist-get p :lsep))
  1494. (cbon (plist-get p :cbon))
  1495. (cboff (plist-get p :cboff)))
  1496. (let ((wrapper
  1497. (cond ((eq (car list) 'ordered)
  1498. (concat ostart "\n%s" oend "\n"))
  1499. ((eq (car list) 'unordered)
  1500. (concat ustart "\n%s" uend "\n"))
  1501. ((eq (car list) 'descriptive)
  1502. (concat dstart "\n%s" dend "\n"))))
  1503. rtn term defstart defend)
  1504. (while (setq sublist (pop list))
  1505. (cond ((symbolp sublist) nil)
  1506. ((stringp sublist)
  1507. (when (string-match "^\\(\\S-+\\)[ \t]+::" sublist)
  1508. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1509. (match-string 1 sublist))))
  1510. (setq sublist (concat ddstart
  1511. (org-trim (substring sublist (match-end 0)))
  1512. ddend)))
  1513. (if (string-match "\\[CBON\\]" sublist)
  1514. (setq sublist (replace-match cbon t t sublist)))
  1515. (if (string-match "\\[CBOFF\\]" sublist)
  1516. (setq sublist (replace-match cboff t t sublist)))
  1517. (if (string-match "\\[-\\]" sublist)
  1518. (setq sublist (replace-match "$\\boxminus$" t t sublist)))
  1519. (setq rtn (concat rtn istart term sublist iend isep)))
  1520. (t (setq rtn (concat rtn ;; previous list
  1521. lsep ;; list separator
  1522. (org-list-to-generic sublist p)
  1523. lsep ;; list separator
  1524. )))))
  1525. (format wrapper rtn))))
  1526. (defun org-list-to-latex (list &optional params)
  1527. "Convert LIST into a LaTeX list.
  1528. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1529. with overruling parameters for `org-list-to-generic'."
  1530. (org-list-to-generic
  1531. list
  1532. (org-combine-plists
  1533. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1534. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1535. :dstart "\\begin{description}" :dend "\\end{description}"
  1536. :dtstart "[" :dtend "]"
  1537. :ddstart "" :ddend ""
  1538. :istart "\\item " :iend ""
  1539. :isep "\n" :lsep "\n"
  1540. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1541. params)))
  1542. (defun org-list-to-html (list &optional params)
  1543. "Convert LIST into a HTML list.
  1544. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1545. with overruling parameters for `org-list-to-generic'."
  1546. (org-list-to-generic
  1547. list
  1548. (org-combine-plists
  1549. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1550. :ustart "<ul>" :uend "</ul>"
  1551. :dstart "<dl>" :dend "</dl>"
  1552. :dtstart "<dt>" :dtend "</dt>"
  1553. :ddstart "<dd>" :ddend "</dd>"
  1554. :istart "<li>" :iend "</li>"
  1555. :isep "\n" :lsep "\n"
  1556. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1557. params)))
  1558. (defun org-list-to-texinfo (list &optional params)
  1559. "Convert LIST into a Texinfo list.
  1560. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1561. with overruling parameters for `org-list-to-generic'."
  1562. (org-list-to-generic
  1563. list
  1564. (org-combine-plists
  1565. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1566. :ustart "@enumerate" :uend "@end enumerate"
  1567. :dstart "@table" :dend "@end table"
  1568. :dtstart "@item " :dtend "\n"
  1569. :ddstart "" :ddend ""
  1570. :istart "@item\n" :iend ""
  1571. :isep "\n" :lsep "\n"
  1572. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1573. params)))
  1574. (provide 'org-list)
  1575. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1576. ;;; org-list.el ends here