Bug 214547 - Kosole KPart needs an API to retrieve $PWD
Summary: Kosole KPart needs an API to retrieve $PWD
Status: RESOLVED FIXED
Alias: None
Product: yakuake
Classification: Applications
Component: general (show other bugs)
Version: 3.0.4
Platform: unspecified Linux
: NOR wishlist
Target Milestone: ---
Assignee: Eike Hein
URL:
Keywords:
: 169058 227656 297222 321834 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-11-14 17:48 UTC by Mathias Panzenböck
Modified: 2021-04-14 16:15 UTC (History)
24 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
This patch makes a new session use the same $PWD as the current session (9.69 KB, patch)
2009-11-15 00:14 UTC, Mathias Panzenböck
Details
Rebased patch to master (10.28 KB, patch)
2017-09-10 15:45 UTC, Mathias Panzenböck
Details
Don't use TerminalInterface::currentWorkingDirectory() under Linux (921 bytes, patch)
2017-09-10 20:18 UTC, Mathias Panzenböck
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mathias Panzenböck 2009-11-14 17:48:58 UTC
It would be nice to have a shortcut that opens a new session in the same $PWD as the current one.
Comment 1 Eike Hein 2009-11-14 17:52:11 UTC
Unfortunately it's not possible to retrieve the $PWD from the Konsole KPart component that Yakuake embeds, so I can't get at the data needed to spawn another at the same location. A wish along those lines needs to be filed against Konsole: I'm reappropriating this one for the purpose. Renamin & reassigning.
Comment 2 Mathias Panzenböck 2009-11-14 23:16:56 UTC
It is possible. I've just written a patch. I'll upload it to gitorious or github in a minute and post a link here (usually I like hg more but it failed to clone the svn repo for some reason). :)

The API to retrieve the PWD is this:
http://api.kde.org/4.x-api/kdelibs-apidocs/interfaces/html/classTerminalInterfaceV2.html

Well, this way you get the PID of the terminal. You can get the PWD by reading /proc/$PID/cwd (like konsole does it already!). Well yes, this is Linux only. But better than nothing. Maybe one can do something similar under BSD. I guess Yakuake is not used on other OSes.
Comment 3 Mathias Panzenböck 2009-11-15 00:12:34 UTC
Here is the git repo:
http://github.com/panzi/Yakuake
Comment 4 Mathias Panzenböck 2009-11-15 00:14:27 UTC
Created attachment 38333 [details]
This patch makes a new session use the same $PWD as the current session

Currently this feature is not optional. When I have time I might make it optional, so that there is in fact a additional shortcut for "open new session in same $PWD".
Comment 5 Mathias Panzenböck 2009-11-15 00:25:52 UTC
PS: (Off Topic) I've seen that you often use code like this:

    if (!m_terminals.contains(terminalId)) return;

    Terminal* terminal = m_terminals.value(terminalId);

This makes two hastable lookups. Ok, this is really not performance critical, but I think this would still be much more nice:

    Terminal* terminal = m_terminals.value(terminalId);

    if (!terminal) return;

See:
http://doc.trolltech.com/4.5/qhash.html#value
http://doc.trolltech.com/4.5/containers.html#default-constructed-values
Comment 6 Eike Hein 2009-11-15 09:11:10 UTC
Right, I forgot about the TerminalInterfaceV2 additions for Dolphin :). However: There's also the fact that Konsole profiles already have a "Start in same directory as current tab" checkbox, which however is non-functional for the KPart -- imho it'd be better to fix this in Konsole, which also takes care of the "making it optional" part.

And for a shortcut, I still think engineering-wise there should be an API in the KPart to avoid the code duplication between Konsole and Yakuake, e.g. so that when a BSD solution is found it can be added in one place rather than two. I'm not very fond of hacks.

Regarding your point in commant #5, you're right in principle. I use contains() out of habit (and consciously) because value() returns a default-constructed value for a non-existing key, which can bite you in the ass in some situations. It doesn't matter for a pointer type, as you rightly point out - a default-constructed one is 0 - but I generally find it more useful to foster cautious habits than micro-optimize. However as with any programmer the efficiency loss has been nagging me too, and now that someone else has noticed I might feel forced to change it ;).
Comment 7 Eike Hein 2009-11-15 09:22:59 UTC
Btw, to take things further: I also want to eventually have an API in the Konsole KPart that allows listing available profiles and spawning a KPart with a particular profile. Yakuake's New Session menu (click and hold the "New Tab" button) could then list the available profiles, and there could also be a facility to assign shortcuts to profiles (the current entries of the New Session menu are already KActions in the action collection, i.e. found in Configure Shortcuts). Your shortcut could then be realized by way of spawning a tab/terminal with a profile that has the "Start in
same directory as current tab" checked (once it's fixed anyway).
Comment 8 Eike Hein 2010-02-19 12:33:00 UTC
*** Bug 227656 has been marked as a duplicate of this bug. ***
Comment 9 Jekyll Wu 2010-09-12 23:28:26 UTC
*** This bug has been confirmed by popular vote. ***
Comment 10 Jekyll Wu 2011-08-23 08:54:33 UTC
*** Bug 169058 has been marked as a duplicate of this bug. ***
Comment 11 Jekyll Wu 2011-09-15 07:23:57 UTC
If #156919 is implemented, does that make this problem solvable(at least for yakuake)?
Comment 12 Eike Hein 2011-09-15 13:42:40 UTC
It'd be inefficient, but solvable, yeah.
Comment 13 Jekyll Wu 2012-03-23 14:00:13 UTC
Git commit a7f71fe903df30b9aaa27f02df35d0eef92fb597 by Jekyll Wu.
Committed on 22/03/2012 at 08:55.
Pushed by jekyllwu into branch 'master'.

Add slot currentWorkingDirectory() for getting the current wokring directory
REVIEW:104362

CCMAIL: hein@kde.org

M  +7    -0    src/Part.cpp
M  +7    -0    src/Part.h

http://commits.kde.org/konsole/a7f71fe903df30b9aaa27f02df35d0eef92fb597
Comment 14 Eike Hein 2012-04-01 19:23:24 UTC
*** Bug 297222 has been marked as a duplicate of this bug. ***
Comment 15 Eike Hein 2013-07-01 19:03:25 UTC
*** Bug 321834 has been marked as a duplicate of this bug. ***
Comment 16 ilya.kolodnik 2013-08-25 16:08:41 UTC
And what is the situation with this bug for now? I'm using archlinux with yakuake 2.9.9-1 and this option doesn't work
Comment 17 dalz59q+gk5jns 2014-02-13 04:20:29 UTC
This is kind of fixed but seems to only work if the current tab isn't running anything in the foreground. Otherwise a new tab lands me in my home directory.

Quite tedious as often I want a few tabs in the current directory for doing different things. For example I like evince and gedit to have it's own tab because they write a continuous stream of stupid warnings.
Comment 18 Mathias Panzenböck 2014-02-13 18:59:13 UTC
Well, my patch still works:
https://github.com/panzi/Yakuake/commit/e5668b2ce4d142070631374306667f3e3a3d8369

And it does also work when an application (e.g. rails server) is running in the current tab. (I'm on KDE 4.11.5)
Comment 19 Ismael Barros 2014-11-04 17:21:32 UTC
Still not working on Archlinux's Yakuake 2.9.9-3 and yakuake-git :(

Is there any issue left with Mathias' patch?
Comment 20 Brallan Aguilar 2015-06-17 03:22:47 UTC
Is there some news?
Comment 21 Aaron Wolf 2015-08-05 17:30:11 UTC
I'm on Ubuntu 14.04.2 using KDE 4.13.3. I have the checkbox set for opening in same directory in my profile. It doesn't work. In Yakuake, I always land back in my home directory…
Comment 22 Walter Rudametkin 2015-08-18 16:28:50 UTC
Bug is still present on Arch, running Yakuake Version 2.9.9
Konsole remembers the current PWD just fine though, as does gnome-terminal, xfce4-terminal, guake, ... 

Maybe somebody could give us an update on this 6 year old bug?

Regards
Comment 23 Egmont Koblinger 2017-09-10 14:06:27 UTC
On a side note, have you guys thought about replacing whatever konsole is doing now (digging under /proc) with what gnome-terminal and other vte derivatives, macOS's Terminal.app and perhaps iTerm2 do, namely using the OSC 7 escape sequence by the shell to explicitly tell the current working directory to the terminal?

I'm not aware of the API and whether this change would have any implications there, but...

> This is kind of fixed but seems to only work if the current tab isn't running
> anything in the foreground. Otherwise a new tab lands me in my home directory.

... I guess it would (among others) have the benefit that these kinds of problems could no longer occur.
Comment 24 Kurt Hindenburg 2017-09-10 14:12:18 UTC
OSC 7 supoort was committed over a year ago - I'd have to check how the KPart handles it.  I assume from the recent reports, it is not working as people expects it to.
Comment 25 Egmont Koblinger 2017-09-10 14:14:38 UTC
Haha, cool... sorry I've missed out on this :)
Comment 26 Walter Rudametkin 2017-09-10 14:46:48 UTC
Good to see some new comments. I really like Yakuake but this bug drives me crazy.

Any one know if Mathias' patch still works?
Comment 27 Mathias Panzenböck 2017-09-10 15:43:51 UTC
I updated Yakuake for the first time in a long while and rebased my patch on top of master: Now it seems to be buggy for the first time you try to open a new tab in the same directory for some reason. Didn't had that behavior before. Konsole doesn't behave that way.
Comment 28 Mathias Panzenböck 2017-09-10 15:45:04 UTC
Created attachment 107784 [details]
Rebased patch to master
Comment 29 Walter Rudametkin 2017-09-10 16:34:36 UTC
I applied your patch, it's working on my end.

Just to be clear, the patch remembers the $PWD in the current session (e.g., when splitting terminals horizontally/vertically), but not for new sessions (e.g., opening a new tab with ctrl+shift+t). I'm not sure if that's the desired behavior, I'd prefer it to always remember the $PWD, but it's still a lot better than before.

Thanks Mathias!

Regards,
Walter
Comment 30 Mathias Panzenböck 2017-09-10 16:52:13 UTC
What do you mean by remembers? Also personally I only use tabs, not splitting sessions. The buggy behavior it has now annoys me. How did that happen? :/
Comment 31 Walter Rudametkin 2017-09-10 17:20:04 UTC
By "remembers" I means that it opens the terminal at the same $PWD as the last selected terminal.

For me, when I open a new terminal in a new tab by pressing ctrl+shift+t, the terminal opens in ~ every time, so the patch doesn't seem to work.

When I open a new terminal in a split, e.g., ctrl+( or ctrl+) or right-clicking, the new terminal opens in the same $PWD as the previously selected terminal.

So it doesn't seem to work for me as it does for you, but it's still better than what I had.

I might mention that I'm running Yakuake in XFCE in Arch, not in KDE, maybe that has something to do with it? I'll try again tomorrow with a clean config too.

Regards
Comment 32 Mathias Panzenböck 2017-09-10 17:52:18 UTC
For me it opens the new tab in the $PWD the current tab had _before the last command_. That means if you e.g. do `cd ~/images` and then open a new tab it will open in ~. But if you just do an extra enter after the cd the new tab WILL be in ~/images. Weird. It wasn't like that before I updated (before I rebased to current master).
Comment 33 Mathias Panzenböck 2017-09-10 20:18:00 UTC
Created attachment 107788 [details]
Don't use TerminalInterface::currentWorkingDirectory() under Linux

Workaround for erratic TerminalInterface::currentWorkingDirectory() behavior by reading /proc/$PID/cwd directly (which is Linux-only).
Comment 34 Mathias Panzenböck 2017-09-10 20:19:20 UTC
Comment on attachment 107788 [details]
Don't use TerminalInterface::currentWorkingDirectory() under Linux

This only changes Terminal::currentWorkingDirectory() and doesn't the the rest of my patch. I use this personally because the erratic behavior was too annoying for me.
Comment 35 Kurt Hindenburg 2017-09-10 21:11:50 UTC
I see that this bug was originally set to yakuake and then moved to Konsole.

Since these patches are for yakuake, I would suggest either moving this back over to yakuake or creating a new bug report (which might be cleaner ATM).
Comment 36 Walter Rudametkin 2017-09-11 17:41:35 UTC
Matías, I updated git and applied your second patch, nothing changed on my side, behavior is the same.

To check if it had something to do with my environment (XFCE, .bashrc, yakuakerc), I installed plasma-desktop and created a new user. Exactly the same behavior as before, the $PWD is properly set for split terminals, but not for new tabs.

It's still an improvement for me :)

As I side-note, with split terminals I don't remember seeing the issue you noticed where you need to hit enter after changing directories.
Comment 37 Mathias Panzenböck 2017-09-11 17:49:39 UTC
Do you use the shortcut for "New Session (Same Working Directory)"? Because my patch does not change the "New Session" shortcut, but instead adds a new shortcut. This way you can have both behaviors with different shortcuts.
Comment 38 Walter Rudametkin 2017-09-11 18:57:00 UTC
You're right, I forgot to set the new shortcut! I think what got me is that your patch made it work for split terminals without any new shortcuts.

Works perfectly now in all cases. Any chance of getting the patch into Yakuake?
Comment 39 Christoph Feck 2017-09-17 21:11:50 UTC
Reassigning (back) to yakuake developers. See comment #35 and comment #38.
Comment 40 Pavol Harar 2021-04-14 07:49:12 UTC
Since there seems not to be any activity for 4 years I would like to help other users who stumble upon this bug with a dirty fix by Michael Wiebusch which works for me on Ubuntu 20.04. I found it on his blog from 2016 here: https://acidbourbon.wordpress.com/2016/12/03/a-quick-and-dirty-fix-for-yakuakes-open-new-tab-in-same-directory-issue/. 


From the blog:
----------------

You simply append the following code to your /home/$USER/.bashrc

```
if [ $(basename "/"$(ps -f -p $(cat /proc/$(echo $$)/stat | cut -d \  -f 4) | tail -1 | sed 's/^.* //')) == "yakuake" ]; then
# go to last active cwd
  if [ -e /dev/shm/$USER-yakuake-cwd ]; then
    cd "$(cat /dev/shm/$USER-yakuake-cwd)"
  fi
# on each stroke of the return key, save cwd in a shared memory
  export PS1=$PS1'$(pwd > /dev/shm/$USER-yakuake-cwd)'
fi
```
Comment 41 Antonio Orefice 2021-04-14 08:11:01 UTC
yakuake 20.12.3 opens new tabs in the same directories of the old one.
Is this bug still valid?
Comment 42 Aaron Wolf 2021-04-14 16:15:43 UTC
Maybe I'm too rash to be the one to mark this resolved, but I'm doing it. For years, I never had Yakuake keep the directory in new sessions. But it works now. Something clearly changed that fixed this. I think if anyone has further issues, it should be a new bug.