al-org.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ;;; al-org.el --- Additional functionality for org-mode
  2. ;; Copyright © 2012–2016, 2018 Alex Kost
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;;
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'al-text-cmd)
  17. (require 'org)
  18. (require 'org-table)
  19. (defvar al/org-link-no-description-regexp
  20. (rx string-start (or "file" "emms") ":")
  21. "Regexp for `al/org-set-link-description'.")
  22. (defun al/org-set-link-description (fun link &optional description)
  23. "Call FUN with LINK and fixed DESCRIPTION.
  24. This function is intended to be used as an 'around' advice for
  25. `org-make-link-string':
  26. (advice-add 'org-make-link-string
  27. :around #'al/org-set-link-description)
  28. If `al/org-link-no-description-regexp' matches LINK or if
  29. DESCRIPTION is the same as LINK, then description is ignored (FUN
  30. is called with LINK only)."
  31. (if (or (string-match-p al/org-link-no-description-regexp link)
  32. (and description
  33. (string= link description)))
  34. (funcall fun link)
  35. (funcall fun link description)))
  36. (defun al/org-get-time-stamp (time &optional with-hm)
  37. "Return org time stamp string from TIME (iso or system format).
  38. WITH-HM means use the stamp format that includes the time of the day."
  39. (let ((fmt (funcall (if with-hm 'cdr 'car)
  40. org-time-stamp-formats)))
  41. (and (stringp time)
  42. (setq time (org-read-date nil t time)))
  43. (format-time-string fmt time)))
  44. (defun al/org-get-time-from-stamp (org-time &optional end-time-p force)
  45. "Return time value from org time stamp or range ORG-TIME.
  46. Use the start part of the time range if END-TIME-P is nil.
  47. If ORG-TIME is a single time-stamp and END-TIME-P is non-nil,
  48. return nil; with FORCE return its time value. "
  49. (or (string-match org-tsr-regexp org-time)
  50. (error "Wrong org time stamp/range"))
  51. (if (string-match "---?" org-time)
  52. (setq org-time
  53. (if end-time-p
  54. (substring org-time (match-end 0))
  55. (substring org-time 0 (match-beginning 0))))
  56. (and end-time-p (not force)
  57. (setq org-time nil)))
  58. (and org-time
  59. (eval (cons 'encode-time
  60. (org-parse-time-string org-time)))))
  61. ;;; Tables
  62. (defun al/org-table-beginning-of-section ()
  63. "Move point to beginning of current section (a space between
  64. horizontal lines) - behaviour is similar to `backward-word' or
  65. `org-table-beginning-of-field'."
  66. (interactive)
  67. (let ((cur-col (current-column))
  68. (beg (org-table-begin)))
  69. ;; position a point on a proper line
  70. (if (re-search-backward org-table-hline-regexp beg t)
  71. (forward-line)
  72. (org-table-goto-line 1))
  73. (move-to-column cur-col)))
  74. (defun al/org-table-next-column ()
  75. "Move point to first row, next column of the current section"
  76. (interactive)
  77. (al/org-table-beginning-of-section)
  78. (org-table-next-field))
  79. (defun al/org-table-kill-rows-recalculate ()
  80. "Kill all empty rows in the current section and recalculate a
  81. table. Emptiness is checked in the current column after the current
  82. row."
  83. (interactive)
  84. (let ((cur-col (org-table-current-column)))
  85. (save-excursion
  86. (beginning-of-line)
  87. (while (and (org-at-table-p)
  88. (not (looking-at org-table-hline-regexp)))
  89. (if (equal "" (org-table-get (org-table-current-line) cur-col))
  90. (org-table-kill-row)
  91. (forward-line))))
  92. (org-table-recalculate 'iterate 'no-align)
  93. (org-table-align)))
  94. (defun al/org-table-next-table ()
  95. "Move point to the next org-table in the current buffer"
  96. (interactive)
  97. (beginning-of-line)
  98. (and (al/re-search-forward "^[^|]")
  99. (al/re-search-forward "^|")
  100. (org-table-goto-line (+ 1 (org-table-current-line)))))
  101. (provide 'al-org)
  102. ;;; al-org.el ends here