Guest User

Untitled

a guest
Feb 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. (require 'org-archive)
  2.  
  3. ; Set the function to use for org-archive-default (C-c C-x C-a)
  4. ; (setq org-archive-location (concat org-directory "/archive/%s_archive::"))
  5. ; (setq org-archive-location "archive/archived_%s::")
  6. (setq org-archive-location "::* ARCHIVED")
  7.  
  8. ; unmap org-archive-subtree
  9. (define-key org-mode-map (kbd "C-c C-x C-s") nil)
  10.  
  11. ; select command to execute via org-archive-subtree-default (C-c C-x C-a)
  12. (setq org-archive-default-command 'org-archive-subtree-hierarchical)
  13.  
  14. (defun line-content-as-string ()
  15. "Returns the content of the current line as a string"
  16. (save-excursion
  17. (beginning-of-line)
  18. (buffer-substring-no-properties
  19. (line-beginning-position) (line-end-position))))
  20.  
  21. (defun org-child-list (&optional top-level)
  22. "This function returns all children of a heading as a list. "
  23. (interactive)
  24. (save-excursion
  25. ;; this only works with org-version > 8.0, since in previous
  26. ;; org-mode versions the function (org-outline-level) returns
  27. ;; gargabe when the point is not on a heading.
  28. (unless top-level
  29. (if (= (org-outline-level) 0)
  30. (outline-next-visible-heading 1)
  31. (org-goto-first-child)))
  32. (let ((child-list (list (line-content-as-string))))
  33. (while (org-goto-sibling)
  34. (setq child-list (cons (line-content-as-string) child-list)))
  35. child-list)))
  36.  
  37. (defun fa/org-struct-subtree ()
  38. "This function returns the tree structure in which a subtree
  39. belongs as a list."
  40. (interactive)
  41. (let ((archive-tree nil))
  42. (save-excursion
  43. (while (org-up-heading-safe)
  44. (let ((heading
  45. (buffer-substring-no-properties
  46. (line-beginning-position) (line-end-position))))
  47. (if (eq archive-tree nil)
  48. (setq archive-tree (list heading))
  49. (setq archive-tree (cons heading archive-tree))))))
  50. archive-tree))
  51.  
  52. (defun org-archive-subtree-hierarchical ()
  53. "This function archives a subtree hierarchical"
  54. (interactive)
  55. (let ((org-tree (fa/org-struct-subtree))
  56. (this-buffer (current-buffer))
  57. (file (abbreviate-file-name
  58. (or (buffer-file-name (buffer-base-buffer))
  59. (error "No file associated to buffer")))))
  60. (save-excursion
  61. (setq location (org-get-local-archive-location)
  62. afile (org-extract-archive-file location)
  63. heading (org-extract-archive-heading location)
  64. infile-p (equal file (abbreviate-file-name (or afile ""))))
  65. (unless afile
  66. (error "Invalid `org-archive-location'"))
  67. (if (not (equal heading ""))
  68. (progn
  69. (setq org-tree (cons heading
  70. (mapcar (lambda (s) (concat "*" s)) org-tree)))
  71. (org-demote-subtree)))
  72. (if (> (length afile) 0)
  73. (setq newfile-p (not (file-exists-p afile))
  74. visiting (find-buffer-visiting afile)
  75. buffer (or visiting (find-file-noselect afile)))
  76. (progn
  77. (clone-indirect-buffer)
  78. (setq buffer (current-buffer))))
  79. (unless buffer
  80. (error "Cannot access file \"%s\"" afile))
  81. (org-cut-subtree)
  82. (set-buffer buffer)
  83. (org-mode)
  84. (goto-char (point-min))
  85.  
  86. ; simplified version of org-complex-heading-regexp-format
  87. (setq my-org-complex-heading-regexp-format
  88. (concat "^"
  89. "\\(%s\\)"
  90. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  91. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?"
  92. "[ \t]*$"))
  93. (setq top-level-p t)
  94. (while (not (equal org-tree nil))
  95. (let ((child-list (org-child-list top-level-p))
  96. (re (format my-org-complex-heading-regexp-format (regexp-quote (car org-tree))))
  97. )
  98. (if (member "______FOUND_MATCH" (mapcar (lambda (s) (replace-regexp-in-string re "______FOUND_MATCH" s)) child-list))
  99. (progn
  100. (re-search-forward re nil t)
  101. (setq org-tree (cdr org-tree)))
  102. (progn
  103. (if (not top-level-p) (newline))
  104. (org-insert-struct org-tree)
  105. (setq org-tree nil))))
  106. (setq top-level-p nil))
  107. (newline)
  108. (org-yank)
  109. ;; Save and kill the buffer, if it is not the same buffer.
  110. (when (not (eq this-buffer buffer))
  111. (save-buffer)
  112. (kill-buffer))
  113. (message "Subtree archived %s"
  114. (concat "in file: " (abbreviate-file-name afile))))))
  115.  
  116. (defun org-insert-struct (struct)
  117. "TODO"
  118. (interactive)
  119. (when struct
  120. (insert (car struct))
  121. (if (not (equal (length struct) 1))
  122. (newline))
  123. (org-insert-struct (cdr struct))))
Add Comment
Please, Sign In to add comment