Emacs and eww, open links in new window? - browser

I configured Emacs to open new links in eww like so:
(setq browse-url-browser-function 'eww-browse-url)
Now when I click on a link, it opens in the same buffer.
I would like it to open a new window (i.e split vertically like C - x 3) and open the page in the newly created frame on the right. So that I still have the original org-mode notes on the left.
[Edit]
I hacked together something. But it only works when I activate the hotkey, not when another function opens a link.
Ideally, I want something like the below, but for whenever I open a link (e.g in helm-google).
(defun my/open-in-right-window ()
"Open the selected link on the right window plane"
(interactive)
(delete-other-windows nil)
(split-window-right nil)
(other-window 1)
(org-return nil)
)
(defun my/eww-quitAndSingleWin ()
"Quit the current browser session and activate single window mode."
(interactive)
(quit-window nil)
(delete-other-windows nil)
)
(defun my/eww-split-right ()
"Splits the Window. Moves eww to the right and underlying content on the left."
(interactive)
(split-window-right nil)
(quit-window nil)
(other-window 1)
)
(global-set-key (kbd "H-r") 'my/open-in-right-window)
(add-hook 'eww-mode-hook ;no impact.
(lambda ()
(local-set-key (kbd "s") 'my/eww-split-right)
(local-set-key (kbd "Q") 'my/eww-quitAndSingleWin)
))
It kills other windows, opens new window, switches to new window, then presses return [which is configured to open links in my config].
Further then in eww-mode a 'Q' (capital) quits the session and kills the other window so as to avoid too many open windows.
It's not the most elegant solution. I'm open to better ideas?

I had the similar issue of wanting to have multiple eww buffers open and did it by advising eww-render. You could probably put your code in there to make it always run.
(defadvice eww-render (after set-eww-buffer-name activate)
(rename-buffer (concat "*eww-" (or eww-current-title
(if (string-match "://" eww-current-url)
(substring eww-current-url (match-beginning 0))
eww-current-url)) "*") t))

While russel's answer may have been correct in the past, eww-current-title and eww-current-url has been obsoleted in favour of a buffer-local plist called eww-data.
Current eww-implementation also includes the hooks we need to plug in after a buffer is rendered, thus avoiding the need to do "messy" things like defadvice.
As per August 2015 and this Git-commit, the following elisp works for me:
(defun my-set-eww-buffer-title ()
(let* ((title (plist-get eww-data :title))
(url (plist-get eww-data :url))
(result (concat "*eww-" (or title
(if (string-match "://" url)
(substring url (match-beginning 0))
url)) "*")))
(rename-buffer result t)))
(add-hook 'eww-after-render-hook 'my-set-eww-buffer-title)
You can probably use this hook to add your desired keybindings too.

Related

OPENDCL (AUTOCAD): avoid exiting modeless dialog if [ENTER] is pressed

I was looking for a way to avoid exiting modeless dialogs in autocad (created by opendcl) when [ENTER] is pressed.
Any idea?
Thanks,
Dennis
I've implemented such a feature for the OpenDCL grid system. You have to activate the OnCancelClose function on your form. This is how I did it:
(defun c:MyFunction_Form1_OnCancelClose (Reason /)
;; Reason = 0 when Enter is pressed
(if (= Reason 0)
(progn
;; Shift active editing cell one row down
(setq rowAndCol(dcl_Grid_GetCurCell MyFunction_Form1_revisions))
(dcl_Grid_StartCellEdit MyFunction_Form1_revisions (1+ (car rowAndCol)) (cadr rowAndCol))
)
)
;; Reason = 1 when ESC key is pressed, or the closing button in the titlebar
;; is clicked. Hence, enter won't work to cancel the dialog
(/= Reason 1)
)

Autohotkey remapping alt+j to left but alt+shift+j doesn't select left nor do alt+ctr+j move a caret , if getKeyState does not work

Basically,
It's inspired by Vim I want to use a key(e.g Alt, F1) combination(+I J K L) to map to Arrow Keys
What is already done in Autohotkey
Ralt & j::send{Left}
Ralt & k::send{Right}
...
Now I take Alt+I as up etc,which is pretty fine for me But the problem comes when you press
Ralt+Shift+j (Suppose to select the last charater)
Ralt+Ctrl+j (Suppose to move a caramel text)
These kind of combination would not work and it just get overrided to basic move cursor to left
Even if I use if/while statement with GetKeyState, it doesn't gonna work
if GetKeyState("Shift","P")
Ralt+j::send +{Left}
This kind of stuff didn't work
Any Ideas on that ?It would make coding very efficient without having to move the right hand.
Thanks in advance.
You are missing 2 things:
Must use a # symbol when doing a context sensitive hotkey
The bottom section of code is using a + instead of the & you used previously.
See the below modification:
RAlt & j:: Send {Left}
RAlt & k:: Send {Right}
#If GetKeyState("Shift","P")
RAlt & j:: Send +{Left}
RAlt & k:: Send +{Right}
; Close with #If to remove the context or simply start another '#If GetKeystate ....'

Cannot access mouse coords relative to CANVAS widget

I am working with SBCL for Linux on an AMD64 machine.
Function ANIMTEST instantiates an LTK window with a CANVAS widget. Two items, BARRIER and FOLLOWER, live in the canvas. Both spin continuously, with BARRIER at the center of the canvas and FOLLOWER intended to follow the mouse, which is not working as intended. My first attempt (see comment) resulted in absolute screen coordinates of the mouse being interpreted as relative coordinates within the canvas with no account of the offset between the two. After searching through ltk.lisp and docs, I found SCREEN-MOUSE-X/Y (Second Attempt, see comment). I feel like I am using SCREEN-MOUSE-X & -Y according to the documentation, but why is it not working?
= Note =
The file that contains ANIMTEST and the packages that support it load and run with no errors.
Functions I have defined (UCTK-BEAM, etc.) are tested and run fine.
(defun animtest ()
"Test a spinning figure in LTK"
(with-ltk ()
(let* ((cnvs (make-instance 'canvas :width 400 :height 400))
(barrier (uctk-beam 200 200 40 20))
(follower (uctk-beam 0 40 40 20))
(slp-time 50) ; in ms
(bar-theta 0)
(fol-theta 0))
(labels ((update ()
(draw barrier nil)
(draw follower nil)
(incf bar-theta (/ pi 15))
(incf fol-theta (/ pi 15))
(geo:set-theta barrier bar-theta)
(geo:set-theta follower fol-theta)
(geo:set-center follower
;== FIRST ATTEMPT ==
(cons (screen-mouse-x cnvs)
(screen-mouse-y cnvs)))
; == SECOND ATTEMPT ==
;(cons (canvasx cnvs (screen-mouse-x cnvs))
; (canvasy cnvs (screen-mouse-y cnvs))))
(after slp-time #'update)))
(pack cnvs :fill :both :expand 1)
(update)))))
Thanks in advance!
To grab the mouse position in a canvas widget, I don't call the
screen-mouse functions, but rather bind the motion and button press
events. The callback gets passed the event structure which contains
the slots event-x and event-y which are canvas coordinates. Not only
are you getting the right values directly this way, but also it is
more efficient, as you don't have to poll the mouse position - you get
updates automatically when it changes. In your case you could either
choose to update the barrier on mouse-move or alternatively, just
store the mouse coordinates in a variable you read inside your update
loop.
Although it still appears that the CANVASX/Y functions do not work as intended, LTK offers WINDOW-X/-Y to return the X and Y screen coordinates of a widget such that you can write the following to achieve the desired effect:
(cons (- (screen-mouse-x) (window-x cnvs))
(- (screen-mouse-y) (window-y cnvs)))
This assumes that the mouse cursor is on the same screen as the canvas widget named CNVS.

How to setup Emacs to follow links in two ways depend on CTRL key

Is there a way to change follow link in emacs to open it in w3m when click and in Chrome when click with CTRL.
Here is how you can do it:
(defun browse-url-with-w3m (event)
(interactive "e")
(let ((browse-url-browser-function 'w3m-browse-url))
(browse-url-at-mouse event)))
(defun browse-url-with-default-browser (event)
(interactive "e")
(let ((browse-url-browser-function 'browse-url-default-browser))
(browse-url-at-mouse event)))
(global-set-key (kbd "<mouse-1>") 'browse-url-with-w3m)
(global-set-key (kbd "<C-down-mouse-1>") 'browse-url-with-default-browser)
Note that I assume that Chrome is your default browser and that you're
using emacs-w3m. Of course you can customize the variable
browse-url-browser-function.
A slight modification of Daimrod's solution worked pretty well for me with three different browsers:
(defun browse-url-with-default-browser (event)
(interactive "e")
(let ((browse-url-browser-function 'browse-url-default-browser))
(mouse-set-point event)
(org-open-at-point)))
(defun browse-url-with-generic (event)
(interactive "e")
(let ((browse-url-browser-function 'browse-url-generic))
(mouse-set-point event)
(org-open-at-point)))
(defun browse-url-with-w3m (event)
(interactive "e")
(let ((browse-url-browser-function 'w3m-browse-url))
(mouse-set-point event)
(org-open-at-point)))
(global-set-key (kbd "<mouse-1>") 'browse-url-with-default-browser)
(global-set-key (kbd "<C-mouse-1>") 'browse-url-with-generic)
(global-set-key (kbd "C-x <mouse-1>") 'browse-url-with-w3m)
(custom-set-variables
'(browse-url-browser-function (quote browse-url-default-browser))
'(browse-url-generic-program "chromium-browser"))
Though Firefox is not my default system browser Emacs somehow treats it as default. So, Firefox is launched with mouse-1, Chromium with C-mouse-1 and w3m with C-x mouse-1

Is there a way to diff two registers in vim? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
(Vim)diff two subroutines in same file
Sometimes I see a block of code I suspect to be identical to another block in the same file, but it's a bit too long for visual inspection and I may just be missing something. I've tried to visually select the block and yank to the the default register, put that register into / (find), but it didn't match even the original block.
Is there a way to select a section, yank it in a register, select another section then diff the two, without creating a bunch of new files? I imagine the diff results opening in a new buffer in a tab or split.
EDIT: My question is basically a duplicate of This one. I found this answer to be the most helpful & closest to what I was looking for. The only thing I'd change is to make it output in Unified format so it looks like the diff output I'm used to (it has more info as well). I suppose this means using a different diff utility.
Inspired from my lh#path#strip_common() function:
echo matchstr(#a.'##'.#b, '^\zs\(.*\)\ze.\{-}##\1.*$')
will show what is common between registers #a and #b.
You can show more information with:
function ShowDiff(a,b)
" I expect neither string to contain '##'
let start = matchstr(a:a.'##'.a:b, '^\zs\(.*\)\ze.\{-}##\1.*$')
let end= matchstr(a:a.'##'.a:b, '^.\{-}\zs\(.*\)\ze##.\{-}\1$')
let a = a:a[len(start): -len(end)-1]
let b = a:b[len(start): -len(end)-1]
echo "identical beginning: ".strlen(start )." chars -> ".start
echo "identical ending : ".strlen(end)." chars -> ".end
echo "typical to a : ".strlen(a)." chars -> ".a
echo "typical to b : ".strlen(b)." chars -> ".b
endfunction
Used with:
:call ShowDiff(#a, #b)
You could use the following sequence assuming that the two segments are already in registers, 'a and 'b. Could probably be put into a macro or function.
new
only
put a
diffthis
vnew
put b
diffthis
This creates a new buffer, makes it the only visible buffer, puts 'a into it, sets it up to be diff'd, then opens a new buffer in a vertical split, puts 'b into this split empty buffer and also sets it up to diff. Immediately vim (or gvim) will show the differences.
When done, type :ls to get the list of buffers, use :buffer *N* to return back to the original file and use :bdel! *N* to delete the created buffers (named "[No Name]").
Here's a function to open two new windows side by side, each containing the specified register contents (called as DiffRegs(#a, #1), for instance) and diff them. The new buffers will not be written or modifiable:
" A list for bookkeeping..
let g:diffreg_buffers = []
function! DiffRegs(reg1, reg2)
" Preserve the unnamed register
let s:nonamereg = ##
let ## = a:reg1
" new window
:new
normal P
setlocal nomodifiable
setlocal buftype=nofile
diffthis
call add(g:diffreg_buffers, bufnr('%'))
let ## = a:reg2
:vsp +enew
normal P
setlocal nomodifiable
setlocal buftype=nofile
diffthis
call add(g:diffreg_buffers, bufnr('%'))
let ## = s:nonamereg
endfunction " DiffRegs(reg1, reg2)
" Function to wipe all buffers we're diffing with the function above
function! EndDiffs()
for buffer in g:diffreg_buffers
exe ':buffer ' . buffer
diffoff
quit
endfor
let g:diffreg_buffers = []
endfunction " EndDiffs()
You can bind those to key combinations of your choice, but if you don't call EndDiffs() after each call to DiffRegs(), you'll run into issues.
To compare quickly two different parts of a file, you can split the view in two by using:
:sp horizontal split
or
:vsp vertical split
Once you have splitted the screen, you must use :diffthis in each window to hightlight the differences. (Then :diffoff to leave diff mode)
Then to go back to a single window you can quit one of them with :q or use CTRLwo

Resources