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

Add 'C-u C-c C-o' to open links with find-file #132

Closed
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
44 changes: 32 additions & 12 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,11 @@
;; * Following Links: `C-c C-o`
;;
;; Press `C-c C-o` when the point is on an inline or reference
;; link to open the URL in a browser. When the point is at a
;; wiki link, open it in another buffer (in the current window,
;; or in the other window with the `C-u` prefix). Use `M-p` and
;; `M-n` to quickly jump to the previous or next link of any type.
;; link to open the URL in a browser (or in the current window
;; with the `C-u` prefix). When the point is at a wiki link, open
;; it in another buffer (in the current window, or in the other
;; window with the `C-u` prefix). Use `M-p` and `M-n` to quickly
;; jump to the previous or next link of any type.
;;
;; * Jumping: `C-c C-l`
;;
Expand Down Expand Up @@ -1181,6 +1182,12 @@ This applies to insertions done with
"--[ \t]*>"
"Regular expression matches HTML comment closing.")

(defconst markdown-regex-link-fragment
"\\([^#]*\\)#?\\(.*\\)"
"Regular expression matches URL fragments.
Group 1 matches the URL before the fragment.
Group 2 matches the fragment identifier.")

(defconst markdown-regex-link-inline
"\\(!\\)?\\(\\[\\)\\([^]^][^]]*\\|\\)\\(\\]\\)\\((\\)\\([^)]*?\\)\\(?:\\s-+\\(\"[^\"]*\"\\)\\)?\\()\\)"
"Regular expression for a [text](file) or an image link ![text](file).
Expand Down Expand Up @@ -6099,10 +6106,23 @@ not at a link or the link reference is not defined returns nil."
(match-string-no-properties 2))
(t nil)))

(defun markdown-follow-link-at-point ()
"Open the current non-wiki link in a browser."
(interactive)
(if (markdown-link-p) (browse-url (markdown-link-link))
(defun markdown-link-filename (name)
"Remove fragment from file links."
(string-match markdown-regex-link-fragment name)
(match-string 1 name))

(defun markdown-follow-link (name &optional other)
"Follow the link NAME in a browser.
Open the with `find-file' when OTHER is non-nil."
(if other (find-file (markdown-link-filename name))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is ffap better rather than find-file ? If the link is HTTP URL(like http://github.com/docker/docker/blob/master/README.md), C-u M-x markdown-follow-link-at-point creates terrible file.

And should file open function be customizable ? (For example default value is find-file or ffap and user sets find-file-other-window or their own function).

Copy link
Author

Choose a reason for hiding this comment

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

On Wed, Apr 27, 2016 at 12:47:18AM -0700, Syohei YOSHIDA wrote:

+(defun markdown-follow-link (name &optional other)

  • "Follow the link NAME in a browser.
    +Open the with `find-file' when OTHER is non-nil."
  • (if other (find-file (markdown-link-filename name))

Is ffap better rather than find-file ? If the link is HTTP
URL(like http://github.com/docker/docker/blob/master/README.md),
C-u M-x markdown-follow-link-at-point creates terrible file.

Ah, that sounds like the “find-file vs. browse-url logic” I wished for
in the initial comment 1. However, in this case the target is a
name, not at the point. Ideally we'd have a
find-file-with-ffap-url-regexp. Does a function like that exist?
Should we use ffap-url-regexp ourselves?

With the current patch, I'm leaving it up to the user to C-u as
appropriate, but with reference-style links, they may not have the
link-target available to inform their choice.

(browse-url name)))

(defun markdown-follow-link-at-point (&optional arg)
"Open the current non-wiki link in a browser.
With prefix argument ARG, open the file with `find-file'.
See `markdown-link-p' and `markdown-follow-link'."
(interactive "P")
(if (markdown-link-p) (markdown-follow-link (markdown-link-link) arg)
(error "Point is not at a Markdown link or URI")))


Expand Down Expand Up @@ -6286,14 +6306,14 @@ Designed to be used with the `after-change-functions' hook."

(defun markdown-follow-thing-at-point (arg)
"Follow thing at point if possible, such as a reference link or wiki link.
Opens inline and reference links in a browser. Opens wiki links
to other files in the current window, or the another window if
ARG is non-nil.
Opens inline and reference links in a browser, or with
`find-file' if ARG is non-nil. Opens wiki links to other files in
the current window, or the another window if ARG is non-nil.
See `markdown-follow-link-at-point' and
`markdown-follow-wiki-link-at-point'."
(interactive "P")
(cond ((markdown-link-p)
(markdown-follow-link-at-point))
(markdown-follow-link-at-point arg))
((markdown-wiki-link-p)
(markdown-follow-wiki-link-at-point arg))
(t
Expand Down