Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Function for formatting githubified org files for Spacemacs #5345

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions core/core-funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,77 @@ buffer."
. (,feature . ,repl-func))
spacemacs-repl-list))

(defun spacemacs/orgify-orgbuf ()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to split the function and reuse its parts in spacemacs/org-githubify-orgbuf But then both of them should be in the same PR.

"Transforms current README.org file buffer from the GitHub friendly
form(see `spacemacs/org-githubify-orgbuf' ) to the canonical."
(interactive)
(let ((toc-line-rexp "^.*:TOC.*:.*$")
(toc-item-rexp "^[\s]*\-\s\\[\\[.*\\]\\[.*\\]\\].*$")
(org-heading-rexp "^[\\*]+\s.*$"))
(save-excursion
(save-match-data

;; Find TOC
(let ((last-pos 0)
(toc-line-count 0))
(progn (beginning-of-buffer)
(while (search-forward-regexp toc-line-rexp nil t)
(incf toc-line-count))
(unless (= toc-line-count 1)
(warn (format (concat "Found %d lines annotated with TOC. "
"Should be exactly 1.") toc-line-count)))))

(let ((line-after-toc-head-beg-pos (progn
(beginning-of-buffer)
(search-forward-regexp toc-line-rexp)
(line-beginning-position 2)))
headings
toc-items-and-levels)

(progn (goto-char line-after-toc-head-beg-pos)
;; Remove TOC items
(while (looking-at-p toc-item-rexp) (kill-whole-line))
;; Find all headings
(while (search-forward-regexp org-heading-rexp nil t)
(push (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))
headings))
;; Calculate headings levels
(mapcar (lambda (heading)
(let ((heading-lvl (- (length heading)
(length (replace-regexp-in-string
"^[\*]+"
""
heading))))
;; List of filters to be applied to the headings
(filters `(,(apply-partially 'replace-regexp-in-string "^[\s]*" "")
,(apply-partially 'replace-regexp-in-string "[\s]*$" ""))))

(progn
;; Apply filters
(mapcar (lambda (filter) (setq heading (funcall filter heading))) filters)

(push (cons heading heading-lvl) toc-items-and-levels))))
(reverse headings))

;; Populate TOC items
(mapcar* (lambda (item-and-lvl)
(progn (setq item (format " - [[%s][%s]]\n"
(replace-regexp-in-string "^[\*]+\s"
""
;; Link
(car item-and-lvl))
;; Remove *'s from title
(replace-regexp-in-string "^[\*]+\s"
""
;; Title
(pop headings))))
;; Create indentations
(dotimes , (1- (cdr item-and-lvl)) (setq item (concat " " item)))
;; Inset the items after the TOC header
(goto-char line-after-toc-head-beg-pos)
(insert item)))
toc-items-and-levels)))))))

(provide 'core-funcs)