config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

ol-irc.el (9826B)


      1 ;;; ol-irc.el --- Links to IRC Sessions              -*- lexical-binding: t; -*-
      2 ;;
      3 ;; Copyright (C) 2008-2024 Free Software Foundation, Inc.
      4 ;;
      5 ;; Author: Philip Jackson <emacs@shellarchive.co.uk>
      6 ;; Keywords: erc, irc, link, org
      7 ;;
      8 ;; This file is part of GNU Emacs.
      9 ;;
     10 ;; GNU Emacs is free software: you can redistribute it and/or modify
     11 ;; it under the terms of the GNU General Public License as published by
     12 ;; the Free Software Foundation, either version 3 of the License, or
     13 ;; (at your option) any later version.
     14 
     15 ;; GNU Emacs is distributed in the hope that it will be useful,
     16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
     18 ;; GNU General Public License for more details.
     19 
     20 ;; You should have received a copy of the GNU General Public License
     21 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
     22 
     23 ;;; Commentary:
     24 
     25 ;; This file implements links to an IRC session from within Org mode.
     26 ;; Org mode loads this module by default - if this is not what you want,
     27 ;; configure the variable `org-modules'.
     28 ;;
     29 ;; Please customize the variable `org-modules' to select
     30 ;; extensions you would like to use, and to deselect those which you don't
     31 ;; want.
     32 ;;
     33 ;; Please note that at the moment only ERC is supported.  Other clients
     34 ;; shouldn't be difficult to add though.
     35 ;;
     36 ;; Then set `org-irc-link-to-logs' to non-nil if you would like a
     37 ;; file:/ type link to be created to the current line in the logs or
     38 ;; to t if you would like to create an irc:/ style link.
     39 ;;
     40 ;; Links within an org buffer might look like this:
     41 ;;
     42 ;; [[irc:/irc.libera.chat/#emacs/bob][chat with bob in #emacs on Libera.Chat]]
     43 ;; [[irc:/irc.libera.chat/#emacs][#emacs on Libera.Chat]]
     44 ;; [[irc:/irc.libera.chat/]]
     45 ;;
     46 ;; If, when the resulting link is visited, there is no connection to a
     47 ;; requested server then one will be created.
     48 
     49 ;;; Code:
     50 
     51 (require 'org-macs)
     52 (org-assert-version)
     53 
     54 (require 'ol)
     55 
     56 (declare-function erc-buffer-filter "erc" (predicate &optional proc))
     57 (declare-function erc-channel-p "erc" (channel))
     58 (declare-function erc-cmd-JOIN "erc" (channel &optional key))
     59 (declare-function erc-current-logfile "erc-log" (&optional buffer))
     60 (declare-function erc-default-target "erc" ())
     61 (declare-function erc-get-server-nickname-list "erc" ())
     62 (declare-function erc-logging-enabled "erc-log" (&optional buffer))
     63 (declare-function erc-prompt "erc" ())
     64 (declare-function erc-save-buffer-in-logs "erc-log" (&optional buffer))
     65 (declare-function erc-server-buffer "erc" ())
     66 
     67 (defvar org-irc-client 'erc
     68   "The IRC client to act on.")
     69 
     70 (defvar org-irc-link-to-logs nil
     71   "Non-nil will store a link to the logs, nil will store an irc: style link.")
     72 
     73 (defvar erc-default-port)   ; dynamically scoped from erc.el
     74 (defvar erc-session-port)   ; dynamically scoped form erc-backend.el
     75 (defvar erc-session-server) ; dynamically scoped form erc-backend.el
     76 
     77 ;; Generic functions/config (extend these for other clients)
     78 
     79 (org-link-set-parameters "irc"
     80 			 :follow #'org-irc-visit
     81 			 :store #'org-irc-store-link
     82 			 :export #'org-irc-export)
     83 
     84 (defun org-irc-visit (link _)
     85   "Parse LINK and dispatch to the correct function based on the client found."
     86   (let ((link (org-irc-parse-link link)))
     87     (cond
     88      ((eq org-irc-client 'erc)
     89       (org-irc-visit-erc link))
     90      (t
     91       (error "ERC only known client")))))
     92 
     93 (defun org-irc-parse-link (link)
     94   "Parse an IRC LINK and return the attributes found.
     95 Parse a LINK that looks like server:port/chan/user (port, chan
     96 and user being optional) and return any of the port, channel or user
     97 attributes that are found."
     98   (let* ((parts (split-string link "/" t))
     99 	 (len (length parts)))
    100     (when (or (< len 1) (> len 3))
    101       (error "Failed to parse link needed 1-3 parts, got %d" len))
    102     (setcar parts (split-string (car parts) ":" t))
    103     parts))
    104 
    105 ;;;###autoload
    106 (defun org-irc-store-link (&optional _interactive?)
    107   "Dispatch to the appropriate function to store a link to an IRC session."
    108   (cond
    109    ((eq major-mode 'erc-mode)
    110     (org-irc-erc-store-link))))
    111 
    112 (defun org-irc-ellipsify-description (string &optional after)
    113   "Remove unnecessary white space from STRING and add ellipses if necessary.
    114 Strip starting and ending white space from STRING and replace any
    115 chars that the value AFTER with `...'"
    116   (let* ((after (number-to-string (or after 30)))
    117 	 (replace-map (list (cons "^[ \t]*" "")
    118 			    (cons "[ \t]*$" "")
    119 			    (cons (concat "^\\(.\\{" after
    120 					  "\\}\\).*") "\\1..."))))
    121     (dolist (x replace-map string)
    122       (when (string-match (car x) string)
    123 	(setq string (replace-match (cdr x) nil nil string))))))
    124 
    125 ;; ERC specific functions
    126 
    127 (defun org-irc-erc-get-line-from-log (erc-line)
    128   "Find the best line to link to from the ERC logs given ERC-LINE as a start.
    129 If the user is on the ERC-prompt then search backward for the
    130 first non-blank line, otherwise return the current line.  The
    131 result is a cons of the filename and search string."
    132   (erc-save-buffer-in-logs)
    133   (require 'erc-log)
    134   (with-current-buffer (find-file-noselect (erc-current-logfile))
    135     (goto-char (point-max))
    136     (list
    137      (abbreviate-file-name buffer-file-name)
    138      ;; can we get a '::' part?
    139      (if (string= erc-line (erc-prompt))
    140 	 (progn
    141            (goto-char (line-beginning-position))
    142 	   (when (search-backward-regexp "^[^	]" nil t)
    143              (buffer-substring-no-properties (line-beginning-position)
    144                                              (line-end-position))))
    145        (when (search-backward erc-line nil t)
    146          (buffer-substring-no-properties (line-beginning-position)
    147                                          (line-end-position)))))))
    148 
    149 (defun org-irc-erc-store-link ()
    150   "Store a link to the IRC log file or the session itself.
    151 Depending on the variable `org-irc-link-to-logs' store either a
    152 link to the log file for the current session or an irc: link to
    153 the session itself."
    154   (require 'erc-log)
    155   (if org-irc-link-to-logs
    156       (let* ((erc-line (buffer-substring-no-properties
    157                         (line-beginning-position) (line-end-position)))
    158 	     (parsed-line (org-irc-erc-get-line-from-log erc-line)))
    159 	(if (erc-logging-enabled nil)
    160 	    (progn
    161 	      (org-link-store-props
    162 	       :type "file"
    163 	       :description (concat "'" (org-irc-ellipsify-description
    164 					 (cadr parsed-line) 20)
    165 				    "' from an IRC conversation")
    166 	       :link (concat "file:" (car parsed-line) "::"
    167 			     (cadr parsed-line)))
    168 	      t)
    169 	  (error "This ERC session is not being logged")))
    170     (let* ((link-text (org-irc-get-erc-link))
    171 	   (link (org-irc-parse-link link-text)))
    172       (if link-text
    173 	  (progn
    174 	    (org-link-store-props
    175 	     :type "irc"
    176 	     :link (concat "irc:/" link-text)
    177 	     :description (concat "irc session `" link-text "'")
    178 	     :server (car (car link))
    179 	     :port (or (string-to-number (cadr (pop link))) erc-default-port)
    180 	     :nick (pop link))
    181 	    t)
    182 	(error "Failed to create ('irc:/' style) ERC link")))))
    183 
    184 (defun org-irc-get-erc-link ()
    185   "Return an org compatible irc:/ link from an ERC buffer."
    186   (let* ((session-port (if (numberp erc-session-port)
    187 			   (number-to-string erc-session-port)
    188 			 erc-session-port))
    189 	 (link (concat erc-session-server ":" session-port)))
    190     (concat link "/"
    191 	    (if (and (erc-default-target)
    192 		     (erc-channel-p (erc-default-target))
    193 		     (car (get-text-property (point) 'erc-data)))
    194 		;; we can get a nick
    195 		(let ((nick (car (get-text-property (point) 'erc-data))))
    196 		  (concat (erc-default-target) "/" nick))
    197 	      (erc-default-target)))))
    198 
    199 (defun org-irc-get-current-erc-port ()
    200   "Return the current port as a number.
    201 Return the current port number or, if none is set, return the ERC
    202 default."
    203   (cond
    204    ((stringp erc-session-port)
    205     (string-to-number erc-session-port))
    206    ((numberp erc-session-port)
    207     erc-session-port)
    208    (t
    209     erc-default-port)))
    210 
    211 (defun org-irc-visit-erc (link)
    212   "Visit an ERC buffer based on criteria found in LINK."
    213   (require 'erc)
    214   (require 'erc-log)
    215   (let* ((server (car (car link)))
    216 	 (port (let ((p (cadr (pop link))))
    217 		 (if p (string-to-number p) erc-default-port)))
    218 	 (server-buffer)
    219 	 (buffer-list
    220 	  (erc-buffer-filter
    221 	   (lambda nil
    222 	     (let ((tmp-server-buf (erc-server-buffer)))
    223 	       (and tmp-server-buf
    224 		    (with-current-buffer tmp-server-buf
    225 		      (and
    226 		       (eq (org-irc-get-current-erc-port) port)
    227 		       (string= erc-session-server server)
    228 		       (setq server-buffer tmp-server-buf)))))))))
    229     (if buffer-list
    230 	(let ((chan-name (pop link)))
    231 	  ;; if we got a channel name then switch to it or join it
    232 	  (if chan-name
    233 	      (let ((chan-buf (catch 'found
    234 				(dolist (x buffer-list)
    235 				  (if (string= (buffer-name x) chan-name)
    236 				      (throw 'found x))))))
    237 		(if chan-buf
    238 		    (progn
    239 		      (pop-to-buffer-same-window chan-buf)
    240 		      ;; if we got a nick, and they're in the chan,
    241 		      ;; then start a chat with them
    242 		      (let ((nick (pop link)))
    243 			(when nick
    244 			  (if (member nick (erc-get-server-nickname-list))
    245 			      (progn
    246 				(goto-char (point-max))
    247 				(insert (concat nick ": ")))
    248 			    (error "%s not found in %s" nick chan-name)))))
    249 		  (progn
    250 		    (pop-to-buffer-same-window server-buffer)
    251 		    (erc-cmd-JOIN chan-name))))
    252 	    (pop-to-buffer-same-window server-buffer)))
    253       ;; no server match, make new connection
    254       (erc-select :server server :port port))))
    255 
    256 (defun org-irc-export (link description format)
    257   "Export an IRC link.
    258 See `org-link-parameters' for details about LINK, DESCRIPTION and
    259 FORMAT."
    260   (let ((desc (or description link)))
    261     (pcase format
    262       (`html (format "<a href=\"irc:%s\">%s</a>" link desc))
    263       (`md (format "[%s](irc:%s)" desc link))
    264       (_ nil))))
    265 
    266 (provide 'ol-irc)
    267 
    268 ;; Local variables:
    269 ;; generated-autoload-file: "org-loaddefs.el"
    270 ;; End:
    271 
    272 ;;; ol-irc.el ends here