You are not logged in.

#426 2020-11-03 19:16:02

Ogashi
Member
Registered: 2017-03-05
Posts: 5

Re: Dmenu Hacking Thread

qinohe wrote:

You could also use a plugin like fashion, the code needed however is a little bloated for your purpose, however, it works fine

Exactly, a little bit overkilling and now I am frustrated with the script I made and wanted to make it work.

Trilbi wrote:

Ogashi, the problem is a trailing newline(s) at the end of path2file.  You'll need to strip that or rethink your script.

I also thought of that, tried using printf to get rid of the new line, now I also tried

path2file=$(awk ' /\47[0-9]/  {print $4} ' ~/.viminfo | dmenu -p 'Edit recent file:' | tr -d '\n')

But I keep having the same behaviour, it is driving me nuts.

qinohe wrote:
eval alacritty -e vim -o "$path2file"

This actually works. I read the manual you cite, but it does not mention the dangerous stuff. I have looked it up a bit and I see why is not a very good idea to use it, although in this case the operands concatenated should be fine.

I fail to see why it does not work without eval, maybe it means that the variable holding the path is not passed to vim correctly?

Offline

#427 2020-11-03 19:33:16

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,113

Re: Dmenu Hacking Thread

Ogashi wrote:
qinohe wrote:

You could also use a plugin like fashion, the code needed however is a little bloated for your purpose, however, it works fine

Exactly, a little bit overkilling and now I am frustrated with the script I made and wanted to make it work.

I know, but like I said, you use it in an already opened vim instance with a keybind, you could use it for more things, like search, find and open in a vim tab.
It  may be a biy overkill, but ,I like using it this way;)

eval alacritty -e vim -o "$path2file"

This actually works. I read the manual you cite, but it does not mention the dangerous stuff. I have looked it up a bit and I see why is not a very good idea to use it, although in this case the operands concatenated should be fine.

I fail to see why it does not work without eval, maybe it means that the variable holding the path is not passed to vim correctly?

Yes, the variable doesn't get passed correctly. About 'eval' mostly referred to as 'evil' can do nasty things when the variable would be a command, it would simply execute it without you being able to prevent that. In this case however you're variable is a path which don't pose a threat. It would be good to avoid 'eval' as much as possible, better read this wooledge wiki https://mywiki.wooledge.org/BashFAQ/048 it explains it much better than me.

edit: Btw. you shouldn't use the command as is, because if you don't open a file and simply escape dmenu, it would still try to open a 'no name' file, check if there is a file otherwise exit

[ -n "$path2file" ] && alacritty -e vim -o "$path2file" || exit

Last edited by qinohe (2020-11-03 19:57:26)

Offline

#428 2020-11-03 22:20:00

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,113

Re: Dmenu Hacking Thread

@Ogashi, Let me explain a little more why the plugin way could be a good idea.
Mostly you simply open the file you want, but, if you already have an instance running, using the plugin in combination with dmenu works very comfortable.
Lets say we have your script and mine for searching files and opening them in a tab, you would only need one plugin(with two functions) and two dmenu scripts. which is in fact not so bloated anymore, as you could add more functions and separate dmenu scripts to your liking. I showed in post#423 what you would need to make it working for your script, but here it is for both;) You would also not have to call alacritty and eval anymore unless you choose that method still...

Two keybinds in your vimrc or exrc

map <c-t> :call DmenuVimFind("tabe")<cr>
map <c-f> :call DmenuVimEdit("tabe")<cr>

One plugin with the two functions(DmenuVimEdit & DmenuVimFind) , put it in ~/.vim/plugin/dedit.vim

" Vim Dmenu edit plugin
" Original Author:      qinohe
" Maintainer:           qinohe
" Last Change:          2020 Nov 02
" License:              Public

" Dmenu edit
" Remove newline from string
function! Chomp(str)
  return substitute(a:str, '\n$', '', '')
endfunction

" Edit file from vim history(viminfo) & rm temp file
function! DmenuVimEdit(cmd)
  let dmena = system("./.config/dmenu/d-script/dmenu_vim_edit")
  if exists("dmena")
    let dmenb = Chomp(system("cat /tmp/vde"))
  endif

  if filereadable("/tmp/vde")
    execute a:cmd . " " . dmenb
  endif
  let dmenc = system('rm -rf /tmp/vde')
endfunction

" Find file and open it in vim tab & rm temp file
function! DmenuVimFind(cmd)
  let dmena = system("./.config/dmenu/d-script/dmenu_vim_find")
  if exists("dmena")
    let dmenb = Chomp(system("cat /tmp/vde"))
  endif

  if filereadable("/tmp/vde")
    execute a:cmd . " " . dmenb
  endif
  let dmenc = system('rm -rf /tmp/vde')
endfunction
" End Dmenu Edit

And last two dmenu scripts, put them in ~/.config/dmenu/d-script or change the path in the plugin
first; dmenu_vim_edit

#!/bin/sh
# shellcheck disable=1090

[ -f "$XDG_CONFIG_HOME"/dmenu/dmenurc ] &&
 . "$XDG_CONFIG_HOME"/dmenu/dmenurc || dmnews='dmenu -i'

# Find recently edited file in viminfo
vde=$( vde 2> /dev/null ) || vde=/tmp/vde

input="$( awk '/\47[0-9]/  {print $4}' ~/.viminfo | $dmnews -p "Edit recent file:" )"

[ -n "$input" ] && echo "$input" > "$vde"

exit 0

second; dmenu_vim_find

#!/bin/sh
# shellcheck disable=1090

[ -f "$XDG_CONFIG_HOME"/dmenu/dmenurc ] &&
 . "$XDG_CONFIG_HOME"/dmenu/dmenurc || dmnews='dmenu -i'

vde=$( vde 2> /dev/null ) || vde=/tmp/vde

input="$( xsel -o | $dmnews -p "file search:" )"

[ "$input" != '' ] && result="$( locate -e -r "$input" | $dmnews  -p "search result:" )"

[ -n "$result" ] && echo "$result" > "$vde"

exit 0

Btw. I would also use a dmenurc which holds colors for dmenu, uses vertical instead of horiz. lists etc.
I put this in ~/.config/dmenu/dmenurc, an example;

# standardize dmenu
# RAL 9004 0n RAL 9003 & choice RAL 9007 or reversed.
dmnews='dmenu -i -l 40 -fn DejaVuSansMono-11 -nb #2e3032 -nf #f4f8f4 -sb #2e3032 -sf #8f8f8c'

Offline

Board footer

Powered by FluxBB