Skip to content

Commit

Permalink
Add: Remove invisible text from link descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
alphapapa committed Nov 23, 2018
1 parent f1a5101 commit be4c1f8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ See [[https://github.com/alphapapa/org-make-toc/blob/master/example.org][example
:TOC: 0
:END:

** 0.4-pre

*Additions*
+ Omit invisible characters from link titles (e.g. Org markup characters like ~=~ and =~=).

** 0.3

*Additions*
Expand Down
65 changes: 59 additions & 6 deletions org-make-toc.el
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

;; Author: Adam Porter <adam@alphapapa.net>
;; URL: http://github.com/alphapapa/org-make-toc
;; Version: 0.3
;; Version: 0.4-pre
;; Package-Requires: ((emacs "25.1") (dash "2.12") (s "1.10.0") (org "9.0"))
;; Keywords: Org, convenience

Expand Down Expand Up @@ -287,14 +287,16 @@ When KEEP-ALL is non-nil, return all entries."

(defun org-make-toc--link-entry-github (entry)
"Return text for ENTRY converted to GitHub style link."
(-when-let* ((title (org-element-property :title entry))
(title (org-link-display-format title))
(target (replace-regexp-in-string " " "-" (downcase title)))
(target (replace-regexp-in-string "[^[:alnum:]_-]" "" target))
(-when-let* ((title (org-element-property :title it))
(target (--> title
(downcase it)
(replace-regexp-in-string " " "-" it)
(replace-regexp-in-string "[^[:alnum:]_-]" "" it)))
(filename (if org-make-toc-filename-prefix
(file-name-nondirectory (buffer-file-name))
"")))
(format "[[%s#%s][%s]]" filename target title)))
(org-make-link-string (concat "#" filename target)
(org-make-toc--visible-text title))))

;;;;; Misc

Expand All @@ -318,6 +320,57 @@ When KEEP-ALL is non-nil, return all entries."
(beginning-of-line)
(setf (buffer-substring (point) end) contents))))

(defun org-make-toc--visible-text (string)
"Return only visible text in STRING after fontifying it like in Org-mode.
`org-fontify-like-in-org-mode' is a very, very slow function
because it creates a new temporary buffer and runs `org-mode' for
every string it fontifies. This function reuses a single
invisible buffer and only runs `org-mode' when the buffer is
created."
(let ((buffer (get-buffer " *org-make-toc-fontification*")))
(unless buffer
(setq buffer (get-buffer-create " *org-make-toc-fontification*"))
(with-current-buffer buffer
(buffer-disable-undo)
(org-mode)
(setq-local org-hide-emphasis-markers t)))
(with-current-buffer buffer
(insert string)
;; FIXME: "Warning: ‘font-lock-fontify-buffer’ is for interactive use only; use
;; ‘font-lock-ensure’ or ‘font-lock-flush’ instead."
(font-lock-fontify-buffer)
;; This is more complicated than I would like, but the `org-find-invisible' and
;; `org-find-visible' functions don't seem to be appropriate to this task, so this works.
(prog1
(cl-flet ((visible-p () (not (get-char-property (point) 'invisible)))
(invisible-p () (get-char-property (point) 'invisible))
(forward-until (until)
(forward-char 1)
(cl-loop until (or (eobp)
(funcall until))
do (goto-char (next-overlay-change (point))))
(point))
(backward-until (until)
(backward-char 1)
(cl-loop until (or (eobp)
(funcall until))
do (goto-char (previous-overlay-change (point))))
(point)))
(goto-char (point-min))
(unless (visible-p)
(forward-until #'visible-p))
(setq string (cl-loop concat (buffer-substring (point) (forward-until #'invisible-p))
until (eobp)
do (forward-until #'visible-p)))
(when (progn
(backward-char 1)
(invisible-p))
;; Remove trailing invisible chars.
(setq string (substring string 0 (- (point) (backward-until #'invisible-p) 1))))
string)
(erase-buffer)))))

;;;; Mode

(define-minor-mode org-make-toc-mode
Expand Down

0 comments on commit be4c1f8

Please sign in to comment.