aboutsummaryrefslogtreecommitdiffstats
path: root/nyxt/.config/nyxt/init.lisp.org
blob: ce1ec0795e9b09e97573f6f43808f7da1ab3a1cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
Always restore the previous session when the browser starts.

#+begin_src lisp
  (define-configuration browser
    ((session-restore-prompt :always-restore)))
#+end_src

Add a function that automatically redirects certain URLs to free alternatives.

#+begin_src lisp
  (defun init-redirect-to-alternative (request-data)
    (labels ((log-uri (uri)
               (log:info "Switching to URL: ~a://~a~a"
                         (quri:uri-scheme uri)
                         (quri:uri-host uri)
                         (quri:uri-path uri))))
      (let* ((uri (url request-data))
             (host (quri:uri-host uri)))
        (cond
          ((search "blog.bitsrc.io" host)
           (setf (url request-data)
                 (progn
                   (setf (quri:uri-host uri) "scribe.rip"
                         (quri:uri-path uri) (concatenate 'string "/bitsrc" (quri:uri-path uri)))
                   (log-uri uri)
                   uri)))
          ((search "levelup.gitconnected.com" host)
           (setf (url request-data)
                 (progn
                   (setf (quri:uri-host uri) "scribe.rip"
                         (quri:uri-path uri) (concatenate 'string "/gitconnected" (quri:uri-path uri)))
                   (log-uri uri)
                   uri)))
          ((or (search "kevingosse.medium.com" host)
               (search "minidump.net" host))
           (setf (url request-data)
                 (progn
                   (setf (quri:uri-host uri) "scribe.rip"
                         (quri:uri-path uri) (concatenate 'string "/kevingosse" (quri:uri-path uri)))
                   (log-uri uri)
                   uri)))
          ((search "medium.com" host :end2 (length "medium.com"))
           (setf (url request-data)
                 (progn
                   (setf (quri:uri-host uri) "scribe.rip")
                   (log-uri uri)
                   uri)))
          ((search "twitter.com" host)
           (setf (url request-data)
                 (progn
                   (setf (quri:uri-host uri) "nitter.net")
                   (log-uri uri)
                   uri)))
          ((search "www.youtube.com" host)
           (setf (url request-data)
                 (progn
                   (setf (quri:uri-host uri) "redirect.invidious.io")
                   (log-uri uri)
                   uri))))))
    request-data)
#+end_src

I want the keys to function more like Emacs, so I enable =emacs-mode= for all buffers. I also want my downloads to go to =~/downloads= instead of =~/Downloads=.

#+begin_src lisp
  (define-configuration buffer
    ((default-modes (append '(emacs-mode) %slot-default%))
     (download-path (make-instance 'data-path :dirname "~/downloads"))
     (request-resource-hook
      (reduce #'hooks:add-hook
              (mapcar #'make-handler-resource (list #'init-redirect-to-alternative))
              :initial-value %slot-default%))
     ;; Bind org-capture to `C-c o c', but only in emacs-mode.
     (override-map
      (let ((map (make-keymap "override-map")))
        (define-key map "C-c o c" 'org-capture)))))
#+end_src

Set up capturing URLs with org-capture in Emacs. From https://ag91.github.io/blog/2021/07/09/org-capture-in-nyxt-taking-notes-while-browsing/.

#+begin_src lisp
(defun replace-all (string part replacement &key (test #'char=))
  "Return a new string in which all the occurences of the part is replaced with replacement."
  (with-output-to-string (out)
    (loop with part-length = (length part)
          for old-pos = 0 then (+ pos part-length)
          for pos = (search part string
                            :start2 old-pos
                            :test test)
          do (write-string string out
                           :start old-pos
                           :end (or pos (length string)))
          when pos do (write-string replacement out)
            while pos)))

(defun eval-in-emacs (&rest s-exps)
  "Evaluate S-EXPS with emacsclient."
  (let ((s-exps-string (replace-all
                        (write-to-string
                         `(progn ,@s-exps) :case :downcase)
                        ;; Discard the package prefix.
                        "nyxt::" "")))
    (format *error-output* "Sending to Emacs:~%~a~%" s-exps-string)
    (uiop:run-program
     (list "emacsclient" "--eval" s-exps-string))))

(define-command-global org-capture ()
  "Org-capture current page."
  (eval-in-emacs
   `(org-link-set-parameters
     "nyxt"
     :store (lambda ()
              (org-store-link-props
               :type "nyxt"
               :link ,(quri:render-uri (url (current-buffer)))
               :description ,(title (current-buffer)))))
   `(org-capture nil "U"))
  (echo "Note stored!"))
#+end_src

Start Swank so I can connect to it through Emacs.

#+begin_src lisp
  (defvar *init-swank-started* nil)

  (unless *init-swank-started*
    (start-swank)
    (setf *init-swank-started* t))
#+end_src