al-browse-url.el 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ;;; al-browse-url.el --- Additional functionality for browse-url package
  2. ;; Copyright © 2013-2016 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. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. (require 'browse-url)
  15. (require 'cl-lib)
  16. ;;; Browse IRC logs from gnunet.
  17. (defvar al/irc-log-base-url "https://gnunet.org/bot/log/"
  18. "Base URL with IRC logs.")
  19. (defvar al/irc-log-channels '("guix" "guile" "gnunet")
  20. "List of channels that are logged by gnunet bot.")
  21. (declare-function url-expand-file-name "url-expand" t)
  22. (declare-function org-read-date "org" t)
  23. ;;;###autoload
  24. (defun al/browse-irc-log (channel &optional date)
  25. "Browse IRC log of the CHANNEL from DATE."
  26. (interactive
  27. (list (completing-read "IRC channel: " al/irc-log-channels nil t)
  28. (org-read-date nil nil nil "Log date: ")))
  29. (require 'url-expand)
  30. (browse-url (url-expand-file-name (concat channel "/" date)
  31. al/irc-log-base-url)))
  32. ;;; Add support for the Conkeror browser.
  33. (defcustom al/browse-url-conkeror-program "conkeror"
  34. "The name by which to invoke Conkeror."
  35. :type 'string
  36. :group 'browse-url)
  37. (defcustom al/browse-url-conkeror-arguments nil
  38. "A list of strings to pass to Conkeror as arguments."
  39. :type '(repeat (string :tag "Argument"))
  40. :group 'browse-url)
  41. ;;;###autoload
  42. (defun al/browse-url-conkeror (url &optional new-window)
  43. "Ask the Conkeror WWW browser to load URL.
  44. Default to the URL around or before point. The strings in
  45. variable `al/browse-url-conkeror-arguments' are also passed to
  46. Conkeror."
  47. (interactive (browse-url-interactive-arg "URL: "))
  48. (setq url (browse-url-encode-url url))
  49. (let* ((process-environment (browse-url-process-environment)))
  50. (apply #'start-process
  51. (concat "conkeror " url) nil
  52. al/browse-url-conkeror-program
  53. (append al/browse-url-conkeror-arguments
  54. (list url)))))
  55. ;;; Choosing a browser
  56. ;; I use the following to be prompted for a browser before opening an URL:
  57. ;;
  58. ;; (setq browse-url-browser-function 'al/choose-browser)
  59. (defvar al/browser-choices
  60. '(((?c ?\C-m) "conkeror" al/browse-url-conkeror)
  61. (?f "firefox" browse-url-firefox)
  62. (?w "w3m" w3m-browse-url)
  63. (?e "eww" eww))
  64. "List of the browser choices for `al/choose-browser'.
  65. Each choice has a form:
  66. (CHAR NAME FUN)
  67. CHAR is a character or a list of characters that can be pressed.
  68. NAME is a name of the browser.
  69. FUN is a function to call for browsing (should take URL as an argument).
  70. The first choice is used as default (pressing RET will call the
  71. first function).")
  72. ;;;###autoload
  73. (defun al/choose-browser (url &rest args)
  74. "Choose a browser for openning URL.
  75. Suitable for `browse-url-browser-function'."
  76. (interactive "sURL: ")
  77. (let* ((choices (mapcar
  78. (lambda (spec)
  79. (let* ((chars (car spec))
  80. (chars (if (listp chars) chars (list chars)))
  81. (name (cadr spec)))
  82. (list chars name)))
  83. al/browser-choices))
  84. (chars (cons ?\C-g
  85. (apply #'append (mapcar #'car choices))))
  86. (str (mapconcat
  87. (lambda (spec)
  88. (let ((chars (car spec))
  89. (name (cadr spec)))
  90. (format "%s (%s)"
  91. (mapconcat
  92. (lambda (char)
  93. (propertize (string char)
  94. 'face 'font-lock-warning-face))
  95. chars
  96. "/")
  97. name)))
  98. choices
  99. ", "))
  100. (char (read-char-choice
  101. (concat (propertize "Choose a browser for '"
  102. 'face 'default)
  103. url "'\n" str ": ")
  104. chars t)))
  105. (unless (eq char ?\C-g)
  106. (funcall (nth 2 (cl-find-if
  107. (lambda (spec)
  108. (let ((chars (car spec)))
  109. (if (listp chars)
  110. (memq char chars)
  111. (eq char chars))))
  112. al/browser-choices))
  113. url))
  114. (message "")))
  115. (provide 'al-browse-url)
  116. ;;; al-browse-url.el ends here